[PATCH] D125994: [NFC] Define move and copy constructors and copy assignment operators for MDOperand.

Wolfgang Pieb via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 23 14:19:32 PDT 2022


wolfgangp updated this revision to Diff 431480.
wolfgangp added a comment.

Added a unit test to make sure the move constructor and the move assignment op of MDOperand adjust tracking info.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D125994/new/

https://reviews.llvm.org/D125994

Files:
  llvm/include/llvm/IR/Metadata.h
  llvm/unittests/IR/MetadataTest.cpp


Index: llvm/include/llvm/IR/Metadata.h
===================================================================
--- llvm/include/llvm/IR/Metadata.h
+++ llvm/include/llvm/IR/Metadata.h
@@ -775,10 +775,21 @@
 
 public:
   MDOperand() = default;
-  MDOperand(MDOperand &&) = delete;
   MDOperand(const MDOperand &) = delete;
-  MDOperand &operator=(MDOperand &&) = delete;
+  MDOperand(MDOperand &&Op) {
+    MD = Op.MD;
+    if (MD)
+      (void)MetadataTracking::retrack(Op.MD, MD);
+    Op.MD = nullptr;
+  }
   MDOperand &operator=(const MDOperand &) = delete;
+  MDOperand &operator=(MDOperand &&Op) {
+    MD = Op.MD;
+    if (MD)
+      (void)MetadataTracking::retrack(Op.MD, MD);
+    Op.MD = nullptr;
+    return *this;
+  }
   ~MDOperand() { untrack(); }
 
   Metadata *get() const { return MD; }
Index: llvm/unittests/IR/MetadataTest.cpp
===================================================================
--- llvm/unittests/IR/MetadataTest.cpp
+++ llvm/unittests/IR/MetadataTest.cpp
@@ -3602,4 +3602,33 @@
   EXPECT_EQ(DebugVariableMap.find(DebugVariableFragB)->second, 12u);
 }
 
+typedef MetadataTest MDTupleAllocationTest;
+TEST_F(MDTupleAllocationTest, Tracking) {
+  // Make sure that the move constructor and move assignment op
+  // for MDOperand correctly adjust tracking information.
+  auto *Value1 = getConstantAsMetadata();
+  MDTuple *A = MDTuple::getDistinct(Context, {Value1, Value1});
+  EXPECT_EQ(A->getOperand(0), Value1);
+  EXPECT_EQ(A->getOperand(1), Value1);
+
+  MDNode::op_range Ops = A->operands();
+
+  MDOperand NewOps1;
+  // Move assignment operator.
+  NewOps1 = std::move(*const_cast<MDOperand *>(Ops.begin()));
+  // Move constructor.
+  MDOperand NewOps2(std::move(*const_cast<MDOperand *>(Ops.begin() + 1)));
+
+  EXPECT_EQ(NewOps1.get(), static_cast<Metadata *>(Value1));
+  EXPECT_EQ(NewOps2.get(), static_cast<Metadata *>(Value1));
+
+  auto *Value2 = getConstantAsMetadata();
+  Value *V1 = Value1->getValue();
+  Value *V2 = Value2->getValue();
+  ValueAsMetadata::handleRAUW(V1, V2);
+
+  EXPECT_EQ(NewOps1.get(), static_cast<Metadata *>(Value2));
+  EXPECT_EQ(NewOps2.get(), static_cast<Metadata *>(Value2));
+}
+
 } // end namespace


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125994.431480.patch
Type: text/x-patch
Size: 2179 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220523/04ffb27c/attachment.bin>


More information about the llvm-commits mailing list