[llvm] [SPIR-V] Add spirv-use-user-semantic-for-linkage option (PR #181661)

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 16 06:14:09 PST 2026


https://github.com/MrSidims updated https://github.com/llvm/llvm-project/pull/181661

>From 53560f824f844edd9d80a9bcc604419418e7a3a4 Mon Sep 17 00:00:00 2001
From: Dmitry Sidorov <Dmitry.Sidorov at amd.com>
Date: Sat, 14 Feb 2026 23:56:15 +0100
Subject: [PATCH 1/2] [SPIR-V] Add spirv-use-user-semantic-for-linkage option

when enabled, LLVM linkage types that have no SPIR-V counterparts are translated
as UserSemantic "linkage:%type%" decoration, where %type% can be weak,
weak_odr etc.

Ideal solution is to have new extension(s) for this, but we don't have them.
Using UserSemantic is not correct, but it doesn't worsen the current situation,
which is:
if in 2 modules there is a weak function, and these modules are linked together
after llvm-spirv roundtrip, then llvm-link errors out because functions from
both modules become extern.

So with this patch, if linkage type is preserved via UserSemantic we won't see
this error, and if UserSemantic is ignored (which SPIR-V consumer has right to
do) - the linkage error remains.
---
 llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp   |  1 +
 .../Target/SPIRV/SPIRVInstructionSelector.cpp |  1 +
 llvm/lib/Target/SPIRV/SPIRVUtils.cpp          | 44 +++++++++++++++++
 llvm/lib/Target/SPIRV/SPIRVUtils.h            |  3 ++
 .../SPIRV/linkage/linkage-user-semantic.ll    | 48 +++++++++++++++++++
 5 files changed, 97 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
index bc6418f2ceceb..b88ec4da0f5e8 100644
--- a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
@@ -418,6 +418,7 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
     buildOpDecorate(FuncVReg, MIRBuilder, SPIRV::Decoration::LinkageAttributes,
                     {static_cast<uint32_t>(*LnkTy)}, F.getName());
   }
+  emitLinkageAsUserSemantic(FuncVReg, F, MIRBuilder);
 
   // Handle function pointers decoration
   bool hasFunctionPointers =
diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index b649189c39ea0..2bb5b8d076e81 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -5237,6 +5237,7 @@ bool SPIRVInstructionSelector::selectGlobalValue(
   Register Reg = GR.buildGlobalVariable(
       ResVReg, ResType, GlobalIdent, GV, StorageClass, Init,
       GlobalVar->isConstant(), LnkType, MIRBuilder, true);
+  emitLinkageAsUserSemantic(Reg, *GV, MIRBuilder);
   // TODO: For AMDGCN, we pipe externally_initialized through via
   // HostAccessINTEL, with ReadWrite (3) access, which is we then handle during
   // reverse translation. We should remove this once SPIR-V gains the ability to
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index c69eb6f92a7c4..2c67bfafed843 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -24,9 +24,18 @@
 #include "llvm/Demangle/Demangle.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/IntrinsicsSPIRV.h"
+#include "llvm/Support/CommandLine.h"
 #include <queue>
 #include <vector>
 
+static llvm::cl::opt<bool> SPVEmitLinkageUserSemantic(
+    "spirv-use-user-semantic-for-linkage",
+    llvm::cl::desc("Emit UserSemantic decoration with \"linkage:<type>\" "
+                   "string on functions and globals whose linkage has no "
+                   "native SPIR-V representation (note: it's an experimental "
+                   "flag)."),
+    llvm::cl::Optional, llvm::cl::init(false));
+
 namespace llvm {
 namespace SPIRV {
 // This code restores function args/retvalue types for composite cases
@@ -1195,6 +1204,41 @@ Type *reconstitutePeeledArrayType(Type *Ty) {
   return ResultTy;
 }
 
+void emitLinkageAsUserSemantic(Register Reg, const GlobalValue &GV,
+                               MachineIRBuilder &MIRBuilder) {
+  if (!SPVEmitLinkageUserSemantic)
+    return;
+
+  const char *LinkageName = nullptr;
+  switch (GV.getLinkage()) {
+  case GlobalValue::WeakAnyLinkage:
+    LinkageName = "weak";
+    break;
+  case GlobalValue::WeakODRLinkage:
+    LinkageName = "weak_odr";
+    break;
+  case GlobalValue::LinkOnceAnyLinkage:
+    LinkageName = "linkonce";
+    break;
+  case GlobalValue::AvailableExternallyLinkage:
+    LinkageName = "available_externally";
+    break;
+  case GlobalValue::CommonLinkage:
+    LinkageName = "common";
+    break;
+  case GlobalValue::AppendingLinkage:
+    LinkageName = "appending";
+    break;
+  case GlobalValue::ExternalWeakLinkage:
+    LinkageName = "extern_weak";
+    break;
+  default:
+    return;
+  }
+  buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::UserSemantic, {},
+                  std::string("linkage:") + LinkageName);
+}
+
 std::optional<SPIRV::LinkageType::LinkageType>
 getSpirvLinkageTypeFor(const SPIRVSubtarget &ST, const GlobalValue &GV) {
   if (GV.hasLocalLinkage() || GV.hasHiddenVisibility())
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h
index 50042c90a86d3..31b91a293827e 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.h
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h
@@ -584,6 +584,9 @@ unsigned getArrayComponentCount(const MachineRegisterInfo *MRI,
 MachineBasicBlock::iterator
 getFirstValidInstructionInsertPoint(MachineBasicBlock &BB);
 
+void emitLinkageAsUserSemantic(Register Reg, const GlobalValue &GV,
+                               MachineIRBuilder &MIRBuilder);
+
 std::optional<SPIRV::LinkageType::LinkageType>
 getSpirvLinkageTypeFor(const SPIRVSubtarget &ST, const GlobalValue &GV);
 } // namespace llvm
diff --git a/llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll b/llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll
new file mode 100644
index 0000000000000..1cc49537a2822
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll
@@ -0,0 +1,48 @@
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown --spirv-use-user-semantic-for-linkage %s -o - | FileCheck %s --check-prefix=CHECK-ON
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-OFF
+
+; FIXME: currently spirv-val is not erroring out when UserSemantic decoration
+; is applied to a function, but it actually should.
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown --spirv-use-user-semantic-for-linkage %s -o - -filetype=obj | spirv-val %}
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; Test that --spirv-use-user-semantic-for-linkage emits UserSemantic decorations
+; for LLVM linkage types with no native SPIR-V representation.
+
+; CHECK-ON-DAG: OpName %[[#WEAK_FN:]] "weak_func"
+; CHECK-ON-DAG: OpName %[[#WEAK:]] "weak_var"
+; CHECK-ON-DAG: OpName %[[#WEAK_ODR:]] "weak_odr_var"
+; CHECK-ON-DAG: OpName %[[#LINKONCE:]] "linkonce_var"
+; CHECK-ON-DAG: OpName %[[#COMMON:]] "common_var"
+; CHECK-ON-DAG: OpName %[[#AE:]] "ae_var"
+; CHECK-ON-DAG: OpName %[[#EW:]] "ew_func"
+
+; CHECK-ON-DAG: OpDecorate %[[#WEAK_FN]] UserSemantic "linkage:weak"
+; CHECK-ON-DAG: OpDecorate %[[#WEAK]] UserSemantic "linkage:weak"
+; CHECK-ON-DAG: OpDecorate %[[#WEAK_ODR]] UserSemantic "linkage:weak_odr"
+; CHECK-ON-DAG: OpDecorate %[[#LINKONCE]] UserSemantic "linkage:linkonce"
+; CHECK-ON-DAG: OpDecorate %[[#COMMON]] UserSemantic "linkage:common"
+; CHECK-ON-DAG: OpDecorate %[[#AE]] UserSemantic "linkage:available_externally"
+; CHECK-ON-DAG: OpDecorate %[[#EW]] UserSemantic "linkage:extern_weak"
+
+; CHECK-OFF-NOT: UserSemantic "linkage:
+
+ at weak_var = weak addrspace(1) global i32 0, align 4
+ at weak_odr_var = weak_odr addrspace(1) global i32 0, align 4
+ at linkonce_var = linkonce addrspace(1) global i32 0, align 4
+ at common_var = common addrspace(1) global i32 0, align 4
+ at ae_var = available_externally addrspace(1) global i32 79, align 4
+
+declare extern_weak spir_func void @ew_func()
+
+define weak spir_func i32 @weak_func(i32 %x) {
+entry:
+  ret i32 %x
+}
+
+define spir_func void @caller() {
+entry:
+  %val = call i32 @weak_func(i32 42)
+  call spir_func void @ew_func()
+  ret void
+}

>From 3a2e2000bcf8db0effc37b093e8696076d785a1e Mon Sep 17 00:00:00 2001
From: Dmitry Sidorov <Dmitry.Sidorov at amd.com>
Date: Mon, 16 Feb 2026 08:13:46 -0600
Subject: [PATCH 2/2] remove Import linkage

---
 llvm/lib/Target/SPIRV/SPIRVUtils.cpp                     | 3 ---
 llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll | 3 ---
 2 files changed, 6 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index 2c67bfafed843..71b7ad107d9e2 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -1220,9 +1220,6 @@ void emitLinkageAsUserSemantic(Register Reg, const GlobalValue &GV,
   case GlobalValue::LinkOnceAnyLinkage:
     LinkageName = "linkonce";
     break;
-  case GlobalValue::AvailableExternallyLinkage:
-    LinkageName = "available_externally";
-    break;
   case GlobalValue::CommonLinkage:
     LinkageName = "common";
     break;
diff --git a/llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll b/llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll
index 1cc49537a2822..20752e5946856 100644
--- a/llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll
+++ b/llvm/test/CodeGen/SPIRV/linkage/linkage-user-semantic.ll
@@ -14,7 +14,6 @@
 ; CHECK-ON-DAG: OpName %[[#WEAK_ODR:]] "weak_odr_var"
 ; CHECK-ON-DAG: OpName %[[#LINKONCE:]] "linkonce_var"
 ; CHECK-ON-DAG: OpName %[[#COMMON:]] "common_var"
-; CHECK-ON-DAG: OpName %[[#AE:]] "ae_var"
 ; CHECK-ON-DAG: OpName %[[#EW:]] "ew_func"
 
 ; CHECK-ON-DAG: OpDecorate %[[#WEAK_FN]] UserSemantic "linkage:weak"
@@ -22,7 +21,6 @@
 ; CHECK-ON-DAG: OpDecorate %[[#WEAK_ODR]] UserSemantic "linkage:weak_odr"
 ; CHECK-ON-DAG: OpDecorate %[[#LINKONCE]] UserSemantic "linkage:linkonce"
 ; CHECK-ON-DAG: OpDecorate %[[#COMMON]] UserSemantic "linkage:common"
-; CHECK-ON-DAG: OpDecorate %[[#AE]] UserSemantic "linkage:available_externally"
 ; CHECK-ON-DAG: OpDecorate %[[#EW]] UserSemantic "linkage:extern_weak"
 
 ; CHECK-OFF-NOT: UserSemantic "linkage:
@@ -31,7 +29,6 @@
 @weak_odr_var = weak_odr addrspace(1) global i32 0, align 4
 @linkonce_var = linkonce addrspace(1) global i32 0, align 4
 @common_var = common addrspace(1) global i32 0, align 4
- at ae_var = available_externally addrspace(1) global i32 79, align 4
 
 declare extern_weak spir_func void @ew_func()
 



More information about the llvm-commits mailing list