Fix validation of constant matrices (#2794)
Fixes #2793
* Don't special case matrix validation compared to other composites
* just check the constituents are constants or undefs
* later checking validates the column type
* new test
diff --git a/source/val/validate_constants.cpp b/source/val/validate_constants.cpp
index 04544aa..565518b 100644
--- a/source/val/validate_constants.cpp
+++ b/source/val/validate_constants.cpp
@@ -116,15 +116,13 @@
inst->GetOperandAs<uint32_t>(constituent_index);
const auto constituent = _.FindDef(constituent_id);
if (!constituent ||
- !(SpvOpConstantComposite == constituent->opcode() ||
- SpvOpSpecConstantComposite == constituent->opcode() ||
- SpvOpUndef == constituent->opcode())) {
+ !spvOpcodeIsConstantOrUndef(constituent->opcode())) {
// The message says "... or undef" because the spec does not say
// undef is a constant.
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< opcode_name << " Constituent <id> '"
<< _.getIdName(constituent_id)
- << "' is not a constant composite or undef.";
+ << "' is not a constant or undef.";
}
const auto vector = _.FindDef(constituent->type_id());
if (!vector) {
diff --git a/test/val/val_constants_test.cpp b/test/val/val_constants_test.cpp
index 72ce8df..2499f5c 100644
--- a/test/val/val_constants_test.cpp
+++ b/test/val/val_constants_test.cpp
@@ -442,6 +442,22 @@
EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
}
+TEST_F(ValidateConstant, NullMatrix) {
+ std::string spirv = R"(
+OpCapability Shader
+OpCapability Linkage
+OpMemoryModel Logical GLSL450
+%float = OpTypeFloat 32
+%v2float = OpTypeVector %float 2
+%mat2x2 = OpTypeMatrix %v2float 2
+%null_vector = OpConstantNull %v2float
+%null_matrix = OpConstantComposite %mat2x2 %null_vector %null_vector
+)";
+
+ CompileSuccessfully(spirv);
+ EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
+}
+
} // namespace
} // namespace val
} // namespace spvtools
diff --git a/test/val/val_id_test.cpp b/test/val/val_id_test.cpp
index 299e38e..ec5715c 100644
--- a/test/val/val_id_test.cpp
+++ b/test/val/val_id_test.cpp
@@ -1715,7 +1715,7 @@
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("OpSpecConstantComposite Constituent <id> '7[%7]' is "
- "not a constant composite or undef."));
+ "not a constant or undef."));
}
// Invalid: Composite contains a column that is *not* a vector (it's an array)