[llvm] [NFC][AsmPrinter] Refactor constructVariableDIE (PR #66435)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 14:14:35 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo
            
<details>
<summary>Changes</summary>
Fold constructVariableDIEImpl into constructVariableDIE, simplify it and group related functions.

Pull out the previously inline lambdas for visiting the active variant of the DbgVariable to add location and related attributes as an overload set for a private method
applyConcreteDbgVariableAttributes.

Rename applyVariableAttribute to reflect what kinds of attributes it applies, and to contrast it with the new
applyConcreteDbgVariableAttributes.

Move constructLabelDIE down in the implementation file, so all of the constructVariableDIE-related function impls are adjacent.
--

Patch is 26.31 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/66435.diff

2 Files Affected:

- (modified) llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp (+210-217) 
- (modified) llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h (+36-5) 


<pre>
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index e37dd14c96f76b3..57531e3660c039c 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -740,232 +740,213 @@ DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
   return ScopeDIE;
 }
 
-/// constructVariableDIE - Construct a DIE for the given DbgVariable.
 DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &amp;DV, bool Abstract) {
-  auto D = constructVariableDIEImpl(DV, Abstract);
-  DV.setDIE(*D);
-  return D;
+  auto *VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
+  insertDIE(DV.getVariable(), VariableDie);
+  DV.setDIE(*VariableDie);
+  // Abstract variables don&#x27;t get common attributes later, so apply them now.
+  if (Abstract) {
+    applyCommonDbgVariableAttributes(DV, *VariableDie);
+  } else {
+    std::visit(
+        [&amp;](const auto &amp;V) {
+          applyConcreteDbgVariableAttributes(V, DV, VariableDie);
+        },
+        DV.asVariant());
+  }
+  return VariableDie;
 }
 
-DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &amp;DL,
-                                         const LexicalScope &amp;Scope) {
-  auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag());
-  insertDIE(DL.getLabel(), LabelDie);
-  DL.setDIE(*LabelDie);
+void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
+    const Loc::Single &amp;Single, const DbgVariable &amp;DV, DIE *VariableDie) {
+  const DbgValueLoc *DVal = &amp;Single.getValueLoc();
+  if (!DVal-&gt;isVariadic()) {
+    const DbgValueLocEntry *Entry = DVal-&gt;getLocEntries().begin();
+    if (Entry-&gt;isLocation()) {
+      addVariableAddress(DV, *VariableDie, Entry-&gt;getLoc());
+    } else if (Entry-&gt;isInt()) {
+      auto *Expr = Single.getExpr();
+      if (Expr &amp;&amp; Expr-&gt;getNumElements()) {
+        DIELoc *Loc = new (DIEValueAllocator) DIELoc;
+        DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
+        // If there is an expression, emit raw unsigned bytes.
+        DwarfExpr.addFragmentOffset(Expr);
+        DwarfExpr.addUnsignedConstant(Entry-&gt;getInt());
+        DwarfExpr.addExpression(Expr);
+        addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
+        if (DwarfExpr.TagOffset)
+          addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset,
+                  dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
+      } else
+        addConstantValue(*VariableDie, Entry-&gt;getInt(), DV.getType());
+    } else if (Entry-&gt;isConstantFP()) {
+      addConstantFPValue(*VariableDie, Entry-&gt;getConstantFP());
+    } else if (Entry-&gt;isConstantInt()) {
+      addConstantValue(*VariableDie, Entry-&gt;getConstantInt(), DV.getType());
+    } else if (Entry-&gt;isTargetIndexLocation()) {
+      DIELoc *Loc = new (DIEValueAllocator) DIELoc;
+      DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
+      const DIBasicType *BT = dyn_cast&lt;DIBasicType&gt;(
+          static_cast&lt;const Metadata *&gt;(DV.getVariable()-&gt;getType()));
+      DwarfDebug::emitDebugLocValue(*Asm, BT, *DVal, DwarfExpr);
+      addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
+    }
+    return;
+  }
+  // If any of the location entries are registers with the value 0,
+  // then the location is undefined.
+  if (any_of(DVal-&gt;getLocEntries(), [](const DbgValueLocEntry &amp;Entry) {
+        return Entry.isLocation() &amp;&amp; !Entry.getLoc().getReg();
+      }))
+    return;
+  const DIExpression *Expr = Single.getExpr();
+  assert(Expr &amp;&amp; &quot;Variadic Debug Value must have an Expression.&quot;);
+  DIELoc *Loc = new (DIEValueAllocator) DIELoc;
+  DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
+  DwarfExpr.addFragmentOffset(Expr);
+  DIExpressionCursor Cursor(Expr);
+  const TargetRegisterInfo &amp;TRI = *Asm-&gt;MF-&gt;getSubtarget().getRegisterInfo();
 
-  if (Scope.isAbstractScope())
-    applyLabelAttributes(DL, *LabelDie);
+  auto AddEntry = [&amp;](const DbgValueLocEntry &amp;Entry,
+                      DIExpressionCursor &amp;Cursor) {
+    if (Entry.isLocation()) {
+      if (!DwarfExpr.addMachineRegExpression(TRI, Cursor,
+                                             Entry.getLoc().getReg()))
+        return false;
+    } else if (Entry.isInt()) {
+      // If there is an expression, emit raw unsigned bytes.
+      DwarfExpr.addUnsignedConstant(Entry.getInt());
+    } else if (Entry.isConstantFP()) {
+      // DwarfExpression does not support arguments wider than 64 bits
+      // (see PR52584).
+      // TODO: Consider chunking expressions containing overly wide
+      // arguments into separate pointer-sized fragment expressions.
+      APInt RawBytes = Entry.getConstantFP()-&gt;getValueAPF().bitcastToAPInt();
+      if (RawBytes.getBitWidth() &gt; 64)
+        return false;
+      DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
+    } else if (Entry.isConstantInt()) {
+      APInt RawBytes = Entry.getConstantInt()-&gt;getValue();
+      if (RawBytes.getBitWidth() &gt; 64)
+        return false;
+      DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
+    } else if (Entry.isTargetIndexLocation()) {
+      TargetIndexLocation Loc = Entry.getTargetIndexLocation();
+      // TODO TargetIndexLocation is a target-independent. Currently
+      // only the WebAssembly-specific encoding is supported.
+      assert(Asm-&gt;TM.getTargetTriple().isWasm());
+      DwarfExpr.addWasmLocation(Loc.Index, static_cast&lt;uint64_t&gt;(Loc.Offset));
+    } else {
+      llvm_unreachable(&quot;Unsupported Entry type.&quot;);
+    }
+    return true;
+  };
 
-  return LabelDie;
+  if (!DwarfExpr.addExpression(
+          std::move(Cursor),
+          [&amp;](unsigned Idx, DIExpressionCursor &amp;Cursor) -&gt; bool {
+            return AddEntry(DVal-&gt;getLocEntries()[Idx], Cursor);
+          }))
+    return;
+
+  // Now attach the location information to the DIE.
+  addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
+  if (DwarfExpr.TagOffset)
+    addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
+            *DwarfExpr.TagOffset);
 }
 
-DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &amp;DV,
-                                                bool Abstract) {
-  // Define variable debug information entry.
-  auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
-  insertDIE(DV.getVariable(), VariableDie);
+void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
+    const Loc::Multi &amp;Multi, const DbgVariable &amp;DV, DIE *VariableDie) {
+  addLocationList(*VariableDie, dwarf::DW_AT_location,
+                  Multi.getDebugLocListIndex());
+  auto TagOffset = Multi.getDebugLocListTagOffset();
+  if (TagOffset)
+    addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
+            *TagOffset);
+}
 
-  if (Abstract) {
-    applyVariableAttributes(DV, *VariableDie);
-    return VariableDie;
-  }
+void DwarfCompileUnit::applyConcreteDbgVariableAttributes(const Loc::MMI &amp;MMI,
+                                                          const DbgVariable &amp;DV,
+                                                          DIE *VariableDie) {
+  std::optional&lt;unsigned&gt; NVPTXAddressSpace;
+  DIELoc *Loc = new (DIEValueAllocator) DIELoc;
+  DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
+  for (const auto &amp;Fragment : MMI.getFrameIndexExprs()) {
+    Register FrameReg;
+    const DIExpression *Expr = Fragment.Expr;
+    const TargetFrameLowering *TFI = Asm-&gt;MF-&gt;getSubtarget().getFrameLowering();
+    StackOffset Offset =
+        TFI-&gt;getFrameIndexReference(*Asm-&gt;MF, Fragment.FI, FrameReg);
+    DwarfExpr.addFragmentOffset(Expr);
 
-  // Add variable address.
-  std::visit(
-      makeVisitor(
-          [=, &amp;DV](const Loc::Single &amp;Single) {
-            const DbgValueLoc *DVal = &amp;Single.getValueLoc();
-            if (!DVal-&gt;isVariadic()) {
-              const DbgValueLocEntry *Entry = DVal-&gt;getLocEntries().begin();
-              if (Entry-&gt;isLocation()) {
-                addVariableAddress(DV, *VariableDie, Entry-&gt;getLoc());
-              } else if (Entry-&gt;isInt()) {
-                auto *Expr = Single.getExpr();
-                if (Expr &amp;&amp; Expr-&gt;getNumElements()) {
-                  DIELoc *Loc = new (DIEValueAllocator) DIELoc;
-                  DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
-                  // If there is an expression, emit raw unsigned bytes.
-                  DwarfExpr.addFragmentOffset(Expr);
-                  DwarfExpr.addUnsignedConstant(Entry-&gt;getInt());
-                  DwarfExpr.addExpression(Expr);
-                  addBlock(*VariableDie, dwarf::DW_AT_location,
-                           DwarfExpr.finalize());
-                  if (DwarfExpr.TagOffset)
-                    addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset,
-                            dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
-                } else
-                  addConstantValue(*VariableDie, Entry-&gt;getInt(), DV.getType());
-              } else if (Entry-&gt;isConstantFP()) {
-                addConstantFPValue(*VariableDie, Entry-&gt;getConstantFP());
-              } else if (Entry-&gt;isConstantInt()) {
-                addConstantValue(*VariableDie, Entry-&gt;getConstantInt(),
-                                 DV.getType());
-              } else if (Entry-&gt;isTargetIndexLocation()) {
-                DIELoc *Loc = new (DIEValueAllocator) DIELoc;
-                DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
-                const DIBasicType *BT = dyn_cast&lt;DIBasicType&gt;(
-                    static_cast&lt;const Metadata *&gt;(DV.getVariable()-&gt;getType()));
-                DwarfDebug::emitDebugLocValue(*Asm, BT, *DVal, DwarfExpr);
-                addBlock(*VariableDie, dwarf::DW_AT_location,
-                         DwarfExpr.finalize());
-              }
-              return;
-            }
-            // If any of the location entries are registers with the value 0,
-            // then the location is undefined.
-            if (any_of(DVal-&gt;getLocEntries(),
-                       [](const DbgValueLocEntry &amp;Entry) {
-                         return Entry.isLocation() &amp;&amp; !Entry.getLoc().getReg();
-                       }))
-              return;
-            const DIExpression *Expr = Single.getExpr();
-            assert(Expr &amp;&amp; &quot;Variadic Debug Value must have an Expression.&quot;);
-            DIELoc *Loc = new (DIEValueAllocator) DIELoc;
-            DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
-            DwarfExpr.addFragmentOffset(Expr);
-            DIExpressionCursor Cursor(Expr);
-            const TargetRegisterInfo &amp;TRI =
-                *Asm-&gt;MF-&gt;getSubtarget().getRegisterInfo();
-
-            auto AddEntry = [&amp;](const DbgValueLocEntry &amp;Entry,
-                                DIExpressionCursor &amp;Cursor) {
-              if (Entry.isLocation()) {
-                if (!DwarfExpr.addMachineRegExpression(TRI, Cursor,
-                                                       Entry.getLoc().getReg()))
-                  return false;
-              } else if (Entry.isInt()) {
-                // If there is an expression, emit raw unsigned bytes.
-                DwarfExpr.addUnsignedConstant(Entry.getInt());
-              } else if (Entry.isConstantFP()) {
-                // DwarfExpression does not support arguments wider than 64 bits
-                // (see PR52584).
-                // TODO: Consider chunking expressions containing overly wide
-                // arguments into separate pointer-sized fragment expressions.
-                APInt RawBytes =
-                    Entry.getConstantFP()-&gt;getValueAPF().bitcastToAPInt();
-                if (RawBytes.getBitWidth() &gt; 64)
-                  return false;
-                DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
-              } else if (Entry.isConstantInt()) {
-                APInt RawBytes = Entry.getConstantInt()-&gt;getValue();
-                if (RawBytes.getBitWidth() &gt; 64)
-                  return false;
-                DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
-              } else if (Entry.isTargetIndexLocation()) {
-                TargetIndexLocation Loc = Entry.getTargetIndexLocation();
-                // TODO TargetIndexLocation is a target-independent. Currently
-                // only the WebAssembly-specific encoding is supported.
-                assert(Asm-&gt;TM.getTargetTriple().isWasm());
-                DwarfExpr.addWasmLocation(Loc.Index,
-                                          static_cast&lt;uint64_t&gt;(Loc.Offset));
-              } else {
-                llvm_unreachable(&quot;Unsupported Entry type.&quot;);
-              }
-              return true;
-            };
-
-            if (!DwarfExpr.addExpression(
-                    std::move(Cursor),
-                    [&amp;](unsigned Idx, DIExpressionCursor &amp;Cursor) -&gt; bool {
-                      return AddEntry(DVal-&gt;getLocEntries()[Idx], Cursor);
-                    }))
-              return;
-
-            // Now attach the location information to the DIE.
-            addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
-            if (DwarfExpr.TagOffset)
-              addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset,
-                      dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
-          },
-          [=](const Loc::Multi &amp;Multi) {
-            addLocationList(*VariableDie, dwarf::DW_AT_location,
-                            Multi.getDebugLocListIndex());
-            auto TagOffset = Multi.getDebugLocListTagOffset();
-            if (TagOffset)
-              addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset,
-                      dwarf::DW_FORM_data1, *TagOffset);
-          },
-          [=](const Loc::MMI &amp;MMI) {
-            std::optional&lt;unsigned&gt; NVPTXAddressSpace;
-            DIELoc *Loc = new (DIEValueAllocator) DIELoc;
-            DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
-            for (const auto &amp;Fragment : MMI.getFrameIndexExprs()) {
-              Register FrameReg;
-              const DIExpression *Expr = Fragment.Expr;
-              const TargetFrameLowering *TFI =
-                  Asm-&gt;MF-&gt;getSubtarget().getFrameLowering();
-              StackOffset Offset =
-                  TFI-&gt;getFrameIndexReference(*Asm-&gt;MF, Fragment.FI, FrameReg);
-              DwarfExpr.addFragmentOffset(Expr);
-
-              auto *TRI = Asm-&gt;MF-&gt;getSubtarget().getRegisterInfo();
-              SmallVector&lt;uint64_t, 8&gt; Ops;
-              TRI-&gt;getOffsetOpcodes(Offset, Ops);
-
-              // According to
-              // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
-              // cuda-gdb requires DW_AT_address_class for all variables to be
-              // able to correctly interpret address space of the variable
-              // address. Decode DW_OP_constu &lt;DWARF Address Space&gt; DW_OP_swap
-              // DW_OP_xderef sequence for the NVPTX + gdb target.
-              unsigned LocalNVPTXAddressSpace;
-              if (Asm-&gt;TM.getTargetTriple().isNVPTX() &amp;&amp; DD-&gt;tuneForGDB()) {
-                const DIExpression *NewExpr = DIExpression::extractAddressClass(
-                    Expr, LocalNVPTXAddressSpace);
-                if (NewExpr != Expr) {
-                  Expr = NewExpr;
-                  NVPTXAddressSpace = LocalNVPTXAddressSpace;
-                }
-              }
-              if (Expr)
-                Ops.append(Expr-&gt;elements_begin(), Expr-&gt;elements_end());
-              DIExpressionCursor Cursor(Ops);
-              DwarfExpr.setMemoryLocationKind();
-              if (const MCSymbol *FrameSymbol = Asm-&gt;getFunctionFrameSymbol())
-                addOpAddress(*Loc, FrameSymbol);
-              else
-                DwarfExpr.addMachineRegExpression(
-                    *Asm-&gt;MF-&gt;getSubtarget().getRegisterInfo(), Cursor,
-                    FrameReg);
-              DwarfExpr.addExpression(std::move(Cursor));
-            }
-            if (Asm-&gt;TM.getTargetTriple().isNVPTX() &amp;&amp; DD-&gt;tuneForGDB()) {
-              // According to
-              // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
-              // cuda-gdb requires DW_AT_address_class for all variables to be
-              // able to correctly interpret address space of the variable
-              // address.
-              const unsigned NVPTX_ADDR_local_space = 6;
-              addUInt(*VariableDie, dwarf::DW_AT_address_class,
-                      dwarf::DW_FORM_data1,
-                      NVPTXAddressSpace.value_or(NVPTX_ADDR_local_space));
-            }
-            addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
-            if (DwarfExpr.TagOffset)
-              addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset,
-                      dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
-          },
-          [=](const Loc::EntryValue &amp;EntryValue) {
-            DIELoc *Loc = new (DIEValueAllocator) DIELoc;
-            DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
-            // Emit each expression as: EntryValue(Register) &lt;other ops&gt;
-            // &lt;Fragment&gt;.
-            for (auto [Register, Expr] : EntryValue.EntryValues) {
-              DwarfExpr.addFragmentOffset(&amp;Expr);
-              DIExpressionCursor Cursor(Expr.getElements());
-              DwarfExpr.beginEntryValueExpression(Cursor);
-              DwarfExpr.addMachineRegExpression(
-                  *Asm-&gt;MF-&gt;getSubtarget().getRegisterInfo(), Cursor, Register);
-              DwarfExpr.addExpression(std::move(Cursor));
-            }
-            addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
-          },
-          [](const std::monostate &amp;) {}),
-      DV.asVariant());
+    auto *TRI = Asm-&gt;MF-&gt;getSubtarget().getRegisterInfo();
+    SmallVector&lt;uint64_t, 8&gt; Ops;
+    TRI-&gt;getOffsetOpcodes(Offset, Ops);
 
-  return VariableDie;
+    // According to
+    // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
+    // cuda-gdb requires DW_AT_address_class for all variables to be
+    // able to correctly interpret address space of the variable
+    // address. Decode DW_OP_constu &lt;DWARF Address Space&gt; DW_OP_swap
+    // DW_OP_xderef sequence for the NVPTX + gdb target.
+    unsigned LocalNVPTXAddressSpace;
+    if (Asm-&gt;TM.getTargetTriple().isNVPTX() &amp;&amp; DD-&gt;tuneForGDB()) {
+      const DIExpression *NewExpr =
+          DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
+      if (NewExpr != Expr) {
+        Expr = NewExpr;
+        NVPTXAddressSpace = LocalNVPTXAddressSpace;
+      }
+    }
+    if (Expr)
+      Ops.append(Expr-&gt;elements_begin(), Expr-&gt;elements_end());
+    DIExpressionCursor Cursor(Ops);
+    DwarfExpr.setMemoryLocationKind();
+    if (const MCSymbol *FrameSymbol = Asm-&gt;getFunctionFrameSymbol())
+      addOpAddress(*Loc, FrameSymbol);
+    else
+      DwarfExpr.addMachineRegExpression(
+ ...
<truncated>
</pre>
</details>


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


More information about the llvm-commits mailing list