Skip to content

Commit 8baa212

Browse files
authored
[DirectX] Handle <1 x ...> loads in DXILResourceAccess (llvm#137076)
We can end up with loads of single element vectors when we have scalar values, because the vectorizer may introduce these to use ops like shufflevector in some cases. Make sure we're maintaining the correct type when translating these into resource load operations. Fixes llvm#136409.
1 parent a7e5312 commit 8baa212

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

llvm/lib/Target/DirectX/DXILResourceAccess.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ static void createTypedBufferLoad(IntrinsicInst *II, LoadInst *LI,
143143
if (Offset)
144144
V = Builder.CreateExtractElement(V, Offset);
145145

146+
// If we loaded a <1 x ...> instead of a scalar (presumably to feed a
147+
// shufflevector), then make sure we're maintaining the resulting type.
148+
if (auto *VT = dyn_cast<FixedVectorType>(LI->getType()))
149+
if (VT->getNumElements() == 1 && !isa<FixedVectorType>(V->getType()))
150+
V = Builder.CreateInsertElement(PoisonValue::get(VT), V,
151+
Builder.getInt32(0));
152+
146153
LI->replaceAllUsesWith(V);
147154
}
148155

llvm/test/CodeGen/DirectX/ResourceAccess/load_typedbuffer.ll

+47-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ target triple = "dxil-pc-shadermodel6.6-compute"
44

55
declare void @use_float4(<4 x float>)
66
declare void @use_float(float)
7+
declare void @use_float1(<1 x float>)
78

8-
; CHECK-LABEL: define void @load_float4
9+
; CHECK-LABEL: define void @load_float4(
910
define void @load_float4(i32 %index, i32 %elemindex) {
1011
%buffer = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
1112
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_v4f32_1_0_0(
@@ -35,3 +36,48 @@ define void @load_float4(i32 %index, i32 %elemindex) {
3536

3637
ret void
3738
}
39+
40+
; CHECK-LABEL: define void @load_float(
41+
define void @load_float(i32 %index) {
42+
%buffer = call target("dx.TypedBuffer", float, 1, 0, 0)
43+
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
44+
45+
; CHECK-NOT: @llvm.dx.resource.getpointer
46+
%ptr = call ptr @llvm.dx.resource.getpointer(
47+
target("dx.TypedBuffer", float, 1, 0, 0) %buffer, i32 %index)
48+
49+
; CHECK: %[[LOAD:.*]] = call { float, i1 } @llvm.dx.resource.load.typedbuffer.f32.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %buffer, i32 %index)
50+
; CHECK: %[[VALUE:.*]] = extractvalue { float, i1 } %[[LOAD]], 0
51+
; CHECK: call void @use_float(float %[[VALUE]])
52+
%data = load float, ptr %ptr
53+
call void @use_float(float %data)
54+
55+
; CHECK: %[[LOAD:.*]] = call { float, i1 } @llvm.dx.resource.load.typedbuffer.f32.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %buffer, i32 %index)
56+
; CHECK: %[[VALUE:.*]] = extractvalue { float, i1 } %[[LOAD]], 0
57+
; CHECK: %[[VEC:.*]] = insertelement <1 x float> poison, float %[[VALUE]], i32 0
58+
; CHECK: call void @use_float1(<1 x float> %[[VEC]])
59+
%vec_data = load <1 x float>, ptr %ptr
60+
call void @use_float1(<1 x float> %vec_data)
61+
62+
ret void
63+
}
64+
65+
; CHECK-LABEL: define void @load_float1(
66+
define void @load_float1(i32 %index) {
67+
%buffer = call target("dx.TypedBuffer", <1 x float>, 1, 0, 0)
68+
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
69+
70+
; CHECK-NOT: @llvm.dx.resource.getpointer
71+
%ptr = call ptr @llvm.dx.resource.getpointer(
72+
target("dx.TypedBuffer", <1 x float>, 1, 0, 0) %buffer, i32 %index)
73+
74+
; CHECK: %[[LOAD:.*]] = call { <1 x float>, i1 } @llvm.dx.resource.load.typedbuffer.v1f32.tdx.TypedBuffer_v1f32_1_0_0t(target("dx.TypedBuffer", <1 x float>, 1, 0, 0) %buffer, i32 %index)
75+
; CHECK: %[[VALUE:.*]] = extractvalue { <1 x float>, i1 } %[[LOAD]], 0
76+
; CHECK: %[[SHUF:.*]] = shufflevector <1 x float> %[[VALUE]], <1 x float> poison, <4 x i32> zeroinitializer
77+
; CHECK: call void @use_float4(<4 x float> %[[SHUF]])
78+
%load = load <1 x float>, ptr %ptr
79+
%shuf = shufflevector <1 x float> %load, <1 x float> poison, <4 x i32> zeroinitializer
80+
call void @use_float4(<4 x float> %shuf)
81+
82+
ret void
83+
}

0 commit comments

Comments
 (0)