Skip to content

Commit d9660d0

Browse files
committed
[SYCL] Do not emit const static data members that are not const-initialized
Const static data members need to be either zero-initialized or constant-initialized. We were emitting all const static data members before, which needed address space casts from constant-space to private-space. This change also reverts the address-space cast introduced in #1774 Signed-off-by: Premanand M Rao <[email protected]>
1 parent 8a8ea32 commit d9660d0

File tree

4 files changed

+97
-5
lines changed

4 files changed

+97
-5
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,8 +1288,7 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
12881288
ctor.addInt(Int32Ty, I.Priority);
12891289
ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
12901290
if (I.AssociatedData)
1291-
ctor.add(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1292-
I.AssociatedData, VoidPtrTy));
1291+
ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
12931292
else
12941293
ctor.addNullPointer(VoidPtrTy);
12951294
ctor.finishAndAddTo(ctors);

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5255,7 +5255,10 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
52555255
// Do not explicitly emit non-const static data member definitions
52565256
// on SYCL device.
52575257
if (!SemaRef.getLangOpts().SYCLIsDevice || !Var->isStaticDataMember() ||
5258-
Var->isConstexpr() || Var->getType().isConstQualified())
5258+
Var->isConstexpr() ||
5259+
(Var->getType().isConstQualified() && Var->getInit() &&
5260+
Var->getInit()->isConstantInitializer(SemaRef.getASTContext(),
5261+
false)))
52595262
Consumer.HandleCXXStaticMemberVarInstantiation(Var);
52605263
}
52615264
} PassToConsumerRAII(*this, Consumer, Var);
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// RUN: %clang_cc1 -fsycl -fsycl-is-device -triple spir64-unknown-unknown-sycldevice -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s
2+
// Tests that static data members that are not constant-initialized are not
3+
// emitted in device code.
4+
5+
// CHECK:%struct._ZTS1B.B = type { i8 }
6+
// CHECK:%struct._ZTS1C.C = type { i32, i32 }
7+
// CHECK:%struct._ZTS1D.D = type { i8 }
8+
// CHECK:$_ZN2TTIiE2c1E = comdat any
9+
// CHECK:$_ZN2TTIiE2d1E = comdat any
10+
// CHECK:$_ZN2TTIiE4var1E = comdat any
11+
// CHECK:$_ZN2TTIiE4var3E = comdat any
12+
// CHECK:@_ZN1S2b1E = addrspace(1) global %struct._ZTS1B.B zeroinitializer, align 1
13+
// CHECK:@_ZN1S2c1E = addrspace(1) constant %struct._ZTS1C.C { i32 2, i32 5 }, align 4
14+
// CHECK:@_ZN1S2d1E = addrspace(1) constant %struct._ZTS1D.D zeroinitializer, align 1
15+
// CHECK:@_ZN1S4var1E = addrspace(1) constant i32 1, align 4
16+
// CHECK:@_ZN1S4var2E = available_externally addrspace(1) constant i32 3, align 4
17+
// CHECK:@_ZN1S4var3E = addrspace(1) constant i32 4, align 4
18+
// CHECK:@_ZN2TTIiE2b1E = external addrspace(1) global %struct._ZTS1B.B, align 1
19+
// CHECK:@_ZN2TTIiE2c1E = linkonce_odr addrspace(1) constant %struct._ZTS1C.C { i32 2, i32 5 }, comdat, align 4
20+
// CHECK:@_ZN2TTIiE2d1E = linkonce_odr addrspace(1) constant %struct._ZTS1D.D zeroinitializer, comdat, align 1
21+
// CHECK:@_ZN2TTIiE4var1E = linkonce_odr addrspace(1) constant i32 1, comdat, align 4
22+
// CHECK:@_ZN2TTIiE4var2E = available_externally addrspace(1) constant i32 3, align 4
23+
// CHECK:@_ZN2TTIiE4var3E = linkonce_odr addrspace(1) constant i32 4, comdat, align 4
24+
// CHECK:@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535
25+
// CHECK-NOT: addrspacecast
26+
// CHECK: define spir_kernel void @_ZTSZ4mainE11fake_kernel()
27+
28+
struct TestBaseType {};
29+
struct B {
30+
B();
31+
};
32+
struct C {
33+
int i, j;
34+
};
35+
struct D {
36+
};
37+
38+
struct S {
39+
static const B b1;
40+
static const int var1;
41+
static constexpr const int var2 = 3;
42+
static const int var3;
43+
static const C c1;
44+
static const D d1;
45+
};
46+
const B S::b1;
47+
const C S::c1{2, 5};
48+
const int S::var1 = 1;
49+
const int S::var3 = 1 + 3;
50+
const D S::d1;
51+
52+
template <typename T>
53+
struct TT {
54+
static const B b1;
55+
static const int var1;
56+
static constexpr const int var2 = 3;
57+
static const int var3;
58+
static const C c1;
59+
static const D d1;
60+
};
61+
template <typename T>
62+
const B TT<T>::b1;
63+
template <typename T>
64+
const C TT<T>::c1{2, 5};
65+
template <typename T>
66+
const int TT<T>::var1 = 1;
67+
template <typename T>
68+
const int TT<T>::var3 = 1 + 3;
69+
template <typename T>
70+
const D TT<T>::d1;
71+
72+
template <typename name, typename Func>
73+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
74+
kernelFunc();
75+
(void)S::b1;
76+
(void)S::c1;
77+
(void)S::d1;
78+
(void)S::var1;
79+
(void)S::var2;
80+
(void)S::var3;
81+
(void)TT<int>::b1;
82+
(void)TT<int>::c1;
83+
(void)TT<int>::d1;
84+
(void)TT<int>::var1;
85+
(void)TT<int>::var2;
86+
(void)TT<int>::var3;
87+
}
88+
int main() {
89+
kernel_single_task<class fake_kernel>([=]() {});
90+
return 0;
91+
}

clang/test/CodeGenSYCL/sycl-device-static-init.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
// CHECK: %struct._ZTS16RegisterBaseInit.RegisterBaseInit = type { i8 }
55
// CHECK-NOT: $_ZN8BaseInitI12TestBaseTypeE15s_regbase_ncsdmE = comdat any
66
// CHECK: $_ZN8BaseInitI12TestBaseTypeE3varE = comdat any
7+
// CHECK: @_ZN8BaseInitI12TestBaseTypeE3varE = weak_odr addrspace(1) constant i32 9, comdat, align 4
78
// CHECK: @_ZN8BaseInitI12TestBaseTypeE9s_regbaseE = {{.*}} global %struct._ZTS16RegisterBaseInit.RegisterBaseInit
89
// CHECK-NOT: @_ZN8BaseInitI12TestBaseTypeE15s_regbase_ncsdmE = weak_odr addrspace(1) global %struct._ZTS16RegisterBaseInit.RegisterBaseInit zeroinitializer, comdat, align 1
9-
// CHECK: @_ZN8BaseInitI12TestBaseTypeE3varE = weak_odr addrspace(1) constant i32 9, comdat, align 4
10-
// CHECK: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @__cxx_global_var_init, i8* addrspacecast (i8 addrspace(1)* getelementptr inbounds (%struct._ZTS16RegisterBaseInit.RegisterBaseInit, %struct._ZTS16RegisterBaseInit.RegisterBaseInit addrspace(1)* @_ZN8BaseInitI12TestBaseTypeE9s_regbaseE, i32 0, i32 0) to i8*) }]
1110
// CHECK-NOT: @_ZGVN8BaseInitI12TestBaseTypeE15s_regbase_ncsdmE = weak_odr global i64 0, comdat($_ZN8BaseInitI12TestBaseTypeE9s_regbaseE), align 8
1211
// CHECK: define spir_kernel void @_ZTSZ4mainE11fake_kernel()
1312
// CHECK: call spir_func void @"_ZZ4mainENK3$_0clE16RegisterBaseInit

0 commit comments

Comments
 (0)