Skip to content

Commit f41f506

Browse files
authored
[clang-offload-bundler] Do not add globals with constant address space to .tgtsym (#4236)
To avoid an invalid address space cast when a reference to such global value is added to the llvm.used list which has global address space. Signed-off-by: Sergey Dmitriev <[email protected]>
1 parent 9ddb8f0 commit f41f506

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clang/test/Driver/clang-offload-bundler-tgtsym.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
// CHECK-NOT: static_used
2222
// CHECK-NOT: sycl-spir64.llvm.used
2323
// CHECK-NOT: sycl-spir64.llvm.compiler.used
24+
// CHECK-NOT: sycl-spir64.const_as
25+
26+
const __attribute__((opencl_constant)) char const_as[] = "abc";
2427

2528
extern void undefined_func(void);
2629

clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,22 @@ class ObjectFileHandler final : public FileHandler {
643643
return createStringError(inconvertibleErrorCode(),
644644
Err.getMessage());
645645

646+
bool UpdateBuf = false;
646647
if (!Mod->getModuleInlineAsm().empty()) {
647648
Mod->setModuleInlineAsm("");
648-
649+
UpdateBuf = true;
650+
}
651+
for (auto I = Mod->global_begin(), E = Mod->global_end(); I != E;) {
652+
GlobalVariable &GV = *I++;
653+
// Do not add globals with constant address space to the tgtsym.
654+
if (!GV.isDeclaration() && !GV.hasLocalLinkage() &&
655+
GV.getAddressSpace() == 2) {
656+
GV.dropAllReferences();
657+
GV.eraseFromParent();
658+
UpdateBuf = true;
659+
}
660+
}
661+
if (UpdateBuf) {
649662
SmallVector<char, 0> ModuleBuf;
650663
raw_svector_ostream ModuleOS(ModuleBuf);
651664
WriteBitcodeToFile(*Mod, ModuleOS);

0 commit comments

Comments
 (0)