spirv-opt: fix crash in function declarations (#5796)

* spirv-opt: fix crash in function declarations

Function declarations contain no blocks, so bail before segfaulting
in function optimization passes that operate on blocks.

Fixes #5795

* spirv-opt: add test for optimizing declarations
This commit is contained in:
cheneym2 2024-09-12 07:21:12 -04:00 committed by GitHub
parent 380275eacd
commit 37d2fcb485
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 0 deletions

View File

@ -267,6 +267,7 @@ void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
} }
bool AggressiveDCEPass::AggressiveDCE(Function* func) { bool AggressiveDCEPass::AggressiveDCE(Function* func) {
if (func->IsDeclaration()) return false;
std::list<BasicBlock*> structured_order; std::list<BasicBlock*> structured_order;
cfg()->ComputeStructuredOrder(func, &*func->begin(), &structured_order); cfg()->ComputeStructuredOrder(func, &*func->begin(), &structured_order);
live_local_vars_.clear(); live_local_vars_.clear();

View File

@ -415,6 +415,7 @@ void MemPass::RemoveBlock(Function::iterator* bi) {
} }
bool MemPass::RemoveUnreachableBlocks(Function* func) { bool MemPass::RemoveUnreachableBlocks(Function* func) {
if (func->IsDeclaration()) return false;
bool modified = false; bool modified = false;
// Mark reachable all blocks reachable from the function's entry block. // Mark reachable all blocks reachable from the function's entry block.

View File

@ -8070,6 +8070,42 @@ TEST_F(AggressiveDCETest, StoringAPointer) {
SinglePassRunAndMatch<AggressiveDCEPass>(text, true); SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
} }
TEST_F(AggressiveDCETest, FunctionDeclaration) {
// Ensure the optimizer can handle traversing over a function declaration
// 'myfunc' which has no blocks
const std::string text = R"(OpCapability Linkage
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %PSMain "main" %entryPointParam_PSMain
OpExecutionMode %PSMain OriginUpperLeft
OpSource Slang 1
OpName %myfunc "myfunc"
OpName %entryPointParam_PSMain "entryPointParam_PSMain"
OpName %PSMain "PSMain"
OpDecorate %myfunc LinkageAttributes "_S6myfuncp0pv4f" Import
OpDecorate %entryPointParam_PSMain Location 0
%void = OpTypeVoid
%5 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%8 = OpTypeFunction %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%entryPointParam_PSMain = OpVariable %_ptr_Output_v4float Output
%myfunc = OpFunction %v4float None %8
OpFunctionEnd
%PSMain = OpFunction %void None %5
%10 = OpLabel
%11 = OpFunctionCall %v4float %myfunc
OpStore %entryPointParam_PSMain %11
OpReturn
OpFunctionEnd
)";
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
}
} // namespace } // namespace
} // namespace opt } // namespace opt
} // namespace spvtools } // namespace spvtools