Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions source/opt/folding_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ std::vector<uint32_t> GetWordsFromNumericScalarOrVectorConstant(
uint32_t compacted_word = 0;
for (int32_t i = static_cast<int32_t>(words.size()) - 1; i >= 0; --i) {
compacted_word <<= 8;
compacted_word |= words[i];
compacted_word |= (words[i] & 0xFF);
}
return {compacted_word};
} else if (ElementWidth(c->type()) == 16) {
Expand All @@ -205,7 +205,7 @@ std::vector<uint32_t> GetWordsFromNumericScalarOrVectorConstant(
for (uint32_t i = 0; i < words.size(); i += 2) {
uint32_t word1 = words[i];
uint32_t word2 = (i + 1 < words.size()) ? words[i + 1] : 0;
uint32_t compacted_word = (word2 << 16) | word1;
uint32_t compacted_word = (word2 << 16) | (word1 & 0xFFFF);
compacted_words.push_back(compacted_word);
}
return compacted_words;
Expand Down
57 changes: 39 additions & 18 deletions test/opt/fold_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ OpName %main "main"
%v4int = OpTypeVector %int 4
%v2ubyte = OpTypeVector %ubyte 2
%v4ubyte = OpTypeVector %ubyte 4
%v4byte = OpTypeVector %byte 4
%v2ushort = OpTypeVector %ushort 2
%v4ushort = OpTypeVector %ushort 4
%v2short = OpTypeVector %short 2
Expand Down Expand Up @@ -288,6 +289,7 @@ OpName %main "main"
%_ptr_v2double = OpTypePointer Function %v2double
%int_2 = OpConstant %int 2
%int_arr_2 = OpTypeArray %int %int_2
%short_n1 = OpConstant %short -1
%short_0 = OpConstant %short 0
%short_2 = OpConstant %short 2
%short_3 = OpConstant %short 3
Expand All @@ -298,6 +300,7 @@ OpName %main "main"
%ubyte_c = OpConstant %ubyte 12
%ubyte_d = OpConstant %ubyte 13
%byte_n1 = OpConstant %byte -1
%byte_0 = OpConstant %byte 0
%ushort_0 = OpConstant %ushort 0
%ushort_1 = OpConstant %ushort 1
%ushort_2 = OpConstant %ushort 2
Expand Down Expand Up @@ -362,6 +365,7 @@ OpName %main "main"
%v2int_4_4 = OpConstantComposite %v2int %int_4 %int_4
%v2int_min_max = OpConstantComposite %v2int %int_min %int_max
%v2short_2_n5 = OpConstantComposite %v2short %short_2 %short_n5
%v2short_n1_0 = OpConstantComposite %v2short %short_n1 %short_0
%v2short_null = OpConstantNull %v2short
%v2long_2_2 = OpConstantComposite %v2long %long_2 %long_2
%v2long_2_3 = OpConstantComposite %v2long %long_2 %long_3
Expand Down Expand Up @@ -461,6 +465,7 @@ OpName %main "main"
%v4ushort_0_1_2_3 = OpConstantComposite %v4ushort %ushort_0 %ushort_1 %ushort_2 %ushort_3
%v2ubyte_a_b = OpConstantComposite %v2ubyte %ubyte_a %ubyte_b
%v4ubyte_a_b_c_d = OpConstantComposite %v4ubyte %ubyte_a %ubyte_b %ubyte_c %ubyte_d
%v4byte_n1_0_0_0 = OpConstantComposite %v4byte %byte_n1 %byte_0 %byte_0 %byte_0
%long_0xbf8000003f800000 = OpConstant %long 0xbf8000003f800000
%int_0x3FF00000 = OpConstant %int 0x3FF00000
%int_0x00000000 = OpConstant %int 0x00000000
Expand Down Expand Up @@ -1169,79 +1174,95 @@ INSTANTIATE_TEST_SUITE_P(TestCase, IntegerInstructionFoldingTest,
"OpReturn\n" +
"OpFunctionEnd",
2, 0x0B0A),
// Test case 78: Negate 2.
// Test case 78: Bit-cast v2short (-1, 0) to uint
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpBitcast %uint %v2short_n1_0\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0x0000FFFF),
// Test case 79: Bit-cast v4bytes (-1, 0, 0, 0) to uint
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpBitcast %uint %v4byte_n1_0_0_0\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0x000000FF),
// Test case 80: Negate 2.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpSNegate %int %int_2\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, -2),
// Test case 79: Negate negative short.
// Test case 81: Negate negative short.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpSNegate %short %short_0xBC00\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0x4400 /* expected to be sign extended. */),
// Test case 80: Negate positive short.
// Test case 82: Negate positive short.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpSNegate %short %short_0x4400\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0xFFFFBC00 /* expected to be sign extended. */),
// Test case 81: Negate a negative short.
// Test case 83: Negate a negative short.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpSNegate %ushort %ushort_0xBC00\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0x4400 /* expected to be zero extended. */),
// Test case 82: Negate positive short.
// Test case 84: Negate positive short.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpSNegate %ushort %ushort_0x4400\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0xBC00 /* expected to be zero extended. */),
// Test case 83: Fold 2 + 3 (short)
// Test case 85: Fold 2 + 3 (short)
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpIAdd %short %short_2 %short_3\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 5),
// Test case 84: Fold 2 + -5 (short)
// Test case 86: Fold 2 + -5 (short)
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpIAdd %short %short_2 %short_n5\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, -3),
// Test case 85: Fold int(3ll)
// Test case 87: Fold int(3ll)
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpSConvert %int %long_3\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 3),
// Test case 86: Fold short(-3ll)
// Test case 88: Fold short(-3ll)
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpSConvert %short %long_n3\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, -3),
// Test case 87: Fold short(32768ll) - This should do a sign extend when
// Test case 89: Fold short(32768ll) - This should do a sign extend when
// converting to short.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
Expand All @@ -1250,7 +1271,7 @@ INSTANTIATE_TEST_SUITE_P(TestCase, IntegerInstructionFoldingTest,
"OpReturn\n" +
"OpFunctionEnd",
2, -32768),
// Test case 88: Fold short(-57344) - This should do a sign extend when
// Test case 90: Fold short(-57344) - This should do a sign extend when
// converting to short making the upper bits 0.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
Expand All @@ -1259,54 +1280,54 @@ INSTANTIATE_TEST_SUITE_P(TestCase, IntegerInstructionFoldingTest,
"OpReturn\n" +
"OpFunctionEnd",
2, 8192),
// Test case 89: Fold int(-5(short)). The -5 should be interpreted as an unsigned value, and be zero extended to 32-bits.
// Test case 91: Fold int(-5(short)). The -5 should be interpreted as an unsigned value, and be zero extended to 32-bits.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpUConvert %uint %short_n5\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 65531),
// Test case 90: Fold short(-24(int)). The upper bits should be cleared. So 0xFFFFFFE8 should become 0x0000FFE8.
// Test case 92: Fold short(-24(int)). The upper bits should be cleared. So 0xFFFFFFE8 should become 0x0000FFE8.
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpUConvert %ushort %int_n24\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 65512),
// Test case 91: Fold BitReverse of 1
// Test case 93: Fold BitReverse of 1
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpBitReverse %uint %uint_1\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0x80000000),
// Test case 92: Fold BitReverse of 0
// Test case 94: Fold BitReverse of 0
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpBitReverse %uint %uint_0\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0),
// Test case 93: Fold BitReverse of uint max
// Test case 95: Fold BitReverse of uint max
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpBitReverse %uint %uint_max\n" +
"OpReturn\n" +
"OpFunctionEnd",
2, 0xffffffff),
// Test case 94: Fold BitReverse of 0x0000FFFF
// Test case 96: Fold BitReverse of 0x0000FFFF
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
"%2 = OpBitReverse %uint %uint_0x0000ffff\n" +
"OpReturn\n" + "OpFunctionEnd",
2, 0xffff0000),
// Test case 95: Fold BitReverse of OpConstNull
// Test case 97: Fold BitReverse of OpConstNull
InstructionFoldingCase<uint32_t>(
Header() + "%main = OpFunction %void None %void_func\n" +
"%main_lab = OpLabel\n" +
Expand Down