Skip to content

Commit 3b89cc9

Browse files
authored
[clang][Driver] Default to DIOp-based DIExpressions in SPIRV (llvm#1320)
2 parents 225a606 + 0866fe3 commit 3b89cc9

File tree

9 files changed

+129
-14
lines changed

9 files changed

+129
-14
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4457,8 +4457,8 @@ def gheterogeneous_dwarf_EQ : Joined<["-"], "gheterogeneous-dwarf=">,
44574457
MarshallingInfoEnum<CodeGenOpts<"HeterogeneousDwarfMode">, "Disabled">;
44584458
def gheterogeneous_dwarf : Flag<["-"], "gheterogeneous-dwarf">, Group<g_Group>,
44594459
Visibility<[ClangOption, CC1Option]>,
4460-
HelpText<"Enable DIExpr-based DWARF extensions for heterogeneous debugging">,
4461-
Alias<gheterogeneous_dwarf_EQ>, AliasArgs<["diexpr"]>;
4460+
HelpText<"Enable DIExpression-based DWARF extensions for heterogeneous debugging">,
4461+
Alias<gheterogeneous_dwarf_EQ>, AliasArgs<["diexpression"]>;
44624462
def gno_heterogeneous_dwarf : Flag<["-"], "gno-heterogeneous-dwarf">,
44634463
Visibility<[ClangOption, CC1Option]>,
44644464
Group<g_Group>,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4821,10 +4821,16 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
48214821
renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion);
48224822
RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
48234823

4824-
bool EmitDwarfForAMDGCN = EmitDwarf && T.isAMDGCN();
4824+
bool EmitDwarfForAMDGCN =
4825+
EmitDwarf &&
4826+
(T.isAMDGCN() || (T.isSPIRV() && T.getVendor() == llvm::Triple::AMD));
48254827
if (EmitDwarfForAMDGCN)
48264828
CmdArgs.append({"-mllvm", "-amdgpu-spill-cfi-saved-regs"});
48274829
if (Arg *A = Args.getLastArg(options::OPT_gheterogeneous_dwarf_EQ)) {
4830+
if (StringRef(A->getValue()) == "diexpr" && T.isSPIRV() &&
4831+
T.getVendor() == llvm::Triple::AMD)
4832+
D.Diag(clang::diag::err_drv_unsupported_opt_with_suggestion)
4833+
<< A->getAsString(Args) << "-gheterogeneous-dwarf=diexpression";
48284834
A->render(Args, CmdArgs);
48294835
} else if (EmitDwarfForAMDGCN) {
48304836
#ifndef NDEBUG
@@ -4842,11 +4848,12 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
48424848
assert(Aliased.isValid() && "gheterogeneous-dwarf must be an alias");
48434849
assert(Aliased.getName() == "gheterogeneous-dwarf=" &&
48444850
"gheterogeneous-dwarf must alias gheterogeneous-dwarf=");
4845-
assert(StringRef(GHeterogeneousDwarf.getAliasArgs()) == "diexpr" &&
4846-
GHeterogeneousDwarf.getAliasArgs()[strlen("diexpr") + 1] == '\0' &&
4847-
"gheterogeneous-dwarf must alias gheterogeneous-dwarf=diexpr");
4851+
assert(StringRef(GHeterogeneousDwarf.getAliasArgs()) == "diexpression" &&
4852+
GHeterogeneousDwarf.getAliasArgs()[strlen("diexpression") + 1] ==
4853+
'\0' &&
4854+
"gheterogeneous-dwarf must alias gheterogeneous-dwarf=diexpression");
48484855
#endif
4849-
CmdArgs.push_back("-gheterogeneous-dwarf=diexpr");
4856+
CmdArgs.push_back("-gheterogeneous-dwarf=diexpression");
48504857
}
48514858

48524859
// This controls whether or not we perform JustMyCode instrumentation.

clang/test/CodeGenHIP/debug-info-cc1-option.hip

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
// and aliases the new default. This is needed for transitioning flang-legacy
66
// as it depends on the -cc1 interface.
77

8-
// CHECK: call void @llvm.dbg.def
9-
// CHECK: !DIExpr(
8+
// CHECK: #dbg_declare{{.*}}DIExpression{{.*}}DIOp
109
__attribute__((device)) void kernel1(int Arg) {
1110
int FuncVar;
1211
}

clang/test/Driver/amdgpu-debug.cl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// CHECK-SIMPLE-NOT: "-disable-O0-optnone"
88
// CHECK-SIMPLE-NOT: "-debug-info-kind=line-tables-only"
99
// CHECK-SIMPLE-DAG: "-mllvm" "-amdgpu-spill-cfi-saved-regs"
10-
// CHECK-SIMPLE-DAG: "-gheterogeneous-dwarf=diexpr"
10+
// CHECK-SIMPLE-DAG: "-gheterogeneous-dwarf=diexpression"
1111
// CHECK-SIMPLE-DAG: "-debugger-tuning=gdb"
1212
// CHECK-SIMPLE-NOT: "-disable-O0-optnone"
1313
// CHECK-SIMPLE-NOT: "-debug-info-kind=line-tables-only"
@@ -21,7 +21,7 @@
2121
// Check that -gheterogeneous-dwarf can be enabled for non-AMDGCN
2222
// RUN: %clang -### -target x86_64-linux-gnu -x cl -c -nogpuinc -nogpulib -emit-llvm -gheterogeneous-dwarf %s 2>&1 | FileCheck -check-prefix=CHECK-EXPLICIT-HETEROGENEOUS %s
2323
// CHECK-EXPLICIT-HETEROGENEOUS: "-cc1"
24-
// CHECK-EXPLICIT-HETEROGENEOUS: "-gheterogeneous-dwarf=diexpr"
24+
// CHECK-EXPLICIT-HETEROGENEOUS: "-gheterogeneous-dwarf=diexpression"
2525

2626
// Check that -gheterogeneous-dwarf can be disabled for AMDGCN
2727
// RUN: %clang -### -target amdgcn-amd-amdhsa -x cl -c -nogpuinc -nogpulib -emit-llvm -g -gno-heterogeneous-dwarf %s 2>&1 | FileCheck -check-prefix=CHECK-NO-HETEROGENEOUS %s
@@ -46,3 +46,14 @@
4646
// Check that -gheterogeneous-dwarf= fails for unknown option
4747
// RUN: not %clang -target amdgcn-amd-amdhsa -x cl -c -nogpuinc -nogpulib -emit-llvm -g -gheterogeneous-dwarf=unknown %s 2>&1 | FileCheck -check-prefix=CHECK-UNKNOWN %s
4848
// CHECK-UNKNOWN: error: invalid value
49+
50+
// Check that =diexpression is implied by -g + spirv
51+
// RUN: %clang -### -target spirv64-amd-amdhsa -x cl -c -nogpuinc -nogpulib -emit-llvm -g %s 2>&1 | FileCheck -check-prefix=CHECK-SPIRV %s
52+
// CHECK-SPIRV: "-cc1"
53+
// CHECK-SPIRV-DAG: "-mllvm" "-amdgpu-spill-cfi-saved-regs"
54+
// CHECK-SPIRV-DAG: "-gheterogeneous-dwarf=diexpression"
55+
// CHECK-SPIRV-DAG: "-debugger-tuning=gdb"
56+
57+
// Check that =diexpr produces an error on spirv.
58+
// RUN: not %clang -### -target spirv64-amd-amdhsa -x cl -c -nogpuinc -nogpulib -emit-llvm -g -gheterogeneous-dwarf=diexpr %s 2>&1 | FileCheck -check-prefix=CHECK-SPIRV-ERR %s
59+
// CHECK-SPIRV-ERR: error: unsupported option '-gheterogeneous-dwarf=diexpr'; did you mean '-gheterogeneous-dwarf=diexpression'?

llvm/lib/IR/BasicBlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ cl::opt<cl::boolOrDefault> PreserveInputDbgFormat(
4747
bool WriteNewDbgInfoFormatToBitcode /*set default value in cl::init() below*/;
4848
cl::opt<bool, true> WriteNewDbgInfoFormatToBitcode2(
4949
"write-experimental-debuginfo-iterators-to-bitcode", cl::Hidden,
50-
cl::location(WriteNewDbgInfoFormatToBitcode), cl::init(false));
50+
cl::location(WriteNewDbgInfoFormatToBitcode), cl::init(true));
5151

5252
DbgMarker *BasicBlock::createMarker(Instruction *I) {
5353
assert(IsNewDbgInfoFormat &&

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,12 @@ DbgLabelRecord::createDebugIntrinsic(Module *M,
474474

475475
Value *DbgVariableRecord::getAddress() const {
476476
auto *MD = getRawAddress();
477-
if (auto *V = dyn_cast<ValueAsMetadata>(MD))
477+
if (auto *V = dyn_cast_or_null<ValueAsMetadata>(MD))
478478
return V->getValue();
479479

480480
// When the value goes to null, it gets replaced by an empty MDNode.
481-
assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
481+
assert(!MD ||
482+
!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
482483
return nullptr;
483484
}
484485

llvm/lib/IR/TypeFinder.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
8888
for (const auto &MD : MDForInst)
8989
incorporateMDNode(MD.second);
9090
MDForInst.clear();
91+
92+
// Incorporate types hiding in variable-location information.
93+
for (const auto &Dbg : I.getDbgRecordRange()) {
94+
// Pick out records that have Values.
95+
if (const DbgVariableRecord *DVI =
96+
dyn_cast<DbgVariableRecord>(&Dbg)) {
97+
for (Value *V : DVI->location_ops())
98+
incorporateValue(V);
99+
if (DVI->isDbgAssign()) {
100+
if (Value *Addr = DVI->getAddress())
101+
incorporateValue(Addr);
102+
if (auto *Expr = DVI->getRawAddressExpression())
103+
incorporateMDNode(Expr);
104+
}
105+
if (auto *Expr = DVI->getRawExpression())
106+
incorporateMDNode(Expr);
107+
}
108+
}
91109
}
92110
}
93111

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
2+
3+
; CHECK: %struct.S = type { i32 }
4+
%struct.S = type { i32 }
5+
6+
define dso_local i32 @f() !dbg !7 {
7+
entry:
8+
; CHECK: #dbg_value(ptr null, !9, !DIExpression(DIOpArg(0, ptr), DIOpDeref(%struct.S)), !11)
9+
#dbg_value(ptr null, !9, !DIExpression(DIOpArg(0, ptr), DIOpDeref(%struct.S)), !11)
10+
ret i32 0, !dbg !11
11+
}
12+
13+
!llvm.dbg.cu = !{!0}
14+
!llvm.module.flags = !{!3, !4, !5}
15+
!llvm.ident = !{!6}
16+
17+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 18.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
18+
!1 = !DIFile(filename: "print.c", directory: "/tmp")
19+
!2 = !{}
20+
!3 = !{i32 2, !"Dwarf Version", i32 5}
21+
!4 = !{i32 2, !"Debug Info Version", i32 3}
22+
!5 = !{i32 1, !"wchar_size", i32 4}
23+
!6 = !{!"clang version 18.0.0"}
24+
!7 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 3, type: !8, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
25+
!8 = !DISubroutineType(types: !2)
26+
!9 = !DILocalVariable(name: "a", arg: 1, scope: !7, file: !1, line: 3, type: !10)
27+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
28+
!11 = !DILocation(line: 3, column: 15, scope: !7)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; RUN: opt --passes=verify %s -o - -S | FileCheck %s
2+
3+
;; Test that the type definitions are discovered when serialising to LLVM-IR,
4+
;; even if they're only present inside a DbgRecord, and thus not normally
5+
;; visible.
6+
7+
; CHECK: %union.anon = type { %struct.a }
8+
; CHECK: %struct.a = type { i32 }
9+
; CHECK: %union.anon2 = type { %struct.a2 }
10+
; CHECK: %struct.a2 = type { i32 }
11+
12+
; ModuleID = 'bbi-98372.ll'
13+
source_filename = "bbi-98372.ll"
14+
15+
%union.anon = type { %struct.a }
16+
%struct.a = type { i32 }
17+
%union.anon2 = type { %struct.a2 }
18+
%struct.a2 = type { i32 }
19+
20+
@d = global [1 x { i16, i16 }] [{ i16, i16 } { i16 0, i16 undef }], align 1
21+
@e = global [1 x { i16, i16 }] [{ i16, i16 } { i16 0, i16 undef }], align 1
22+
23+
define void @f() {
24+
entry:
25+
#dbg_value(ptr getelementptr inbounds ([1 x %union.anon], ptr @d, i32 0, i32 3), !7, !DIExpression(), !14)
26+
#dbg_assign(ptr null, !7, !DIExpression(), !16, ptr getelementptr inbounds ([1 x %union.anon2], ptr @e, i32 0, i32 3), !17, !14)
27+
ret void, !dbg !15
28+
}
29+
30+
!llvm.dbg.cu = !{!0}
31+
!llvm.module.flags = !{!2, !3, !4, !5}
32+
!llvm.ident = !{!6}
33+
34+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
35+
!1 = !DIFile(filename: "foo.c", directory: "/bar")
36+
!2 = !{i32 7, !"Dwarf Version", i32 4}
37+
!3 = !{i32 2, !"Debug Info Version", i32 3}
38+
!4 = !{i32 1, !"wchar_size", i32 1}
39+
!5 = !{i32 7, !"frame-pointer", i32 2}
40+
!6 = !{!"clang"}
41+
!7 = !DILocalVariable(name: "f", scope: !8, file: !1, line: 8, type: !12)
42+
!8 = distinct !DISubprogram(name: "e", scope: !1, file: !1, line: 8, type: !9, scopeLine: 8, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
43+
!9 = !DISubroutineType(types: !10)
44+
!10 = !{null}
45+
!11 = !{!7}
46+
!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 16)
47+
!13 = !DIBasicType(name: "int", size: 16, encoding: DW_ATE_signed)
48+
!14 = !DILocation(line: 0, scope: !8)
49+
!15 = !DILocation(line: 8, column: 28, scope: !8)
50+
!16 = distinct !DIAssignID()
51+
!17 = !DIExpression()

0 commit comments

Comments
 (0)