Skip to content

Commit f6940d2

Browse files
authored
spirv-val: accept graphs with no inputs and/or no body (KhronosGroup#6291)
Also add a couple of missing layout tests - Graphs with disallowed body instructions should be ejected - Instructions that must be used within a graph body after a first graph should be rejected. Change-Id: I4c48f933fa130733b151c7f10d43b7d843bab70c Signed-off-by: Kevin Petit <[email protected]>
1 parent 578cda7 commit f6940d2

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

source/val/validate_layout.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ spv_result_t GraphScopedInstructions(ValidationState_t& _,
372372
_.SetGraphDefinitionRegion(kGraphDefinitionInputs);
373373
} break;
374374
case spv::Op::OpGraphSetOutputARM: {
375-
if ((_.graph_definition_region() != kGraphDefinitionInputs) &&
375+
if ((_.graph_definition_region() != kGraphDefinitionBegin) &&
376+
(_.graph_definition_region() != kGraphDefinitionInputs) &&
376377
(_.graph_definition_region() != kGraphDefinitionBody) &&
377378
(_.graph_definition_region() != kGraphDefinitionOutputs)) {
378379
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
@@ -399,9 +400,10 @@ spv_result_t GraphScopedInstructions(ValidationState_t& _,
399400
}
400401
break;
401402
default:
402-
if (_.graph_definition_region() < kGraphDefinitionInputs) {
403+
if (_.graph_definition_region() == kGraphDefinitionOutside) {
403404
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
404-
<< spvOpcodeString(opcode) << " must appear in a graph body";
405+
<< "Op" << spvOpcodeString(opcode)
406+
<< " must appear in a graph body";
405407
}
406408
if (_.graph_definition_region() == kGraphDefinitionOutputs) {
407409
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
@@ -413,7 +415,7 @@ spv_result_t GraphScopedInstructions(ValidationState_t& _,
413415
}
414416
} else {
415417
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
416-
<< spvOpcodeString(opcode)
418+
<< "Op" << spvOpcodeString(opcode)
417419
<< " cannot appear in the graph definitions section";
418420
}
419421
return SPV_SUCCESS;

test/val/val_graph_test.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ std::string GenerateModule(const std::string& src) {
6060
%uint_4 = OpConstant %uint 4
6161
%float_1 = OpConstant %float 1.0
6262
%int8tensor = OpTypeTensorARM %int8 %uint_4
63+
%int8r3tensor = OpTypeTensorARM %int8 %uint_3
6364
%int32tensor = OpTypeTensorARM %int32 %uint_4
6465
%int8tensor_array3 = OpTypeArray %int8tensor %uint_3
6566
%int32tensor_array3 = OpTypeArray %int32tensor %uint_3
@@ -209,6 +210,61 @@ TEST_F(ValidateGraph, ValidGraphConstantOusideGraph) {
209210
EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
210211
}
211212

213+
TEST_F(ValidateGraph, ValidGraphWithNoInputsNoBody) {
214+
const std::string src = R"(
215+
%cst = OpGraphConstantARM %int8tensor 1
216+
%graph_type = OpTypeGraphARM 0 %int8tensor
217+
OpGraphEntryPointARM %graph "longname" %var_int8tensor
218+
%graph = OpGraphARM %graph_type
219+
OpGraphSetOutputARM %cst %uint_0
220+
OpGraphEndARM
221+
)";
222+
std::string spvasm = GenerateModule(src);
223+
224+
CompileSuccessfully(spvasm, SPVENV);
225+
EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
226+
}
227+
228+
TEST_F(ValidateGraph, InvalidGraphWithDisallowedBodyInstructions) {
229+
const std::string src = R"(
230+
%graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
231+
OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
232+
%graph = OpGraphARM %graph_type
233+
%in = OpGraphInputARM %int8tensor %uint_0
234+
%val = OpCompositeExtract %int8r3tensor %in 0
235+
%out = OpCompositeInsert %int8tensor %val %in 0
236+
OpGraphSetOutputARM %out %uint_0
237+
OpGraphEndARM
238+
)";
239+
std::string spvasm = GenerateModule(src);
240+
241+
CompileSuccessfully(spvasm, SPVENV);
242+
EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
243+
EXPECT_THAT(
244+
getDiagnosticString(),
245+
HasSubstr(
246+
"OpCompositeInsert cannot appear in the graph definitions section"));
247+
}
248+
249+
TEST_F(ValidateGraph, InvalidInstructionOutsideGraphAfterGraph) {
250+
const std::string src = R"(
251+
%graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
252+
%cst = OpGraphConstantARM %int8tensor 1
253+
OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
254+
%graph = OpGraphARM %graph_type
255+
%in = OpGraphInputARM %int8tensor %uint_0
256+
OpGraphSetOutputARM %in %uint_0
257+
OpGraphEndARM
258+
%val = OpCompositeExtract %int8r3tensor %cst 0
259+
)";
260+
std::string spvasm = GenerateModule(src);
261+
262+
CompileSuccessfully(spvasm, SPVENV);
263+
EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
264+
EXPECT_THAT(getDiagnosticString(),
265+
HasSubstr("OpCompositeExtract must appear in a graph body"));
266+
}
267+
212268
//
213269
// Type tests
214270
//

0 commit comments

Comments
 (0)