Skip to content

Commit

Permalink
Assembler: Can't set an ID in instruction without result ID
Browse files Browse the repository at this point in the history
Fix tests that violated this rule.

Fixes KhronosGroup#2257
  • Loading branch information
dneto0 committed Sep 11, 2019
1 parent c0e9807 commit 0ef3f16
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 19 deletions.
5 changes: 5 additions & 0 deletions source/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ spv_result_t spvTextEncodeOpcode(const spvtools::AssemblyGrammar& grammar,
<< "Expected <result-id> at the beginning of an instruction, found '"
<< firstWord << "'.";
}
if (!opcodeEntry->hasResult && !result_id.empty()) {
return context->diagnostic()
<< "Cannot set ID " << result_id << " because " << opcodeName
<< " does not produce a result ID.";
}
pInst->opcode = opcodeEntry->opcode;
context->setPosition(nextPosition);
// Reserve the first word for the instruction.
Expand Down
15 changes: 14 additions & 1 deletion test/assembly_format_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace {
using spvtest::ScopedContext;
using spvtest::TextToBinaryTest;

TEST_F(TextToBinaryTest, NotPlacingResultIDAtTheBeginning) {
TEST_F(TextToBinaryTest, InstOpcodeProducesResultIDButNoIDDefinedFails) {
SetText("OpTypeMatrix %1 %2 1000");
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
spvTextToBinary(ScopedContext().context, text.str, text.length,
Expand All @@ -33,5 +33,18 @@ TEST_F(TextToBinaryTest, NotPlacingResultIDAtTheBeginning) {
EXPECT_EQ(0u, diagnostic->position.line);
}

TEST_F(TextToBinaryTest,
InstDefinesResultIDButOpcodeDoesNotProduceAResultFails) {
SetText("\n\n%foo = OpName %1 \"bar\"");
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
spvTextToBinary(ScopedContext().context, text.str, text.length,
&binary, &diagnostic));
ASSERT_NE(nullptr, diagnostic);
EXPECT_STREQ(
"Cannot set ID %foo because OpName does not produce a result ID.",
diagnostic->error);
EXPECT_EQ(2u, diagnostic->position.line);
}

} // namespace
} // namespace svptools
4 changes: 2 additions & 2 deletions test/c_interface_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ TEST(CInterface, DefaultConsumerNullDiagnosticForInvalidValidating) {
}

TEST(CInterface, SpecifyConsumerNullDiagnosticForAssembling) {
const char input_text[] = "%1 = OpName\n";
const char input_text[] = " OpName\n";

auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
Expand Down Expand Up @@ -213,7 +213,7 @@ TEST(CInterface, SpecifyConsumerNullDiagnosticForValidating) {
// When having both a consumer and an diagnostic object, the diagnostic object
// should take priority.
TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForAssembling) {
const char input_text[] = "%1 = OpName";
const char input_text[] = " OpName";

auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
Expand Down
6 changes: 3 additions & 3 deletions test/opt/ccp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,10 @@ TEST_F(CCPTest, SkipSpecConstantInstrucitons) {
%10 = OpSpecConstantFalse %bool
%main = OpFunction %void None %4
%11 = OpLabel
%12 = OpBranchConditional %10 %l1 %l2
%l1 = OpLabel
OpBranchConditional %10 %L1 %L2
%L1 = OpLabel
OpReturn
%l2 = OpLabel
%L2 = OpLabel
OpReturn
OpFunctionEnd
)";
Expand Down
2 changes: 1 addition & 1 deletion test/opt/copy_prop_array_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ OpDecorate %MyCBuffer Binding 0
OpStore %23 %35
%36 = OpAccessChain %_ptr_Function_v4float %23 %24
%37 = OpLoad %v4float %36
%39 = OpStore %36 %v4const
OpStore %36 %v4const
OpStore %out_var_SV_Target %37
OpReturn
OpFunctionEnd
Expand Down
5 changes: 3 additions & 2 deletions test/opt/optimizer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ TEST_P(VulkanToWebGPUPassTest, Ran) {
std::vector<uint32_t> optimized;
class ValidatorOptions validator_options;
ASSERT_TRUE(opt.Run(binary.data(), binary.size(), &optimized,
validator_options, true));
validator_options, true))
<< GetParam().input << "\n";
std::string disassembly;
{
SpirvTools tools(SPV_ENV_WEBGPU_0);
Expand Down Expand Up @@ -412,7 +413,7 @@ INSTANTIATE_TEST_SUITE_P(
"%void_f = OpTypeFunction %void\n"
"%func = OpFunction %void None %void_f\n"
"%label = OpLabel\n"
"%val0 = OpAtomicStore %u32_var %cross_device "
" OpAtomicStore %u32_var %cross_device "
"%acquire_release_atomic_counter_workgroup %u32_1\n"
"%val1 = OpAtomicIIncrement %u32 %u32_var %cross_device "
"%acquire_release_atomic_counter_workgroup\n"
Expand Down
4 changes: 2 additions & 2 deletions test/opt/upgrade_memory_model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ OpMemoryModel Logical GLSL450
%func = OpFunction %void None %func_ty
%1 = OpLabel
%ld = OpLoad %int %var
%st = OpStore %var %ld
OpStore %var %ld
OpReturn
OpFunctionEnd
)";
Expand All @@ -116,7 +116,7 @@ OpMemoryModel Logical GLSL450
%param = OpFunctionParameter %ptr_int_Workgroup
%1 = OpLabel
%ld = OpLoad %int %param
%st = OpStore %param %ld
OpStore %param %ld
OpReturn
OpFunctionEnd
)";
Expand Down
14 changes: 9 additions & 5 deletions test/text_to_binary.constant_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ INSTANTIATE_TEST_SUITE_P(

using OpConstantInvalidTypeTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;

TEST_P(OpConstantInvalidTypeTest, InvalidTypes) {
const std::string input = "%1 = " + GetParam() +
"\n"
Expand Down Expand Up @@ -360,8 +359,11 @@ INSTANTIATE_TEST_SUITE_P(
"OpTypeReserveId",
"OpTypeQueue",
"OpTypePipe ReadOnly",
"OpTypeForwardPointer %a UniformConstant",
// At least one thing that isn't a type at all

// Skip OpTypeForwardPointer doesn't even produce a result ID.
// The assembler errors out if we try to check it in this scenario.

// Try at least one thing that isn't a type at all
"OpNot %a %b"
},
}));
Expand Down Expand Up @@ -470,8 +472,10 @@ INSTANTIATE_TEST_SUITE_P(
"OpTypeReserveId",
"OpTypeQueue",
"OpTypePipe ReadOnly",
"OpTypeForwardPointer %a UniformConstant",
// At least one thing that isn't a type at all

// Skip testing OpTypeForwardPointer because it doesn't even produce a result ID.

// Try at least one thing that isn't a type at all
"OpNot %a %b"
},
}));
Expand Down
9 changes: 6 additions & 3 deletions test/text_to_binary.control_flow_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ TEST_P(OpSwitchInvalidTypeTestCase, InvalidTypes) {
"%1 = " + GetParam() +
"\n"
"%3 = OpCopyObject %1 %2\n" // We only care the type of the expression
"%4 = OpSwitch %3 %default 32 %c\n";
" OpSwitch %3 %default 32 %c\n";
EXPECT_THAT(CompileFailure(input),
Eq("The selector operand for OpSwitch must be the result of an "
"instruction that generates an integer scalar"));
Expand Down Expand Up @@ -371,8 +371,11 @@ INSTANTIATE_TEST_SUITE_P(
"OpTypeReserveId",
"OpTypeQueue",
"OpTypePipe ReadOnly",
"OpTypeForwardPointer %a UniformConstant",
// At least one thing that isn't a type at all

// Skip OpTypeForwardPointer becasuse it doesn't even produce a result
// ID.

// At least one thing that isn't a type at all
"OpNot %a %b"
},
}));
Expand Down

0 comments on commit 0ef3f16

Please sign in to comment.