[PATCH] D139928: [clang][DebugInfo] Backport emitting DW_AT_default_value for default template arguments

Michael Buch via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 02:50:32 PST 2022


Michael137 created this revision.
Michael137 added reviewers: aprantl, dblaikie.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Michael137 requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

With this patch Clang emits `DW_AT_default_value` to indicate defaulted
template arguments (this usage was introduced in DWARFv5) in earlier
versions of DWARF, unless compiling with `-gstrict-dwarf`.

Changes:

1. Previously the DwarfVersion check in `CGDebugInfo` was inconsistent: For non-type template arguments we attached the attribute to the debug metadata in DWARFv5 only. Whereas for type template arguments we didn't have such a version restriction. With this patch we attach the attribute regardless of DWARF version (and instead offload the check to the AsmPrinter).
2. The AsmPrinter will now also check for whether we compiled with `-gstrict-dwarf`


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139928

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/test/CodeGenCXX/debug-info-template-parameter.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h


Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
+++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
@@ -350,6 +350,10 @@
 
   virtual bool isDwoUnit() const = 0;
   const MCSymbol *getCrossSectionRelativeBaseAddress() const override;
+
+  /// Returns 'true' if 'DW_AT_default_value' should be emitted
+  /// to indicate defaulted template arguments.
+  bool shouldEmitTemplateDefaultAttr() const;
 };
 
 class DwarfTypeUnit final : public DwarfUnit {
Index: llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -1060,7 +1060,7 @@
     addType(ParamDIE, TP->getType());
   if (!TP->getName().empty())
     addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
-  if (TP->isDefault() && (DD->getDwarfVersion() >= 5))
+  if (TP->isDefault() && shouldEmitTemplateDefaultAttr())
     addFlag(ParamDIE, dwarf::DW_AT_default_value);
 }
 
@@ -1074,7 +1074,7 @@
     addType(ParamDIE, VP->getType());
   if (!VP->getName().empty())
     addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
-  if (VP->isDefault() && (DD->getDwarfVersion() >= 5))
+  if (VP->isDefault() && shouldEmitTemplateDefaultAttr())
     addFlag(ParamDIE, dwarf::DW_AT_default_value);
   if (Metadata *Val = VP->getValue()) {
     if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
@@ -1852,3 +1852,9 @@
 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
   DD->getAddressPool().resetUsedFlag(true);
 }
+
+bool DwarfUnit::shouldEmitTemplateDefaultAttr() const {
+  assert(Asm != nullptr);
+  assert(DD != nullptr);
+  return !Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5;
+}
Index: clang/test/CodeGenCXX/debug-info-template-parameter.cpp
===================================================================
--- clang/test/CodeGenCXX/debug-info-template-parameter.cpp
+++ clang/test/CodeGenCXX/debug-info-template-parameter.cpp
@@ -3,6 +3,9 @@
 
 // RUN: %clang_cc1 -emit-llvm %std_cxx11-14 -dwarf-version=5 -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s --check-prefixes=CHECK,PRE17
 // RUN: %clang_cc1 -emit-llvm %std_cxx17- -dwarf-version=5 -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s --check-prefixes=CHECK,CXX17
+// RUN: %clang_cc1 -emit-llvm %std_cxx17- -dwarf-version=4 -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -o - | FileCheck %s --check-prefixes=CHECK,CXX17
+// RUN: %clang_cc1 %std_cxx17- -dwarf-version=4 -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -emit-obj -o - %s | llvm-dwarfdump --debug-info - | FileCheck %s --check-prefixes=DWARF-DUMP,DWARFv4
+// RUN: %clang_cc1 %std_cxx17- -dwarf-version=4 -gstrict-dwarf -triple x86_64 %s -O0 -disable-llvm-passes -debug-info-kind=standalone -emit-obj -o - %s | llvm-dwarfdump --debug-info - | FileCheck %s --check-prefixes=DWARF-DUMP,STRICT
 
 // CHECK: DILocalVariable(name: "f1", {{.*}}, type: ![[TEMPLATE_TYPE:[0-9]+]]
 // CHECK: [[TEMPLATE_TYPE]] = {{.*}}!DICompositeType({{.*}}, templateParams: ![[F1_TYPE:[0-9]+]]
@@ -20,6 +23,24 @@
 // PRE17: [[THIRD]] = !DITemplateValueParameter(name: "b", type: !{{[0-9]*}}, defaulted: true, value: i8 1)
 // CXX17: [[THIRD]] = !DITemplateValueParameter(name: "b", type: !{{[0-9]*}}, defaulted: true, value: i1 true)
 
+// DWARF-DUMP:     DW_TAG_class_type
+// DWARF-DUMP:       DW_AT_name      ("foo<char, 3, true, 1>")
+// DWARF-DUMP:       DW_TAG_template_type_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "char")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("T")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:      DW_AT_default_value   (true)
+// DWARF-DUMP:       DW_TAG_template_value_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "int")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("i")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+// DWARF-DUMP:       DW_TAG_template_value_parameter
+// DWARF-DUMP-NEXT:    DW_AT_type    ({{.*}} "bool")
+// DWARF-DUMP-NEXT:    DW_AT_name    ("b")
+// DWARFv4-NEXT:       DW_AT_default_value   (true)
+// STRICT-NOT:         DW_AT_default_value   (true)
+
 template <typename T = char, int i = 3, bool b = true, int x = sizeof(T)>
 class foo {
 };
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -2015,7 +2015,7 @@
     } break;
     case TemplateArgument::Integral: {
       llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
-      if (Args.TList && CGM.getCodeGenOpts().DwarfVersion >= 5)
+      if (Args.TList)
         if (auto *templateType = dyn_cast_or_null<NonTypeTemplateParmDecl>(
                 Args.TList->getParam(i)))
           if (templateType->hasDefaultArgument() &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139928.482416.patch
Type: text/x-patch
Size: 5101 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221213/4710b4e2/attachment.bin>


More information about the llvm-commits mailing list