Skip to content

Commit 37d2fcb

Browse files
authored
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
1 parent 380275e commit 37d2fcb

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

source/opt/aggressive_dead_code_elim_pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
267267
}
268268

269269
bool AggressiveDCEPass::AggressiveDCE(Function* func) {
270+
if (func->IsDeclaration()) return false;
270271
std::list<BasicBlock*> structured_order;
271272
cfg()->ComputeStructuredOrder(func, &*func->begin(), &structured_order);
272273
live_local_vars_.clear();

source/opt/mem_pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ void MemPass::RemoveBlock(Function::iterator* bi) {
415415
}
416416

417417
bool MemPass::RemoveUnreachableBlocks(Function* func) {
418+
if (func->IsDeclaration()) return false;
418419
bool modified = false;
419420

420421
// Mark reachable all blocks reachable from the function's entry block.

test/opt/aggressive_dead_code_elim_test.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8070,6 +8070,42 @@ TEST_F(AggressiveDCETest, StoringAPointer) {
80708070
SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
80718071
}
80728072

8073+
TEST_F(AggressiveDCETest, FunctionDeclaration) {
8074+
// Ensure the optimizer can handle traversing over a function declaration
8075+
// 'myfunc' which has no blocks
8076+
8077+
const std::string text = R"(OpCapability Linkage
8078+
OpCapability Shader
8079+
OpMemoryModel Logical GLSL450
8080+
OpEntryPoint Fragment %PSMain "main" %entryPointParam_PSMain
8081+
OpExecutionMode %PSMain OriginUpperLeft
8082+
OpSource Slang 1
8083+
OpName %myfunc "myfunc"
8084+
OpName %entryPointParam_PSMain "entryPointParam_PSMain"
8085+
OpName %PSMain "PSMain"
8086+
OpDecorate %myfunc LinkageAttributes "_S6myfuncp0pv4f" Import
8087+
OpDecorate %entryPointParam_PSMain Location 0
8088+
%void = OpTypeVoid
8089+
%5 = OpTypeFunction %void
8090+
%float = OpTypeFloat 32
8091+
%v4float = OpTypeVector %float 4
8092+
%8 = OpTypeFunction %v4float
8093+
%_ptr_Output_v4float = OpTypePointer Output %v4float
8094+
%entryPointParam_PSMain = OpVariable %_ptr_Output_v4float Output
8095+
%myfunc = OpFunction %v4float None %8
8096+
OpFunctionEnd
8097+
%PSMain = OpFunction %void None %5
8098+
%10 = OpLabel
8099+
%11 = OpFunctionCall %v4float %myfunc
8100+
OpStore %entryPointParam_PSMain %11
8101+
OpReturn
8102+
OpFunctionEnd
8103+
)";
8104+
8105+
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8106+
SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
8107+
}
8108+
80738109
} // namespace
80748110
} // namespace opt
80758111
} // namespace spvtools

0 commit comments

Comments
 (0)