From cc3e93c4e6fb776b67863decfd69eb307df74ae1 Mon Sep 17 00:00:00 2001 From: alan-baker <33432579+alan-baker@users.noreply.github.com> Date: Wed, 8 May 2019 14:06:04 -0400 Subject: [PATCH] Add tests for folding 1.4 selects (#2568) Fixes #2554 * Folding rules already handle 1.4 selects so I simply added some tests --- test/opt/fold_test.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/test/opt/fold_test.cpp b/test/opt/fold_test.cpp index 7458e1ce3..17cd0d4f7 100644 --- a/test/opt/fold_test.cpp +++ b/test/opt/fold_test.cpp @@ -6311,6 +6311,103 @@ INSTANTIATE_TEST_SUITE_P(OpEntryPointFoldingTest, EntryPointFoldingTest, 9, true) )); +using SPV14FoldingTest = +::testing::TestWithParam>; + +TEST_P(SPV14FoldingTest, Case) { + const auto& tc = GetParam(); + + // Build module. + std::unique_ptr context = + BuildModule(SPV_ENV_UNIVERSAL_1_4, 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); + std::unique_ptr original_inst(inst->Clone(context.get())); + bool succeeded = context->get_instruction_folder().FoldInstruction(inst); + EXPECT_EQ(succeeded, tc.expected_result); + if (succeeded) { + Match(tc.test_body, context.get()); + } +} + +INSTANTIATE_TEST_SUITE_P(SPV14FoldingTest, SPV14FoldingTest, +::testing::Values( + // Test case 0: select vectors with scalar condition. + InstructionFoldingCase(std::string() + +"; CHECK-NOT: OpSelect\n" + +"; CHECK: %3 = OpCopyObject {{%\\w+}} %1\n" + +"OpCapability Shader\n" + +"OpCapability Linkage\n" + +"%void = OpTypeVoid\n" + +"%bool = OpTypeBool\n" + +"%true = OpConstantTrue %bool\n" + +"%int = OpTypeInt 32 0\n" + +"%int4 = OpTypeVector %int 4\n" + +"%int_0 = OpConstant %int 0\n" + +"%int_1 = OpConstant %int 1\n" + +"%1 = OpUndef %int4\n" + +"%2 = OpUndef %int4\n" + +"%void_fn = OpTypeFunction %void\n" + +"%func = OpFunction %void None %void_fn\n" + +"%entry = OpLabel\n" + +"%3 = OpSelect %int4 %true %1 %2\n" + +"OpReturn\n" + +"OpFunctionEnd\n" +, + 3, true), + // Test case 1: select struct with scalar condition. + InstructionFoldingCase(std::string() + +"; CHECK-NOT: OpSelect\n" + +"; CHECK: %3 = OpCopyObject {{%\\w+}} %2\n" + +"OpCapability Shader\n" + +"OpCapability Linkage\n" + +"%void = OpTypeVoid\n" + +"%bool = OpTypeBool\n" + +"%true = OpConstantFalse %bool\n" + +"%int = OpTypeInt 32 0\n" + +"%struct = OpTypeStruct %int %int %int %int\n" + +"%int_0 = OpConstant %int 0\n" + +"%int_1 = OpConstant %int 1\n" + +"%1 = OpUndef %struct\n" + +"%2 = OpUndef %struct\n" + +"%void_fn = OpTypeFunction %void\n" + +"%func = OpFunction %void None %void_fn\n" + +"%entry = OpLabel\n" + +"%3 = OpSelect %struct %true %1 %2\n" + +"OpReturn\n" + +"OpFunctionEnd\n" +, + 3, true), + // Test case 1: select array with scalar condition. + InstructionFoldingCase(std::string() + +"; CHECK-NOT: OpSelect\n" + +"; CHECK: %3 = OpCopyObject {{%\\w+}} %2\n" + +"OpCapability Shader\n" + +"OpCapability Linkage\n" + +"%void = OpTypeVoid\n" + +"%bool = OpTypeBool\n" + +"%true = OpConstantFalse %bool\n" + +"%int = OpTypeInt 32 0\n" + +"%int_0 = OpConstant %int 0\n" + +"%int_1 = OpConstant %int 1\n" + +"%int_4 = OpConstant %int 4\n" + +"%array = OpTypeStruct %int %int %int %int\n" + +"%1 = OpUndef %array\n" + +"%2 = OpUndef %array\n" + +"%void_fn = OpTypeFunction %void\n" + +"%func = OpFunction %void None %void_fn\n" + +"%entry = OpLabel\n" + +"%3 = OpSelect %array %true %1 %2\n" + +"OpReturn\n" + +"OpFunctionEnd\n" +, + 3, true) +)); + } // namespace } // namespace opt } // namespace spvtools