Skip to content

Commit 5b2cfe2

Browse files
authored
[SYCL] Emit llvm.compiler.used for internal device globals (#5958)
This prevents early optimizations from removing device globals with internal linkage.
1 parent 2178134 commit 5b2cfe2

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,19 @@ void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
22542254
VD->getStorageDuration() == SD_Static)
22552255
addUsedOrCompilerUsedGlobal(GV);
22562256
}
2257+
2258+
if (getLangOpts().SYCLIsDevice) {
2259+
// Add internal device_global variables to llvm.compiler.used array to
2260+
// prevent early optimizations from removing these variables from the
2261+
// module.
2262+
if (D && isa<VarDecl>(D)) {
2263+
const auto *VD = cast<VarDecl>(D);
2264+
const RecordDecl *RD = VD->getType()->getAsRecordDecl();
2265+
if (RD && RD->hasAttr<SYCLDeviceGlobalAttr>() &&
2266+
VD->getFormalLinkage() == InternalLinkage)
2267+
addUsedOrCompilerUsedGlobal(GV);
2268+
}
2269+
}
22572270
}
22582271

22592272
bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,

clang/test/CodeGenSYCL/device_global.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ void bar() {
9191

9292
// CHECK: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr addrspace(4) }] [{ i32, ptr, ptr addrspace(4) } { i32 65535, ptr @__cxx_global_var_init{{.*}}, ptr addrspace(4) addrspacecast (ptr addrspace(1) @[[TEMPL_DEV_GLOB]] to ptr addrspace(4)) }, { i32, ptr, ptr addrspace(4) } { i32 65535, ptr @_GLOBAL__sub_I_device_global.cpp, ptr addrspace(4) null }]
9393
// CHECK: @llvm.used = appending global [1 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @[[TEMPL_DEV_GLOB]] to ptr addrspace(4))], section "llvm.metadata"
94+
// CHECK: @llvm.compiler.used = appending global [2 x ptr addrspace(4)]
95+
// CHECK-SAME: @_ZL1B
96+
// CHECK-SAME: @_ZN12_GLOBAL__N_19same_nameE
9497

9598
// CHECK: attributes #[[A_ATTRS]] = { "sycl-unique-id"="_Z1A" }
9699
// CHECK: attributes #[[B_ATTRS]] = { "sycl-unique-id"="THE_PREFIX____ZL1B" }

0 commit comments

Comments
 (0)