Remove unreachable block validation (#2525)
* Remove the check that blocks terminated by OpUnreachable are not
statically reachable in the CFG
* Updated tests
diff --git a/source/val/validate_cfg.cpp b/source/val/validate_cfg.cpp
index 1d6f920..6e7acbd 100644
--- a/source/val/validate_cfg.cpp
+++ b/source/val/validate_cfg.cpp
@@ -725,36 +725,6 @@
return SPV_SUCCESS;
}
-// Checks that there are no OpUnreachable instructions reachable from the entry
-// block of |function|.
-spv_result_t ValidateUnreachableBlocks(ValidationState_t& _,
- Function& function) {
- auto* entry_block = function.first_block();
- std::vector<BasicBlock*> stack;
- std::unordered_set<BasicBlock*> seen;
- if (entry_block) stack.push_back(entry_block);
-
- while (!stack.empty()) {
- auto* block = stack.back();
- stack.pop_back();
-
- if (!seen.insert(block).second) continue;
-
- auto* terminator = block->terminator();
- if (terminator->opcode() == SpvOpUnreachable) {
- return _.diag(SPV_ERROR_INVALID_CFG, block->label())
- << "Statically reachable blocks cannot be terminated by "
- "OpUnreachable";
- }
-
- for (auto* succ : *block->successors()) {
- stack.push_back(succ);
- }
- }
-
- return SPV_SUCCESS;
-}
-
spv_result_t PerformCfgChecks(ValidationState_t& _) {
for (auto& function : _.functions()) {
// Check all referenced blocks are defined within a function
@@ -857,8 +827,6 @@
}
}
- if (auto error = ValidateUnreachableBlocks(_, function)) return error;
-
/// Structured control flow checks are only required for shader capabilities
if (_.HasCapability(SpvCapabilityShader)) {
if (auto error = StructuredControlFlowChecks(_, &function, back_edges))
diff --git a/test/val/val_cfg_test.cpp b/test/val/val_cfg_test.cpp
index 8f39a00..72bfa42 100644
--- a/test/val/val_cfg_test.cpp
+++ b/test/val/val_cfg_test.cpp
@@ -1203,11 +1203,7 @@
TEST_P(ValidateCFG, UnreachableMergeWithBranchUse) {
CompileSuccessfully(
GetUnreachableMergeWithBranchUse(GetParam(), SPV_ENV_UNIVERSAL_1_0));
- EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Statically reachable blocks cannot be terminated by OpUnreachable"));
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateCFG, WebGPUUnreachableMergeWithBranchUse) {
@@ -1942,10 +1938,7 @@
str += "OpFunctionEnd";
CompileSuccessfully(str);
- EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
- EXPECT_THAT(getDiagnosticString(),
- HasSubstr("Statically reachable blocks cannot be terminated by "
- "OpUnreachable"));
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateCFG, LoopWithZeroBackEdgesBad) {
@@ -2873,11 +2866,7 @@
)";
CompileSuccessfully(text);
- EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Statically reachable blocks cannot be terminated by OpUnreachable"));
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateCFG, ReachableOpUnreachableOpBranch) {
@@ -2896,11 +2885,7 @@
)";
CompileSuccessfully(text);
- EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Statically reachable blocks cannot be terminated by OpUnreachable"));
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateCFG, ReachableOpUnreachableOpBranchConditional) {
@@ -2923,11 +2908,7 @@
)";
CompileSuccessfully(text);
- EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Statically reachable blocks cannot be terminated by OpUnreachable"));
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateCFG, ReachableOpUnreachableOpSwitch) {
@@ -2952,11 +2933,7 @@
)";
CompileSuccessfully(text);
- EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Statically reachable blocks cannot be terminated by OpUnreachable"));
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateCFG, ReachableOpUnreachableLoop) {
@@ -2980,11 +2957,7 @@
)";
CompileSuccessfully(text);
- EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
- EXPECT_THAT(
- getDiagnosticString(),
- HasSubstr(
- "Statically reachable blocks cannot be terminated by OpUnreachable"));
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateCFG, UnreachableLoopBadBackedge) {