From bc09f53c96075e80f4f7e932fabf04f380c73958 Mon Sep 17 00:00:00 2001 From: alan-baker <33432579+alan-baker@users.noreply.github.com> Date: Wed, 10 Oct 2018 13:25:48 -0400 Subject: [PATCH] Fix calculation of case fall through (#1965) Fixes #1959 * Code erroneously concluded that the target's fall through was itself * Added a test --- source/val/validate_cfg.cpp | 4 +++- test/val/val_cfg_test.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/source/val/validate_cfg.cpp b/source/val/validate_cfg.cpp index 7e22a27e9..162588aa8 100644 --- a/source/val/validate_cfg.cpp +++ b/source/val/validate_cfg.cpp @@ -336,7 +336,9 @@ spv_result_t FindCaseFallThrough( } if (*case_fall_through == 0u) { - *case_fall_through = block->id(); + if (target_block != block) { + *case_fall_through = block->id(); + } } else if (*case_fall_through != block->id()) { // Case construct has at most one branch to another case construct. return _.diag(SPV_ERROR_INVALID_CFG, target_block->label()) diff --git a/test/val/val_cfg_test.cpp b/test/val/val_cfg_test.cpp index d45fd101b..d1f1a66ae 100644 --- a/test/val/val_cfg_test.cpp +++ b/test/val/val_cfg_test.cpp @@ -1777,6 +1777,40 @@ OpFunctionEnd " OpSwitch %uint_0 %10 0 %11 1 %12 2 %13")); } +TEST_F(ValidateCFG, GoodUnreachableSwitch) { + const std::string text = R"( +OpCapability Shader +OpMemoryModel Logical GLSL450 +OpEntryPoint Fragment %2 "main" +OpExecutionMode %2 OriginUpperLeft +%3 = OpTypeVoid +%4 = OpTypeFunction %3 +%5 = OpTypeBool +%6 = OpConstantTrue %5 +%7 = OpTypeInt 32 1 +%9 = OpConstant %7 0 +%2 = OpFunction %3 None %4 +%10 = OpLabel +OpSelectionMerge %11 None +OpBranchConditional %6 %12 %13 +%12 = OpLabel +OpReturn +%13 = OpLabel +OpReturn +%11 = OpLabel +OpSelectionMerge %14 None +OpSwitch %9 %14 0 %15 +%15 = OpLabel +OpBranch %14 +%14 = OpLabel +OpReturn +OpFunctionEnd +)"; + + CompileSuccessfully(text); + EXPECT_THAT(SPV_SUCCESS, ValidateInstructions()); +} + TEST_F(ValidateCFG, InvalidCaseExit) { const std::string text = R"( OpCapability Shader