Skip to content

Commit 0a43a84

Browse files
authored
Fix shuffle feeding shuffle with undef literal (#4883)
When folding a vector shuffle with an undef literal, it is possible that the literal is adjusted so that it will then be interpreted as an index into the input operands. This is fixed by special casing that case, and not adjusting those operands. Fixes #4859
1 parent 0ebcdc4 commit 0a43a84

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

source/opt/folding_rules.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2748,7 +2748,8 @@ FoldingRule VectorShuffleFeedingShuffle() {
27482748

27492749
if (adjustment != 0) {
27502750
for (uint32_t i = 2; i < new_operands.size(); i++) {
2751-
if (inst->GetSingleWordInOperand(i) >= op0_length) {
2751+
uint32_t operand = inst->GetSingleWordInOperand(i);
2752+
if (operand >= op0_length && operand != undef_literal) {
27522753
new_operands[i].words[0] -= adjustment;
27532754
}
27542755
}

test/opt/fold_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7963,6 +7963,27 @@ ::testing::Values(
79637963
"%9 = OpVectorShuffle %v4double %7 %8 2 0 1 4294967295\n" +
79647964
"OpReturn\n" +
79657965
"OpFunctionEnd",
7966+
9, true),
7967+
// Test case 14: Shuffle with undef literal and change size of first input vector.
7968+
InstructionFoldingCase<bool>(
7969+
Header() +
7970+
"; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
7971+
"; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 2\n" +
7972+
"; CHECK: OpVectorShuffle\n" +
7973+
"; CHECK: OpVectorShuffle {{%\\w+}} %5 %7 0 1 4 4294967295\n" +
7974+
"; CHECK: OpReturn\n" +
7975+
"%main = OpFunction %void None %void_func\n" +
7976+
"%main_lab = OpLabel\n" +
7977+
"%2 = OpVariable %_ptr_v4double Function\n" +
7978+
"%3 = OpVariable %_ptr_v4double Function\n" +
7979+
"%4 = OpVariable %_ptr_v4double Function\n" +
7980+
"%5 = OpLoad %v4double %2\n" +
7981+
"%6 = OpLoad %v4double %3\n" +
7982+
"%7 = OpLoad %v4double %4\n" +
7983+
"%8 = OpVectorShuffle %v2double %5 %5 0 1\n" +
7984+
"%9 = OpVectorShuffle %v4double %8 %7 0 1 2 4294967295\n" +
7985+
"OpReturn\n" +
7986+
"OpFunctionEnd",
79667987
9, true)
79677988
));
79687989

0 commit comments

Comments
 (0)