https://github.com/rj123-nv updated https://github.com/llvm/llvm-project/pull/243531
>From 231580bce88122823494e567dcabd11566a2bbcb Mon Sep 17 00:00:00 2001 From: rjennings <[email protected]> Date: Mon, 16 Nov 2026 20:58:33 -0800 Subject: [PATCH 1/4] [hlsl][spirv] Dont emit string constants for resource names Resource intrinsics (handlefrombinding, handlefromimplicitbinding) take const char * arguments which get lowered in backend into byte arrays in the final spirv module. These should be ignored except to provide the literal to OpName instructions. Add a pass intentionally before SPIRVRemoveUnusedResources and SPIRVLegalizeResourceBinding, which converts handle intrinsics to variants taking a metadata arg for the name instead. This lets us avoid emitting actual constants in the spirv, and simplifies, for example, string global removal in the unused resources pass, and lowering of heap resources to handlefrombinding. It would be preferable to emit these metadata variants from Clang, but this is not currently possible due to handlefrombinding generation taking place in generated initializers, eg __createFromBinding, so a string arg must be passed initially. assisted by: codex --- .../CodeGenHLSL/resources/resource-names.hlsl | 15 ++++ llvm/docs/SPIRVUsage.md | 10 +++ llvm/include/llvm/IR/IntrinsicsSPIRV.td | 14 +++ llvm/lib/Target/SPIRV/CMakeLists.txt | 1 + llvm/lib/Target/SPIRV/SPIRV.h | 9 ++ llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 2 +- .../Target/SPIRV/SPIRVInstructionSelector.cpp | 18 ++-- .../SPIRV/SPIRVLegalizeResourceBinding.cpp | 85 +++++------------- .../Target/SPIRV/SPIRVLowerResourceNames.cpp | 89 +++++++++++++++++++ llvm/lib/Target/SPIRV/SPIRVPassRegistry.def | 1 + .../SPIRV/SPIRVRemoveUnusedResources.cpp | 16 ++-- llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp | 3 + .../hlsl-resources/HeapBinding-cvb-srv-uav.ll | 6 +- .../HeapBinding-texture-sampler.ll | 4 +- .../hlsl-resources/HeapBinding-uav-counter.ll | 4 +- .../hlsl-resources/StorageImageConstIdx.ll | 1 - .../remove-unused-resource-dead-handle.ll | 2 +- .../remove-unused-resources-counter.ll | 4 +- .../remove-unused-resources-live-handle.ll | 2 +- .../remove-unused-resources-loaded-handle.ll | 3 +- .../SPIRV/hlsl-resources/resource-names.ll | 42 +++++++++ .../SPIRV/hlsl-resources/test_counters.ll | 1 - llvm/test/CodeGen/SPIRV/llc-pipeline.ll | 2 + .../SPIRVLegalizeRegisterBinding-Heap.ll | 22 ++--- .../SPIRVLegalizeRegisterBinding-Heap2.ll | 14 ++- .../SPIRVLegalizeResourceBinding-Implicit.ll | 9 +- .../pointers/resource-vector-load-store.ll | 1 - .../pointers/structured-buffer-access.ll | 30 +++---- .../structured-buffer-vector-access.ll | 20 ++--- .../EarlyCSE/SPIRV/resource-name-metadata.ll | 53 +++++++++++ 30 files changed, 335 insertions(+), 148 deletions(-) create mode 100644 clang/test/CodeGenHLSL/resources/resource-names.hlsl create mode 100644 llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp create mode 100644 llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll create mode 100644 llvm/test/Transforms/EarlyCSE/SPIRV/resource-name-metadata.ll diff --git a/clang/test/CodeGenHLSL/resources/resource-names.hlsl b/clang/test/CodeGenHLSL/resources/resource-names.hlsl new file mode 100644 index 00000000000000..8ae7608cb008da --- /dev/null +++ b/clang/test/CodeGenHLSL/resources/resource-names.hlsl @@ -0,0 +1,15 @@ +// REQUIRES: spirv-registered-target +// RUN: %clang_cc1 -triple spirv1.6-unknown-vulkan1.3-compute -S -O1 -o - %s | FileCheck %s + +// Resource names must not introduce byte arrays or require Int8 support. +// CHECK-NOT: OpCapability Int8 +// CHECK: OpName [[BUF:%[0-9]+]] "buf" +// CHECK-NOT: OpTypeInt 8 +// CHECK: [[BUF]] = OpVariable {{.*}} StorageBuffer + +RWStructuredBuffer<int> buf : register(u0); + +[numthreads(1, 1, 1)] +void main() { + buf[0] = 1; +} diff --git a/llvm/docs/SPIRVUsage.md b/llvm/docs/SPIRVUsage.md index 2cfe73bf398d95..baec24176987af 100644 --- a/llvm/docs/SPIRVUsage.md +++ b/llvm/docs/SPIRVUsage.md @@ -550,6 +550,16 @@ SPIR-V backend, along with their descriptions and argument details. If `arraySize > 1`, then the binding represents an array of resources of the given size, and the handle for the resource at the given index is returned. If the index is possibly non-uniform, then `isUniformIndex` must get set to true. + * - `int_spv_resource_handlefrombinding_mdname` + - Resource type + - `[32-bit Integer set, 32-bit Integer binding, 32-bit Integer arraySize, 32-bit Integer index, metadata name]` + - Backend form of `int_spv_resource_handlefrombinding`, produced by resource + name lowering. The name is a metadata string + literal as to not emit a real spirv constant. + * - `int_spv_resource_handlefromimplicitbinding_mdname` + - Resource type + - `[32-bit Integer orderId, 32-bit Integer space, 32-bit Integer arraySize, 32-bit Integer index, metadata name]` + - Metadata-name * - `int_spv_typeBufferLoad` - Scalar or vector - `[spirv.Image ImageBuffer, 32-bit Integer coordinate]` diff --git a/llvm/include/llvm/IR/IntrinsicsSPIRV.td b/llvm/include/llvm/IR/IntrinsicsSPIRV.td index 1fd6ab3cc77ecb..38eadb686f4fe7 100644 --- a/llvm/include/llvm/IR/IntrinsicsSPIRV.td +++ b/llvm/include/llvm/IR/IntrinsicsSPIRV.td @@ -527,12 +527,26 @@ let TargetPrefix = "spv" in { llvm_i32_ty, llvm_ptr_ty], [IntrNoMem]>; + // Identical to int_spv_resource_handlefrombinding but uses string + // literal metadata to carry the name. + def int_spv_resource_handlefrombinding_mdname + : DefaultAttrsIntrinsic<[llvm_any_ty], + [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty, + llvm_i32_ty, llvm_metadata_ty], + [IntrNoMem]>; + def int_spv_resource_handlefromimplicitbinding : DefaultAttrsIntrinsic<[llvm_any_ty], [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty, llvm_i32_ty, llvm_ptr_ty], [IntrNoMem]>; + def int_spv_resource_handlefromimplicitbinding_mdname + : DefaultAttrsIntrinsic<[llvm_any_ty], + [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty, + llvm_i32_ty, llvm_metadata_ty], + [IntrNoMem]>; + def int_spv_resource_counterhandlefromimplicitbinding : DefaultAttrsIntrinsic<[llvm_any_ty], [llvm_any_ty, llvm_i32_ty, llvm_i32_ty], diff --git a/llvm/lib/Target/SPIRV/CMakeLists.txt b/llvm/lib/Target/SPIRV/CMakeLists.txt index e391edba450a62..738ed3959e6079 100644 --- a/llvm/lib/Target/SPIRV/CMakeLists.txt +++ b/llvm/lib/Target/SPIRV/CMakeLists.txt @@ -52,6 +52,7 @@ add_llvm_target(SPIRVCodeGen SPIRVRegularizer.cpp SPIRVFinalizeShaderLinkage.cpp SPIRVRemoveUnusedResources.cpp + SPIRVLowerResourceNames.cpp SPIRVSubtarget.cpp SPIRVTargetMachine.cpp SPIRVTargetTransformInfo.cpp diff --git a/llvm/lib/Target/SPIRV/SPIRV.h b/llvm/lib/Target/SPIRV/SPIRV.h index 29c6d8cba7eb7f..ce04508e3b8263 100644 --- a/llvm/lib/Target/SPIRV/SPIRV.h +++ b/llvm/lib/Target/SPIRV/SPIRV.h @@ -43,6 +43,14 @@ class SPIRVStructurizerPass FunctionPass *createSPIRVStructurizerPass(); +class SPIRVLowerResourceNamesPass + : public RequiredPassInfoMixin<SPIRVLowerResourceNamesPass> { +public: + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; + +ModulePass *createSPIRVLowerResourceNamesLegacyPass(); + class SPIRVRemoveUnusedResourcesPass : public RequiredPassInfoMixin<SPIRVRemoveUnusedResourcesPass> { public: @@ -211,6 +219,7 @@ void initializeSPIRVPrepareModuleAnalysisLegacyPass(PassRegistry &); void initializeSPIRVStructurizerPass(PassRegistry &); void initializeSPIRVCBufferAccessLegacyPass(PassRegistry &); void initializeSPIRVRemoveUnusedResourcesLegacyPass(PassRegistry &); +void initializeSPIRVLowerResourceNamesLegacyPass(PassRegistry &); void initializeSPIRVPushConstantAccessLegacyPass(PassRegistry &); void initializeSPIRVEmitIntrinsicsLegacyPass(PassRegistry &); void initializeSPIRVLegalizePointerCastLegacyPass(PassRegistry &); diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp index daaa18d2222788..4a44a7b68d14dc 100644 --- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp @@ -429,7 +429,7 @@ bool isConvergenceIntrinsic(const Instruction *I) { bool expectIgnoredInIRTranslation(const Instruction *I) { return match(I, m_AnyIntrinsic<Intrinsic::invariant_start, - Intrinsic::spv_resource_handlefrombinding, + Intrinsic::spv_resource_handlefrombinding_mdname, Intrinsic::spv_resource_getbasepointer, Intrinsic::spv_resource_getpointer>()); } diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp index f489048faa09a1..389c92db3bca15 100644 --- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp @@ -738,8 +738,8 @@ static bool intrinsicHasSideEffects(Intrinsic::ID ID) { case Intrinsic::spv_refract: case Intrinsic::spv_resource_getbasepointer: case Intrinsic::spv_resource_getpointer: - case Intrinsic::spv_resource_handlefrombinding: - case Intrinsic::spv_resource_handlefromimplicitbinding: + case Intrinsic::spv_resource_handlefrombinding_mdname: + case Intrinsic::spv_resource_handlefromimplicitbinding_mdname: case Intrinsic::spv_resource_nonuniformindex: case Intrinsic::spv_resource_sample: case Intrinsic::spv_rsqrt: @@ -5950,7 +5950,7 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg, // Discard internal intrinsics. case Intrinsic::spv_value_md: break; - case Intrinsic::spv_resource_handlefrombinding: { + case Intrinsic::spv_resource_handlefrombinding_mdname: { return selectHandleFromBinding(ResVReg, ResType, I); } case Intrinsic::spv_resource_counterhandlefrombinding: @@ -6077,14 +6077,16 @@ bool SPIRVInstructionSelector::selectCounterHandleFromBinding( Register MainHandleReg = Intr.getOperand(2).getReg(); auto *MainHandleDef = cast<GIntrinsic>(getVRegDef(*MRI, MainHandleReg)); assert(MainHandleDef->getIntrinsicID() == - Intrinsic::spv_resource_handlefrombinding); + Intrinsic::spv_resource_handlefrombinding_mdname); uint32_t Set = getIConstVal(Intr.getOperand(3).getReg(), MRI); uint32_t Binding = getIConstVal(Intr.getOperand(4).getReg(), MRI); uint32_t ArraySize = getIConstVal(MainHandleDef->getOperand(4).getReg(), MRI); Register IndexReg = MainHandleDef->getOperand(5).getReg(); std::string CounterName = - getStringValueFromReg(MainHandleDef->getOperand(6).getReg(), *MRI) + + cast<MDString>(MainHandleDef->getOperand(6).getMetadata()->getOperand(0)) + ->getString() + .str() + ".counter"; // Create the counter variable. @@ -7900,13 +7902,15 @@ bool SPIRVInstructionSelector::loadHandleBeforePosition( MachineInstr &Pos) const { assert(HandleDef.getIntrinsicID() == - Intrinsic::spv_resource_handlefrombinding); + Intrinsic::spv_resource_handlefrombinding_mdname); uint32_t Set = foldImm(HandleDef.getOperand(2), MRI); uint32_t Binding = foldImm(HandleDef.getOperand(3), MRI); uint32_t ArraySize = foldImm(HandleDef.getOperand(4), MRI); Register IndexReg = HandleDef.getOperand(5).getReg(); std::string Name = - getStringValueFromReg(HandleDef.getOperand(6).getReg(), *MRI); + cast<MDString>(HandleDef.getOperand(6).getMetadata()->getOperand(0)) + ->getString() + .str(); bool IsStructuredBuffer = ResType->getOpcode() == SPIRV::OpTypePointer; MachineIRBuilder MIRBuilder(HandleDef); diff --git a/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp b/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp index 5a54e553789879..27bd6ce4e256cf 100644 --- a/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp @@ -19,8 +19,8 @@ #include "SPIRV.h" #include "llvm/ADT/BitVector.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/Twine.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/InstVisitor.h" #include "llvm/IR/Intrinsics.h" @@ -68,7 +68,7 @@ class SPIRVLegalizeResourceBindingLegacy : public ModulePass { static uint32_t getDescSet(const CallInst *CI) { uint32_t DescSetArgIdx; switch (CI->getIntrinsicID()) { - case Intrinsic::spv_resource_handlefromimplicitbinding: + case Intrinsic::spv_resource_handlefromimplicitbinding_mdname: DescSetArgIdx = 1; break; case Intrinsic::spv_resource_counterhandlefromimplicitbinding: @@ -114,13 +114,13 @@ void SPIRVLegalizeResourceBindingImpl::collectBindingInfo(Module &M) { continue; switch (F.getIntrinsicID()) { - case Intrinsic::spv_resource_handlefrombinding: + case Intrinsic::spv_resource_handlefrombinding_mdname: collectBinding(F, /*ArgDescSetIdx*/ 0, /*ArgBindingIdx*/ 1); break; case Intrinsic::spv_resource_counterhandlefrombinding: collectBinding(F, /*ArgDescSetIdx*/ 1, /*ArgBindingIdx*/ 2); break; - case Intrinsic::spv_resource_handlefromimplicitbinding: + case Intrinsic::spv_resource_handlefromimplicitbinding_mdname: case Intrinsic::spv_resource_counterhandlefromimplicitbinding: MayHaveImplicitBindings = true; break; @@ -157,14 +157,14 @@ static void replaceWithHandleFromBinding(Module &M, CallInst *CI, Value *RangeOp, Value *IndexOp, Value *Name) { assert((CI->getIntrinsicID() == - Intrinsic::spv_resource_handlefromimplicitbinding || + Intrinsic::spv_resource_handlefromimplicitbinding_mdname || CI->getIntrinsicID() == Intrinsic::spv_resource_handlefromheap) && "unexpected binding intrinsic"); IRBuilder<> Builder(CI); Value *DescSetOp = Builder.getInt32(DescSet); Value *BindingOp = Builder.getInt32(Binding); Function *NewFunc = Intrinsic::getOrInsertDeclaration( - &M, Intrinsic::spv_resource_handlefrombinding, {CI->getType()}); + &M, Intrinsic::spv_resource_handlefrombinding_mdname, {CI->getType()}); CallInst *NewCI = Builder.CreateCall( NewFunc, {DescSetOp, BindingOp, RangeOp, IndexOp, Name}); NewCI->setCallingConv(CI->getCallingConv()); @@ -205,7 +205,8 @@ bool SPIRVLegalizeResourceBindingImpl::replaceImplicitBindingCalls(Module &M) { continue; uint32_t OrderIdIdx; - if (F.getIntrinsicID() == Intrinsic::spv_resource_handlefromimplicitbinding) + if (F.getIntrinsicID() == + Intrinsic::spv_resource_handlefromimplicitbinding_mdname) OrderIdIdx = 0; else if (F.getIntrinsicID() == Intrinsic::spv_resource_counterhandlefromimplicitbinding) @@ -247,7 +248,7 @@ bool SPIRVLegalizeResourceBindingImpl::replaceImplicitBindingCalls(Module &M) { // Replace the implicit binding call with an explicit binding call. if (CI->getIntrinsicID() == - Intrinsic::spv_resource_handlefromimplicitbinding) + Intrinsic::spv_resource_handlefromimplicitbinding_mdname) replaceWithHandleFromBinding(M, CI, DescSet, Binding, CI->getArgOperand(2), CI->getArgOperand(3), CI->getArgOperand(4)); @@ -262,46 +263,11 @@ bool SPIRVLegalizeResourceBindingImpl::replaceImplicitBindingCalls(Module &M) { return Changed; } -bool moduleContainsConstantString(Module &M, StringRef Str) { - for (GlobalVariable &GV : M.globals()) { - if (!GV.hasInitializer()) - continue; - if (ConstantDataArray *CDA = - dyn_cast<ConstantDataArray>(GV.getInitializer())) { - if (CDA->isString() && CDA->getAsCString() == Str) - return true; - } - } - return false; -} - -// Creates unique global variable for the heap name string, making -// sure that each heap name string is unique within the module. -GlobalVariable *createHeapNameString(Module &M, StringRef Name) { - SmallString<32> GlobalStringName(Name); - uint32_t HeapNameLen = Name.size(); - StringRef HeapName; - for (unsigned Suffix = 1;; ++Suffix) { - GlobalStringName.append(".str"); - if (!M.getNamedValue(GlobalStringName)) { - // Make sure the module does not already have a constant - // string with this value. - HeapName = GlobalStringName.substr(0, HeapNameLen); - if (!moduleContainsConstantString(M, HeapName)) - break; - } - GlobalStringName.resize(Name.size()); - raw_svector_ostream(GlobalStringName) << '.' << Suffix; - HeapNameLen = GlobalStringName.size(); - } - Constant *Init = ConstantDataArray::getString(M.getContext(), HeapName); - GlobalVariable *HeapNameGV = new GlobalVariable( - M, Init->getType(), /*isConstant=*/true, GlobalValue::PrivateLinkage, - Init, GlobalStringName, /*InsertBefore=*/nullptr, - GlobalVariable::NotThreadLocal, /*AddressSpace=*/0); - HeapNameGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); - HeapNameGV->setAlignment(Align(1)); - return HeapNameGV; +static Value *createHeapNameMetadata(LLVMContext &Ctx, StringRef Name, + unsigned ID) { + std::string HeapName = (Twine(".") + Name + "." + Twine(ID)).str(); + return MetadataAsValue::get(Ctx, + MDNode::get(Ctx, MDString::get(Ctx, HeapName))); } // The SPIR-V backend represents dynamic resources as unbounded resource arrays. @@ -363,25 +329,16 @@ bool SPIRVLegalizeResourceBindingImpl::replaceHeapBindingCalls(Module &M) { // ResourceDescriptorHeap name for each unique resource type. They will all // share the same binding and will overlap. uint32_t Binding = getAndReserveFirstUnusedBinding(DescSet); - SmallDenseMap<TargetExtType *, GlobalVariable *> ResourceDescriptorHeaps; + unsigned HeapID = 1; for (Function *F : CbvSrvUavs) { - TargetExtType *ResType = cast<TargetExtType>(F->getReturnType()); - GlobalVariable *HeapNameGV = nullptr; - auto It = ResourceDescriptorHeaps.find(ResType); - if (It == ResourceDescriptorHeaps.end()) { - HeapNameGV = createHeapNameString(M, "ResourceDescriptorHeap"); - [[maybe_unused]] auto [InsertedIt, Inserted] = - ResourceDescriptorHeaps.try_emplace(ResType, HeapNameGV); - assert(Inserted && "resource heap name already exists"); - } else { - HeapNameGV = It->second; - } + Value *HeapName = + createHeapNameMetadata(M.getContext(), "BufferHeap", HeapID++); for (User *U : make_early_inc_range(F->users())) { if (CallInst *CI = dyn_cast<CallInst>(U)) { Value *HeapIdx = CI->getArgOperand(0); replaceWithHandleFromBinding(M, CI, DescSet, Binding, Zero, HeapIdx, - HeapNameGV); + HeapName); Changed = true; } } @@ -396,8 +353,8 @@ bool SPIRVLegalizeResourceBindingImpl::replaceHeapBindingCalls(Module &M) { [[maybe_unused]] TargetExtType *SamplerHandleType = cast<TargetExtType>(Samplers.front()->getReturnType()); - GlobalVariable *HeapNameGV = - createHeapNameString(M, "SamplerDescriptorHeap"); + Value *HeapName = + createHeapNameMetadata(M.getContext(), "SamplerHeap", 1); for (Function *F : Samplers) { assert(F->getReturnType() == SamplerHandleType && "sampler handle type mismatch"); @@ -405,7 +362,7 @@ bool SPIRVLegalizeResourceBindingImpl::replaceHeapBindingCalls(Module &M) { if (CallInst *CI = dyn_cast<CallInst>(U)) { Value *HeapIdx = CI->getArgOperand(0); replaceWithHandleFromBinding(M, CI, DescSet, Binding, Zero, HeapIdx, - HeapNameGV); + HeapName); Changed = true; } } diff --git a/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp new file mode 100644 index 00000000000000..89b8246369198e --- /dev/null +++ b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp @@ -0,0 +1,89 @@ +//===- SPIRVLowerResourceNames.cpp - Lower resource names to metadata -------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SPIRV.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/Analysis/ValueTracking.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/IntrinsicsSPIRV.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" + +using namespace llvm; + +#define DEBUG_TYPE "spirv-lower-resource-names" + +static bool lowerResourceNames(Module &M) { + SmallPtrSet<GlobalVariable *, 8> Names; + bool Changed = false; + for (Function &F : make_early_inc_range(M)) { + Intrinsic::ID MdID; + switch (F.getIntrinsicID()) { + case Intrinsic::spv_resource_handlefrombinding: + MdID = Intrinsic::spv_resource_handlefrombinding_mdname; + break; + case Intrinsic::spv_resource_handlefromimplicitbinding: + MdID = Intrinsic::spv_resource_handlefromimplicitbinding_mdname; + break; + default: + continue; + } + if (F.use_empty()) + continue; + Function *MdFn = + Intrinsic::getOrInsertDeclaration(&M, MdID, {F.getReturnType()}); + for (User *U : make_early_inc_range(F.users())) { + auto *CI = cast<CallInst>(U); + Value *NamePtr = CI->getArgOperand(4); + StringRef Name; + if (!isa<ConstantPointerNull>(NamePtr) && + !getConstantStringInfo(NamePtr, Name)) + report_fatal_error("expected a constant resource name"); + if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(NamePtr))) + Names.insert(GV); + LLVMContext &Ctx = M.getContext(); + auto *NameMD = MDNode::get(Ctx, MDString::get(Ctx, Name)); + CI->setCalledFunction(MdFn); + CI->setArgOperand(4, MetadataAsValue::get(Ctx, NameMD)); + CI->setAttributes(CI->getAttributes().removeParamAttributes(Ctx, 4)); + Changed = true; + } + F.eraseFromParent(); + } + for (GlobalVariable *GV : Names) { + GV->removeDeadConstantUsers(); + if (GV->hasLocalLinkage() && GV->use_empty()) + GV->eraseFromParent(); + } + return Changed; +} + +PreservedAnalyses +SPIRVLowerResourceNamesPass::run(Module &M, ModuleAnalysisManager &AM) { + return lowerResourceNames(M) ? PreservedAnalyses::none() + : PreservedAnalyses::all(); +} + +namespace { +class SPIRVLowerResourceNamesLegacy : public ModulePass { +public: + static char ID; + SPIRVLowerResourceNamesLegacy() : ModulePass(ID) {} + bool runOnModule(Module &M) override { return lowerResourceNames(M); } +}; +char SPIRVLowerResourceNamesLegacy::ID = 0; +} // namespace + +INITIALIZE_PASS(SPIRVLowerResourceNamesLegacy, DEBUG_TYPE, + "SPIRV Lower Resource Names", false, false) + +ModulePass *llvm::createSPIRVLowerResourceNamesLegacyPass() { + return new SPIRVLowerResourceNamesLegacy(); +} diff --git a/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def b/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def index a61caae8d408ef..dd1d3c22a357dd 100644 --- a/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def +++ b/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def @@ -34,6 +34,7 @@ MODULE_PASS("spirv-prepare-functions", SPIRVPrepareFunctionsPass(*static_cast<co MODULE_PASS("spirv-prepare-globals", SPIRVPrepareGlobalsPass()) MODULE_PASS("spirv-pushconstant-access", SPIRVPushConstantAccessPass(*static_cast<const SPIRVTargetMachine *>(this))) MODULE_PASS("spirv-remove-unused-resources", SPIRVRemoveUnusedResourcesPass()) +MODULE_PASS("spirv-lower-resource-names", SPIRVLowerResourceNamesPass()) #undef MODULE_PASS #ifndef FUNCTION_PASS diff --git a/llvm/lib/Target/SPIRV/SPIRVRemoveUnusedResources.cpp b/llvm/lib/Target/SPIRV/SPIRVRemoveUnusedResources.cpp index e0e6ac5bf96522..8502f4a99bfe15 100644 --- a/llvm/lib/Target/SPIRV/SPIRVRemoveUnusedResources.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVRemoveUnusedResources.cpp @@ -45,8 +45,8 @@ static bool removeUnusedResources(Module &M) { switch (II->getIntrinsicID()) { default: continue; - case Intrinsic::spv_resource_handlefrombinding: - case Intrinsic::spv_resource_handlefromimplicitbinding: + case Intrinsic::spv_resource_handlefrombinding_mdname: + case Intrinsic::spv_resource_handlefromimplicitbinding_mdname: Handles.push_back(II); break; case Intrinsic::spv_resource_counterhandlefrombinding: @@ -60,7 +60,7 @@ static bool removeUnusedResources(Module &M) { bool Changed = false; - auto VisitHandle = [&](Instruction *Handle, bool IsCounter) { + auto VisitHandle = [&](Instruction *Handle) { for (User *U : make_early_inc_range(Handle->users())) { auto *SI = dyn_cast<StoreInst>(U); if (!SI || SI->isVolatile() || SI->getValueOperand() != Handle) @@ -73,13 +73,7 @@ static bool removeUnusedResources(Module &M) { Changed = true; } - const unsigned ResourceNameOpIndex = 4; if (Handle->use_empty()) { - if (!IsCounter) { - if (auto *Name = dyn_cast<GlobalVariable>( - Handle->getOperand(ResourceNameOpIndex))) - DeadGlobals.insert(Name); - } Handle->eraseFromParent(); Changed = true; } @@ -88,9 +82,9 @@ static bool removeUnusedResources(Module &M) { // Visit implicit counters first since they may use other handles // that are otherwise dead for (auto *Counter : CounterHandles) - VisitHandle(Counter, true); + VisitHandle(Counter); for (auto *Handle : Handles) - VisitHandle(Handle, false); + VisitHandle(Handle); for (auto *GV : DeadGlobals) { if (GV->user_empty()) { diff --git a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp index 69d38cd3b73793..f7ab8c68d394e4 100644 --- a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp @@ -70,6 +70,7 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() { initializeSPIRVCtorDtorLoweringLegacyPass(PR); initializeSPIRVFinalizeShaderLinkageLegacyPass(PR); initializeSPIRVRemoveUnusedResourcesLegacyPass(PR); + initializeSPIRVLowerResourceNamesLegacyPass(PR); } static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) { @@ -190,6 +191,8 @@ void SPIRVPassConfig::addIRPasses() { TargetPassConfig::addIRPasses(); + addPass(createSPIRVLowerResourceNamesLegacyPass()); + if (TM.getSubtargetImpl()->isShader()) { if (getOptLevel() != CodeGenOptLevel::None) addPass(createSPIRVFinalizeShaderLinkagePass(TM)); diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll index 084bb40ecf8961..239b29a6d216b2 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll @@ -18,15 +18,15 @@ target datalayout = "e-ve-i64:64-n8:16:32:64-G10" ; CHECK-DAG: OpDecorate [[Buf]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Buf]] Binding 1 -; CHECK-DAG: OpName [[Heap0:%[0-9]+]] "ResourceDescriptorHeap" +; CHECK-DAG: OpName [[Heap0:%[0-9]+]] ".BufferHeap.1" ; CHECK-DAG: OpDecorate [[Heap0]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap0]] Binding 0 -; CHECK-DAG: OpName [[Heap1:%[0-9]+]] "ResourceDescriptorHeap.1" +; CHECK-DAG: OpName [[Heap1:%[0-9]+]] ".BufferHeap.2" ; CHECK-DAG: OpDecorate [[Heap1]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap1]] Binding 0 -; CHECK-DAG: OpName [[Heap2:%[0-9]+]] "ResourceDescriptorHeap.2" +; CHECK-DAG: OpName [[Heap2:%[0-9]+]] ".BufferHeap.3" ; CHECK-DAG: OpDecorate [[Heap2]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap2]] Binding 0 diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll index 8f29f498381744..8de96d0997ea1f 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll @@ -15,11 +15,11 @@ ; CHECK-DAG: OpDecorate [[Buf]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Buf]] Binding 1 -; CHECK-DAG: OpName [[ResourceHeap:%[0-9]+]] "ResourceDescriptorHeap" +; CHECK-DAG: OpName [[ResourceHeap:%[0-9]+]] ".BufferHeap.1" ; CHECK-DAG: OpDecorate [[ResourceHeap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[ResourceHeap]] Binding 0 -; CHECK-DAG: OpName [[SamplerHeap:%[0-9]+]] "SamplerDescriptorHeap" +; CHECK-DAG: OpName [[SamplerHeap:%[0-9]+]] ".SamplerHeap.1" ; CHECK-DAG: OpDecorate [[SamplerHeap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[SamplerHeap]] Binding 2 diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll index 4613b27d0aee31..d2b8589d6b3e89 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll @@ -15,11 +15,11 @@ ; CHECK-DAG: OpDecorate [[Buf]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Buf]] Binding 1 -; CHECK-DAG: OpName [[Heap:%[0-9]+]] "ResourceDescriptorHeap" +; CHECK-DAG: OpName [[Heap:%[0-9]+]] ".BufferHeap.1" ; CHECK-DAG: OpDecorate [[Heap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap]] Binding 0 -; CHECK-DAG: OpName [[CounterHeap:%[0-9]+]] "ResourceDescriptorHeap.counter" +; CHECK-DAG: OpName [[CounterHeap:%[0-9]+]] ".BufferHeap.1.counter" ; CHECK-DAG: OpDecorate [[CounterHeap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[CounterHeap]] Binding 2 diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/StorageImageConstIdx.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/StorageImageConstIdx.ll index e4ec23171490f4..a41218e1127809 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/StorageImageConstIdx.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/StorageImageConstIdx.ll @@ -5,7 +5,6 @@ ; CHECK-DAG: OpCapability Shader ; CHECK-DAG: OpCapability Image1D -; CHECK-DAG: OpCapability Int8 ; CHECK-NOT: OpCapability ; CHECK-DAG: OpDecorate [[Var:%[0-9]+]] DescriptorSet 3 diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resource-dead-handle.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resource-dead-handle.ll index c6f643bb8fce08..9696dbeaf78b35 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resource-dead-handle.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resource-dead-handle.ll @@ -1,4 +1,4 @@ -; RUN: opt -S -passes=spirv-remove-unused-resources %s -o - | FileCheck %s +; RUN: opt -S -passes=spirv-lower-resource-names,spirv-remove-unused-resources %s -o - | FileCheck %s ; Verify that the handle creation is removed when its only use was a store to ; an unused resource global. diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-counter.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-counter.ll index bfb3d7693653d1..09b9efa9561d81 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-counter.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-counter.ll @@ -1,4 +1,4 @@ -; RUN: opt -S -passes=spirv-remove-unused-resources %s -o - | FileCheck %s +; RUN: opt -S -passes=spirv-lower-resource-names,spirv-remove-unused-resources %s -o - | FileCheck %s ; ; When using entry point main, eliminate the unused buffer handle and ; it's associated counter handle @@ -22,7 +22,7 @@ ; CHECK-NOT: @.str = ; CHECK-LABEL: define void @main() ; CHECK-NOT: call target("spirv.VulkanBuffer", -; CHECK: %[[USED:.*]] = call target("spirv.Image", {{.*}}) @llvm.spv.resource.handlefromimplicitbinding{{.*}}(i32 2, i32 0, i32 1, i32 0, ptr @.str.2) +; CHECK: %[[USED:.*]] = call target("spirv.Image", {{.*}}) @llvm.spv.resource.handlefromimplicitbinding.mdname{{.*}}(i32 2, i32 0, i32 1, i32 0, metadata ; CHECK: @llvm.spv.resource.getpointer{{.*}}(target("spirv.Image", {{.*}}) %[[USED]], i32 0) ; CHECK: store float 1.000000e+00 ; CHECK: ret void diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-live-handle.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-live-handle.ll index faffe63cd4c530..26f1557e0a3cd9 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-live-handle.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-live-handle.ll @@ -1,4 +1,4 @@ -; RUN: opt -S -passes=spirv-remove-unused-resources %s -o - | FileCheck %s +; RUN: opt -S -passes=spirv-lower-resource-names,spirv-remove-unused-resources %s -o - | FileCheck %s ; For reference, this test contains the IR for the following HLSL ; after selecting frag_ep1 and just after spirv-finalize-shader-linkage diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-loaded-handle.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-loaded-handle.ll index 9f0cb536e49991..d542ad636ab5ed 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-loaded-handle.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/remove-unused-resources-loaded-handle.ll @@ -1,4 +1,4 @@ -; RUN: opt -S -passes=spirv-remove-unused-resources %s -o - | FileCheck %s +; RUN: opt -S -passes=spirv-lower-resource-names,spirv-remove-unused-resources %s -o - | FileCheck %s ; Make sure the handle/global survive the removal pass since they're actually used ; Original HLSL: @@ -15,7 +15,6 @@ ; } ; CHECK: @_ZL4data.0 = internal {{.*}}global target("spirv.VulkanBuffer", %Data, 2, 0) -; CHECK: @.str = private unnamed_addr constant [5 x i8] c"data\00" ; CHECK-LABEL: define internal {{.*}}@_Z8readDatav() ; CHECK: load target("spirv.VulkanBuffer", %Data, 2, 0), ptr @_ZL4data.0 ; CHECK-LABEL: define void @main() diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll new file mode 100644 index 00000000000000..8639122e04be16 --- /dev/null +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll @@ -0,0 +1,42 @@ +; RUN: opt -S -passes=spirv-lower-resource-names %s | FileCheck %s + +target triple = "spirv1.6-unknown-vulkan1.3-library" + +; CHECK-NOT: @.str = +; CHECK-NOT: @.sampler = +; CHECK: @.runtime = private unnamed_addr constant [4 x i8] c"buf\00" + [email protected] = private unnamed_addr constant [4 x i8] c"buf\00" [email protected] = private unnamed_addr constant [8 x i8] c"sampler\00" [email protected] = private unnamed_addr constant [4 x i8] c"buf\00" + +; CHECK-LABEL: define void @explicit() +; CHECK: %handle = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata ![[BUF:[0-9]+]]) +define void @explicit() { + %handle = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr @.str) + %ptr = call ptr addrspace(11) @llvm.spv.resource.getpointer(target("spirv.VulkanBuffer", [0 x i32], 12, 1) %handle, i32 0) + store i32 1, ptr addrspace(11) %ptr + ret void +} + +; CHECK-LABEL: define target("spirv.Sampler") @implicit() +; CHECK: %handle = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata ![[SAMPLER:[0-9]+]]) +define target("spirv.Sampler") @implicit() { + %handle = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding(i32 0, i32 0, i32 1, i32 0, ptr @.sampler) + ret target("spirv.Sampler") %handle +} + +; CHECK-LABEL: define void @runtime_use() +; CHECK: %handle = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata ![[BUF]]) +; CHECK: load volatile i8, ptr @.runtime +define void @runtime_use() { + %handle = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr @.runtime) + %ptr = call ptr addrspace(11) @llvm.spv.resource.getpointer(target("spirv.VulkanBuffer", [0 x i32], 12, 1) %handle, i32 0) + %char = load volatile i8, ptr @.runtime + %value = zext i8 %char to i32 + store i32 %value, ptr addrspace(11) %ptr + ret void +} + +; CHECK-DAG: ![[BUF]] = !{!"buf"} +; CHECK-DAG: ![[SAMPLER]] = !{!"sampler"} diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/test_counters.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/test_counters.ll index b178a56f717519..cd4de1ac55e7a7 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/test_counters.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/test_counters.ll @@ -4,7 +4,6 @@ ; ModuleID = 'test_counters.hlsl' source_filename = "test_counters.hlsl" -; CHECK: OpCapability Int8 ; CHECK-DAG: OpName [[OutputBuffer:%[0-9]+]] "OutputBuffer" ; CHECK-DAG: OpName [[InputBuffer:%[0-9]+]] "InputBuffer" ; CHECK-DAG: OpName [[OutputBufferCounter:%[0-9]+]] "OutputBuffer.counter" diff --git a/llvm/test/CodeGen/SPIRV/llc-pipeline.ll b/llvm/test/CodeGen/SPIRV/llc-pipeline.ll index ebf6ec7418fcbb..240c6ba3c3d321 100644 --- a/llvm/test/CodeGen/SPIRV/llc-pipeline.ll +++ b/llvm/test/CodeGen/SPIRV/llc-pipeline.ll @@ -33,6 +33,7 @@ ; SPIRV-O0-NEXT: Instrument function entry/exit with calls to e.g. mcount() (post inlining) ; SPIRV-O0-NEXT: Scalarize Masked Memory Intrinsics ; SPIRV-O0-NEXT: Expand reduction intrinsics +; SPIRV-O0-NEXT: SPIRV Lower Resource Names ; SPIRV-O0-NEXT: Expand variadic functions ; SPIRV-O0-NEXT: FunctionPass Manager ; SPIRV-O0-NEXT: SPIR-V Regularizer @@ -128,6 +129,7 @@ ; SPIRV-Opt-NEXT: Instrument function entry/exit with calls to e.g. mcount() (post inlining) ; SPIRV-Opt-NEXT: Scalarize Masked Memory Intrinsics ; SPIRV-Opt-NEXT: Expand reduction intrinsics +; SPIRV-Opt-NEXT: SPIRV Lower Resource Names ; SPIRV-Opt-NEXT: Expand variadic functions ; SPIRV-Opt-NEXT: FunctionPass Manager ; SPIRV-Opt-NEXT: SPIR-V Regularizer diff --git a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll index e4afc7a956e16c..f8611ee78b52aa 100644 --- a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll +++ b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll @@ -1,34 +1,30 @@ -; RUN: opt -S -passes=spirv-legalize-resource-binding -mtriple=spirv1.6-vulkan1.3-library < %s | FileCheck %s +; RUN: opt -S -passes=spirv-lower-resource-names,spirv-legalize-resource-binding -mtriple=spirv1.6-vulkan1.3-library < %s | FileCheck %s @.str.used = private unnamed_addr constant [5 x i8] c"used\00", align 1 ; Verify heap-binding intrinsic calls are assigned bindings after bindings ; already used in descriptor set 0 and use the heap index as the array index. -; CHECK-DAG: @ResourceDescriptorHeap.str = private unnamed_addr constant [23 x i8] c"ResourceDescriptorHeap\00", align 1 -; CHECK-DAG: @ResourceDescriptorHeap.1.str = private unnamed_addr constant [25 x i8] c"ResourceDescriptorHeap.1\00", align 1 -; CHECK-DAG: @SamplerDescriptorHeap.str = private unnamed_addr constant [22 x i8] c"SamplerDescriptorHeap\00", align 1 - ; CHECK-LABEL: define void @main( define void @main() local_unnamed_addr #0 { entry: ; Resource bound at desc 0 binding 0 -; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 0, i32 1, i32 0, ptr @.str.used) +; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata %used = call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding.tspirv.SignedImage_i32_5_2_0_0_2_0t(i32 0, i32 0, i32 1, i32 0, ptr @.str.used) ; Heap resource - bound to resource array at desc 0 binding 1 -; CHECK: [[HEAPHANDLE:%.*]] = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 1, i32 0, i32 7, ptr @ResourceDescriptorHeap.1.str) +; CHECK: [[HEAPHANDLE:%.*]] = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 1, i32 0, i32 7, metadata ![[BUFFER:[0-9]+]]) %heap_resource_type1 = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefromheap.tspirv.VulkanBuffer_a0i32_12_1t(i32 7) ; Heap resource of different type - also bound to desc 0 binding 1 -; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 1, i32 0, i32 13, ptr @ResourceDescriptorHeap.str) -; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 1, i32 0, i32 19, ptr @ResourceDescriptorHeap.str) +; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 1, i32 0, i32 13, metadata ![[IMAGE:[0-9]+]]) +; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 1, i32 0, i32 19, metadata ![[IMAGE]]) %heap_resource_type2 = call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefromheap.tspirv.SignedImage_i32_5_2_0_0_2_0t(i32 13) %heap_resource_type2_again = call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefromheap.tspirv.SignedImage_i32_5_2_0_0_2_0t(i32 19) ; Heap sampler - bound to desc 0 binding 2 -; CHECK: call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 2, i32 0, i32 8, ptr @SamplerDescriptorHeap.str) +; CHECK: call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 2, i32 0, i32 8, metadata ![[SAMPLER:[0-9]+]]) %heap_sampler = call target("spirv.Sampler") @llvm.spv.resource.handlefromheap.tspirv.Samplert(i32 8) ; Counters for heap resources - bound to desc 0 binding 3 @@ -41,4 +37,8 @@ entry: ; CHECK-NOT: @llvm.spv.resource.handlefromheap ; CHECK-NOT: @llvm.spv.resource.counterhandlefromheap -attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } \ No newline at end of file +attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } + +; CHECK-DAG: ![[BUFFER]] = !{!".BufferHeap.2"} +; CHECK-DAG: ![[IMAGE]] = !{!".BufferHeap.1"} +; CHECK-DAG: ![[SAMPLER]] = !{!".SamplerHeap.1"} diff --git a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll index aa469afa96d3ce..40965dbdb4b91d 100644 --- a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll +++ b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll @@ -1,20 +1,16 @@ -; RUN: opt -S -passes=spirv-legalize-resource-binding -mtriple=spirv1.6-vulkan1.3-library < %s | FileCheck %s +; RUN: opt -S -passes=spirv-lower-resource-names,spirv-legalize-resource-binding -mtriple=spirv1.6-vulkan1.3-library < %s | FileCheck %s @.str.used = private unnamed_addr constant [5 x i8] c"used\00", align 1 @.str = private unnamed_addr constant [23 x i8] c"ResourceDescriptorHeap\00", align 1 -; Verify that a unique string is created for the heap name if the constant -; string "ResourceDescriptorHeap" already exists in the module. - -; CHECK-DAG: @ResourceDescriptorHeap.1.str = private unnamed_addr constant [25 x i8] c"ResourceDescriptorHeap.1\00", align 1 -; CHECK-NOT: @ResourceDescriptorHeap.str = private unnamed_addr constant [23 x i8] c"ResourceDescriptorHeap\00", align 1 +; Verify that an existing string does not affect the generated heap name. ; CHECK-LABEL: define void @main( define void @main() local_unnamed_addr #0 { entry: ; Heap resource - bound to resource array at desc 0 binding 0 -; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 0, i32 0, i32 13, ptr @ResourceDescriptorHeap.1.str) +; CHECK: call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 0, i32 0, i32 13, metadata ![[NAME:[0-9]+]]) %heap_resource = call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefromheap.tspirv.SignedImage_i32_5_2_0_0_2_0t(i32 13) ret void @@ -23,4 +19,6 @@ entry: ; CHECK-NOT: @llvm.spv.resource.handlefromheap ; CHECK-NOT: @llvm.spv.resource.counterhandlefromheap -attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } \ No newline at end of file +attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } + +; CHECK: ![[NAME]] = !{!".BufferHeap.1"} diff --git a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeResourceBinding-Implicit.ll b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeResourceBinding-Implicit.ll index c69c52f4af81c5..cc8090a16b67a3 100644 --- a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeResourceBinding-Implicit.ll +++ b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeResourceBinding-Implicit.ll @@ -1,4 +1,4 @@ -; RUN: opt -S -passes=spirv-legalize-resource-binding -mtriple=spirv1.6-vulkan1.3-library < %s | FileCheck %s +; RUN: opt -S -passes=spirv-lower-resource-names,spirv-legalize-resource-binding -mtriple=spirv1.6-vulkan1.3-library < %s | FileCheck %s @.str.b = private unnamed_addr constant [2 x i8] c"b\00", align 1 @.str.c = private unnamed_addr constant [2 x i8] c"c\00", align 1 @@ -10,11 +10,14 @@ define void @main() local_unnamed_addr #0 { entry: ; CHECK-LABEL: define void @main( -; CHECK: call target({{.*}}) @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 0, i32 1, i32 0, ptr {{.*}}@.str.b) -; CHECK: call target({{.*}}) @llvm.spv.resource.handlefrombinding{{.*}}(i32 1, i32 0, i32 1, i32 0, ptr {{.*}}@.str.c) +; CHECK: call target({{.*}}) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata ![[B:[0-9]+]]) +; CHECK: call target({{.*}}) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 1, i32 0, i32 1, i32 0, metadata ![[C:[0-9]+]]) %0 = tail call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefromimplicitbinding.tspirv.SignedImage_i32_5_2_0_0_2_0t(i32 0, i32 0, i32 1, i32 0, ptr nonnull @.str.b) %1 = tail call target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 0) @llvm.spv.resource.handlefromimplicitbinding.tspirv.SignedImage_i32_5_2_0_0_2_0t(i32 1, i32 1, i32 1, i32 0, ptr nonnull @.str.c) ret void } attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } + +; CHECK-DAG: ![[B]] = !{!"b"} +; CHECK-DAG: ![[C]] = !{!"c"} diff --git a/llvm/test/CodeGen/SPIRV/pointers/resource-vector-load-store.ll b/llvm/test/CodeGen/SPIRV/pointers/resource-vector-load-store.ll index 6fc03a386d14d5..ffb8bafdad03e7 100644 --- a/llvm/test/CodeGen/SPIRV/pointers/resource-vector-load-store.ll +++ b/llvm/test/CodeGen/SPIRV/pointers/resource-vector-load-store.ll @@ -12,7 +12,6 @@ define void @main() "hlsl.shader"="pixel" { ; CHECK: %[[FUNC:[0-9]+]] = OpFunction %[[VOID:[0-9]+]] None %[[FNTYPE:[0-9]+]] ; -- Begin function main ; CHECK-NEXT: %[[LABEL:[0-9]+]] = OpLabel -; CHECK-NEXT: %[[VAR:[0-9]+]] = OpVariable %[[PTR_FN:[a-zA-Z0-9_]+]] Function %[[INIT:[a-zA-Z0-9_]+]] ; CHECK-NEXT: %[[LOAD1:[0-9]+]] = OpLoad %[[IMG_TYPE:[a-zA-Z0-9_]+]] %[[IMG_VAR:[a-zA-Z0-9_]+]] ; CHECK-NEXT: %[[READ:[0-9]+]] = OpImageRead %[[VEC4:[a-zA-Z0-9_]+]] %[[LOAD1]] %[[COORD:[a-zA-Z0-9_]+]] ; CHECK-NEXT: %[[EXTRACT1:[0-9]+]] = OpCompositeExtract %[[FLOAT:[a-zA-Z0-9_]+]] %[[READ]] 0 diff --git a/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-access.ll b/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-access.ll index 89971f0e708a86..d24feb5efbc15f 100644 --- a/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-access.ll +++ b/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-access.ll @@ -29,23 +29,21 @@ define void @main() local_unnamed_addr #0 { ; CHECK-NOT: OpCapability VariablePointersStorageBuffer ; CHECK-LABEL: main -; CHECK: %42 = OpFunction %2 None %3 ; -- Begin function main +; CHECK: %27 = OpFunction %2 None %3 ; -- Begin function main ; CHECK-NEXT: %1 = OpLabel -; CHECK-NEXT: %43 = OpVariable %27 Function %37 -; CHECK-NEXT: %44 = OpVariable %26 Function %38 -; CHECK-NEXT: %45 = OpCopyObject %19 %39 -; CHECK-NEXT: %46 = OpCopyObject %16 %40 -; CHECK-NEXT: %47 = OpLoad %4 %41 -; CHECK-NEXT: %48 = OpAccessChain %13 %45 %28 %47 -; CHECK-NEXT: %49 = OpInBoundsAccessChain %9 %48 %30 -; CHECK-NEXT: %50 = OpLoad %8 %49 -; CHECK-NEXT: %51 = OpAccessChain %11 %46 %28 %47 -; CHECK-NEXT: %52 = OpInBoundsAccessChain %9 %51 %28 -; CHECK-NEXT: OpStore %52 %50 -; CHECK-NEXT: %53 = OpAccessChain %6 %48 %28 -; CHECK-NEXT: %54 = OpLoad %5 %53 -; CHECK-NEXT: %55 = OpInBoundsAccessChain %6 %51 %30 -; CHECK-NEXT: OpStore %55 %54 +; CHECK-NEXT: %28 = OpCopyObject %19 %24 +; CHECK-NEXT: %29 = OpCopyObject %16 %25 +; CHECK-NEXT: %30 = OpLoad %4 %26 +; CHECK-NEXT: %31 = OpAccessChain %13 %28 %21 %30 +; CHECK-NEXT: %32 = OpInBoundsAccessChain %9 %31 %23 +; CHECK-NEXT: %33 = OpLoad %8 %32 +; CHECK-NEXT: %34 = OpAccessChain %11 %29 %21 %30 +; CHECK-NEXT: %35 = OpInBoundsAccessChain %9 %34 %21 +; CHECK-NEXT: OpStore %35 %33 +; CHECK-NEXT: %36 = OpAccessChain %6 %31 %21 +; CHECK-NEXT: %37 = OpLoad %5 %36 +; CHECK-NEXT: %38 = OpInBoundsAccessChain %6 %34 %23 +; CHECK-NEXT: OpStore %38 %37 ; CHECK-NEXT: OpReturn ; CHECK-NEXT: OpFunctionEnd entry: diff --git a/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-vector-access.ll b/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-vector-access.ll index d7681787ec678e..c470d7d78508e8 100644 --- a/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-vector-access.ll +++ b/llvm/test/CodeGen/SPIRV/pointers/structured-buffer-vector-access.ll @@ -15,18 +15,16 @@ define void @main() local_unnamed_addr #0 { ; CHECK-LABEL: main -; CHECK: %33 = OpFunction %2 None %3 ; -- Begin function main +; CHECK: %18 = OpFunction %2 None %3 ; -- Begin function main ; CHECK-NEXT: %1 = OpLabel -; CHECK-NEXT: %34 = OpVariable %20 Function %29 -; CHECK-NEXT: %35 = OpVariable %19 Function %30 -; CHECK-NEXT: %36 = OpCopyObject %12 %31 -; CHECK-NEXT: %37 = OpCopyObject %10 %32 -; CHECK-NEXT: %38 = OpAccessChain %7 %36 %21 %21 -; CHECK-NEXT: %39 = OpLoad %6 %38 -; CHECK-NEXT: %40 = OpCompositeExtract %4 %39 1 -; CHECK-NEXT: %41 = OpAccessChain %7 %37 %21 %21 -; CHECK-NEXT: %42 = OpInBoundsAccessChain %5 %41 %22 -; CHECK-NEXT: OpStore %42 %40 +; CHECK-NEXT: %19 = OpCopyObject %12 %16 +; CHECK-NEXT: %20 = OpCopyObject %10 %17 +; CHECK-NEXT: %21 = OpAccessChain %7 %19 %14 %14 +; CHECK-NEXT: %22 = OpLoad %6 %21 +; CHECK-NEXT: %23 = OpCompositeExtract %4 %22 1 +; CHECK-NEXT: %24 = OpAccessChain %7 %20 %14 %14 +; CHECK-NEXT: %25 = OpInBoundsAccessChain %5 %24 %15 +; CHECK-NEXT: OpStore %25 %23 ; CHECK-NEXT: OpReturn ; CHECK-NEXT: OpFunctionEnd entry: diff --git a/llvm/test/Transforms/EarlyCSE/SPIRV/resource-name-metadata.ll b/llvm/test/Transforms/EarlyCSE/SPIRV/resource-name-metadata.ll new file mode 100644 index 00000000000000..26e6b3893a133c --- /dev/null +++ b/llvm/test/Transforms/EarlyCSE/SPIRV/resource-name-metadata.ll @@ -0,0 +1,53 @@ +; RUN: opt -S -passes=early-cse %s | FileCheck %s +; RUN: opt -S -passes=gvn %s | FileCheck %s + +; check that handle intrinsics are deduplicated (or not) according to whether their +; metadata name is unique along with other operands + +; CHECK-LABEL: define void @same_name( +; CHECK: [[H:%.*]] = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname +; CHECK-NOT: call target("spirv.Sampler") +; CHECK: call void @consume(target("spirv.Sampler") [[H]], target("spirv.Sampler") [[H]]) +define void @same_name() { + %a = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname(i32 0, i32 1, i32 1, i32 0, metadata !{!"a"}) + %b = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname(i32 0, i32 1, i32 1, i32 0, metadata !{!"a"}) + call void @consume(target("spirv.Sampler") %a, target("spirv.Sampler") %b) + ret void +} + +; CHECK-LABEL: define void @different_names( +; CHECK: [[A:%.*]] = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname +; CHECK: [[B:%.*]] = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname +; CHECK: call void @consume(target("spirv.Sampler") [[A]], target("spirv.Sampler") [[B]]) +define void @different_names() { + %a = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname(i32 0, i32 1, i32 1, i32 0, metadata !{!"a"}) + %b = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname(i32 0, i32 1, i32 1, i32 0, metadata !{!"b"}) + call void @consume(target("spirv.Sampler") %a, target("spirv.Sampler") %b) + ret void +} + +; CHECK-LABEL: define void @implicit_same_name( +; CHECK: [[H:%.*]] = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname +; CHECK-NOT: call target("spirv.Sampler") +; CHECK: call void @consume(target("spirv.Sampler") [[H]], target("spirv.Sampler") [[H]]) +define void @implicit_same_name() { + %a = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname(i32 0, i32 0, i32 1, i32 0, metadata !{!"a"}) + %b = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname(i32 0, i32 0, i32 1, i32 0, metadata !{!"a"}) + call void @consume(target("spirv.Sampler") %a, target("spirv.Sampler") %b) + ret void +} + +; CHECK-LABEL: define void @implicit_different_names( +; CHECK: [[A:%.*]] = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname +; CHECK: [[B:%.*]] = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname +; CHECK: call void @consume(target("spirv.Sampler") [[A]], target("spirv.Sampler") [[B]]) +define void @implicit_different_names() { + %a = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname(i32 0, i32 0, i32 1, i32 0, metadata !{!"a"}) + %b = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname(i32 0, i32 0, i32 1, i32 0, metadata !{!"b"}) + call void @consume(target("spirv.Sampler") %a, target("spirv.Sampler") %b) + ret void +} + +declare target("spirv.Sampler") @llvm.spv.resource.handlefrombinding.mdname(i32, i32, i32, i32, metadata) +declare target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname(i32, i32, i32, i32, metadata) +declare void @consume(target("spirv.Sampler"), target("spirv.Sampler")) >From 3152333638db18880d3e52ca471f02ca2adce53e Mon Sep 17 00:00:00 2001 From: rjennings <[email protected]> Date: Tue, 17 Nov 2026 09:12:27 -0800 Subject: [PATCH 2/4] clang format --- llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 9 +++++---- .../lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp | 11 +++++------ llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp | 7 ++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp index 4a44a7b68d14dc..c7ef272d2b8092 100644 --- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp @@ -428,10 +428,11 @@ bool isConvergenceIntrinsic(const Instruction *I) { } bool expectIgnoredInIRTranslation(const Instruction *I) { - return match(I, m_AnyIntrinsic<Intrinsic::invariant_start, - Intrinsic::spv_resource_handlefrombinding_mdname, - Intrinsic::spv_resource_getbasepointer, - Intrinsic::spv_resource_getpointer>()); + return match(I, + m_AnyIntrinsic<Intrinsic::invariant_start, + Intrinsic::spv_resource_handlefrombinding_mdname, + Intrinsic::spv_resource_getbasepointer, + Intrinsic::spv_resource_getpointer>()); } // Returns the source pointer from `I` ignoring intermediate ptrcast. diff --git a/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp b/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp index 27bd6ce4e256cf..58f54de54c80f1 100644 --- a/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp @@ -206,7 +206,7 @@ bool SPIRVLegalizeResourceBindingImpl::replaceImplicitBindingCalls(Module &M) { uint32_t OrderIdIdx; if (F.getIntrinsicID() == - Intrinsic::spv_resource_handlefromimplicitbinding_mdname) + Intrinsic::spv_resource_handlefromimplicitbinding_mdname) OrderIdIdx = 0; else if (F.getIntrinsicID() == Intrinsic::spv_resource_counterhandlefromimplicitbinding) @@ -248,7 +248,7 @@ bool SPIRVLegalizeResourceBindingImpl::replaceImplicitBindingCalls(Module &M) { // Replace the implicit binding call with an explicit binding call. if (CI->getIntrinsicID() == - Intrinsic::spv_resource_handlefromimplicitbinding_mdname) + Intrinsic::spv_resource_handlefromimplicitbinding_mdname) replaceWithHandleFromBinding(M, CI, DescSet, Binding, CI->getArgOperand(2), CI->getArgOperand(3), CI->getArgOperand(4)); @@ -264,10 +264,10 @@ bool SPIRVLegalizeResourceBindingImpl::replaceImplicitBindingCalls(Module &M) { } static Value *createHeapNameMetadata(LLVMContext &Ctx, StringRef Name, - unsigned ID) { + unsigned ID) { std::string HeapName = (Twine(".") + Name + "." + Twine(ID)).str(); return MetadataAsValue::get(Ctx, - MDNode::get(Ctx, MDString::get(Ctx, HeapName))); + MDNode::get(Ctx, MDString::get(Ctx, HeapName))); } // The SPIR-V backend represents dynamic resources as unbounded resource arrays. @@ -353,8 +353,7 @@ bool SPIRVLegalizeResourceBindingImpl::replaceHeapBindingCalls(Module &M) { [[maybe_unused]] TargetExtType *SamplerHandleType = cast<TargetExtType>(Samplers.front()->getReturnType()); - Value *HeapName = - createHeapNameMetadata(M.getContext(), "SamplerHeap", 1); + Value *HeapName = createHeapNameMetadata(M.getContext(), "SamplerHeap", 1); for (Function *F : Samplers) { assert(F->getReturnType() == SamplerHandleType && "sampler handle type mismatch"); diff --git a/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp index 89b8246369198e..ef82178697d672 100644 --- a/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp @@ -1,4 +1,5 @@ -//===- SPIRVLowerResourceNames.cpp - Lower resource names to metadata -------===// +//===- SPIRVLowerResourceNames.cpp - Lower resource names to metadata +//-------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -65,8 +66,8 @@ static bool lowerResourceNames(Module &M) { return Changed; } -PreservedAnalyses -SPIRVLowerResourceNamesPass::run(Module &M, ModuleAnalysisManager &AM) { +PreservedAnalyses SPIRVLowerResourceNamesPass::run(Module &M, + ModuleAnalysisManager &AM) { return lowerResourceNames(M) ? PreservedAnalyses::none() : PreservedAnalyses::all(); } >From 5c2e4976400a8ab1966b7855c59f1dc796945de9 Mon Sep 17 00:00:00 2001 From: rjennings <[email protected]> Date: Wed, 18 Nov 2026 15:14:48 -0800 Subject: [PATCH 3/4] Use limited mem2reg when necessary to resolve resource names, such as at -Od. Also address some review feedback --- llvm/docs/SPIRVUsage.md | 4 +- .../Target/SPIRV/SPIRVCodeGenPassBuilder.cpp | 2 + .../SPIRV/SPIRVLegalizeResourceBinding.cpp | 13 +-- .../Target/SPIRV/SPIRVLowerResourceNames.cpp | 93 ++++++++++++------- llvm/lib/Target/SPIRV/SPIRVUtils.cpp | 10 -- llvm/lib/Target/SPIRV/SPIRVUtils.h | 4 - .../hlsl-resources/HeapBinding-cvb-srv-uav.ll | 6 +- .../HeapBinding-texture-sampler.ll | 4 +- .../hlsl-resources/HeapBinding-uav-counter.ll | 4 +- .../SPIRV/hlsl-resources/resource-names.ll | 23 +++++ .../SPIRVLegalizeRegisterBinding-Heap.ll | 6 +- .../SPIRVLegalizeRegisterBinding-Heap2.ll | 2 +- 12 files changed, 108 insertions(+), 63 deletions(-) diff --git a/llvm/docs/SPIRVUsage.md b/llvm/docs/SPIRVUsage.md index baec24176987af..1e3e8dd7329e26 100644 --- a/llvm/docs/SPIRVUsage.md +++ b/llvm/docs/SPIRVUsage.md @@ -559,7 +559,9 @@ SPIR-V backend, along with their descriptions and argument details. * - `int_spv_resource_handlefromimplicitbinding_mdname` - Resource type - `[32-bit Integer orderId, 32-bit Integer space, 32-bit Integer arraySize, 32-bit Integer index, metadata name]` - - Metadata-name + - Backend form of `int_spv_resource_handlefromimplicitbinding`, produced by + name lowering. The name is a metadata string + literal as to not emit a real spirv constant. * - `int_spv_typeBufferLoad` - Scalar or vector - `[spirv.Image ImageBuffer, 32-bit Integer coordinate]` diff --git a/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp b/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp index 3db1a7a0452cfb..fe56417acf261f 100644 --- a/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp @@ -91,6 +91,8 @@ void SPIRVCodeGenPassBuilder::addIRPasses(PassManagerWrapper &PMW) { flushFPMsToMPM(PMW); + addModulePass(SPIRVLowerResourceNamesPass(), PMW); + if (getTM().getSubtargetImpl()->isShader()) { if (getOptLevel() != CodeGenOptLevel::None) { addModulePass(SPIRVFinalizeShaderLinkagePass(getTM()), PMW); diff --git a/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp b/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp index 58f54de54c80f1..6c19ad17c578d7 100644 --- a/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVLegalizeResourceBinding.cpp @@ -7,9 +7,9 @@ // //===----------------------------------------------------------------------===// // -// This pass legalizes the @llvm.spv.resource.handlefromimplicitbinding +// This pass legalizes the @llvm.spv.resource.handlefromimplicitbinding.mdname // and @llvm.spv.resource.handlefromheap intrinsics by replacing them with a -// call to @llvm.spv.resource.handlefrombinding. +// call to @llvm.spv.resource.handlefrombinding.mdname. // It also replaces any @llvm.spv.resource.counterhandlefromimplicitbinding and // @llvm.spv.resource.counterhandlefromheap intrinsics with calls to // @llvm.spv.resource.counterhandlefrombinding. @@ -329,10 +329,10 @@ bool SPIRVLegalizeResourceBindingImpl::replaceHeapBindingCalls(Module &M) { // ResourceDescriptorHeap name for each unique resource type. They will all // share the same binding and will overlap. uint32_t Binding = getAndReserveFirstUnusedBinding(DescSet); - unsigned HeapID = 1; + unsigned HeapID = 0; for (Function *F : CbvSrvUavs) { - Value *HeapName = - createHeapNameMetadata(M.getContext(), "BufferHeap", HeapID++); + Value *HeapName = createHeapNameMetadata( + M.getContext(), "ResourceDescriptorHeap", HeapID++); for (User *U : make_early_inc_range(F->users())) { if (CallInst *CI = dyn_cast<CallInst>(U)) { @@ -353,7 +353,8 @@ bool SPIRVLegalizeResourceBindingImpl::replaceHeapBindingCalls(Module &M) { [[maybe_unused]] TargetExtType *SamplerHandleType = cast<TargetExtType>(Samplers.front()->getReturnType()); - Value *HeapName = createHeapNameMetadata(M.getContext(), "SamplerHeap", 1); + Value *HeapName = + createHeapNameMetadata(M.getContext(), "SamplerDescriptorHeap", 0); for (Function *F : Samplers) { assert(F->getReturnType() == SamplerHandleType && "sampler handle type mismatch"); diff --git a/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp index ef82178697d672..51687faf7ed125 100644 --- a/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp @@ -1,5 +1,4 @@ -//===- SPIRVLowerResourceNames.cpp - Lower resource names to metadata -//-------===// +//===- SPIRVLowerResourceNames.cpp - Lower resource names to metadata -----===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -8,62 +7,94 @@ //===----------------------------------------------------------------------===// #include "SPIRV.h" +#include "llvm/ADT/MapVector.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/Analysis/ValueTracking.h" +#include "llvm/IR/Dominators.h" #include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/IntrinsicsSPIRV.h" #include "llvm/IR/Module.h" #include "llvm/Pass.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Transforms/Utils/PromoteMemToReg.h" using namespace llvm; #define DEBUG_TYPE "spirv-lower-resource-names" +static void promoteNameAllocas(ArrayRef<CallInst *> Intrinsics) { + MapVector<Function *, SmallVector<AllocaInst *, 4>> Allocas; + SmallPtrSet<AllocaInst *, 8> Seen; + for (CallInst *CI : Intrinsics) { + auto *LI = dyn_cast<LoadInst>(CI->getArgOperand(4)); + if (!LI) + continue; + auto *AI = cast<AllocaInst>(LI->getPointerOperand()); + if (!Seen.insert(AI).second) + continue; + if (AI->isStaticAlloca() && isAllocaPromotable(AI)) + Allocas[CI->getFunction()].push_back(AI); + else + reportFatalInternalError("unresolvable SPIR-V resource name"); + } + for (auto &[F, Names] : Allocas) { + DominatorTree DT(*F); + PromoteMemToReg(Names, DT); + } +} + static bool lowerResourceNames(Module &M) { - SmallPtrSet<GlobalVariable *, 8> Names; - bool Changed = false; - for (Function &F : make_early_inc_range(M)) { - Intrinsic::ID MdID; + SmallVector<CallInst *, 8> Intrinsics; + for (Function &F : M) { switch (F.getIntrinsicID()) { case Intrinsic::spv_resource_handlefrombinding: - MdID = Intrinsic::spv_resource_handlefrombinding_mdname; - break; case Intrinsic::spv_resource_handlefromimplicitbinding: - MdID = Intrinsic::spv_resource_handlefromimplicitbinding_mdname; + for (User *U : F.users()) { + if (auto *CI = dyn_cast<CallInst>(U)) + Intrinsics.push_back(CI); + } break; default: - continue; + break; } - if (F.use_empty()) - continue; + } + if (Intrinsics.empty()) + return false; + + promoteNameAllocas(Intrinsics); + + SmallPtrSet<GlobalVariable *, 8> Names; + for (CallInst *CI : Intrinsics) { + Function *F = CI->getCalledFunction(); + Intrinsic::ID MdID = + F->getIntrinsicID() == Intrinsic::spv_resource_handlefrombinding + ? Intrinsic::spv_resource_handlefrombinding_mdname + : Intrinsic::spv_resource_handlefromimplicitbinding_mdname; Function *MdFn = - Intrinsic::getOrInsertDeclaration(&M, MdID, {F.getReturnType()}); - for (User *U : make_early_inc_range(F.users())) { - auto *CI = cast<CallInst>(U); - Value *NamePtr = CI->getArgOperand(4); - StringRef Name; - if (!isa<ConstantPointerNull>(NamePtr) && - !getConstantStringInfo(NamePtr, Name)) - report_fatal_error("expected a constant resource name"); - if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(NamePtr))) - Names.insert(GV); - LLVMContext &Ctx = M.getContext(); - auto *NameMD = MDNode::get(Ctx, MDString::get(Ctx, Name)); - CI->setCalledFunction(MdFn); - CI->setArgOperand(4, MetadataAsValue::get(Ctx, NameMD)); - CI->setAttributes(CI->getAttributes().removeParamAttributes(Ctx, 4)); - Changed = true; - } - F.eraseFromParent(); + Intrinsic::getOrInsertDeclaration(&M, MdID, {F->getReturnType()}); + Value *NamePtr = CI->getArgOperand(4); + StringRef Name; + if (!isa<ConstantPointerNull>(NamePtr) && + !getConstantStringInfo(NamePtr, Name)) + report_fatal_error("expected a constant resource name"); + if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(NamePtr))) + Names.insert(GV); + LLVMContext &Ctx = M.getContext(); + auto *NameMD = MDNode::get(Ctx, MDString::get(Ctx, Name)); + CI->setCalledFunction(MdFn); + CI->setArgOperand(4, MetadataAsValue::get(Ctx, NameMD)); + CI->setAttributes(CI->getAttributes().removeParamAttributes(Ctx, 4)); } for (GlobalVariable *GV : Names) { GV->removeDeadConstantUsers(); if (GV->hasLocalLinkage() && GV->use_empty()) GV->eraseFromParent(); } - return Changed; + return true; } PreservedAnalyses SPIRVLowerResourceNamesPass::run(Module &M, diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp index e38101d64e5367..51c218eaea48fc 100644 --- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp @@ -180,16 +180,6 @@ std::string getStringImm(const MachineInstr &MI, unsigned StartIndex) { return getSPIRVStringOperand(MI, StartIndex); } -std::string getStringValueFromReg(Register Reg, MachineRegisterInfo &MRI) { - MachineInstr *Def = getVRegDef(MRI, Reg); - assert(Def && Def->getOpcode() == TargetOpcode::G_GLOBAL_VALUE && - "Expected G_GLOBAL_VALUE"); - const GlobalValue *GV = Def->getOperand(1).getGlobal(); - Value *V = GV->getOperand(0); - const ConstantDataArray *CDA = cast<ConstantDataArray>(V); - return CDA->getAsCString().str(); -} - void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB) { const auto Bitwidth = Imm.getBitWidth(); if (Bitwidth == 1) diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h index 4724e2679527f9..39f7b64581f9b7 100644 --- a/llvm/lib/Target/SPIRV/SPIRVUtils.h +++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h @@ -182,10 +182,6 @@ void addStringImm(StringRef Str, MachineInstrBuilder &MIB); // the reverse of the logic in addStringImm. std::string getStringImm(const MachineInstr &MI, unsigned StartIndex); -// Returns the string constant that the register refers to. It is assumed that -// Reg is a global value that contains a string. -std::string getStringValueFromReg(Register Reg, MachineRegisterInfo &MRI); - // Add the given numerical immediate to MIB. void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB); diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll index 239b29a6d216b2..36cead4f2b35c3 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-cvb-srv-uav.ll @@ -18,15 +18,15 @@ target datalayout = "e-ve-i64:64-n8:16:32:64-G10" ; CHECK-DAG: OpDecorate [[Buf]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Buf]] Binding 1 -; CHECK-DAG: OpName [[Heap0:%[0-9]+]] ".BufferHeap.1" +; CHECK-DAG: OpName [[Heap0:%[0-9]+]] ".ResourceDescriptorHeap.0" ; CHECK-DAG: OpDecorate [[Heap0]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap0]] Binding 0 -; CHECK-DAG: OpName [[Heap1:%[0-9]+]] ".BufferHeap.2" +; CHECK-DAG: OpName [[Heap1:%[0-9]+]] ".ResourceDescriptorHeap.1" ; CHECK-DAG: OpDecorate [[Heap1]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap1]] Binding 0 -; CHECK-DAG: OpName [[Heap2:%[0-9]+]] ".BufferHeap.3" +; CHECK-DAG: OpName [[Heap2:%[0-9]+]] ".ResourceDescriptorHeap.2" ; CHECK-DAG: OpDecorate [[Heap2]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap2]] Binding 0 diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll index 8de96d0997ea1f..3d647f7e0ee3ec 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-texture-sampler.ll @@ -15,11 +15,11 @@ ; CHECK-DAG: OpDecorate [[Buf]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Buf]] Binding 1 -; CHECK-DAG: OpName [[ResourceHeap:%[0-9]+]] ".BufferHeap.1" +; CHECK-DAG: OpName [[ResourceHeap:%[0-9]+]] ".ResourceDescriptorHeap.0" ; CHECK-DAG: OpDecorate [[ResourceHeap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[ResourceHeap]] Binding 0 -; CHECK-DAG: OpName [[SamplerHeap:%[0-9]+]] ".SamplerHeap.1" +; CHECK-DAG: OpName [[SamplerHeap:%[0-9]+]] ".SamplerDescriptorHeap.0" ; CHECK-DAG: OpDecorate [[SamplerHeap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[SamplerHeap]] Binding 2 diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll index d2b8589d6b3e89..36dee634c71a71 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/HeapBinding-uav-counter.ll @@ -15,11 +15,11 @@ ; CHECK-DAG: OpDecorate [[Buf]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Buf]] Binding 1 -; CHECK-DAG: OpName [[Heap:%[0-9]+]] ".BufferHeap.1" +; CHECK-DAG: OpName [[Heap:%[0-9]+]] ".ResourceDescriptorHeap.0" ; CHECK-DAG: OpDecorate [[Heap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[Heap]] Binding 0 -; CHECK-DAG: OpName [[CounterHeap:%[0-9]+]] ".BufferHeap.1.counter" +; CHECK-DAG: OpName [[CounterHeap:%[0-9]+]] ".ResourceDescriptorHeap.0.counter" ; CHECK-DAG: OpDecorate [[CounterHeap]] DescriptorSet 0 ; CHECK-DAG: OpDecorate [[CounterHeap]] Binding 2 diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll index 8639122e04be16..22b6737a6db8aa 100644 --- a/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names.ll @@ -1,4 +1,8 @@ ; RUN: opt -S -passes=spirv-lower-resource-names %s | FileCheck %s +; RUN: llc -O0 -enable-new-pm -verify-machineinstrs %s -o - | FileCheck %s --check-prefix=SPIRV + +; SPIRV-DAG: OpName {{%[0-9]+}} "buf" +; SPIRV-DAG: OpName {{%[0-9]+}} "sampler" target triple = "spirv1.6-unknown-vulkan1.3-library" @@ -26,6 +30,25 @@ define target("spirv.Sampler") @implicit() { ret target("spirv.Sampler") %handle } +; IR like this can be encountered at -Od +; +; CHECK-LABEL: define void @loaded_name() +; CHECK-NOT: alloca +; CHECK: %handle = call target("spirv.Image", float, 5, 2, 0, 0, 1, 0) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata ![[BUF]]) +; CHECK: %sampler = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata ![[SAMPLER]]) +define void @loaded_name() { + %name.addr = alloca ptr + %sampler.name.addr = alloca ptr + store ptr @.str, ptr %name.addr + store ptr @.sampler, ptr %sampler.name.addr + %name = load ptr, ptr %name.addr + %sampler.name = load ptr, ptr %sampler.name.addr + %handle = call target("spirv.Image", float, 5, 2, 0, 0, 1, 0) @llvm.spv.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr %name) + %other = call target("spirv.Image", float, 5, 2, 0, 0, 1, 0) @llvm.spv.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr %name) + %sampler = call target("spirv.Sampler") @llvm.spv.resource.handlefromimplicitbinding(i32 0, i32 0, i32 1, i32 0, ptr %sampler.name) + ret void +} + ; CHECK-LABEL: define void @runtime_use() ; CHECK: %handle = call target("spirv.VulkanBuffer", [0 x i32], 12, 1) @llvm.spv.resource.handlefrombinding.mdname{{.*}}(i32 0, i32 0, i32 1, i32 0, metadata ![[BUF]]) ; CHECK: load volatile i8, ptr @.runtime diff --git a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll index f8611ee78b52aa..a49a696a0944f2 100644 --- a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll +++ b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap.ll @@ -39,6 +39,6 @@ entry: attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } -; CHECK-DAG: ![[BUFFER]] = !{!".BufferHeap.2"} -; CHECK-DAG: ![[IMAGE]] = !{!".BufferHeap.1"} -; CHECK-DAG: ![[SAMPLER]] = !{!".SamplerHeap.1"} +; CHECK-DAG: ![[BUFFER]] = !{!".ResourceDescriptorHeap.1"} +; CHECK-DAG: ![[IMAGE]] = !{!".ResourceDescriptorHeap.0"} +; CHECK-DAG: ![[SAMPLER]] = !{!".SamplerDescriptorHeap.0"} diff --git a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll index 40965dbdb4b91d..9fd6cc4f28205c 100644 --- a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll +++ b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizeRegisterBinding-Heap2.ll @@ -21,4 +21,4 @@ entry: attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } -; CHECK: ![[NAME]] = !{!".BufferHeap.1"} +; CHECK: ![[NAME]] = !{!".ResourceDescriptorHeap.0"} >From 9ab1e4fd1d4171f9e7089bd2e706d150f6ad0cff Mon Sep 17 00:00:00 2001 From: rjennings <[email protected]> Date: Thu, 19 Nov 2026 14:52:46 -0800 Subject: [PATCH 4/4] dont crash for unhandled IR, add test --- llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp | 6 +++--- .../SPIRV/hlsl-resources/resource-names-invalid.ll | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names-invalid.ll diff --git a/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp index 51687faf7ed125..3aa6087474f73b 100644 --- a/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVLowerResourceNames.cpp @@ -33,10 +33,10 @@ static void promoteNameAllocas(ArrayRef<CallInst *> Intrinsics) { auto *LI = dyn_cast<LoadInst>(CI->getArgOperand(4)); if (!LI) continue; - auto *AI = cast<AllocaInst>(LI->getPointerOperand()); - if (!Seen.insert(AI).second) + auto *AI = dyn_cast<AllocaInst>(LI->getPointerOperand()); + if (AI && !Seen.insert(AI).second) continue; - if (AI->isStaticAlloca() && isAllocaPromotable(AI)) + if (AI && AI->isStaticAlloca() && isAllocaPromotable(AI)) Allocas[CI->getFunction()].push_back(AI); else reportFatalInternalError("unresolvable SPIR-V resource name"); diff --git a/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names-invalid.ll b/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names-invalid.ll new file mode 100644 index 00000000000000..215f5c461dbe37 --- /dev/null +++ b/llvm/test/CodeGen/SPIRV/hlsl-resources/resource-names-invalid.ll @@ -0,0 +1,14 @@ +; RUN: not --crash opt -passes=spirv-lower-resource-names -disable-output %s 2>&1 | FileCheck %s + +target triple = "spirv1.6-unknown-vulkan1.3-library" + +; CHECK: LLVM ERROR: unresolvable SPIR-V resource name + [email protected] = private unnamed_addr constant [4 x i8] c"buf\00" [email protected] = internal global ptr @.str + +define void @test() { + %name = load ptr, ptr @name.ptr + %handle = call target("spirv.Sampler") @llvm.spv.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr %name) + ret void +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
