Skip to content

[SPIR-V] Ensure no uses of intrinsic global variables after module translation #122729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025

Conversation

VyacheslavLevytskyy
Copy link
Contributor

@VyacheslavLevytskyy VyacheslavLevytskyy commented Jan 13, 2025

Ensure that the backend satisfies the requirement of the verifier that disallows uses of intrinsic global variables. This PR fixes #110495

@llvmbot
Copy link
Member

llvmbot commented Jan 13, 2025

@llvm/pr-subscribers-backend-spir-v

Author: Vyacheslav Levytskyy (VyacheslavLevytskyy)

Changes

Ensure that the backend satisfies the requirement of the verifier that disallows uses of intrinsic global variables.


Full diff: https://github.com/llvm/llvm-project/pull/122729.diff

2 Files Affected:

  • (modified) llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp (+16)
  • (added) llvm/test/CodeGen/SPIRV/global-var-intrinsic.ll (+27)
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
+
+@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
+}

@VyacheslavLevytskyy
Copy link
Contributor Author

Tests fail is KhronosGroup/SPIRV-Tools#5407, not related to this PR

@VyacheslavLevytskyy VyacheslavLevytskyy merged commit 9ba27ca into llvm:main Jan 14, 2025
8 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SPIR-V] Verifier fails with invalid uses of intrinsic global variable
3 participants