[llvm] 9ba27ca - [SPIR-V] Ensure no uses of intrinsic global variables after module translation (#122729)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 08:45:13 PST 2025
Author: Vyacheslav Levytskyy
Date: 2025-01-14T17:45:10+01:00
New Revision: 9ba27ca5c76a18c2b29cbac132b5b8d340b8f237
URL: https://github.com/llvm/llvm-project/commit/9ba27ca5c76a18c2b29cbac132b5b8d340b8f237
DIFF: https://github.com/llvm/llvm-project/commit/9ba27ca5c76a18c2b29cbac132b5b8d340b8f237.diff
LOG: [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
https://github.com/llvm/llvm-project/issues/110495
Added:
llvm/test/CodeGen/SPIRV/global-var-intrinsic.ll
Modified:
llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
index 78add921468269..8b06a7197a11eb 100644
--- a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
@@ -99,6 +99,9 @@ class SPIRVAsmPrinter : public AsmPrinter {
void getAnalysisUsage(AnalysisUsage &AU) const override;
SPIRV::ModuleAnalysisInfo *MAI;
+
+protected:
+ void cleanUp(Module &M);
};
} // namespace
@@ -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() {
diff --git a/llvm/test/CodeGen/SPIRV/global-var-intrinsic.ll b/llvm/test/CodeGen/SPIRV/global-var-intrinsic.ll
new file mode 100644
index 00000000000000..ef9f6d77425e24
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/global-var-intrinsic.ll
@@ -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
+
+ at ptr_0 = dso_local global ptr null, align 4
+ at ptr_1 = dso_local global ptr null, align 4
+ at 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
+}
More information about the llvm-commits
mailing list