[llvm] [SPIRV] Do not emit @llvm.compiler.used (PR #162678)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 01:16:12 PDT 2025


https://github.com/jmmartinez updated https://github.com/llvm/llvm-project/pull/162678

>From 69deae2ba960de4fdf2e8f88a5f472a9229d5d62 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Thu, 9 Oct 2025 16:25:36 +0200
Subject: [PATCH 1/4] Pre-commit test: [SPIRV] Do not emit @llvm.compiler.used

---
 llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll

diff --git a/llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll b/llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll
new file mode 100644
index 0000000000000..5c064ab7f72ac
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll
@@ -0,0 +1,19 @@
+; RUN: llc -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; Verify that llvm.compiler.used is not lowered
+; Currently we do lower it.
+; CHECK: OpName %[[UNUSED:[0-9]+]] "unused"
+; CHECK: OpName %[[LLVM_COMPILER_USED_NAME:[0-9]+]] "llvm.compiler.used"
+
+; Check that the type of llvm.compiler.used is not emited too
+; Currently we do lower it.
+; CHECK: OpTypeArray
+
+ at unused = private addrspace(3) global i32 0
+ at llvm.compiler.used = appending addrspace(2) global [1 x ptr addrspace (4)] [ptr addrspace(4) addrspacecast (ptr addrspace(3) @unused to ptr addrspace(4))]
+
+define spir_kernel void @foo() {
+entry:
+  ret void
+}

>From 0602154365d771f56910a9258bfbe5cac4f1c3d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Thu, 9 Oct 2025 16:59:15 +0200
Subject: [PATCH 2/4] [SPIRV] Do not emit @llvm.compiler.used

This variable holds a series of global values and prevents the compiler
from optimizing it out.
However, the symbols in the variable can be optimized after compilation
as usual by the linker (notice that @llvm.used instead doesn't allow for
the linker to optimize out the globals referenced in it).
---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 9 +++++++--
 llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll | 5 ++---
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index e16c8f0fc302e..12b2f558bf383 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -17,6 +17,7 @@
 #include "SPIRVTargetMachine.h"
 #include "SPIRVUtils.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/InstVisitor.h"
@@ -2028,9 +2029,13 @@ Instruction *SPIRVEmitIntrinsics::visitUnreachableInst(UnreachableInst &I) {
 
 void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
                                              IRBuilder<> &B) {
-  // Skip special artifical variable llvm.global.annotations.
-  if (GV.getName() == "llvm.global.annotations")
+  // Skip special artifical variables
+  static const StringSet<> ArtificialGlobals{"llvm.global.annotations",
+                                             "llvm.compiler.used"};
+
+  if (ArtificialGlobals.contains(GV.getName()))
     return;
+
   Constant *Init = nullptr;
   if (hasInitializer(&GV)) {
     // Deduce element type and store results in Global Registry.
diff --git a/llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll b/llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll
index 5c064ab7f72ac..3a46c8d150b62 100644
--- a/llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll
+++ b/llvm/test/CodeGen/SPIRV/llvm-compiler-used.ll
@@ -4,11 +4,10 @@
 ; Verify that llvm.compiler.used is not lowered
 ; Currently we do lower it.
 ; CHECK: OpName %[[UNUSED:[0-9]+]] "unused"
-; CHECK: OpName %[[LLVM_COMPILER_USED_NAME:[0-9]+]] "llvm.compiler.used"
+; CHECK-NOT: OpName %[[LLVM_COMPILER_USED_NAME:[0-9]+]] "llvm.compiler.used"
 
 ; Check that the type of llvm.compiler.used is not emited too
-; Currently we do lower it.
-; CHECK: OpTypeArray
+; CHECK-NOT: OpTypeArray
 
 @unused = private addrspace(3) global i32 0
 @llvm.compiler.used = appending addrspace(2) global [1 x ptr addrspace (4)] [ptr addrspace(4) addrspacecast (ptr addrspace(3) @unused to ptr addrspace(4))]

>From e94b80c6790eb3e6527d70d4fb6bf4308cce355a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Thu, 9 Oct 2025 16:59:15 +0200
Subject: [PATCH 3/4] [Review][SPIRV] Do not emit @llvm.used

Same as @llvm.compiler.used. We do not do any special handling for this
variable.
---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp |  1 +
 llvm/test/CodeGen/SPIRV/llvm-used.ll          | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/llvm-used.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 12b2f558bf383..1a4975db115aa 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -2031,6 +2031,7 @@ void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
                                              IRBuilder<> &B) {
   // Skip special artifical variables
   static const StringSet<> ArtificialGlobals{"llvm.global.annotations",
+                                             "llvm.used",
                                              "llvm.compiler.used"};
 
   if (ArtificialGlobals.contains(GV.getName()))
diff --git a/llvm/test/CodeGen/SPIRV/llvm-used.ll b/llvm/test/CodeGen/SPIRV/llvm-used.ll
new file mode 100644
index 0000000000000..95eceda097cd5
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/llvm-used.ll
@@ -0,0 +1,18 @@
+; RUN: llc -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; Verify that llvm.used is not lowered
+; Currently we do lower it.
+; CHECK: OpName %[[UNUSED:[0-9]+]] "unused"
+; CHECK-NOT: OpName %[[LLVM_USED_NAME:[0-9]+]] "llvm.used"
+
+; Check that the type of llvm.used is not emited too
+; CHECK-NOT: OpTypeArray
+
+ at unused = private addrspace(3) global i32 0
+ at llvm.used = appending addrspace(2) global [1 x ptr addrspace (4)] [ptr addrspace(4) addrspacecast (ptr addrspace(3) @unused to ptr addrspace(4))]
+
+define spir_kernel void @foo() {
+entry:
+  ret void
+}

>From fe16a73f7343baea7fa27dddec826ce2595b8345 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Fri, 10 Oct 2025 10:15:45 +0200
Subject: [PATCH 4/4] [Reveiw] forgot to run clang-format

---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 1a4975db115aa..3c8b1867fb756 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -2031,8 +2031,7 @@ void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
                                              IRBuilder<> &B) {
   // Skip special artifical variables
   static const StringSet<> ArtificialGlobals{"llvm.global.annotations",
-                                             "llvm.used",
-                                             "llvm.compiler.used"};
+                                             "llvm.used", "llvm.compiler.used"};
 
   if (ArtificialGlobals.contains(GV.getName()))
     return;



More information about the llvm-commits mailing list