[llvm] [SPIRV] Unify unsized array handling for AMGCN flavoured SPIR-V (PR #175848)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 13 13:49:48 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-spir-v
Author: Alex Voicu (AlexVlx)
<details>
<summary>Changes</summary>
Currently we handle 0-sized arrays in multiple places, non-uniformly, either via `SPIRVLegalizeZeroSizeArrays` or via `SPIRVPrepareGlobals`. For AMDGCN flavoured SPIR-V we have a singular, simpler solution: set all 0-sized arrays to be `UINT32_MAX` sized. This is an unambiguous token that we can use during reverse translation to restore the intended 0 size.
---
Full diff: https://github.com/llvm/llvm-project/pull/175848.diff
4 Files Affected:
- (modified) llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp (+3)
- (modified) llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp (+3)
- (modified) llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp (-54)
- (modified) llvm/test/CodeGen/SPIRV/fembed-bitcode-marker.ll (+5-5)
``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index 9a2b0771e4dc0..40f66f10ede82 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -876,6 +876,9 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems,
SPIRVType *ArrayType = nullptr;
const SPIRVSubtarget &ST =
cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
+ if (NumElems == 0 &&
+ ST.getTargetTriple().getVendor() == Triple::VendorType::AMD)
+ NumElems = UINT32_MAX;
if (NumElems != 0) {
Register NumElementsVReg =
buildConstantInt(NumElems, MIRBuilder, SpvTypeInt32, EmitIR);
diff --git a/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp b/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp
index 8c028ae875f9a..c7f993be24636 100644
--- a/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp
@@ -289,6 +289,9 @@ bool SPIRVLegalizeZeroSizeArraysImpl::runOnModule(Module &M) {
// Runtime arrays are allowed for shaders, so we don't need to do anything.
if (TM.getSubtargetImpl()->isShader())
return false;
+ // 0-sized arrays are handled differently for AMDGCN flavoured SPIRV.
+ if (M.getTargetTriple().getVendor() == Triple::VendorType::AMD)
+ return false;
// First pass: create new globals (legalizing the initializer as needed) and
// track mapping (don't erase old ones yet).
diff --git a/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp b/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp
index a568e725b5bf1..7a9d344c8b272 100644
--- a/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp
@@ -35,50 +35,6 @@ struct SPIRVPrepareGlobals : public ModulePass {
bool runOnModule(Module &M) override;
};
-bool tryExtendLLVMBitcodeMarker(GlobalVariable &Bitcode) {
- assert(Bitcode.getName() == "llvm.embedded.module");
-
- ArrayType *AT = cast<ArrayType>(Bitcode.getValueType());
- if (AT->getNumElements() != 0)
- return false;
-
- ArrayType *AT1 = ArrayType::get(AT->getElementType(), 1);
- Constant *OneEltInit = Constant::getNullValue(AT1);
- Bitcode.replaceInitializer(OneEltInit);
- return true;
-}
-
-// In HIP, dynamic LDS variables are represented using 0-element global arrays
-// in the __shared__ language address-space.
-//
-// extern __shared__ int LDS[];
-//
-// These are not representable in SPIRV directly.
-// To represent them, for AMD, we use an array with UINT32_MAX-elements.
-// These are reverse translated to 0-element arrays.
-bool tryExtendDynamicLDSGlobal(GlobalVariable &GV) {
- constexpr unsigned WorkgroupAS =
- storageClassToAddressSpace(SPIRV::StorageClass::Workgroup);
- const bool IsWorkgroupExternal =
- GV.hasExternalLinkage() && GV.getAddressSpace() == WorkgroupAS;
- if (!IsWorkgroupExternal)
- return false;
-
- const ArrayType *AT = dyn_cast<ArrayType>(GV.getValueType());
- if (!AT || AT->getNumElements() != 0)
- return false;
-
- constexpr auto UInt32Max = std::numeric_limits<uint32_t>::max();
- ArrayType *NewAT = ArrayType::get(AT->getElementType(), UInt32Max);
- GlobalVariable *NewGV = new GlobalVariable(
- *GV.getParent(), NewAT, GV.isConstant(), GV.getLinkage(), nullptr, "",
- &GV, GV.getThreadLocalMode(), WorkgroupAS, GV.isExternallyInitialized());
- NewGV->takeName(&GV);
- GV.replaceAllUsesWith(NewGV);
- GV.eraseFromParent();
-
- return true;
-}
// The backend does not support GlobalAlias. Replace aliases with their aliasees
// when possible and remove them from the module.
@@ -123,16 +79,6 @@ bool SPIRVPrepareGlobals::runOnModule(Module &M) {
Changed |= tryReplaceAliasWithAliasee(GA);
}
- const bool IsAMD = M.getTargetTriple().getVendor() == Triple::AMD;
- if (!IsAMD)
- return Changed;
-
- if (GlobalVariable *Bitcode = M.getNamedGlobal("llvm.embedded.module"))
- Changed |= tryExtendLLVMBitcodeMarker(*Bitcode);
-
- for (GlobalVariable &GV : make_early_inc_range(M.globals()))
- Changed |= tryExtendDynamicLDSGlobal(GV);
-
return Changed;
}
char SPIRVPrepareGlobals::ID = 0;
diff --git a/llvm/test/CodeGen/SPIRV/fembed-bitcode-marker.ll b/llvm/test/CodeGen/SPIRV/fembed-bitcode-marker.ll
index 4ffdb9b7f3c7a..c39c72c73212c 100644
--- a/llvm/test/CodeGen/SPIRV/fembed-bitcode-marker.ll
+++ b/llvm/test/CodeGen/SPIRV/fembed-bitcode-marker.ll
@@ -12,11 +12,11 @@
; CHECK: OpDecorate %[[#LLVM_EMBEDDED_MODULE]] Constant
; CHECK: %[[#UCHAR:]] = OpTypeInt 8 0
; CHECK: %[[#UINT:]] = OpTypeInt 32 0
-; CHECK: %[[#ONE:]] = OpConstant %[[#UINT]] 1
-; CHECK: %[[#UCHAR_ARR_1:]] = OpTypeArray %[[#UCHAR]] %[[#ONE]]
-; CHECK: %[[#UCHAR_ARR_1_PTR:]] = OpTypePointer CrossWorkgroup %[[#UCHAR_ARR_1]]
-; CHECK: %[[#CONST_UCHAR_ARR_1:]] = OpConstantNull %[[#UCHAR_ARR_1]]
-; CHECK: %[[#LLVM_EMBEDDED_MODULE]] = OpVariable %[[#UCHAR_ARR_1_PTR]] CrossWorkgroup %[[#CONST_UCHAR_ARR_1]]
+; CHECK: %[[#UINT_MAX:]] = OpConstant %[[#UINT]] 4294967295
+; CHECK: %[[#UCHAR_ARR_UINT_MAX:]] = OpTypeArray %[[#UCHAR]] %[[#UINT_MAX]]
+; CHECK: %[[#UCHAR_ARR_UINT_MAX_PTR:]] = OpTypePointer CrossWorkgroup %[[#UCHAR_ARR_UINT_MAX]]
+; CHECK: %[[#CONST_UCHAR_ARR_UINT_MAX:]] = OpConstantNull %[[#UCHAR_ARR_UINT_MAX]]
+; CHECK: %[[#LLVM_EMBEDDED_MODULE]] = OpVariable %[[#UCHAR_ARR_UINT_MAX_PTR]] CrossWorkgroup %[[#CONST_UCHAR_ARR_UINT_MAX]]
define spir_kernel void @foo() {
entry:
``````````
</details>
https://github.com/llvm/llvm-project/pull/175848
More information about the llvm-commits
mailing list