Skip to content

Commit 6278682

Browse files
committed
In spir functions, llvm.dbg.declare intrinsics created
for parameters and locals need to refer to the stack allocation in the alloca address space.
1 parent 04fa7cb commit 6278682

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,7 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
14471447

14481448
if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
14491449
address = OpenMPLocalAddr;
1450+
AllocaAddr = OpenMPLocalAddr;
14501451
} else if (Ty->isConstantSizeType()) {
14511452
// If this value is an array or struct with a statically determinable
14521453
// constant initializer, there are optimizations we can do.
@@ -1492,6 +1493,7 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
14921493
// return slot, so that we can elide the copy when returning this
14931494
// variable (C++0x [class.copy]p34).
14941495
address = ReturnValue;
1496+
AllocaAddr = ReturnValue;
14951497

14961498
if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
14971499
const auto *RD = RecordTy->getDecl();
@@ -1503,7 +1505,8 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
15031505
// applied.
15041506
llvm::Value *Zero = Builder.getFalse();
15051507
Address NRVOFlag =
1506-
CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo");
1508+
CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo",
1509+
/*ArraySize=*/nullptr, &AllocaAddr);
15071510
EnsureInsertPoint();
15081511
Builder.CreateStore(Zero, NRVOFlag);
15091512

@@ -1605,10 +1608,11 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
16051608
DI->setLocation(D.getLocation());
16061609

16071610
// If NRVO, use a pointer to the return address.
1608-
if (UsePointerValue)
1611+
if (UsePointerValue) {
16091612
DebugAddr = ReturnValuePointer;
1610-
1611-
(void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder,
1613+
AllocaAddr = ReturnValuePointer;
1614+
}
1615+
(void)DI->EmitDeclareOfAutoVariable(&D, AllocaAddr.getPointer(), Builder,
16121616
UsePointerValue);
16131617
}
16141618

@@ -2450,6 +2454,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
24502454
}
24512455

24522456
Address DeclPtr = Address::invalid();
2457+
Address AllocaPtr = Address::invalid();
24532458
bool DoStore = false;
24542459
bool IsScalar = hasScalarEvaluationKind(Ty);
24552460
// If we already have a pointer to the argument, reuse the input pointer.
@@ -2464,6 +2469,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
24642469
// from the default address space.
24652470
auto AllocaAS = CGM.getASTAllocaAddressSpace();
24662471
auto *V = DeclPtr.getPointer();
2472+
AllocaPtr = DeclPtr;
24672473
auto SrcLangAS = getLangOpts().OpenCL ? LangAS::opencl_private : AllocaAS;
24682474
auto DestLangAS =
24692475
getLangOpts().OpenCL ? LangAS::opencl_private : LangAS::Default;
@@ -2500,10 +2506,11 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
25002506
: Address::invalid();
25012507
if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
25022508
DeclPtr = OpenMPLocalAddr;
2509+
AllocaPtr = DeclPtr;
25032510
} else {
25042511
// Otherwise, create a temporary to hold the value.
25052512
DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
2506-
D.getName() + ".addr");
2513+
D.getName() + ".addr", &AllocaPtr);
25072514
}
25082515
DoStore = true;
25092516
}
@@ -2579,7 +2586,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
25792586
if (CGDebugInfo *DI = getDebugInfo()) {
25802587
if (CGM.getCodeGenOpts().hasReducedDebugInfo() && !CurFuncIsThunk) {
25812588
llvm::DILocalVariable *DILocalVar = DI->EmitDeclareOfArgVariable(
2582-
&D, DeclPtr.getPointer(), ArgNo, Builder);
2589+
&D, AllocaPtr.getPointer(), ArgNo, Builder);
25832590
if (const auto *Var = dyn_cast_or_null<ParmVarDecl>(&D))
25842591
DI->getParamDbgMappings().insert({Var, DILocalVar});
25852592
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %clang_cc1 %s -o - -O0 -emit-llvm \
2+
// RUN: -triple spir64-unknown-unknown \
3+
// RUN: -aux-triple x86_64-unknown-linux-gnu \
4+
// RUN: -fsycl-is-device \
5+
// RUN: -finclude-default-header \
6+
// RUN: -debug-info-kind=limited -gno-column-info \
7+
// RUN: | FileCheck %s
8+
//
9+
// In spir functions, validate the llvm.dbg.declare intrinsics created for
10+
// parameters and locals refer to the stack allocation in the alloca address
11+
// space.
12+
//
13+
14+
#define KERNEL __attribute__((sycl_kernel))
15+
16+
template <typename KernelName, typename KernelType>
17+
KERNEL void parallel_for(const KernelType &KernelFunc) {
18+
KernelFunc();
19+
}
20+
21+
void my_kernel(int my_param) {
22+
int my_local = 0;
23+
my_local = my_param;
24+
}
25+
26+
int my_host() {
27+
parallel_for<class K>([=]() { my_kernel(42); });
28+
return 0;
29+
}
30+
31+
// CHECK: define {{.*}}spir_func void @_Z9my_kerneli(
32+
// CHECK-SAME i32 %my_param
33+
// CHECK-SAME: !dbg [[MY_KERNEL:![0-9]+]]
34+
// CHECK-SAME: {
35+
// CHECK: %my_param.addr = alloca i32, align 4
36+
// CHECK: %my_local = alloca i32, align 4
37+
// CHECK: call void @llvm.dbg.declare(
38+
// CHECK-SAME: metadata i32* %my_param.addr,
39+
// CHECK-SAME: metadata [[MY_PARAM:![0-9]+]],
40+
// CHECK-SAME: metadata !DIExpression(DW_OP_constu, 4, DW_OP_swap, DW_OP_xderef)
41+
// CHECK-SAME: )
42+
// CHECK: call void @llvm.dbg.declare(
43+
// CHECK-SAME: metadata i32* %my_local,
44+
// CHECK-SAME: metadata [[MY_LOCAL:![0-9]+]],
45+
// CHECK-SAME: metadata !DIExpression(DW_OP_constu, 4, DW_OP_swap, DW_OP_xderef)
46+
// CHECK-SAME: )
47+
// CHECK: }
48+
49+
// CHECK: [[MY_KERNEL]] = distinct !DISubprogram(
50+
// CHECK-SAME: name: "my_kernel"
51+
// CHECK-SAME: )
52+
// CHECK: [[MY_PARAM]] = !DILocalVariable(
53+
// CHECK-SAME: name: "my_param"
54+
// CHECK-SAME: arg: 1
55+
// CHECK-SAME: scope: [[MY_KERNEL]]
56+
// CHECK-SAME: )
57+
// CHECK: [[MY_LOCAL]] = !DILocalVariable(
58+
// CHECK-SAME: name: "my_local"
59+
// CHECK-SAME: scope: [[MY_KERNEL]]
60+
// CHECK-SAME: )

0 commit comments

Comments
 (0)