Skip to content

Commit b741360

Browse files
authored
[OPT] Use new instruction folder for for all opcodes in spec consti folding (KhronosGroup#5569)
* [OPT] Use new instruction folder for for all opcodes in spec consti folding When folding and OpSpecConstantOp, we use the new instruction folder for a small number of opcodes. This enable the new instruction folder for all opcodes and uses the old one as a fall back. This allows us to remove some code from the older folder that is now covered by the new one. Fixes KhronosGroup#5499
1 parent 784b064 commit b741360

File tree

4 files changed

+9
-171
lines changed

4 files changed

+9
-171
lines changed

source/opt/const_folding_rules.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,8 +1715,10 @@ BinaryScalarFoldingRule FoldBinaryIntegerOperation(uint64_t (*op)(uint64_t,
17151715
assert(result_type != nullptr && a != nullptr && b != nullptr);
17161716
const analysis::Integer* integer_type = result_type->AsInteger();
17171717
assert(integer_type != nullptr);
1718-
assert(integer_type == a->type()->AsInteger());
1719-
assert(integer_type == b->type()->AsInteger());
1718+
assert(a->type()->kind() == analysis::Type::kInteger);
1719+
assert(b->type()->kind() == analysis::Type::kInteger);
1720+
assert(integer_type->width() == a->type()->AsInteger()->width());
1721+
assert(integer_type->width() == b->type()->AsInteger()->width());
17201722

17211723
// In SPIR-V, all operations support unsigned types, but the way they
17221724
// are interpreted depends on the opcode. This is why we use the

source/opt/fold.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -70,58 +70,6 @@ uint32_t InstructionFolder::UnaryOperate(spv::Op opcode,
7070
uint32_t InstructionFolder::BinaryOperate(spv::Op opcode, uint32_t a,
7171
uint32_t b) const {
7272
switch (opcode) {
73-
// Arthimetics
74-
case spv::Op::OpIAdd:
75-
return a + b;
76-
case spv::Op::OpISub:
77-
return a - b;
78-
case spv::Op::OpIMul:
79-
return a * b;
80-
case spv::Op::OpUDiv:
81-
if (b != 0) {
82-
return a / b;
83-
} else {
84-
// Dividing by 0 is undefined, so we will just pick 0.
85-
return 0;
86-
}
87-
case spv::Op::OpSDiv:
88-
if (b != 0u) {
89-
return (static_cast<int32_t>(a)) / (static_cast<int32_t>(b));
90-
} else {
91-
// Dividing by 0 is undefined, so we will just pick 0.
92-
return 0;
93-
}
94-
case spv::Op::OpSRem: {
95-
// The sign of non-zero result comes from the first operand: a. This is
96-
// guaranteed by C++11 rules for integer division operator. The division
97-
// result is rounded toward zero, so the result of '%' has the sign of
98-
// the first operand.
99-
if (b != 0u) {
100-
return static_cast<int32_t>(a) % static_cast<int32_t>(b);
101-
} else {
102-
// Remainder when dividing with 0 is undefined, so we will just pick 0.
103-
return 0;
104-
}
105-
}
106-
case spv::Op::OpSMod: {
107-
// The sign of non-zero result comes from the second operand: b
108-
if (b != 0u) {
109-
int32_t rem = BinaryOperate(spv::Op::OpSRem, a, b);
110-
int32_t b_prim = static_cast<int32_t>(b);
111-
return (rem + b_prim) % b_prim;
112-
} else {
113-
// Mod with 0 is undefined, so we will just pick 0.
114-
return 0;
115-
}
116-
}
117-
case spv::Op::OpUMod:
118-
if (b != 0u) {
119-
return (a % b);
120-
} else {
121-
// Mod with 0 is undefined, so we will just pick 0.
122-
return 0;
123-
}
124-
12573
// Shifting
12674
case spv::Op::OpShiftRightLogical:
12775
if (b >= 32) {

source/opt/fold_spec_constant_op_and_composite_pass.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,9 @@ bool FoldSpecConstantOpAndCompositePass::ProcessOpSpecConstantOp(
115115
"The first in-operand of OpSpecConstantOp instruction must be of "
116116
"SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER type");
117117

118-
switch (static_cast<spv::Op>(inst->GetSingleWordInOperand(0))) {
119-
case spv::Op::OpCompositeExtract:
120-
case spv::Op::OpVectorShuffle:
121-
case spv::Op::OpCompositeInsert:
122-
case spv::Op::OpQuantizeToF16:
123-
folded_inst = FoldWithInstructionFolder(pos);
124-
break;
125-
default:
126-
// TODO: This should use the instruction folder as well, but some folding
127-
// rules are missing.
128-
129-
// Component-wise operations.
130-
folded_inst = DoComponentWiseOperation(pos);
131-
break;
118+
folded_inst = FoldWithInstructionFolder(pos);
119+
if (!folded_inst) {
120+
folded_inst = DoComponentWiseOperation(pos);
132121
}
133122
if (!folded_inst) return false;
134123

test/opt/fold_spec_const_op_composite_test.cpp

Lines changed: 2 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,14 +1058,8 @@ INSTANTIATE_TEST_SUITE_P(
10581058
},
10591059
// expected
10601060
{
1061-
"%true = OpConstantTrue %bool",
1062-
"%true_0 = OpConstantTrue %bool",
10631061
"%spec_bool_t_vec = OpConstantComposite %v2bool %bool_true %bool_true",
1064-
"%false = OpConstantFalse %bool",
1065-
"%false_0 = OpConstantFalse %bool",
10661062
"%spec_bool_f_vec = OpConstantComposite %v2bool %bool_false %bool_false",
1067-
"%false_1 = OpConstantFalse %bool",
1068-
"%false_2 = OpConstantFalse %bool",
10691063
"%spec_bool_from_null = OpConstantComposite %v2bool %bool_false %bool_false",
10701064
},
10711065
},
@@ -1080,14 +1074,8 @@ INSTANTIATE_TEST_SUITE_P(
10801074
},
10811075
// expected
10821076
{
1083-
"%true = OpConstantTrue %bool",
1084-
"%true_0 = OpConstantTrue %bool",
10851077
"%spec_bool_t_vec = OpConstantComposite %v2bool %bool_true %bool_true",
1086-
"%false = OpConstantFalse %bool",
1087-
"%false_0 = OpConstantFalse %bool",
10881078
"%spec_bool_f_vec = OpConstantComposite %v2bool %bool_false %bool_false",
1089-
"%false_1 = OpConstantFalse %bool",
1090-
"%false_2 = OpConstantFalse %bool",
10911079
"%spec_bool_from_null = OpConstantComposite %v2bool %bool_false %bool_false",
10921080
},
10931081
},
@@ -1102,14 +1090,8 @@ INSTANTIATE_TEST_SUITE_P(
11021090
},
11031091
// expected
11041092
{
1105-
"%int_1 = OpConstant %int 1",
1106-
"%int_1_0 = OpConstant %int 1",
11071093
"%spec_int_one_vec = OpConstantComposite %v2int %signed_one %signed_one",
1108-
"%int_0 = OpConstant %int 0",
1109-
"%int_0_0 = OpConstant %int 0",
11101094
"%spec_int_zero_vec = OpConstantComposite %v2int %signed_zero %signed_zero",
1111-
"%int_0_1 = OpConstant %int 0",
1112-
"%int_0_2 = OpConstant %int 0",
11131095
"%spec_int_from_null = OpConstantComposite %v2int %signed_zero %signed_zero",
11141096
},
11151097
},
@@ -1124,14 +1106,8 @@ INSTANTIATE_TEST_SUITE_P(
11241106
},
11251107
// expected
11261108
{
1127-
"%int_1 = OpConstant %int 1",
1128-
"%int_1_0 = OpConstant %int 1",
11291109
"%spec_int_one_vec = OpConstantComposite %v2int %signed_one %signed_one",
1130-
"%int_0 = OpConstant %int 0",
1131-
"%int_0_0 = OpConstant %int 0",
11321110
"%spec_int_zero_vec = OpConstantComposite %v2int %signed_zero %signed_zero",
1133-
"%int_0_1 = OpConstant %int 0",
1134-
"%int_0_2 = OpConstant %int 0",
11351111
"%spec_int_from_null = OpConstantComposite %v2int %signed_zero %signed_zero",
11361112
},
11371113
},
@@ -1146,14 +1122,8 @@ INSTANTIATE_TEST_SUITE_P(
11461122
},
11471123
// expected
11481124
{
1149-
"%uint_1 = OpConstant %uint 1",
1150-
"%uint_1_0 = OpConstant %uint 1",
11511125
"%spec_uint_one_vec = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1152-
"%uint_0 = OpConstant %uint 0",
1153-
"%uint_0_0 = OpConstant %uint 0",
11541126
"%spec_uint_zero_vec = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
1155-
"%uint_0_1 = OpConstant %uint 0",
1156-
"%uint_0_2 = OpConstant %uint 0",
11571127
"%spec_uint_from_null = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
11581128
},
11591129
},
@@ -1168,14 +1138,8 @@ INSTANTIATE_TEST_SUITE_P(
11681138
},
11691139
// expected
11701140
{
1171-
"%uint_1 = OpConstant %uint 1",
1172-
"%uint_1_0 = OpConstant %uint 1",
11731141
"%spec_uint_one_vec = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1174-
"%uint_0 = OpConstant %uint 0",
1175-
"%uint_0_0 = OpConstant %uint 0",
11761142
"%spec_uint_zero_vec = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
1177-
"%uint_0_1 = OpConstant %uint 0",
1178-
"%uint_0_2 = OpConstant %uint 0",
11791143
"%spec_uint_from_null = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
11801144
},
11811145
},
@@ -1184,8 +1148,6 @@ INSTANTIATE_TEST_SUITE_P(
11841148
{
11851149
// original
11861150
{
1187-
"%spec_uint_zero = OpSpecConstantOp %uint UConvert %bool_false",
1188-
"%spec_uint_one = OpSpecConstantOp %uint UConvert %bool_true",
11891151
"%spec_ulong_zero = OpSpecConstantOp %ulong UConvert %unsigned_zero",
11901152
"%spec_ulong_one = OpSpecConstantOp %ulong UConvert %unsigned_one",
11911153
"%spec_short_zero = OpSpecConstantOp %ushort UConvert %unsigned_zero",
@@ -1197,8 +1159,6 @@ INSTANTIATE_TEST_SUITE_P(
11971159
},
11981160
// expected
11991161
{
1200-
"%spec_uint_zero = OpConstant %uint 0",
1201-
"%spec_uint_one = OpConstant %uint 1",
12021162
"%spec_ulong_zero = OpConstant %ulong 0",
12031163
"%spec_ulong_one = OpConstant %ulong 1",
12041164
"%spec_short_zero = OpConstant %ushort 0",
@@ -1236,24 +1196,13 @@ INSTANTIATE_TEST_SUITE_P(
12361196
{
12371197
// original
12381198
{
1239-
"%spec_v2uint_zero = OpSpecConstantOp %v2uint UConvert %bool_false_vec",
1240-
"%spec_v2uint_one = OpSpecConstantOp %v2uint UConvert %bool_true_vec",
12411199
"%spec_v2ulong_zero = OpSpecConstantOp %v2ulong UConvert %unsigned_zero_vec",
12421200
"%spec_v2ulong_one = OpSpecConstantOp %v2ulong UConvert %unsigned_one_vec",
12431201
},
12441202
// expected
12451203
{
1246-
"%uint_0 = OpConstant %uint 0",
1247-
"%uint_0_0 = OpConstant %uint 0",
1248-
"%spec_v2uint_zero = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
1249-
"%uint_1 = OpConstant %uint 1",
1250-
"%uint_1_0 = OpConstant %uint 1",
1251-
"%spec_v2uint_one = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1252-
"%ulong_0 = OpConstant %ulong 0",
1253-
"%ulong_0_0 = OpConstant %ulong 0",
12541204
"%spec_v2ulong_zero = OpConstantComposite %v2ulong %ulong_zero %ulong_zero",
12551205
"%ulong_1 = OpConstant %ulong 1",
1256-
"%ulong_1_0 = OpConstant %ulong 1",
12571206
"%spec_v2ulong_one = OpConstantComposite %v2ulong %ulong_1 %ulong_1",
12581207
},
12591208
},
@@ -1268,14 +1217,10 @@ INSTANTIATE_TEST_SUITE_P(
12681217
},
12691218
// expected
12701219
{
1271-
"%long_0 = OpConstant %long 0",
1272-
"%long_0_0 = OpConstant %long 0",
12731220
"%spec_v2long_zero = OpConstantComposite %v2long %long_zero %long_zero",
12741221
"%long_1 = OpConstant %long 1",
1275-
"%long_1_0 = OpConstant %long 1",
12761222
"%spec_v2long_one = OpConstantComposite %v2long %long_1 %long_1",
12771223
"%long_n1 = OpConstant %long -1",
1278-
"%long_n1_0 = OpConstant %long -1",
12791224
"%spec_v2long_minus_one = OpConstantComposite %v2long %long_n1 %long_n1",
12801225
},
12811226
},
@@ -1372,7 +1317,7 @@ INSTANTIATE_TEST_SUITE_P(
13721317
{
13731318
"%int_minus_1 = OpConstant %int -1",
13741319
"%int_minus_2 = OpConstant %int -2",
1375-
"%int_neg_null = OpConstant %int 0",
1320+
"%int_neg_null = OpConstantNull %int",
13761321
"%int_max = OpConstant %int 2147483647",
13771322
"%int_neg_max = OpConstant %int -2147483647",
13781323
},
@@ -1553,15 +1498,10 @@ INSTANTIATE_TEST_SUITE_P(
15531498
},
15541499
// expected
15551500
{
1556-
"%int_n1 = OpConstant %int -1",
1557-
"%int_n1_0 = OpConstant %int -1",
15581501
"%v2int_minus_1 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
15591502
"%int_n2 = OpConstant %int -2",
1560-
"%int_n2_0 = OpConstant %int -2",
15611503
"%v2int_minus_2 = OpConstantComposite %v2int %int_n2 %int_n2",
1562-
"%int_0 = OpConstant %int 0",
1563-
"%int_0_0 = OpConstant %int 0",
1564-
"%v2int_neg_null = OpConstantComposite %v2int %signed_zero %signed_zero",
1504+
"%v2int_neg_null = OpConstantComposite %v2int %signed_null %signed_null",
15651505
},
15661506
},
15671507
// vector integer (including null vetors) add, sub, div, mul
@@ -1583,35 +1523,23 @@ INSTANTIATE_TEST_SUITE_P(
15831523
// expected
15841524
{
15851525
"%int_5 = OpConstant %int 5",
1586-
"%int_5_0 = OpConstant %int 5",
15871526
"%spec_v2int_iadd = OpConstantComposite %v2int %int_5 %int_5",
15881527
"%int_n4 = OpConstant %int -4",
1589-
"%int_n4_0 = OpConstant %int -4",
15901528
"%spec_v2int_isub = OpConstantComposite %v2int %int_n4 %int_n4",
15911529
"%int_n2 = OpConstant %int -2",
1592-
"%int_n2_0 = OpConstant %int -2",
15931530
"%spec_v2int_sdiv = OpConstantComposite %v2int %int_n2 %int_n2",
15941531
"%int_n6 = OpConstant %int -6",
1595-
"%int_n6_0 = OpConstant %int -6",
15961532
"%spec_v2int_imul = OpConstantComposite %v2int %int_n6 %int_n6",
1597-
"%int_n6_1 = OpConstant %int -6",
1598-
"%int_n6_2 = OpConstant %int -6",
15991533
"%spec_v2int_iadd_null = OpConstantComposite %v2int %int_n6 %int_n6",
16001534

16011535
"%uint_5 = OpConstant %uint 5",
1602-
"%uint_5_0 = OpConstant %uint 5",
16031536
"%spec_v2uint_iadd = OpConstantComposite %v2uint %uint_5 %uint_5",
16041537
"%uint_4294967292 = OpConstant %uint 4294967292",
1605-
"%uint_4294967292_0 = OpConstant %uint 4294967292",
16061538
"%spec_v2uint_isub = OpConstantComposite %v2uint %uint_4294967292 %uint_4294967292",
16071539
"%uint_1431655764 = OpConstant %uint 1431655764",
1608-
"%uint_1431655764_0 = OpConstant %uint 1431655764",
16091540
"%spec_v2uint_udiv = OpConstantComposite %v2uint %uint_1431655764 %uint_1431655764",
16101541
"%uint_2863311528 = OpConstant %uint 2863311528",
1611-
"%uint_2863311528_0 = OpConstant %uint 2863311528",
16121542
"%spec_v2uint_imul = OpConstantComposite %v2uint %uint_2863311528 %uint_2863311528",
1613-
"%uint_2863311528_1 = OpConstant %uint 2863311528",
1614-
"%uint_2863311528_2 = OpConstant %uint 2863311528",
16151543
"%spec_v2uint_isub_null = OpConstantComposite %v2uint %uint_2863311528 %uint_2863311528",
16161544
},
16171545
},
@@ -1655,34 +1583,17 @@ INSTANTIATE_TEST_SUITE_P(
16551583
"%v2int_minus_3 = OpConstantComposite %v2int %int_minus_3 %int_minus_3",
16561584

16571585
// srem
1658-
"%int_1 = OpConstant %int 1",
1659-
"%int_1_0 = OpConstant %int 1",
16601586
"%7_srem_3 = OpConstantComposite %v2int %signed_one %signed_one",
1661-
"%int_n1 = OpConstant %int -1",
1662-
"%int_n1_0 = OpConstant %int -1",
16631587
"%minus_7_srem_3 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
1664-
"%int_1_1 = OpConstant %int 1",
1665-
"%int_1_2 = OpConstant %int 1",
16661588
"%7_srem_minus_3 = OpConstantComposite %v2int %signed_one %signed_one",
1667-
"%int_n1_1 = OpConstant %int -1",
1668-
"%int_n1_2 = OpConstant %int -1",
16691589
"%minus_7_srem_minus_3 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
16701590
// smod
1671-
"%int_1_3 = OpConstant %int 1",
1672-
"%int_1_4 = OpConstant %int 1",
16731591
"%7_smod_3 = OpConstantComposite %v2int %signed_one %signed_one",
1674-
"%int_2 = OpConstant %int 2",
1675-
"%int_2_0 = OpConstant %int 2",
16761592
"%minus_7_smod_3 = OpConstantComposite %v2int %signed_two %signed_two",
16771593
"%int_n2 = OpConstant %int -2",
1678-
"%int_n2_0 = OpConstant %int -2",
16791594
"%7_smod_minus_3 = OpConstantComposite %v2int %int_n2 %int_n2",
1680-
"%int_n1_3 = OpConstant %int -1",
1681-
"%int_n1_4 = OpConstant %int -1",
16821595
"%minus_7_smod_minus_3 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
16831596
// umod
1684-
"%uint_1 = OpConstant %uint 1",
1685-
"%uint_1_0 = OpConstant %uint 1",
16861597
"%7_umod_3 = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
16871598
},
16881599
},
@@ -1702,26 +1613,15 @@ INSTANTIATE_TEST_SUITE_P(
17021613
},
17031614
// expected
17041615
{
1705-
"%int_2 = OpConstant %int 2",
1706-
"%int_2_0 = OpConstant %int 2",
17071616
"%xor_1_3 = OpConstantComposite %v2int %signed_two %signed_two",
1708-
"%int_0 = OpConstant %int 0",
1709-
"%int_0_0 = OpConstant %int 0",
17101617
"%and_1_2 = OpConstantComposite %v2int %signed_zero %signed_zero",
1711-
"%int_3 = OpConstant %int 3",
1712-
"%int_3_0 = OpConstant %int 3",
17131618
"%or_1_2 = OpConstantComposite %v2int %signed_three %signed_three",
17141619

17151620
"%unsigned_31 = OpConstant %uint 31",
17161621
"%v2unsigned_31 = OpConstantComposite %v2uint %unsigned_31 %unsigned_31",
17171622
"%uint_2147483648 = OpConstant %uint 2147483648",
1718-
"%uint_2147483648_0 = OpConstant %uint 2147483648",
17191623
"%unsigned_left_shift_max = OpConstantComposite %v2uint %uint_2147483648 %uint_2147483648",
1720-
"%uint_1 = OpConstant %uint 1",
1721-
"%uint_1_0 = OpConstant %uint 1",
17221624
"%unsigned_right_shift_logical = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1723-
"%int_n1 = OpConstant %int -1",
1724-
"%int_n1_0 = OpConstant %int -1",
17251625
"%signed_right_shift_arithmetic = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
17261626
},
17271627
},
@@ -2091,7 +1991,6 @@ INSTANTIATE_TEST_SUITE_P(
20911991
"%spec_int_20 = OpConstant %int 101",
20921992
"%used_vec_a = OpConstantComposite %v2int %spec_int_18 %spec_int_19",
20931993
"%int_10201 = OpConstant %int 10201",
2094-
"%int_1 = OpConstant %int 1",
20951994
"%used_vec_b = OpConstantComposite %v2int %int_10201 %signed_one",
20961995
"%spec_int_21 = OpConstant %int 10201",
20971996
"%array = OpConstantComposite %type_arr_int_4 %spec_int_20 %spec_int_20 %spec_int_21 %spec_int_21",

0 commit comments

Comments
 (0)