diff --git a/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp b/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp index c44c53129f1e0..14b75d7d16a4d 100644 --- a/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVPrepareGlobals.cpp @@ -12,7 +12,9 @@ //===----------------------------------------------------------------------===// #include "SPIRV.h" +#include "SPIRVUtils.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/IR/Module.h" using namespace llvm; @@ -43,6 +45,38 @@ bool tryExtendLLVMBitcodeMarker(GlobalVariable &Bitcode) { 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(GV.getValueType()); + if (!AT || AT->getNumElements() != 0) + return false; + + constexpr auto UInt32Max = std::numeric_limits::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; +} + bool SPIRVPrepareGlobals::runOnModule(Module &M) { const bool IsAMD = M.getTargetTriple().getVendor() == Triple::AMD; if (!IsAMD) @@ -52,6 +86,9 @@ bool SPIRVPrepareGlobals::runOnModule(Module &M) { 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/hip_dyn_lds.ll b/llvm/test/CodeGen/SPIRV/hip_dyn_lds.ll new file mode 100644 index 0000000000000..f0acfdfdede9d --- /dev/null +++ b/llvm/test/CodeGen/SPIRV/hip_dyn_lds.ll @@ -0,0 +1,20 @@ +; RUN: llc -verify-machineinstrs -mtriple=spirv64-amd-amdhsa %s -o - | FileCheck %s +; RUN: %if spirv-tools %{ llc -mtriple=spirv64-amd-amdhsa %s -o - -filetype=obj | spirv-val %} + +; CHECK: OpName %[[#LDS:]] "lds" +; CHECK: OpDecorate %[[#LDS]] LinkageAttributes "lds" Import +; CHECK: %[[#UINT:]] = OpTypeInt 32 0 +; CHECK: %[[#UINT_MAX:]] = OpConstant %[[#UINT]] 4294967295 +; CHECK: %[[#LDS_ARR_TY:]] = OpTypeArray %[[#UINT]] %[[#UINT_MAX]] +; CHECK: %[[#LDS_ARR_PTR_WG:]] = OpTypePointer Workgroup %[[#LDS_ARR_TY]] +; CHECK: %[[#LDS]] = OpVariable %[[#LDS_ARR_PTR_WG]] Workgroup + +@lds = external addrspace(3) global [0 x i32] + +define spir_kernel void @foo(ptr addrspace(4) %in, ptr addrspace(4) %out) { +entry: + %val = load i32, ptr addrspace(4) %in + %add = add i32 %val, 1 + store i32 %add, ptr addrspace(4) %out + ret void +}