Skip to content

Commit 0db9162

Browse files
authored
[opt] Fix folding rule for extract from copy logical (KhronosGroup#6725)
Fixes KhronosGroup#6712 * Added missing vector cases for composite types
1 parent f3f1169 commit 0db9162

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

source/opt/folding_rules.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,14 +2180,18 @@ uint32_t GetElementType(uint32_t type_id, Instruction::iterator start,
21802180
const Instruction* type_inst = def_use_manager->GetDef(type_id);
21812181
assert(index.type == SPV_OPERAND_TYPE_LITERAL_INTEGER &&
21822182
index.words.size() == 1);
2183-
if (type_inst->opcode() == spv::Op::OpTypeArray) {
2184-
type_id = type_inst->GetSingleWordInOperand(0);
2185-
} else if (type_inst->opcode() == spv::Op::OpTypeMatrix) {
2186-
type_id = type_inst->GetSingleWordInOperand(0);
2187-
} else if (type_inst->opcode() == spv::Op::OpTypeStruct) {
2188-
type_id = type_inst->GetSingleWordInOperand(index.words[0]);
2189-
} else {
2190-
return 0;
2183+
switch (type_inst->opcode()) {
2184+
case spv::Op::OpTypeArray:
2185+
case spv::Op::OpTypeMatrix:
2186+
case spv::Op::OpTypeVector:
2187+
case spv::Op::OpTypeVectorIdEXT:
2188+
type_id = type_inst->GetSingleWordInOperand(0);
2189+
break;
2190+
case spv::Op::OpTypeStruct:
2191+
type_id = type_inst->GetSingleWordInOperand(index.words[0]);
2192+
break;
2193+
default:
2194+
return 0;
21912195
}
21922196
}
21932197
return type_id;

test/opt/fold_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14683,6 +14683,34 @@ ::testing::Values(
1468314683
OpReturn
1468414684
OpFunctionEnd
1468514685
)",
14686+
13, true),
14687+
// Test case 31: Fold OpCopyLogical feeding extract into vector.
14688+
InstructionFoldingCase<bool>(
14689+
Header() + R"(
14690+
; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
14691+
; CHECK: [[v2uint:%\w+]] = OpTypeVector [[uint]] 2
14692+
; CHECK: [[struct_type1:%\w+]] = OpTypeStruct [[v2uint]]
14693+
; CHECK: [[struct_type2:%\w+]] = OpTypeStruct [[v2uint]]
14694+
; CHECK: [[struct_type3:%\w+]] = OpTypeStruct [[struct_type1]]
14695+
; CHECK: [[struct_type4:%\w+]] = OpTypeStruct [[struct_type2]]
14696+
; CHECK: [[var:%\w+]] = OpVariable
14697+
; CHECK: [[ld:%\w+]] = OpLoad [[struct_type3]] [[var]]
14698+
; CHECK: [[ex:%\w+]] = OpCompositeExtract [[uint]] [[ld]] 0 0 0
14699+
; CHECK: %13 = OpCopyObject [[uint]] [[ex]]
14700+
%struct1 = OpTypeStruct %v2uint
14701+
%struct2 = OpTypeStruct %v2uint
14702+
%struct3 = OpTypeStruct %struct1
14703+
%struct4 = OpTypeStruct %struct2
14704+
%_ptr_StorageBuffer_struct3 = OpTypePointer StorageBuffer %struct3
14705+
%var1 = OpVariable %_ptr_StorageBuffer_struct3 StorageBuffer
14706+
%main = OpFunction %void None %void_func
14707+
%4 = OpLabel
14708+
%11 = OpLoad %struct3 %var1
14709+
%12 = OpCopyLogical %struct4 %11
14710+
%13 = OpCompositeExtract %uint %12 0 0 0
14711+
OpReturn
14712+
OpFunctionEnd
14713+
)",
1468614714
13, true)
1468714715
));
1468814716

0 commit comments

Comments
 (0)