Skip to content

Commit a0c5f9b

Browse files
committed
Redo the name for the resource.
1 parent 39e39f6 commit a0c5f9b

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,30 @@ Register SPIRVGlobalRegistry::buildGlobalVariable(
713713
return Reg;
714714
}
715715

716+
static std::string buildSpirvTypeName(const SPIRVType *Type,
717+
MachineIRBuilder &MIRBuilder) {
718+
switch (Type->getOpcode()) {
719+
case SPIRV::OpTypeImage: {
720+
Register SampledTypeReg = Type->getOperand(1).getReg();
721+
auto *SampledType = MIRBuilder.getMRI()->getUniqueVRegDef(SampledTypeReg);
722+
std::string TypeName =
723+
"image_" + buildSpirvTypeName(SampledType, MIRBuilder);
724+
for (uint32_t I = 2; I < Type->getNumOperands(); ++I) {
725+
TypeName = (TypeName + '_' + Twine(Type->getOperand(I).getImm())).str();
726+
}
727+
return TypeName;
728+
}
729+
case SPIRV::OpTypeFloat:
730+
return ("f" + Twine(Type->getOperand(1).getImm())).str();
731+
case SPIRV::OpTypeInt:
732+
if (Type->getOperand(2).getImm())
733+
return ("i" + Twine(Type->getOperand(1).getImm())).str();
734+
return ("u" + Twine(Type->getOperand(1).getImm())).str();
735+
default:
736+
llvm_unreachable("Trying to the the name of an unknown type.");
737+
}
738+
}
739+
716740
Register SPIRVGlobalRegistry::getOrCreateGlobalVariableWithBinding(
717741
const SPIRVType *VarType, uint32_t Set, uint32_t Binding,
718742
MachineIRBuilder &MIRBuilder) {
@@ -723,8 +747,8 @@ Register SPIRVGlobalRegistry::getOrCreateGlobalVariableWithBinding(
723747

724748
// TODO: The name should come from the llvm-ir, but how that name will be
725749
// passed from the HLSL to the backend has not been decided. Using this place
726-
// holder for now. We use the result register of the type in the name.
727-
std::string Name = ("__resource_" + Twine(VarType->getOperand(0).getReg()) +
750+
// holder for now.
751+
std::string Name = ("__resource_" + buildSpirvTypeName(VarType, MIRBuilder) +
728752
"_" + Twine(Set) + "_" + Twine(Binding))
729753
.str();
730754
buildGlobalVariable(VarReg, VarPointerTypeReg, Name, nullptr,

llvm/test/CodeGen/SPIRV/HlslBufferLoad.ll

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-vulkan-library %s -o - | FileCheck %s
22
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-library %s -o - -filetype=obj | spirv-val %}
33

4-
; CHECK-DAG: OpDecorate [[BufferVar:%[0-9]+]] DescriptorSet 16
5-
; CHECK-DAG: OpDecorate [[BufferVar]] Binding 7
4+
; CHECK-DAG: OpDecorate [[IntBufferVar:%[0-9]+]] DescriptorSet 16
5+
; CHECK-DAG: OpDecorate [[IntBufferVar]] Binding 7
6+
; CHECK-DAG: OpDecorate [[FloatBufferVar:%[0-9]+]] DescriptorSet 16
7+
; CHECK-DAG: OpDecorate [[FloatBufferVar]] Binding 7
68

7-
; CHECK: [[float:%[0-9]+]] = OpTypeFloat 32
8-
; CHECK: [[RWBufferType:%[0-9]+]] = OpTypeImage [[float]] Buffer 2 0 0 2 R32i {{$}}
9-
; CHECK: [[BufferPtrType:%[0-9]+]] = OpTypePointer UniformConstant [[RWBufferType]]
10-
; CHECK: [[BufferVar]] = OpVariable [[BufferPtrType]] UniformConstant
9+
; CHECK-DAG: [[float:%[0-9]+]] = OpTypeFloat 32
10+
; CHECK-DAG: [[int:%[0-9]+]] = OpTypeInt 32 0
11+
; CHECK-DAG: [[RWBufferTypeInt:%[0-9]+]] = OpTypeImage [[int]] Buffer 2 0 0 2 R32i {{$}}
12+
; CHECK-DAG: [[RWBufferTypeFloat:%[0-9]+]] = OpTypeImage [[float]] Buffer 2 0 0 2 R32f {{$}}
13+
; CHECK-DAG: [[IntBufferPtrType:%[0-9]+]] = OpTypePointer UniformConstant [[RWBufferTypeInt]]
14+
; CHECK-DAG: [[FloatBufferPtrType:%[0-9]+]] = OpTypePointer UniformConstant [[RWBufferTypeFloat]]
15+
; CHECK-DAG: [[IntBufferVar]] = OpVariable [[IntBufferPtrType]] UniformConstant
16+
; CHECK-DAG: [[FloatBufferVar]] = OpVariable [[FloatBufferPtrType]] UniformConstant
1117

1218
; CHECK: {{%[0-9]+}} = OpFunction {{%[0-9]+}} DontInline {{%[0-9]+}}
1319
; CHECK-NEXT: OpLabel
1420
define void @RWBufferLoad() #0 {
15-
; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferType]] [[BufferVar]]
16-
%buffer0 = call target("spirv.Image", float, 5, 2, 0, 0, 2, 24)
21+
; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeInt]] [[IntBufferVar]]
22+
%buffer0 = call target("spirv.Image", i32, 5, 2, 0, 0, 2, 24)
1723
@llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_24(
1824
i32 16, i32 7, i32 1, i32 0, i1 false)
1925

2026
; Make sure we use the same variable with multiple loads.
21-
; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferType]] [[BufferVar]]
22-
%buffer1 = call target("spirv.Image", float, 5, 2, 0, 0, 2, 24)
27+
; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeInt]] [[IntBufferVar]]
28+
%buffer1 = call target("spirv.Image", i32, 5, 2, 0, 0, 2, 24)
29+
@llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_24(
30+
i32 16, i32 7, i32 1, i32 0, i1 false)
31+
ret void
32+
}
33+
34+
; CHECK: {{%[0-9]+}} = OpFunction {{%[0-9]+}} DontInline {{%[0-9]+}}
35+
; CHECK-NEXT: OpLabel
36+
define void @UseDifferentGlobalVar() #0 {
37+
; Make sure we use a different variable from the first function. They have
38+
; different types.
39+
; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeFloat]] [[FloatBufferVar]]
40+
%buffer0 = call target("spirv.Image", float, 5, 2, 0, 0, 2, 3)
41+
@llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_3(
42+
i32 16, i32 7, i32 1, i32 0, i1 false)
43+
ret void
44+
}
45+
46+
; CHECK: {{%[0-9]+}} = OpFunction {{%[0-9]+}} DontInline {{%[0-9]+}}
47+
; CHECK-NEXT: OpLabel
48+
define void @ReuseGlobalVarFromFirstFunction() #0 {
49+
; Make sure we use the same variable as the first function. They should be the
50+
; same in case one function calls the other.
51+
; CHECK-NEXT: [[buffer:%[0-9]+]] = OpLoad [[RWBufferTypeInt]] [[IntBufferVar]]
52+
%buffer1 = call target("spirv.Image", i32, 5, 2, 0, 0, 2, 24)
2353
@llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_24(
2454
i32 16, i32 7, i32 1, i32 0, i1 false)
2555
ret void

0 commit comments

Comments
 (0)