Only run merge-returnon reachable functions. (#1983)

We currently run merge-return on all functions, but
dead-branch-elimination only runs on function reachable from an entry
point or exported function.  Since dead-branch-elimination is needed for
merge-return, they have to match.

Fixes #1976.
This commit is contained in:
Steven Perron 2018-10-18 08:48:27 -04:00 committed by GitHub
parent 9aa14a38f4
commit 0e68bb3632
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 9 deletions

View File

@ -29,26 +29,35 @@ namespace spvtools {
namespace opt {
Pass::Status MergeReturnPass::Process() {
bool modified = false;
bool is_shader =
context()->get_feature_mgr()->HasCapability(SpvCapabilityShader);
for (auto& function : *get_module()) {
std::vector<BasicBlock*> return_blocks = CollectReturnBlocks(&function);
if (return_blocks.size() <= 1) continue;
function_ = &function;
bool failed = false;
ProcessFunction pfn = [&failed, is_shader, this](Function* function) {
std::vector<BasicBlock*> return_blocks = CollectReturnBlocks(function);
if (return_blocks.size() <= 1) {
return false;
}
function_ = function;
return_flag_ = nullptr;
return_value_ = nullptr;
final_return_block_ = nullptr;
modified = true;
if (is_shader) {
if (!ProcessStructured(&function, return_blocks)) {
return Status::Failure;
if (!ProcessStructured(function, return_blocks)) {
failed = true;
}
} else {
MergeReturnBlocks(&function, return_blocks);
MergeReturnBlocks(function, return_blocks);
}
return true;
};
bool modified = ProcessReachableCallTree(pfn, context());
if (failed) {
return Status::Failure;
}
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;

View File

@ -104,6 +104,7 @@ TEST_F(MergeReturnPassTest, TwoReturnsWithValues) {
R"(OpCapability Linkage
OpCapability Kernel
OpMemoryModel Logical OpenCL
OpDecorate %7 LinkageAttributes "simple_kernel" Export
%1 = OpTypeInt 32 0
%2 = OpTypeBool
%3 = OpConstantFalse %2
@ -124,6 +125,7 @@ OpFunctionEnd
R"(OpCapability Linkage
OpCapability Kernel
OpMemoryModel Logical OpenCL
OpDecorate %7 LinkageAttributes "simple_kernel" Export
%1 = OpTypeInt 32 0
%2 = OpTypeBool
%3 = OpConstantFalse %2
@ -207,6 +209,7 @@ TEST_F(MergeReturnPassTest, UnreachableReturnsWithValues) {
R"(OpCapability Linkage
OpCapability Kernel
OpMemoryModel Logical OpenCL
OpDecorate %7 LinkageAttributes "simple_kernel" Export
%1 = OpTypeInt 32 0
%2 = OpTypeBool
%3 = OpConstantFalse %2
@ -230,6 +233,7 @@ OpFunctionEnd
R"(OpCapability Linkage
OpCapability Kernel
OpMemoryModel Logical OpenCL
OpDecorate %7 LinkageAttributes "simple_kernel" Export
%1 = OpTypeInt 32 0
%2 = OpTypeBool
%3 = OpConstantFalse %2
@ -1004,6 +1008,7 @@ OpDecorate %7 RelaxedPrecision
%13 = OpTypeFunction %12
%11 = OpFunction %12 None %13
%l1 = OpLabel
%fc = OpFunctionCall %1 %7
OpReturn
OpFunctionEnd
%7 = OpFunction %1 None %6