From a0c5f9bdbf1bc7ea8d3cd34ede35c42ed5aac500 Mon Sep 17 00:00:00 2001 From: Steven Perron Date: Fri, 4 Oct 2024 11:41:26 -0400 Subject: [PATCH] Redo the name for the resource. --- llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 28 ++++++++++- llvm/test/CodeGen/SPIRV/HlslBufferLoad.ll | 50 +++++++++++++++---- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp index 8a9a0ad3a6126d..64fde8bf67ab91 100644 --- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp @@ -713,6 +713,30 @@ Register SPIRVGlobalRegistry::buildGlobalVariable( return Reg; } +static std::string buildSpirvTypeName(const SPIRVType *Type, + MachineIRBuilder &MIRBuilder) { + switch (Type->getOpcode()) { + case SPIRV::OpTypeImage: { + Register SampledTypeReg = Type->getOperand(1).getReg(); + auto *SampledType = MIRBuilder.getMRI()->getUniqueVRegDef(SampledTypeReg); + std::string TypeName = + "image_" + buildSpirvTypeName(SampledType, MIRBuilder); + for (uint32_t I = 2; I < Type->getNumOperands(); ++I) { + TypeName = (TypeName + '_' + Twine(Type->getOperand(I).getImm())).str(); + } + return TypeName; + } + case SPIRV::OpTypeFloat: + return ("f" + Twine(Type->getOperand(1).getImm())).str(); + case SPIRV::OpTypeInt: + if (Type->getOperand(2).getImm()) + return ("i" + Twine(Type->getOperand(1).getImm())).str(); + return ("u" + Twine(Type->getOperand(1).getImm())).str(); + default: + llvm_unreachable("Trying to the the name of an unknown type."); + } +} + Register SPIRVGlobalRegistry::getOrCreateGlobalVariableWithBinding( const SPIRVType *VarType, uint32_t Set, uint32_t Binding, MachineIRBuilder &MIRBuilder) { @@ -723,8 +747,8 @@ Register SPIRVGlobalRegistry::getOrCreateGlobalVariableWithBinding( // TODO: The name should come from the llvm-ir, but how that name will be // passed from the HLSL to the backend has not been decided. Using this place - // holder for now. We use the result register of the type in the name. - std::string Name = ("__resource_" + Twine(VarType->getOperand(0).getReg()) + + // holder for now. + std::string Name = ("__resource_" + buildSpirvTypeName(VarType, MIRBuilder) + "_" + Twine(Set) + "_" + Twine(Binding)) .str(); buildGlobalVariable(VarReg, VarPointerTypeReg, Name, nullptr, diff --git a/llvm/test/CodeGen/SPIRV/HlslBufferLoad.ll b/llvm/test/CodeGen/SPIRV/HlslBufferLoad.ll index 8c1777598f4c71..fe960f0d6f2f9a 100644 --- a/llvm/test/CodeGen/SPIRV/HlslBufferLoad.ll +++ b/llvm/test/CodeGen/SPIRV/HlslBufferLoad.ll @@ -1,25 +1,55 @@ ; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-vulkan-library %s -o - | FileCheck %s ; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-library %s -o - -filetype=obj | spirv-val %} -; CHECK-DAG: OpDecorate [[BufferVar:%[0-9]+]] DescriptorSet 16 -; CHECK-DAG: OpDecorate [[BufferVar]] Binding 7 +; CHECK-DAG: OpDecorate [[IntBufferVar:%[0-9]+]] DescriptorSet 16 +; CHECK-DAG: OpDecorate [[IntBufferVar]] Binding 7 +; CHECK-DAG: OpDecorate [[FloatBufferVar:%[0-9]+]] DescriptorSet 16 +; CHECK-DAG: OpDecorate [[FloatBufferVar]] Binding 7 -; CHECK: [[float:%[0-9]+]] = OpTypeFloat 32 -; CHECK: [[RWBufferType:%[0-9]+]] = OpTypeImage [[float]] Buffer 2 0 0 2 R32i {{$}} -; CHECK: [[BufferPtrType:%[0-9]+]] = OpTypePointer UniformConstant [[RWBufferType]] -; CHECK: [[BufferVar]] = OpVariable [[BufferPtrType]] UniformConstant +; CHECK-DAG: [[float:%[0-9]+]] = OpTypeFloat 32 +; CHECK-DAG: [[int:%[0-9]+]] = OpTypeInt 32 0 +; CHECK-DAG: [[RWBufferTypeInt:%[0-9]+]] = OpTypeImage [[int]] Buffer 2 0 0 2 R32i {{$}} +; CHECK-DAG: [[RWBufferTypeFloat:%[0-9]+]] = OpTypeImage [[float]] Buffer 2 0 0 2 R32f {{$}} +; CHECK-DAG: [[IntBufferPtrType:%[0-9]+]] = OpTypePointer UniformConstant [[RWBufferTypeInt]] +; CHECK-DAG: [[FloatBufferPtrType:%[0-9]+]] = OpTypePointer UniformConstant [[RWBufferTypeFloat]] +; CHECK-DAG: [[IntBufferVar]] = OpVariable [[IntBufferPtrType]] UniformConstant +; CHECK-DAG: [[FloatBufferVar]] = OpVariable [[FloatBufferPtrType]] UniformConstant ; CHECK: {{%[0-9]+}} = OpFunction {{%[0-9]+}} DontInline {{%[0-9]+}} ; CHECK-NEXT: OpLabel define void @RWBufferLoad() #0 { -; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferType]] [[BufferVar]] - %buffer0 = call target("spirv.Image", float, 5, 2, 0, 0, 2, 24) +; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeInt]] [[IntBufferVar]] + %buffer0 = call target("spirv.Image", i32, 5, 2, 0, 0, 2, 24) @llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_24( i32 16, i32 7, i32 1, i32 0, i1 false) ; Make sure we use the same variable with multiple loads. -; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferType]] [[BufferVar]] - %buffer1 = call target("spirv.Image", float, 5, 2, 0, 0, 2, 24) +; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeInt]] [[IntBufferVar]] + %buffer1 = call target("spirv.Image", i32, 5, 2, 0, 0, 2, 24) + @llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_24( + i32 16, i32 7, i32 1, i32 0, i1 false) + ret void +} + +; CHECK: {{%[0-9]+}} = OpFunction {{%[0-9]+}} DontInline {{%[0-9]+}} +; CHECK-NEXT: OpLabel +define void @UseDifferentGlobalVar() #0 { +; Make sure we use a different variable from the first function. They have +; different types. +; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeFloat]] [[FloatBufferVar]] + %buffer0 = call target("spirv.Image", float, 5, 2, 0, 0, 2, 3) + @llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_3( + i32 16, i32 7, i32 1, i32 0, i1 false) + ret void +} + +; CHECK: {{%[0-9]+}} = OpFunction {{%[0-9]+}} DontInline {{%[0-9]+}} +; CHECK-NEXT: OpLabel +define void @ReuseGlobalVarFromFirstFunction() #0 { +; Make sure we use the same variable as the first function. They should be the +; same in case one function calls the other. +; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeInt]] [[IntBufferVar]] + %buffer1 = call target("spirv.Image", i32, 5, 2, 0, 0, 2, 24) @llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_24( i32 16, i32 7, i32 1, i32 0, i1 false) ret void