[llvm] [SPIRV] Emit LinkageType for function with hidden visibility (PR #164374)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 21 01:19:15 PDT 2025
Juan Manuel Martinez =?utf-8?q?Caamaño?= <juamarti at amd.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/164374 at github.com>
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-spir-v
Author: Juan Manuel Martinez Caamaño (jmmartinez)
<details>
<summary>Changes</summary>
This patch allows emitting linkage decoration for global values with hidden linkage.
I don't think the "visibility" concept is covered in the SPIRV standard, so I'm not sure if this should rely on a target-triple specific hook.
The translator ignores the visibility, and this patch moves the SPIRV backend closer to the translator's behavior.
"hidden" visibility is different from "internal/private" linkage types. A global variable or function with hidden public linkage should still be visible to other compilation units in the same "shared object".
For the test `link-hidden.ll` with the triple `spirv--` the translator generates:
```asm
; SPIR-V
; Version: 1.0
; Generator: Khronos LLVM/SPIR-V Translator; 14
; Bound: 11
; Schema: 0
OpCapability Addresses
OpCapability Linkage
OpCapability Kernel
%1 = OpExtInstImport "OpenCL.std"
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %8 "foo"
OpExecutionMode %8 ContractionOff
OpSource Unknown 0
OpName %bar "bar"
OpName %foo "foo"
OpName %entry "entry"
OpDecorate %bar LinkageAttributes "bar" Import
OpDecorate %foo LinkageAttributes "foo" Export
%void = OpTypeVoid
%3 = OpTypeFunction %void
%bar = OpFunction %void None %3
OpFunctionEnd
%foo = OpFunction %void None %3
%entry = OpLabel
%7 = OpFunctionCall %void %bar
OpReturn
OpFunctionEnd
%8 = OpFunction %void None %3
%9 = OpLabel
%10 = OpFunctionCall %void %foo
OpReturn
OpFunctionEnd
```
---
Full diff: https://github.com/llvm/llvm-project/pull/164374.diff
2 Files Affected:
- (modified) llvm/lib/Target/SPIRV/SPIRVUtils.cpp (+1-1)
- (added) llvm/test/CodeGen/SPIRV/linkage/link-hidden.ll (+14)
``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index 4e2cc882ed6ba..b4b2c8d5947ce 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -1042,7 +1042,7 @@ getFirstValidInstructionInsertPoint(MachineBasicBlock &BB) {
std::optional<SPIRV::LinkageType::LinkageType>
getSpirvLinkageTypeFor(const SPIRVSubtarget &ST, const GlobalValue &GV) {
- if (GV.hasLocalLinkage() || GV.hasHiddenVisibility())
+ if (GV.hasLocalLinkage())
return std::nullopt;
if (GV.isDeclarationForLinker())
diff --git a/llvm/test/CodeGen/SPIRV/linkage/link-hidden.ll b/llvm/test/CodeGen/SPIRV/linkage/link-hidden.ll
new file mode 100644
index 0000000000000..f4172df38ee8b
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/linkage/link-hidden.ll
@@ -0,0 +1,14 @@
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK: OpName %[[BAR:[0-9]+]] "bar"
+; CHECK: OpDecorate %[[BAR]] LinkageAttributes "bar" Import
+; CHECK: %[[BAR]] = OpFunction
+
+define hidden spir_kernel void @foo() addrspace(4) {
+entry:
+ call spir_func addrspace(4) void @bar()
+ ret void
+}
+
+declare hidden spir_func void @bar() addrspace(4)
``````````
</details>
https://github.com/llvm/llvm-project/pull/164374
More information about the llvm-commits
mailing list