Skip to content

Commit 9ba27ca

Browse files
[SPIR-V] Ensure no uses of intrinsic global variables after module translation (#122729)
Ensure that the backend satisfies the requirement of the verifier that disallows uses of intrinsic global variables. This PR fixes #110495
1 parent c2771ca commit 9ba27ca

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class SPIRVAsmPrinter : public AsmPrinter {
9999

100100
void getAnalysisUsage(AnalysisUsage &AU) const override;
101101
SPIRV::ModuleAnalysisInfo *MAI;
102+
103+
protected:
104+
void cleanUp(Module &M);
102105
};
103106
} // namespace
104107

@@ -125,6 +128,19 @@ void SPIRVAsmPrinter::emitEndOfAsmFile(Module &M) {
125128
if (MCAssembler *Asm = OutStreamer->getAssemblerPtr())
126129
static_cast<SPIRVObjectWriter &>(Asm->getWriter())
127130
.setBuildVersion(Major, Minor, Bound);
131+
132+
cleanUp(M);
133+
}
134+
135+
// Any cleanup actions with the Module after we don't care about its content
136+
// anymore.
137+
void SPIRVAsmPrinter::cleanUp(Module &M) {
138+
// Verifier disallows uses of intrinsic global variables.
139+
for (StringRef GVName : {"llvm.global_ctors", "llvm.global_dtors",
140+
"llvm.used", "llvm.compiler.used"}) {
141+
if (GlobalVariable *GV = M.getNamedGlobal(GVName))
142+
GV->setName("");
143+
}
128144
}
129145

130146
void SPIRVAsmPrinter::emitFunctionHeader() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; Ensure that the backend satisfies the requirement of the verifier
2+
; that disallows uses of intrinsic global variables.
3+
4+
; int *ptr_0 = nullptr;
5+
; void *ptr_1 = ptr_0;
6+
; clang -S -emit-llvm --target=spir example.cpp
7+
8+
; Test passes if use of "-verify-machineinstrs" doesn't lead to crash.
9+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
10+
; CHECK: OpFunction
11+
12+
@ptr_0 = dso_local global ptr null, align 4
13+
@ptr_1 = dso_local global ptr null, align 4
14+
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_example.cpp, ptr null }]
15+
16+
define internal spir_func void @__cxx_global_var_init() {
17+
entry:
18+
%0 = load ptr, ptr @ptr_0, align 4
19+
store ptr %0, ptr @ptr_1, align 4
20+
ret void
21+
}
22+
23+
define internal spir_func void @_GLOBAL__sub_I_example.cpp() {
24+
entry:
25+
call spir_func void @__cxx_global_var_init()
26+
ret void
27+
}

0 commit comments

Comments
 (0)