[llvm] 7707d49 - [Assignment Tracking] Fix DbgVariableIntrinsic::replaceVariableLocationOp

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 23 05:57:08 PST 2022


Author: OCHyams
Date: 2022-11-23T13:56:34Z
New Revision: 7707d4913b6adf9d411ff5ab5fa449dff2dd76b0

URL: https://github.com/llvm/llvm-project/commit/7707d4913b6adf9d411ff5ab5fa449dff2dd76b0
DIFF: https://github.com/llvm/llvm-project/commit/7707d4913b6adf9d411ff5ab5fa449dff2dd76b0.diff

LOG: [Assignment Tracking] Fix DbgVariableIntrinsic::replaceVariableLocationOp

Fix replaceVariableLocationOp unconditionally replacing the first operand of a
dbg.assign.

Reviewed By: jryans

Differential Revision: https://reviews.llvm.org/D138561

Added: 
    

Modified: 
    llvm/lib/IR/IntrinsicInst.cpp
    llvm/unittests/IR/DebugInfoTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/IntrinsicInst.cpp b/llvm/lib/IR/IntrinsicInst.cpp
index b6537b2077ebe..f91b148bfe74a 100644
--- a/llvm/lib/IR/IntrinsicInst.cpp
+++ b/llvm/lib/IR/IntrinsicInst.cpp
@@ -130,6 +130,11 @@ void DbgVariableIntrinsic::replaceVariableLocationOp(Value *OldValue,
   assert((OldIt != Locations.end() || DbgAssignAddrReplaced) &&
          "OldValue must be a current location");
   if (!hasArgList()) {
+    // Additional check necessary to avoid unconditionally replacing this
+    // operand when a dbg.assign address is replaced (DbgAssignAddrReplaced is
+    // true).
+    if (OldValue != getVariableLocationOp(0))
+      return;
     Value *NewOperand = isa<MetadataAsValue>(NewValue)
                             ? NewValue
                             : MetadataAsValue::get(

diff  --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index bf676f9d5f1c6..439c0d00ee4eb 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -368,6 +368,58 @@ TEST(DIBuilder, createDbgAddr) {
   EXPECT_EQ(MDExp->getNumElements(), 0u);
 }
 
+TEST(DbgAssignIntrinsicTest, replaceVariableLocationOp) {
+  LLVMContext C;
+  std::unique_ptr<Module> M = parseIR(C, R"(
+    define dso_local void @fun(i32 %v1, ptr %p1, ptr %p2) !dbg !7 {
+    entry:
+      call void @llvm.dbg.assign(metadata i32 %v1, metadata !14, metadata !DIExpression(), metadata !17, metadata ptr %p1, metadata !DIExpression()), !dbg !16
+      ret void
+    }
+
+    declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
+
+    !llvm.dbg.cu = !{!0}
+    !llvm.module.flags = !{!3}
+
+    !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 14.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
+    !1 = !DIFile(filename: "test.cpp", directory: "/")
+    !3 = !{i32 2, !"Debug Info Version", i32 3}
+    !7 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
+    !8 = !DISubroutineType(types: !9)
+    !9 = !{null}
+    !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+    !11 = !{}
+    !14 = !DILocalVariable(name: "Local", scope: !7, file: !1, line: 3, type: !10)
+    !16 = !DILocation(line: 0, scope: !7)
+    !17 = distinct !DIAssignID()
+    )");
+  // Check the test IR isn't malformed.
+  ASSERT_TRUE(M);
+
+  Function &Fun = *M->getFunction("fun");
+  Value *V1 = Fun.getArg(0);
+  Value *P1 = Fun.getArg(1);
+  Value *P2 = Fun.getArg(2);
+  DbgAssignIntrinsic *DAI = cast<DbgAssignIntrinsic>(Fun.begin()->begin());
+  ASSERT_TRUE(V1 == DAI->getVariableLocationOp(0));
+  ASSERT_TRUE(P1 == DAI->getAddress());
+
+#define TEST_REPLACE(Old, New, ExpectedValue, ExpectedAddr)                    \
+  DAI->replaceVariableLocationOp(Old, New);                                    \
+  EXPECT_EQ(DAI->getVariableLocationOp(0), ExpectedValue);                     \
+  EXPECT_EQ(DAI->getAddress(), ExpectedAddr);
+
+  // Replace address only.
+  TEST_REPLACE(/*Old*/ P1, /*New*/ P2, /*Value*/ V1, /*Address*/ P2);
+  // Replace value only.
+  TEST_REPLACE(/*Old*/ V1, /*New*/ P2, /*Value*/ P2, /*Address*/ P2);
+  // Replace both.
+  TEST_REPLACE(/*Old*/ P2, /*New*/ P1, /*Value*/ P1, /*Address*/ P1);
+
+#undef TEST_REPLACE
+}
+
 TEST(AssignmentTrackingTest, Utils) {
   // Test the assignment tracking utils defined in DebugInfo.h namespace at {}.
   // This includes:


        


More information about the llvm-commits mailing list