Skip to content

[SYCL] Do not emit unneeded static initializations in sycl device code #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,8 @@ void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
ctor.addInt(Int32Ty, I.Priority);
ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
if (I.AssociatedData)
ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
ctor.add(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
I.AssociatedData, VoidPtrTy));
else
ctor.addNullPointer(VoidPtrTy);
ctor.finishAndAddTo(ctors);
Expand Down
13 changes: 9 additions & 4 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5264,16 +5264,21 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,

// Make sure to pass the instantiated variable to the consumer at the end.
struct PassToConsumerRAII {
Sema &SemaRef;
ASTConsumer &Consumer;
VarDecl *Var;

PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
: Consumer(Consumer), Var(Var) { }
PassToConsumerRAII(Sema &SemaRef, ASTConsumer &Consumer, VarDecl *Var)
: SemaRef(SemaRef), Consumer(Consumer), Var(Var) {}

~PassToConsumerRAII() {
Consumer.HandleCXXStaticMemberVarInstantiation(Var);
// Do not explicitly emit non-const static data member definitions
// on SYCL device.
if (!SemaRef.getLangOpts().SYCLIsDevice || !Var->isStaticDataMember() ||
Var->isConstexpr() || Var->getType().isConstQualified())
Consumer.HandleCXXStaticMemberVarInstantiation(Var);
}
} PassToConsumerRAII(Consumer, Var);
} PassToConsumerRAII(*this, Consumer, Var);

// If we already have a definition, we're done.
if (VarDecl *Def = Var->getDefinition()) {
Expand Down
43 changes: 43 additions & 0 deletions clang/test/CodeGenSYCL/sycl-device-static-init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %clang_cc1 -fsycl -fsycl-is-device -triple spir64-unknown-unknown-sycldevice -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s
// Test that static initializers do not force the emission of globals on sycl device

// CHECK: %struct._ZTS16RegisterBaseInit.RegisterBaseInit = type { i8 }
// CHECK-NOT: $_ZN8BaseInitI12TestBaseTypeE15s_regbase_ncsdmE = comdat any
// CHECK: $_ZN8BaseInitI12TestBaseTypeE3varE = comdat any
// CHECK: @_ZN8BaseInitI12TestBaseTypeE9s_regbaseE = {{.*}} global %struct._ZTS16RegisterBaseInit.RegisterBaseInit
// CHECK-NOT: @_ZN8BaseInitI12TestBaseTypeE15s_regbase_ncsdmE = weak_odr addrspace(1) global %struct._ZTS16RegisterBaseInit.RegisterBaseInit zeroinitializer, comdat, align 1
// CHECK: @_ZN8BaseInitI12TestBaseTypeE3varE = weak_odr addrspace(1) constant i32 9, comdat, align 4
// 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*) }]
// CHECK-NOT: @_ZGVN8BaseInitI12TestBaseTypeE15s_regbase_ncsdmE = weak_odr global i64 0, comdat($_ZN8BaseInitI12TestBaseTypeE9s_regbaseE), align 8
// CHECK: define spir_kernel void @_ZTSZ4mainE11fake_kernel()
// CHECK: call spir_func void @"_ZZ4mainENK3$_0clE16RegisterBaseInit
// CHECK: declare spir_func void @_ZN16RegisterBaseInit3fooEv

struct TestBaseType {};
struct RegisterBaseInit {
__attribute__((sycl_device)) void foo();
RegisterBaseInit();
};
template <class T>
struct BaseInit {
static const RegisterBaseInit s_regbase;
static RegisterBaseInit s_regbase_ncsdm;
static const int var;
};
template <class T>
const RegisterBaseInit BaseInit<T>::s_regbase;
template <class T>
RegisterBaseInit BaseInit<T>::s_regbase_ncsdm;
template <class T>
const int BaseInit<T>::var = 9;
template struct BaseInit<TestBaseType>;
template <typename name, typename Func>
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
kernelFunc(BaseInit<TestBaseType>::s_regbase);
}
int main() {
kernel_single_task<class fake_kernel>([=](RegisterBaseInit s) {
s.foo();
});
return 0;
}