aboutsummaryrefslogtreecommitdiff
path: root/test/opt/fold_test.cpp
diff options
context:
space:
mode:
authorSteven Perron <stevenperron@google.com>2019-09-03 09:17:18 -0400
committerGitHub <noreply@github.com>2019-09-03 09:17:18 -0400
commitb54d950298001ad689b404b6871a4136d63b3489 (patch)
tree9a2cd0c57619d6f6f8afbbf69988db0f642748fc /test/opt/fold_test.cpp
parent2c5ed16ba97d84797190a9637e956cf21d9795e1 (diff)
downloadspirv-tools-b54d950298001ad689b404b6871a4136d63b3489.tar.gz
Fold Fmix should accept vector operands. (#2826)
Fixes #2819
Diffstat (limited to 'test/opt/fold_test.cpp')
-rw-r--r--test/opt/fold_test.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/opt/fold_test.cpp b/test/opt/fold_test.cpp
index b5998c72..f24f08e6 100644
--- a/test/opt/fold_test.cpp
+++ b/test/opt/fold_test.cpp
@@ -222,6 +222,7 @@ OpName %main "main"
%v2float_3_2 = OpConstantComposite %v2float %float_3 %float_2
%v2float_4_4 = OpConstantComposite %v2float %float_4 %float_4
%v2float_2_0p5 = OpConstantComposite %v2float %float_2 %float_0p5
+%v2float_0p2_0p5 = OpConstantComposite %v2float %float_0p2 %float_0p5
%v2float_null = OpConstantNull %v2float
%double_n1 = OpConstant %double -1
%105 = OpConstant %double 0 ; Need a def with an numerical id to define id maps.
@@ -643,6 +644,58 @@ INSTANTIATE_TEST_SUITE_P(TestCase, IntVectorInstructionFoldingTest,
));
// clang-format on
+using FloatVectorInstructionFoldingTest =
+ ::testing::TestWithParam<InstructionFoldingCase<std::vector<float>>>;
+
+TEST_P(FloatVectorInstructionFoldingTest, Case) {
+ const auto& tc = GetParam();
+
+ // Build module.
+ std::unique_ptr<IRContext> context =
+ BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, tc.test_body,
+ SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
+ ASSERT_NE(nullptr, context);
+
+ // Fold the instruction to test.
+ analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
+ Instruction* inst = def_use_mgr->GetDef(tc.id_to_fold);
+ SpvOp original_opcode = inst->opcode();
+ bool succeeded = context->get_instruction_folder().FoldInstruction(inst);
+
+ // Make sure the instruction folded as expected.
+ EXPECT_EQ(succeeded, inst == nullptr || inst->opcode() != original_opcode);
+ if (succeeded && inst != nullptr) {
+ EXPECT_EQ(inst->opcode(), SpvOpCopyObject);
+ inst = def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
+ std::vector<SpvOp> opcodes = {SpvOpConstantComposite};
+ EXPECT_THAT(opcodes, Contains(inst->opcode()));
+ analysis::ConstantManager* const_mrg = context->get_constant_mgr();
+ const analysis::Constant* result = const_mrg->GetConstantFromInst(inst);
+ EXPECT_NE(result, nullptr);
+ if (result != nullptr) {
+ const std::vector<const analysis::Constant*>& componenets =
+ result->AsVectorConstant()->GetComponents();
+ EXPECT_EQ(componenets.size(), tc.expected_result.size());
+ for (size_t i = 0; i < componenets.size(); i++) {
+ EXPECT_EQ(tc.expected_result[i], componenets[i]->GetFloat());
+ }
+ }
+ }
+}
+
+// clang-format off
+INSTANTIATE_TEST_SUITE_P(TestCase, FloatVectorInstructionFoldingTest,
+::testing::Values(
+ // Test case 0: FMix {2.0, 2.0}, {2.0, 3.0} {0.2,0.5}
+ InstructionFoldingCase<std::vector<float>>(
+ Header() + "%main = OpFunction %void None %void_func\n" +
+ "%main_lab = OpLabel\n" +
+ "%2 = OpExtInst %v2float %1 FMix %v2float_2_3 %v2float_0_0 %v2float_0p2_0p5\n" +
+ "OpReturn\n" +
+ "OpFunctionEnd",
+ 2, {1.6f,1.5f})
+));
+// clang-format on
using BooleanInstructionFoldingTest =
::testing::TestWithParam<InstructionFoldingCase<bool>>;