Skip to content

Commit 9c06d42

Browse files
wenju-hevmaksimo
authored andcommitted
Fix debug info of work-item builtin translation (#745)
debug info of work-item builtins are lost in both llvm IR -> spirv and spirv -> llvm IR translations. See #744
1 parent 5b563a0 commit 9c06d42

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

llvm-spirv/lib/SPIRV/OCL20ToSPIRV.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,11 +1257,16 @@ void OCL20ToSPIRV::transWorkItemBuiltinsToVariables() {
12571257
for (auto UI = I.user_begin(), UE = I.user_end(); UI != UE; ++UI) {
12581258
auto CI = dyn_cast<CallInst>(*UI);
12591259
assert(CI && "invalid instruction");
1260-
Value *NewValue = new LoadInst(GVType, BV, "", CI);
1260+
const DebugLoc &DLoc = CI->getDebugLoc();
1261+
Instruction *NewValue = new LoadInst(GVType, BV, "", CI);
1262+
if (DLoc)
1263+
NewValue->setDebugLoc(DLoc);
12611264
LLVM_DEBUG(dbgs() << "Transform: " << *CI << " => " << *NewValue << '\n');
12621265
if (IsVec) {
12631266
NewValue =
12641267
ExtractElementInst::Create(NewValue, CI->getArgOperand(0), "", CI);
1268+
if (DLoc)
1269+
NewValue->setDebugLoc(DLoc);
12651270
LLVM_DEBUG(dbgs() << *NewValue << '\n');
12661271
}
12671272
NewValue->takeName(CI);

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,16 @@ bool SPIRVToLLVM::transOCLBuiltinFromVariable(GlobalVariable *GV,
340340
LD->getPointerOperandType()->getPointerElementType());
341341
Value *EmptyVec = UndefValue::get(VecTy);
342342
Vectors.push_back(EmptyVec);
343+
const DebugLoc &DLoc = LD->getDebugLoc();
343344
for (unsigned I = 0; I < VecTy->getNumElements(); ++I) {
344345
auto *Idx = ConstantInt::get(Type::getInt32Ty(*Context), I);
345346
auto *Call = CallInst::Create(Func, {Idx}, "", LD);
347+
if (DLoc)
348+
Call->setDebugLoc(DLoc);
346349
setAttrByCalledFunc(Call);
347350
auto *Insert = InsertElementInst::Create(Vectors.back(), Call, Idx);
351+
if (DLoc)
352+
Insert->setDebugLoc(DLoc);
348353
Insert->insertAfter(Call);
349354
Vectors.push_back(Insert);
350355
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
; Check debug info of builtin get_global_id is preserved from LLVM IR to spirv
2+
; and spirv to LLVM IR translation.
3+
4+
; Original .cl source:
5+
; kernel void test() {
6+
; size_t gid = get_global_id(0);
7+
; }
8+
9+
; Command line:
10+
; ./clang -cc1 1.cl -triple spir64 -cl-std=cl2.0 -emit-llvm -finclude-default-header -debug-info-kind=line-tables-only -O0
11+
12+
; RUN: llvm-as %s -o %t.bc
13+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV
14+
; RUN: llvm-spirv %t.bc -o %t.spv
15+
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s
16+
17+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
18+
target triple = "spir64"
19+
20+
; CHECK-SPIRV: ExtInst {{.*}} DebugScope
21+
; CHECK-SPIRV-NEXT: Line {{[0-9]+}} 2 16
22+
; CHECK-SPIRV-NEXT: Load {{[0-9]+}} [[LoadRes:[0-9]+]]
23+
; CHECK-SPIRV-NEXT: CompositeExtract {{[0-9]+}} {{[0-9]+}} [[LoadRes]] 0
24+
25+
; Function Attrs: convergent noinline norecurse nounwind optnone
26+
define spir_kernel void @test() #0 !dbg !7 !kernel_arg_addr_space !2 !kernel_arg_access_qual !2 !kernel_arg_type !2 !kernel_arg_base_type !2 !kernel_arg_type_qual !2 {
27+
entry:
28+
%gid = alloca i64, align 8
29+
%call = call spir_func i64 @_Z13get_global_idj(i32 0) #2, !dbg !10
30+
; CHECK: [[I0:%[0-9]]] = call spir_func i64 @_Z13get_global_idj(i32 0) #1, !dbg [[DBG:![0-9]+]]
31+
; CHECK-NEXT: [[I1:%[0-9]]] = insertelement <3 x i64> undef, i64 [[I0]], i32 0, !dbg [[DBG]]
32+
; CHECK-NEXT: [[I2:%[0-9]]] = call spir_func i64 @_Z13get_global_idj(i32 1) #1, !dbg [[DBG]]
33+
; CHECK-NEXT: [[I3:%[0-9]]] = insertelement <3 x i64> [[I1]], i64 [[I2]], i32 1, !dbg [[DBG]]
34+
; CHECK-NEXT: [[I4:%[0-9]]] = call spir_func i64 @_Z13get_global_idj(i32 2) #1, !dbg [[DBG]]
35+
; CHECK-NEXT: [[I5:%[0-9]]] = insertelement <3 x i64> [[I3]], i64 [[I4]], i32 2, !dbg [[DBG]]
36+
; CHECK-NEXT: %call = extractelement <3 x i64> [[I5]], i32 0, !dbg [[DBG]]
37+
store i64 %call, i64* %gid, align 8, !dbg !11
38+
ret void, !dbg !12
39+
}
40+
41+
; Function Attrs: convergent nounwind readnone
42+
declare spir_func i64 @_Z13get_global_idj(i32) #1
43+
44+
attributes #0 = { convergent noinline norecurse nounwind optnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "uniform-work-group-size"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
45+
attributes #1 = { convergent nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
46+
attributes #2 = { convergent nounwind readnone }
47+
48+
!llvm.dbg.cu = !{!0}
49+
!llvm.module.flags = !{!3, !4}
50+
!opencl.ocl.version = !{!5}
51+
!opencl.spir.version = !{!5}
52+
!llvm.ident = !{!6}
53+
54+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (https://github.com/llvm/llvm-project.git b5bc56da8aa23dc57db9d286b0591dbcf9b1bdd3)", isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2, nameTableKind: None)
55+
!1 = !DIFile(filename: "<stdin>", directory: "")
56+
!2 = !{}
57+
!3 = !{i32 2, !"Debug Info Version", i32 3}
58+
!4 = !{i32 1, !"wchar_size", i32 4}
59+
!5 = !{i32 2, i32 0}
60+
!6 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git b5bc56da8aa23dc57db9d286b0591dbcf9b1bdd3)"}
61+
!7 = distinct !DISubprogram(name: "test", scope: !8, file: !8, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
62+
!8 = !DIFile(filename: "1.cl", directory: "")
63+
!9 = !DISubroutineType(types: !2)
64+
!10 = !DILocation(line: 2, column: 16, scope: !7)
65+
!11 = !DILocation(line: 2, column: 10, scope: !7)
66+
!12 = !DILocation(line: 3, column: 1, scope: !7)

0 commit comments

Comments
 (0)