[llvm] [RemoveDIs] Update DIBuilder to conditionally insert DbgRecords (PR #84739)

Stephen Tozer via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 11 10:38:35 PDT 2024


================
@@ -452,10 +433,19 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
     }
 
     ::Value *NewValue = Value ? Value : DbgAssign->getValue();
-    auto *NewAssign = createLinkedAssign(
-        DbgAssign, DIB, Inst, NewValue, DbgAssign->getVariable(), Expr, Dest,
-        DIExpression::get(Expr->getContext(), std::nullopt),
-        DbgAssign->getDebugLoc());
+    // insertDbgAssign returns a PointerUnion of {Instruction* | DbgRecord*}.
+    // If DbgAssign is a DPValue* it'll return a DbgRecord*, otherwise if
+    // DbgAssign is a DbgAssignIntrinsic* it'll return a Instruction*.
+    // The ugly code below creates a new debug marker, then gets the
+    // pointer type out of the union based on the type of DbgInstType
+    // (Instruction* or DbgRecord*), which is then cast to DbgAssignIntrinsic*
+    // or DPValue* so that the relevant member functions can be called.
+    auto *NewAssign = static_cast<decltype(DbgAssign)>(
+        DIB.insertDbgAssign(Inst, NewValue, DbgAssign->getVariable(), Expr,
+                            Dest,
+                            DIExpression::get(Expr->getContext(), std::nullopt),
+                            DbgAssign->getDebugLoc())
+            .template get<decltype(DbgInstType)>());
----------------
SLTozer wrote:

C++20 can't come soon enough. That being said, instead of passing the extra `DbgInstType` to this class, you could create an overloaded function just above this function body:
```
DPValue *UnwrapDbgInstPtr(DbgInstPtr P, DPValue *) { return static_cast<DPValue*>(cast<DbgRecord*>(P)); }
`DPValue *UnwrapDbgInstPtr(DbgInstPtr P, DbgAssignIntrinsic *) { return static_cast<DbgAssignIntrinsic*>(cast<Instruction*>(P)); }
```
And then you could just call `UnwrapDbgInstPtr(DIB.insertDbgAssign(...), DbgAssign)` here. YMMV on this though!

https://github.com/llvm/llvm-project/pull/84739


More information about the llvm-commits mailing list