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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 13 07:34:15 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Vyacheslav Levytskyy (VyacheslavLevytskyy)

<details>
<summary>Changes</summary>

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
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
+}

``````````

</details>


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


More information about the llvm-commits mailing list