Skip to content

Commit

Permalink
[SPIR-V] Ensure no uses of intrinsic global variables after module tr…
Browse files Browse the repository at this point in the history
…anslation (llvm#122729)

Ensure that the backend satisfies the requirement of the verifier that
disallows uses of intrinsic global variables. This PR fixes
llvm#110495
  • Loading branch information
VyacheslavLevytskyy authored and DKLoehr committed Jan 17, 2025
1 parent bf8f505 commit 46c44e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class SPIRVAsmPrinter : public AsmPrinter {

void getAnalysisUsage(AnalysisUsage &AU) const override;
SPIRV::ModuleAnalysisInfo *MAI;

protected:
void cleanUp(Module &M);
};
} // namespace

Expand All @@ -125,6 +128,19 @@ void SPIRVAsmPrinter::emitEndOfAsmFile(Module &M) {
if (MCAssembler *Asm = OutStreamer->getAssemblerPtr())
static_cast<SPIRVObjectWriter &>(Asm->getWriter())
.setBuildVersion(Major, Minor, Bound);

cleanUp(M);
}

// Any cleanup actions with the Module after we don't care about its content
// anymore.
void SPIRVAsmPrinter::cleanUp(Module &M) {
// Verifier disallows uses of intrinsic global variables.
for (StringRef GVName : {"llvm.global_ctors", "llvm.global_dtors",
"llvm.used", "llvm.compiler.used"}) {
if (GlobalVariable *GV = M.getNamedGlobal(GVName))
GV->setName("");
}
}

void SPIRVAsmPrinter::emitFunctionHeader() {
Expand Down
27 changes: 27 additions & 0 deletions llvm/test/CodeGen/SPIRV/global-var-intrinsic.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; Ensure that the backend satisfies the requirement of the verifier
; that disallows uses of intrinsic global variables.

; int *ptr_0 = nullptr;
; void *ptr_1 = ptr_0;
; clang -S -emit-llvm --target=spir example.cpp

; Test passes if use of "-verify-machineinstrs" doesn't lead to crash.
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; CHECK: OpFunction

@ptr_0 = dso_local global ptr null, align 4
@ptr_1 = dso_local global ptr null, align 4
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_example.cpp, ptr null }]

define internal spir_func void @__cxx_global_var_init() {
entry:
%0 = load ptr, ptr @ptr_0, align 4
store ptr %0, ptr @ptr_1, align 4
ret void
}

define internal spir_func void @_GLOBAL__sub_I_example.cpp() {
entry:
call spir_func void @__cxx_global_var_init()
ret void
}

0 comments on commit 46c44e3

Please sign in to comment.