Skip to content

Commit 2da75e1

Browse files
Do not crash when tryingto fold unsupported spec constant (KhronosGroup#5496)
Remove assertion in FoldWithInstructionFolder; there are cases where folding spec constants is unsupported. Closes KhronosGroup#5492.
1 parent 0d87845 commit 2da75e1

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

source/opt/fold_spec_constant_op_and_composite_pass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ Instruction* FoldSpecConstantOpAndCompositePass::FoldWithInstructionFolder(
176176
Instruction* new_const_inst =
177177
context()->get_instruction_folder().FoldInstructionToConstant(
178178
inst.get(), identity_map);
179-
assert(new_const_inst != nullptr &&
180-
"Failed to fold instruction that must be folded.");
179+
180+
// new_const_inst == null indicates we cannot fold this spec constant
181+
if (!new_const_inst) return nullptr;
181182

182183
// Get the instruction before |pos| to insert after. |pos| cannot be the
183184
// first instruction in the list because its type has to come first.

test/opt/fold_spec_const_op_composite_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,31 @@ TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeInsertMatrixNull) {
674674
SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
675675
}
676676

677+
// Silently ignore spec constants that cannot be folded
678+
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, UnfoldableOp) {
679+
const std::string test = R"(
680+
OpCapability Shader
681+
OpCapability SignedZeroInfNanPreserve
682+
OpExtension "SPV_KHR_float_controls"
683+
OpMemoryModel Logical GLSL450
684+
OpEntryPoint Vertex %main "main"
685+
OpSource GLSL 450
686+
OpDecorate %v SpecId 1
687+
%void = OpTypeVoid
688+
%3 = OpTypeFunction %void
689+
%float = OpTypeFloat 32
690+
%v = OpConstant %float 0x1p-1
691+
%c = OpSpecConstantOp %float QuantizeToF16 %v
692+
;CHECK: {{%\w+}} = OpSpecConstantOp {{%\w+}} QuantizeToF16 {{%\w+}}
693+
%main = OpFunction %void None %3
694+
%5 = OpLabel
695+
OpReturn
696+
OpFunctionEnd
697+
)";
698+
699+
SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
700+
}
701+
677702
// All types and some common constants that are potentially required in
678703
// FoldSpecConstantOpAndCompositeTest.
679704
std::vector<std::string> CommonTypesAndConstants() {

0 commit comments

Comments
 (0)