From 7cc9f36283085ca5f028993e2fadbcca666132f4 Mon Sep 17 00:00:00 2001 From: Greg Roth Date: Wed, 27 Jun 2018 20:29:53 -0600 Subject: [PATCH] Add test for CFG alteration by compact IDs The Compact IDs pass can corrupt the CFG, but first the CFG has to be setup. To do this, a test that builds the CFG, then performs the compact IDs pass, then checks context consistency. Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/1648 --- test/opt/compact_ids_test.cpp | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/test/opt/compact_ids_test.cpp b/test/opt/compact_ids_test.cpp index d5fcfb95c..ece670cf0 100644 --- a/test/opt/compact_ids_test.cpp +++ b/test/opt/compact_ids_test.cpp @@ -192,4 +192,84 @@ OpFunctionEnd EXPECT_THAT(disassembly, ::testing::Eq(expected)); } +// Test context consistency check after invalidating +// CFG and others by compact IDs Pass. +// Uses a GLSL shader with named labels for variety +TEST(CompactIds, ConsistentCheck) { + const std::string input(R"(OpCapability Shader +OpMemoryModel Logical GLSL450 +OpEntryPoint Fragment %main "main" %in_var_A %out_var_SV_TARGET +OpExecutionMode %main OriginUpperLeft +OpSource HLSL 600 +OpName %main "main" +OpName %in_var_A "in.var.A" +OpName %out_var_SV_TARGET "out.var.SV_TARGET" +OpDecorate %in_var_A Location 0 +OpDecorate %out_var_SV_TARGET Location 0 +%void = OpTypeVoid +%3 = OpTypeFunction %void +%float = OpTypeFloat 32 +%v4float = OpTypeVector %float 4 +%_ptr_Input_v4float = OpTypePointer Input %v4float +%_ptr_Output_v4float = OpTypePointer Output %v4float +%in_var_A = OpVariable %_ptr_Input_v4float Input +%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output +%main = OpFunction %void None %3 +%5 = OpLabel +%12 = OpLoad %v4float %in_var_A +%23 = OpVectorShuffle %v4float %12 %12 0 0 0 1 +OpStore %out_var_SV_TARGET %23 +OpReturn +OpFunctionEnd +)"); + + spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_1); + std::unique_ptr context = + BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, input, + SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); + ASSERT_NE(context, nullptr); + + opt::CompactIdsPass compact_id_pass; + context->BuildInvalidAnalyses(compact_id_pass.GetPreservedAnalyses()); + const auto status = compact_id_pass.Run(context.get()); + ASSERT_NE(status, opt::Pass::Status::Failure); + EXPECT_TRUE(context->IsConsistent()); + + // Test output just in case + std::vector binary; + context->module()->ToBinary(&binary, false); + std::string disassembly; + tools.Disassemble(binary, &disassembly, + SpirvTools::kDefaultDisassembleOption); + + const std::string expected(R"(OpCapability Shader +OpMemoryModel Logical GLSL450 +OpEntryPoint Fragment %main "main" %in_var_A %out_var_SV_TARGET +OpExecutionMode %main OriginUpperLeft +OpSource HLSL 600 +OpName %main "main" +OpName %in_var_A "in.var.A" +OpName %out_var_SV_TARGET "out.var.SV_TARGET" +OpDecorate %in_var_A Location 0 +OpDecorate %out_var_SV_TARGET Location 0 +%void = OpTypeVoid +%5 = OpTypeFunction %void +%float = OpTypeFloat 32 +%v4float = OpTypeVector %float 4 +%_ptr_Input_v4float = OpTypePointer Input %v4float +%_ptr_Output_v4float = OpTypePointer Output %v4float +%in_var_A = OpVariable %_ptr_Input_v4float Input +%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output +%main = OpFunction %void None %5 +%10 = OpLabel +%11 = OpLoad %v4float %in_var_A +%12 = OpVectorShuffle %v4float %11 %11 0 0 0 1 +OpStore %out_var_SV_TARGET %12 +OpReturn +OpFunctionEnd +)"); + + EXPECT_THAT(disassembly, ::testing::Eq(expected)); +} + } // anonymous namespace