spirv-opt: Handle null CompositeInsert (#4998)
Fixes #4996
diff --git a/source/opt/const_folding_rules.cpp b/source/opt/const_folding_rules.cpp
index e91455e..19b39d6 100644
--- a/source/opt/const_folding_rules.cpp
+++ b/source/opt/const_folding_rules.cpp
@@ -140,6 +140,12 @@
// Work down hierarchy and add all the indexes, not including the final
// index.
for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
+ if (composite->AsNullConstant()) {
+ // Return Null for the return type.
+ analysis::TypeManager* type_mgr = context->get_type_mgr();
+ return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {});
+ }
+
if (i != inst->NumInOperands() - 1) {
chain.push_back(composite);
}
diff --git a/test/opt/fold_spec_const_op_composite_test.cpp b/test/opt/fold_spec_const_op_composite_test.cpp
index e2374c5..aae9eb2 100644
--- a/test/opt/fold_spec_const_op_composite_test.cpp
+++ b/test/opt/fold_spec_const_op_composite_test.cpp
@@ -374,6 +374,33 @@
SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
}
+TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeInsertNull) {
+ const std::string test =
+ R"(
+ OpCapability Shader
+ OpMemoryModel Logical GLSL450
+ OpEntryPoint GLCompute %1 "main"
+ OpExecutionMode %1 LocalSize 1 1 1
+ %void = OpTypeVoid
+ %3 = OpTypeFunction %void
+ %float = OpTypeFloat 32
+%v2float = OpTypeVector %float 2
+%mat2v2float = OpTypeMatrix %v2float 2
+%null = OpConstantNull %mat2v2float
+ %float_1 = OpConstant %float 1
+ %v2float_1 = OpConstantComposite %v2float %float_1 %float_1
+ %mat2v2_1 = OpConstantComposite %mat2v2float %v2float_1 %v2float_1
+ ; CHECK: %13 = OpConstantNull %mat2v2float
+ %14 = OpSpecConstantOp %mat2v2float CompositeInsert %mat2v2_1 %null 0 0
+ %1 = OpFunction %void None %3
+ %16 = OpLabel
+ OpReturn
+ OpFunctionEnd
+)";
+
+ SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
+}
+
// All types and some common constants that are potentially required in
// FoldSpecConstantOpAndCompositeTest.
std::vector<std::string> CommonTypesAndConstants() {