Skip to content

[llvm][DebugInfo] Emit DW_AT_const_value for float non-type template parameters #127045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025

Conversation

Michael137
Copy link
Member

In C++20, non-type template parameters can be float/double. Clang didn't emit those constants in DWARF. This patch emits floating point constants the same way we do other integral template value parameters.

@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2025

@llvm/pr-subscribers-debuginfo

Author: Michael Buch (Michael137)

Changes

In C++20, non-type template parameters can be float/double. Clang didn't emit those constants in DWARF. This patch emits floating point constants the same way we do other integral template value parameters.


Full diff: https://github.com/llvm/llvm-project/pull/127045.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp (+2)
  • (modified) llvm/test/DebugInfo/X86/debug-info-template-parameter.ll (+29-8)
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index c0f2878c84bc8..5347c8a049ba6 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -1147,6 +1147,8 @@ void DwarfUnit::constructTemplateValueParameterDIE(
   if (Metadata *Val = VP->getValue()) {
     if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
       addConstantValue(ParamDIE, CI, VP->getType());
+    else if (ConstantFP *CF = mdconst::dyn_extract<ConstantFP>(Val))
+      addConstantFPValue(ParamDIE, CF);
     else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
       // We cannot describe the location of dllimport'd entities: the
       // computation of their address requires loads from the IAT.
diff --git a/llvm/test/DebugInfo/X86/debug-info-template-parameter.ll b/llvm/test/DebugInfo/X86/debug-info-template-parameter.ll
index e552bb02b03cb..e2a92155d718d 100644
--- a/llvm/test/DebugInfo/X86/debug-info-template-parameter.ll
+++ b/llvm/test/DebugInfo/X86/debug-info-template-parameter.ll
@@ -3,12 +3,12 @@
 
 ; C++ source to regenerate:
 
-;template <typename T = char, int i = 3 >
+;template <typename T = char, int i = 3, float f = 1.0f, double d = 2.0>
 ;class foo {
 ;};
 ;
 ;int main() {
-; foo<int,6> f1;
+; foo<int, 6, 1.9f, 1.9> f1;
 ; foo<> f2;
 ; return 0;
 ;}
@@ -20,21 +20,36 @@
 
 ; CHECK: debug_info contents:
 
-; CHECK: DW_AT_name {{.*}} "foo<int, 6>"
+; CHECK: DW_AT_name {{.*}} "foo<int, 6, 1.900000e+00, 1.900000e+00>"
 ; CHECK: DW_AT_type {{.*}} "int"
 ; CHECK-NEXT: DW_AT_name {{.*}} "T"
 ; CHECK-NOT: DW_AT_default_value
 ; CHECK: DW_AT_type {{.*}} "int"
 ; CHECK-NEXT: DW_AT_name {{.*}} "i"
+; CHECK-NEXT: DW_AT_const_value [DW_FORM_sdata] (6)
+; CHECK-NOT: DW_AT_default_value
+; CHECK: DW_AT_type {{.*}} "float"
+; CHECK-NEXT: DW_AT_name {{.*}} "f"
+; CHECK-NEXT: DW_AT_const_value [DW_FORM_udata] (1072902963)
+; CHECK-NOT: DW_AT_default_value
+; CHECK: DW_AT_type {{.*}} "double"
+; CHECK-NEXT: DW_AT_name {{.*}} "d"
+; CHECK-NEXT: DW_AT_const_value [DW_FORM_udata] (4611235658464650854)
 ; CHECK-NOT: DW_AT_default_value
 
-; CHECK: DW_AT_name {{.*}} "foo<char, 3>"
+; CHECK: DW_AT_name {{.*}} "foo<char, 3, 1.000000e+00, 2.000000e+00>"
 ; CHECK: DW_AT_type {{.*}} "char"
 ; CHECK-NEXT: DW_AT_name {{.*}} "T"
 ; CHECK-NEXT: DW_AT_default_value {{.*}} (true)
 ; CHECK: DW_AT_type {{.*}} "int"
 ; CHECK-NEXT: DW_AT_name {{.*}} "i"
 ; CHECK-NEXT: DW_AT_default_value {{.*}} (true)
+; CHECK: DW_AT_type {{.*}} "float"
+; CHECK-NEXT: DW_AT_name {{.*}} "f"
+; CHECK-NEXT: DW_AT_default_value {{.*}} (true)
+; CHECK: DW_AT_type {{.*}} "double"
+; CHECK-NEXT: DW_AT_name {{.*}} "d"
+; CHECK-NEXT: DW_AT_default_value {{.*}} (true)
 
 ; ModuleID = '/dir/test.cpp'
 source_filename = "test.cpp"
@@ -76,16 +91,22 @@ attributes #1 = { nounwind readnone speculatable willreturn }
 !9 = !{!10}
 !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
 !11 = !DILocalVariable(name: "f1", scope: !7, file: !1, line: 30, type: !12)
-!12 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "foo<int, 6>", file: !1, line: 26, size: 8, flags: DIFlagTypePassByValue, elements: !2, templateParams: !13, identifier: "_ZTS3fooIiLi6EE")
-!13 = !{!14, !15}
+!12 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "foo<int, 6, 1.900000e+00, 1.900000e+00>", file: !1, line: 26, size: 8, flags: DIFlagTypePassByValue, elements: !2, templateParams: !13, identifier: "_ZTS3fooIiLi6ELf3ff33333ELd3ffe666666666666EE")
+!13 = !{!14, !15, !25, !27}
 !14 = !DITemplateTypeParameter(name: "T", type: !10)
 !15 = !DITemplateValueParameter(name: "i", type: !10, value: i32 6)
 !16 = !DILocation(line: 30, column: 14, scope: !7)
 !17 = !DILocalVariable(name: "f2", scope: !7, file: !1, line: 31, type: !18)
-!18 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "foo<char, 3>", file: !1, line: 26, size: 8, flags: DIFlagTypePassByValue, elements: !2, templateParams: !19, identifier: "_ZTS3fooIcLi3EE")
-!19 = !{!20, !22}
+!18 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "foo<char, 3, 1.000000e+00, 2.000000e+00>", file: !1, line: 26, size: 8, flags: DIFlagTypePassByValue, elements: !2, templateParams: !19, identifier: "_ZTS3fooIcLi3ELf3f800000ELd4000000000000000EE")
+!19 = !{!20, !22, !29, !30}
 !20 = !DITemplateTypeParameter(name: "T", type: !21, defaulted: true)
 !21 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
 !22 = !DITemplateValueParameter(name: "i", type: !10, defaulted: true, value: i32 3)
 !23 = !DILocation(line: 31, column: 9, scope: !7)
 !24 = !DILocation(line: 32, column: 3, scope: !7)
+!25 = !DITemplateValueParameter(name: "f", type: !26, value: float 0x3FFE666660000000)
+!26 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
+!27 = !DITemplateValueParameter(name: "d", type: !28, value: double 1.900000e+00)
+!28 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
+!29 = !DITemplateValueParameter(name: "f", type: !26, defaulted: true, value: float 1.000000e+00)
+!30 = !DITemplateValueParameter(name: "d", type: !28, defaulted: true, value: double 2.000000e+00)

Copy link
Contributor

@OCHyams OCHyams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - I'll leave it a bit to give others a chance to jump in/approve (since I've not been very active upstream for a bit).

@Michael137 Michael137 merged commit 41f96f9 into llvm:main Feb 13, 2025
10 checks passed
Michael137 added a commit that referenced this pull request Feb 13, 2025
LLVM started emitting the `DW_AT_const_value` for floating point template parameters since #127045. Adjust the expected type in this test. It's still not quite correct but that requires a separate fix in LLDB.
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Feb 14, 2025
…type

LLVM started emitting the `DW_AT_const_value` for floating point template parameters since llvm/llvm-project#127045. Adjust the expected type in this test. It's still not quite correct but that requires a separate fix in LLDB.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…parameters (llvm#127045)

In C++20, non-type template parameters can be float/double. Clang didn't
emit those constants in DWARF. This patch emits floating point constants
the same way we do other integral template value parameters.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
LLVM started emitting the `DW_AT_const_value` for floating point template parameters since llvm#127045. Adjust the expected type in this test. It's still not quite correct but that requires a separate fix in LLDB.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…parameters (llvm#127045)

In C++20, non-type template parameters can be float/double. Clang didn't
emit those constants in DWARF. This patch emits floating point constants
the same way we do other integral template value parameters.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
LLVM started emitting the `DW_AT_const_value` for floating point template parameters since llvm#127045. Adjust the expected type in this test. It's still not quite correct but that requires a separate fix in LLDB.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants