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

Vyacheslav Levytskyy via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 13 07:33:40 PST 2025


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

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

>From f75c2e9c28789e445077ab871a175a8a001ed82c Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Mon, 13 Jan 2025 07:32:36 -0800
Subject: [PATCH] Ensure no uses of intrinsic global variables after module
 translation

---
 llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp     | 16 +++++++++++
 .../CodeGen/SPIRV/global-var-intrinsic.ll     | 27 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/global-var-intrinsic.ll

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