Skip to content

Commit 093814a

Browse files
authored
[DebugInfo] Handle null value of DW_TAG_template_value_parameter (#1956)
It might be set as null in case if a function pointer is passed as auto template parameter. The patch also adds a test for a 'good' DW_TAG_template_value_parameter value for function pointers, just because this case was untested. Signed-off-by: Sidorov, Dmitry <[email protected]>
1 parent 373d46c commit 093814a

File tree

3 files changed

+221
-2
lines changed

3 files changed

+221
-2
lines changed

lib/SPIRV/LLVMToSPIRVDbgTran.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,14 @@ LLVMToSPIRVDbgTran::transDbgTemplateParameter(const DITemplateParameter *TP) {
10141014
Ops[ValueIdx] = getDebugInfoNoneId();
10151015
if (TP->getTag() == dwarf::DW_TAG_template_value_parameter) {
10161016
const DITemplateValueParameter *TVP = cast<DITemplateValueParameter>(TP);
1017-
Constant *C = cast<ConstantAsMetadata>(TVP->getValue())->getValue();
1018-
Ops[ValueIdx] = SPIRVWriter->transValue(C, nullptr)->getId();
1017+
if (auto *TVVal = TVP->getValue()) {
1018+
Constant *C = cast<ConstantAsMetadata>(TVVal)->getValue();
1019+
Ops[ValueIdx] = SPIRVWriter->transValue(C, nullptr)->getId();
1020+
} else {
1021+
SPIRVType *TyPtr =
1022+
SPIRVWriter->transType(PointerType::get(M->getContext(), 0));
1023+
Ops[ValueIdx] = BM->addNullConstant(TyPtr)->getId();
1024+
}
10191025
}
10201026
Ops[SourceIdx] = getDebugInfoNoneId();
10211027
Ops[LineIdx] = 0; // This version of DITemplateParameter has no line number
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
;; compiled from (null added manually):
2+
;; template <auto Func>
3+
;; int foo() {
4+
;; int result = Func();
5+
;; return result;
6+
;; };
7+
;;
8+
;; long get() { return 42; }
9+
;;
10+
;; void boo() {
11+
;; int val = foo<get>();
12+
;; }
13+
14+
; REQUIRES: object-emission
15+
16+
; RUN: llvm-as < %s -o %t.bc
17+
; RUN: llvm-spirv %t.bc -o %t.spv
18+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o - | llvm-dis -o %t.ll
19+
; RUN: FileCheck < %t.ll %s --check-prefix=CHECK-LLVM
20+
21+
; RUN: llc -mtriple=x86_64-linux -O0 -filetype=obj < %t.ll | llvm-dwarfdump -v -debug-info - | FileCheck %s --check-prefix=CHECK-DWARF
22+
23+
; CHECK-LLVM: ![[#]] = !DITemplateValueParameter(name: "Func", type: ![[#Type:]], value: ptr null)
24+
; CHECK-LLVM: ![[#Type]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: 64)
25+
26+
; CHECK-DWARF: DW_TAG_subprogram
27+
; CHECK-DWARF: DW_AT_name{{.*}}"foo<&get>"
28+
29+
; CHECK-DWARF: DW_TAG_template_value_parameter
30+
; CHECK-DWARF: DW_AT_type {{.*}} "int (*)()"
31+
; CHECK-DWARF: DW_AT_name {{.*}} "Func"
32+
33+
; ModuleID = '/app/example.cpp'
34+
source_filename = "/app/example.cpp"
35+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
36+
target triple = "spir64-unknown-unknown"
37+
38+
$_Z3fooIXadL_Z3getvEEEiv = comdat any
39+
40+
; Function Attrs: mustprogress noinline nounwind optnone uwtable
41+
define dso_local noundef i32 @_Z3getv() #0 !dbg !10 {
42+
ret i32 42, !dbg !16
43+
}
44+
45+
; Function Attrs: mustprogress noinline optnone uwtable
46+
define dso_local void @_Z3boov() #1 !dbg !17 {
47+
%1 = alloca i32, align 4
48+
call void @llvm.dbg.declare(metadata ptr %1, metadata !20, metadata !DIExpression()), !dbg !21
49+
%2 = call noundef i32 @_Z3fooIXadL_Z3getvEEEiv(), !dbg !22
50+
store i32 %2, ptr %1, align 4, !dbg !21
51+
ret void, !dbg !23
52+
}
53+
54+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
55+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
56+
57+
; Function Attrs: mustprogress noinline nounwind optnone uwtable
58+
define linkonce_odr dso_local noundef i32 @_Z3fooIXadL_Z3getvEEEiv() #0 comdat !dbg !24 {
59+
%1 = alloca i32, align 4
60+
call void @llvm.dbg.declare(metadata ptr %1, metadata !28, metadata !DIExpression()), !dbg !29
61+
%2 = call noundef i32 @_Z3getv(), !dbg !30
62+
store i32 %2, ptr %1, align 4, !dbg !29
63+
%3 = load i32, ptr %1, align 4, !dbg !31
64+
ret i32 %3, !dbg !32
65+
}
66+
67+
attributes #0 = { mustprogress noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
68+
attributes #1 = { mustprogress noinline optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
69+
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
70+
71+
!llvm.dbg.cu = !{!0}
72+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
73+
!llvm.ident = !{!9}
74+
75+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0 (https://github.com/llvm/llvm-project.git ef38880ce03bc1f1fb3606c5a629151f3d0e975e)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
76+
!1 = !DIFile(filename: "/app/example.cpp", directory: "/app")
77+
!2 = !{i32 7, !"Dwarf Version", i32 4}
78+
!3 = !{i32 2, !"Debug Info Version", i32 3}
79+
!4 = !{i32 1, !"wchar_size", i32 4}
80+
!5 = !{i32 8, !"PIC Level", i32 2}
81+
!6 = !{i32 7, !"PIE Level", i32 2}
82+
!7 = !{i32 7, !"uwtable", i32 2}
83+
!8 = !{i32 7, !"frame-pointer", i32 2}
84+
!9 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git ef38880ce03bc1f1fb3606c5a629151f3d0e975e)"}
85+
!10 = distinct !DISubprogram(name: "get", linkageName: "_Z3getv", scope: !11, file: !11, line: 7, type: !12, scopeLine: 7, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !15)
86+
!11 = !DIFile(filename: "example.cpp", directory: "/app")
87+
!12 = !DISubroutineType(types: !13)
88+
!13 = !{!14}
89+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
90+
!15 = !{}
91+
!16 = !DILocation(line: 7, column: 13, scope: !10)
92+
!17 = distinct !DISubprogram(name: "boo", linkageName: "_Z3boov", scope: !11, file: !11, line: 9, type: !18, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !15)
93+
!18 = !DISubroutineType(types: !19)
94+
!19 = !{null}
95+
!20 = !DILocalVariable(name: "val", scope: !17, file: !11, line: 10, type: !14)
96+
!21 = !DILocation(line: 10, column: 9, scope: !17)
97+
!22 = !DILocation(line: 10, column: 15, scope: !17)
98+
!23 = !DILocation(line: 11, column: 1, scope: !17)
99+
!24 = distinct !DISubprogram(name: "foo<&get>", linkageName: "_Z3fooIXadL_Z3getvEEEiv", scope: !11, file: !11, line: 2, type: !12, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, templateParams: !25, retainedNodes: !15)
100+
!25 = !{!26}
101+
!26 = !DITemplateValueParameter(name: "Func", type: !27, value: null)
102+
!27 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
103+
!28 = !DILocalVariable(name: "result", scope: !24, file: !11, line: 3, type: !14)
104+
!29 = !DILocation(line: 3, column: 9, scope: !24)
105+
!30 = !DILocation(line: 3, column: 18, scope: !24)
106+
!31 = !DILocation(line: 4, column: 12, scope: !24)
107+
!32 = !DILocation(line: 4, column: 5, scope: !24)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
;; compiled from:
2+
;; template <auto Func>
3+
;; int foo() {
4+
;; int result = Func();
5+
;; return result;
6+
;; };
7+
;;
8+
;; long get() { return 42; }
9+
;;
10+
;; void boo() {
11+
;; int val = foo<get>();
12+
;; }
13+
14+
; REQUIRES: object-emission
15+
16+
; RUN: llvm-as < %s -o %t.bc
17+
; RUN: llvm-spirv %t.bc -o %t.spv
18+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o - | llvm-dis -o %t.ll
19+
; RUN: FileCheck < %t.ll %s --check-prefix=CHECK-LLVM
20+
; RUN: llc -mtriple=x86_64-linux -O0 -filetype=obj < %t.ll | llvm-dwarfdump -v -debug-info - | FileCheck %s --check-prefix=CHECK-DWARF
21+
22+
; CHECK-LLVM: ![[#]] = !DITemplateValueParameter(name: "Func", type: ![[#Type:]], value: ptr @_Z3getv)
23+
; CHECK-LLVM: ![[#Type]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: 64)
24+
25+
; CHECK-DWARF: DW_TAG_subprogram
26+
; CHECK-DWARF: DW_AT_name{{.*}}"foo<&get>"
27+
28+
; CHECK-DWARF: DW_TAG_template_value_parameter
29+
; CHECK-DWARF: DW_AT_type {{.*}} "int (*)()"
30+
; CHECK-DWARF: DW_AT_name {{.*}} "Func"
31+
32+
; ModuleID = '/app/example.cpp'
33+
source_filename = "/app/example.cpp"
34+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
35+
target triple = "spir64-unknown-unknown"
36+
37+
$_Z3fooIXadL_Z3getvEEEiv = comdat any
38+
39+
; Function Attrs: mustprogress noinline nounwind optnone uwtable
40+
define dso_local noundef i32 @_Z3getv() #0 !dbg !10 {
41+
ret i32 42, !dbg !16
42+
}
43+
44+
; Function Attrs: mustprogress noinline optnone uwtable
45+
define dso_local void @_Z3boov() #1 !dbg !17 {
46+
%1 = alloca i32, align 4
47+
call void @llvm.dbg.declare(metadata ptr %1, metadata !20, metadata !DIExpression()), !dbg !21
48+
%2 = call noundef i32 @_Z3fooIXadL_Z3getvEEEiv(), !dbg !22
49+
store i32 %2, ptr %1, align 4, !dbg !21
50+
ret void, !dbg !23
51+
}
52+
53+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
54+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
55+
56+
; Function Attrs: mustprogress noinline nounwind optnone uwtable
57+
define linkonce_odr dso_local noundef i32 @_Z3fooIXadL_Z3getvEEEiv() #0 comdat !dbg !24 {
58+
%1 = alloca i32, align 4
59+
call void @llvm.dbg.declare(metadata ptr %1, metadata !28, metadata !DIExpression()), !dbg !29
60+
%2 = call noundef i32 @_Z3getv(), !dbg !30
61+
store i32 %2, ptr %1, align 4, !dbg !29
62+
%3 = load i32, ptr %1, align 4, !dbg !31
63+
ret i32 %3, !dbg !32
64+
}
65+
66+
attributes #0 = { mustprogress noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
67+
attributes #1 = { mustprogress noinline optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
68+
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
69+
70+
!llvm.dbg.cu = !{!0}
71+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
72+
!llvm.ident = !{!9}
73+
74+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0 (https://github.com/llvm/llvm-project.git ef38880ce03bc1f1fb3606c5a629151f3d0e975e)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
75+
!1 = !DIFile(filename: "/app/example.cpp", directory: "/app")
76+
!2 = !{i32 7, !"Dwarf Version", i32 4}
77+
!3 = !{i32 2, !"Debug Info Version", i32 3}
78+
!4 = !{i32 1, !"wchar_size", i32 4}
79+
!5 = !{i32 8, !"PIC Level", i32 2}
80+
!6 = !{i32 7, !"PIE Level", i32 2}
81+
!7 = !{i32 7, !"uwtable", i32 2}
82+
!8 = !{i32 7, !"frame-pointer", i32 2}
83+
!9 = !{!"clang version 17.0.0 (https://github.com/llvm/llvm-project.git ef38880ce03bc1f1fb3606c5a629151f3d0e975e)"}
84+
!10 = distinct !DISubprogram(name: "get", linkageName: "_Z3getv", scope: !11, file: !11, line: 7, type: !12, scopeLine: 7, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !15)
85+
!11 = !DIFile(filename: "example.cpp", directory: "/app")
86+
!12 = !DISubroutineType(types: !13)
87+
!13 = !{!14}
88+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
89+
!15 = !{}
90+
!16 = !DILocation(line: 7, column: 13, scope: !10)
91+
!17 = distinct !DISubprogram(name: "boo", linkageName: "_Z3boov", scope: !11, file: !11, line: 9, type: !18, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !15)
92+
!18 = !DISubroutineType(types: !19)
93+
!19 = !{null}
94+
!20 = !DILocalVariable(name: "val", scope: !17, file: !11, line: 10, type: !14)
95+
!21 = !DILocation(line: 10, column: 9, scope: !17)
96+
!22 = !DILocation(line: 10, column: 15, scope: !17)
97+
!23 = !DILocation(line: 11, column: 1, scope: !17)
98+
!24 = distinct !DISubprogram(name: "foo<&get>", linkageName: "_Z3fooIXadL_Z3getvEEEiv", scope: !11, file: !11, line: 2, type: !12, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, templateParams: !25, retainedNodes: !15)
99+
!25 = !{!26}
100+
!26 = !DITemplateValueParameter(name: "Func", type: !27, value: ptr @_Z3getv)
101+
!27 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
102+
!28 = !DILocalVariable(name: "result", scope: !24, file: !11, line: 3, type: !14)
103+
!29 = !DILocation(line: 3, column: 9, scope: !24)
104+
!30 = !DILocation(line: 3, column: 18, scope: !24)
105+
!31 = !DILocation(line: 4, column: 12, scope: !24)
106+
!32 = !DILocation(line: 4, column: 5, scope: !24)

0 commit comments

Comments
 (0)