No public description

PiperOrigin-RevId: 1021498880
Change-Id: Idd19841345fb26f976834056dce4740d11df0db2
diff --git a/mpact/sim/decoder/slot.cc b/mpact/sim/decoder/slot.cc
index f525345..1161a90 100644
--- a/mpact/sim/decoder/slot.cc
+++ b/mpact/sim/decoder/slot.cc
@@ -261,8 +261,7 @@
   absl::StrAppend(&output, "void ", name, "(Instruction *inst) {\n");
   int size = attribute_names_.size();
   if (size > 0) {
-    // Allocate the array and initialize to zero.
-    absl::StrAppend(&output, "  int *attrs = new int[", size, "]{");
+    absl::StrAppend(&output, "  static constexpr int kAttrs[", size, "] = {");
     for (auto const& attribute_name : attribute_names_) {
       auto it = inst->attribute_map().find(attribute_name);
       if (it == inst->attribute_map().end()) {
@@ -282,7 +281,8 @@
       absl::StrAppend(&output, *value, ", ");
     }
     absl::StrAppend(&output, "};\n");
-    absl::StrAppend(&output, "  inst->SetAttributes(absl::Span<int>(attrs,",
+    absl::StrAppend(&output,
+                    "  inst->SetAttributes(absl::Span<const int>(kAttrs, ",
                     size, "));\n");
   }
   absl::StrAppend(&output, "}\n\n");
diff --git a/mpact/sim/generic/instruction.cc b/mpact/sim/generic/instruction.cc
index 6ba87f4..cd9a1bd 100644
--- a/mpact/sim/generic/instruction.cc
+++ b/mpact/sim/generic/instruction.cc
@@ -94,7 +94,7 @@
     delete op;
   }
   resource_acquire_.clear();
-  if (parent_ != nullptr) {
+  if ((parent_ != nullptr) && (parent_->child_ == this)) {
     // remove reference to this from the parent
     parent_->child_ = nullptr;
   }
@@ -103,7 +103,9 @@
     next_ = nullptr;
   }
   if (child_ != nullptr) {
-    child_->parent_ = nullptr;
+    for (auto* child = child_; child != nullptr; child = child->next_) {
+      child->parent_ = nullptr;
+    }
     child_->DecRef();
     child_ = nullptr;
   }
@@ -112,7 +114,7 @@
 std::string Instruction::AsString() const { return disasm_string_; }
 
 void Instruction::SetAttributes(absl::Span<const int> attributes) {
-  delete attribute_array_;
+  delete[] attribute_array_;
   attribute_array_ = new int[attributes.size()];
   std::memcpy(attribute_array_, attributes.data(),
               attributes.size() * sizeof(int));
diff --git a/mpact/sim/generic/test/instruction_test.cc b/mpact/sim/generic/test/instruction_test.cc
index d1d3b63..cfe66bc 100644
--- a/mpact/sim/generic/test/instruction_test.cc
+++ b/mpact/sim/generic/test/instruction_test.cc
@@ -113,6 +113,25 @@
   EXPECT_EQ(inst->next()->next()->next(), nullptr);
 }
 
+TEST(InstructionTest, ChildOutlivesParent) {
+  Instruction* parent = new Instruction(0x1000, nullptr);
+  Instruction* child0 = new Instruction(0x1000, nullptr);
+  Instruction* child1 = new Instruction(0x1000, nullptr);
+  Instruction* child2 = new Instruction(0x1000, nullptr);
+
+  parent->AppendChild(child0);
+  parent->AppendChild(child1);
+  parent->AppendChild(child2);
+  child0->DecRef();
+  child1->DecRef();
+  // Keep an extra reference to child2 (simulating an in-flight delayed bundle)
+  // while destroying parent first.
+  parent->DecRef();
+
+  EXPECT_EQ(child2->parent(), nullptr);
+  child2->DecRef();
+}
+
 TEST(InstructionTest, Attributes) {
   auto inst = std::make_unique<Instruction>(0x1000, nullptr);
   auto attr = inst->Attributes();
@@ -121,6 +140,10 @@
   inst->SetAttributes(my_array);
   EXPECT_THAT(inst->Attributes(),
               testing::ElementsAreArray(absl::Span<int>(my_array)));
+  int updated_array[] = {6, 7, 8};
+  inst->SetAttributes(updated_array);
+  EXPECT_THAT(inst->Attributes(),
+              testing::ElementsAreArray(absl::Span<int>(updated_array)));
 }
 
 }  // namespace