Skip to content

Commit 43482b7

Browse files
authored
Fix special case with vector swizzle (#5281)
When handling the swizzles in the SPIR-V backen, there is a special case when the final result returns the original vector. It tries to avoid adding the vector shuffle instruction in spir-v. However, that path calls `doExpr` instead of `loadIfGLValue` like the other path. This can sometimes cause a load to be omitted. This is changed so that `loadIfGLValue` is called for both cases. Fixes #5275
1 parent a742bb6 commit 43482b7

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5859,10 +5859,14 @@ SpirvEmitter::doHLSLVectorElementExpr(const HLSLVectorElementExpr *expr,
58595859
originalOrder &= selectors[i] == i;
58605860
}
58615861

5862-
if (originalOrder)
5863-
return doExpr(baseExpr, range);
5864-
58655862
auto *info = loadIfGLValue(baseExpr, range);
5863+
5864+
if (originalOrder) {
5865+
// If the elements are simply the original vector, then return it without a
5866+
// vector shuffle.
5867+
return info;
5868+
}
5869+
58665870
// Use base for both vectors. But we are only selecting values from the
58675871
// first one.
58685872
return spvBuilder.createVectorShuffle(expr->getType(), info, info, selectors,

tools/clang/test/CodeGenSPIRV/op.vector.swizzle.hlsl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// * assignment/compound assignment
1212
// * continuous selection
1313

14+
SamplerState sampler1 : register(s0);
15+
Texture2D<float4> texture1 : register(t0);
16+
1417
void main() {
1518
// CHECK-LABEL: %bb_entry = OpLabel
1619
float4 v4f1, v4f2;
@@ -158,4 +161,13 @@ void main() {
158161
// CHECK-NEXT: [[ptr:%\d+]] = OpAccessChain %_ptr_Function_float %v4f1 %int_3
159162
// CHECK-NEXT: OpStore [[ptr]] %float_5
160163
((((v4f1.xwzy).yxz)).x) = 5.0f;
164+
165+
// CHECK-NEXT: [[texture:%\d+]] = OpLoad %type_2d_image %texture1
166+
// CHECK-NEXT: [[sampler:%\d+]] = OpLoad %type_sampler %sampler1
167+
// CHECK-NEXT: [[v2:%\d+]] = OpLoad %v2float %v2f
168+
// CHECK-NEXT: [[sampled_image:%\d+]] = OpSampledImage %type_sampled_image [[texture]] [[sampler]]
169+
// CHECK-NEXT: [[res:%\w+]] = OpImageSampleExplicitLod %v4float [[sampled_image]] [[v2]] Lod %float_0
170+
// CHECK-NEXT: OpStore %v4f1 [[res]]
171+
v4f1 = texture1.SampleLevel(sampler1, v2f.xyx.xy, 0);
172+
161173
}

0 commit comments

Comments
 (0)