2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-09-14 18:28:33 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-09-14 18:28:33 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-09-14 18:28:33 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-09-14 18:28:33 +00:00
|
|
|
|
|
|
|
// Assembler tests for instructions in the "Control Flow" section of the
|
|
|
|
// SPIR-V spec.
|
|
|
|
|
2016-04-27 20:22:15 +00:00
|
|
|
#include <sstream>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2016-04-27 20:22:15 +00:00
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "test/test_fixture.h"
|
|
|
|
#include "test/unit_spirv.h"
|
2015-09-14 18:28:33 +00:00
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
namespace spvtools {
|
2015-09-14 18:28:33 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-04-27 20:22:15 +00:00
|
|
|
using spvtest::Concatenate;
|
2015-10-01 20:58:17 +00:00
|
|
|
using spvtest::EnumCase;
|
2015-09-14 18:28:33 +00:00
|
|
|
using spvtest::MakeInstruction;
|
2015-09-28 19:02:21 +00:00
|
|
|
using spvtest::TextToBinaryTest;
|
2017-11-27 15:16:41 +00:00
|
|
|
using ::testing::Combine;
|
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::TestWithParam;
|
|
|
|
using ::testing::Values;
|
|
|
|
using ::testing::ValuesIn;
|
2015-09-14 18:28:33 +00:00
|
|
|
|
|
|
|
// Test OpSelectionMerge
|
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
using OpSelectionMergeTest = spvtest::TextToBinaryTestBase<
|
2022-11-04 21:27:10 +00:00
|
|
|
TestWithParam<EnumCase<spv::SelectionControlMask>>>;
|
2015-09-14 18:28:33 +00:00
|
|
|
|
|
|
|
TEST_P(OpSelectionMergeTest, AnySingleSelectionControlMask) {
|
2015-10-13 16:46:13 +00:00
|
|
|
const std::string input = "OpSelectionMerge %1 " + GetParam().name();
|
2022-11-04 21:27:10 +00:00
|
|
|
EXPECT_THAT(CompiledInstructions(input),
|
|
|
|
Eq(MakeInstruction(spv::Op::OpSelectionMerge,
|
|
|
|
{1, uint32_t(GetParam().value())})));
|
2015-09-14 18:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clang-format off
|
2022-11-04 21:27:10 +00:00
|
|
|
#define CASE(VALUE,NAME) { spv::SelectionControlMask::VALUE, NAME}
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(TextToBinarySelectionMerge, OpSelectionMergeTest,
|
2022-11-04 21:27:10 +00:00
|
|
|
ValuesIn(std::vector<EnumCase<spv::SelectionControlMask>>{
|
2015-09-14 18:28:33 +00:00
|
|
|
CASE(MaskNone, "None"),
|
2022-11-04 21:27:10 +00:00
|
|
|
CASE(Flatten, "Flatten"),
|
|
|
|
CASE(DontFlatten, "DontFlatten"),
|
2019-01-29 23:56:52 +00:00
|
|
|
}));
|
2015-09-14 18:28:33 +00:00
|
|
|
#undef CASE
|
|
|
|
// clang-format on
|
|
|
|
|
2015-09-16 22:32:54 +00:00
|
|
|
TEST_F(OpSelectionMergeTest, CombinedSelectionControlMask) {
|
|
|
|
const std::string input = "OpSelectionMerge %1 Flatten|DontFlatten";
|
|
|
|
const uint32_t expected_mask =
|
2022-11-04 21:27:10 +00:00
|
|
|
uint32_t(spv::SelectionControlMask::Flatten |
|
|
|
|
spv::SelectionControlMask::DontFlatten);
|
|
|
|
EXPECT_THAT(
|
|
|
|
CompiledInstructions(input),
|
|
|
|
Eq(MakeInstruction(spv::Op::OpSelectionMerge, {1, expected_mask})));
|
2015-09-16 22:32:54 +00:00
|
|
|
}
|
2015-09-14 18:28:33 +00:00
|
|
|
|
2015-10-13 19:49:53 +00:00
|
|
|
TEST_F(OpSelectionMergeTest, WrongSelectionControl) {
|
|
|
|
// Case sensitive: "flatten" != "Flatten" and thus wrong.
|
|
|
|
EXPECT_THAT(CompileFailure("OpSelectionMerge %1 flatten|DontFlatten"),
|
2015-11-24 23:37:24 +00:00
|
|
|
Eq("Invalid selection control operand 'flatten|DontFlatten'."));
|
2015-10-13 19:49:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 18:33:08 +00:00
|
|
|
// Test OpLoopMerge
|
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
using OpLoopMergeTest = spvtest::TextToBinaryTestBase<
|
2018-08-01 18:58:12 +00:00
|
|
|
TestWithParam<std::tuple<spv_target_env, EnumCase<int>>>>;
|
2015-09-14 18:33:08 +00:00
|
|
|
|
|
|
|
TEST_P(OpLoopMergeTest, AnySingleLoopControlMask) {
|
2018-08-01 18:58:12 +00:00
|
|
|
const auto ctrl = std::get<1>(GetParam());
|
|
|
|
std::ostringstream input;
|
2016-04-27 20:22:15 +00:00
|
|
|
input << "OpLoopMerge %merge %continue " << ctrl.name();
|
|
|
|
for (auto num : ctrl.operands()) input << " " << num;
|
2018-08-01 18:58:12 +00:00
|
|
|
EXPECT_THAT(CompiledInstructions(input.str(), std::get<0>(GetParam())),
|
2022-11-04 21:27:10 +00:00
|
|
|
Eq(MakeInstruction(spv::Op::OpLoopMerge, {1, 2, ctrl.value()},
|
2016-04-27 20:22:15 +00:00
|
|
|
ctrl.operands())));
|
2015-09-14 18:33:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 20:22:15 +00:00
|
|
|
#define CASE(VALUE, NAME) \
|
2022-11-04 21:27:10 +00:00
|
|
|
{ int32_t(spv::LoopControlMask::VALUE), NAME }
|
|
|
|
#define CASE1(VALUE, NAME, PARM) \
|
|
|
|
{ \
|
|
|
|
int32_t(spv::LoopControlMask::VALUE), NAME, { PARM } \
|
2016-04-27 20:22:15 +00:00
|
|
|
}
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2016-04-27 20:22:15 +00:00
|
|
|
TextToBinaryLoopMerge, OpLoopMergeTest,
|
|
|
|
Combine(Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1),
|
|
|
|
ValuesIn(std::vector<EnumCase<int>>{
|
|
|
|
// clang-format off
|
|
|
|
CASE(MaskNone, "None"),
|
2022-11-04 21:27:10 +00:00
|
|
|
CASE(Unroll, "Unroll"),
|
|
|
|
CASE(DontUnroll, "DontUnroll"),
|
2016-04-27 20:22:15 +00:00
|
|
|
// clang-format on
|
2019-01-29 23:56:52 +00:00
|
|
|
})));
|
2016-04-27 20:22:15 +00:00
|
|
|
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2016-04-27 20:22:15 +00:00
|
|
|
TextToBinaryLoopMergeV11, OpLoopMergeTest,
|
|
|
|
Combine(Values(SPV_ENV_UNIVERSAL_1_1),
|
|
|
|
ValuesIn(std::vector<EnumCase<int>>{
|
|
|
|
// clang-format off
|
2022-11-04 21:27:10 +00:00
|
|
|
CASE(DependencyInfinite, "DependencyInfinite"),
|
|
|
|
CASE1(DependencyLength, "DependencyLength", 234),
|
|
|
|
{int32_t(spv::LoopControlMask::Unroll|spv::LoopControlMask::DependencyLength),
|
2016-04-27 20:22:15 +00:00
|
|
|
"DependencyLength|Unroll", {33}},
|
|
|
|
// clang-format on
|
2019-01-29 23:56:52 +00:00
|
|
|
})));
|
2015-09-14 18:33:08 +00:00
|
|
|
#undef CASE
|
2016-04-27 20:22:15 +00:00
|
|
|
#undef CASE1
|
2015-09-14 18:33:08 +00:00
|
|
|
|
2015-09-16 22:32:54 +00:00
|
|
|
TEST_F(OpLoopMergeTest, CombinedLoopControlMask) {
|
2015-09-24 19:45:59 +00:00
|
|
|
const std::string input = "OpLoopMerge %merge %continue Unroll|DontUnroll";
|
2015-09-16 22:32:54 +00:00
|
|
|
const uint32_t expected_mask =
|
2022-11-04 21:27:10 +00:00
|
|
|
uint32_t(spv::LoopControlMask::Unroll | spv::LoopControlMask::DontUnroll);
|
2015-09-16 22:32:54 +00:00
|
|
|
EXPECT_THAT(CompiledInstructions(input),
|
2022-11-04 21:27:10 +00:00
|
|
|
Eq(MakeInstruction(spv::Op::OpLoopMerge, {1, 2, expected_mask})));
|
2015-09-16 22:32:54 +00:00
|
|
|
}
|
2015-10-13 19:53:46 +00:00
|
|
|
|
|
|
|
TEST_F(OpLoopMergeTest, WrongLoopControl) {
|
|
|
|
EXPECT_THAT(CompileFailure("OpLoopMerge %m %c none"),
|
2015-11-24 23:37:24 +00:00
|
|
|
Eq("Invalid loop control operand 'none'."));
|
2015-10-13 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 19:02:21 +00:00
|
|
|
// Test OpSwitch
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchGoodZeroTargets) {
|
|
|
|
EXPECT_THAT(CompiledInstructions("OpSwitch %selector %default"),
|
2022-11-04 21:27:10 +00:00
|
|
|
Eq(MakeInstruction(spv::Op::OpSwitch, {1, 2})));
|
2015-09-28 19:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchGoodOneTarget) {
|
2022-11-04 21:27:10 +00:00
|
|
|
EXPECT_THAT(
|
|
|
|
CompiledInstructions("%1 = OpTypeInt 32 0\n"
|
|
|
|
"%2 = OpConstant %1 52\n"
|
|
|
|
"OpSwitch %2 %default 12 %target0"),
|
|
|
|
Eq(Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 0}),
|
|
|
|
MakeInstruction(spv::Op::OpConstant, {1, 2, 52}),
|
|
|
|
MakeInstruction(spv::Op::OpSwitch, {2, 3, 12, 4})})));
|
2015-09-28 19:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchGoodTwoTargets) {
|
2015-09-29 15:28:34 +00:00
|
|
|
EXPECT_THAT(
|
|
|
|
CompiledInstructions("%1 = OpTypeInt 32 0\n"
|
|
|
|
"%2 = OpConstant %1 52\n"
|
|
|
|
"OpSwitch %2 %default 12 %target0 42 %target1"),
|
|
|
|
Eq(Concatenate({
|
2022-11-04 21:27:10 +00:00
|
|
|
MakeInstruction(spv::Op::OpTypeInt, {1, 32, 0}),
|
|
|
|
MakeInstruction(spv::Op::OpConstant, {1, 2, 52}),
|
|
|
|
MakeInstruction(spv::Op::OpSwitch, {2, 3, 12, 4, 42, 5}),
|
2015-09-29 15:28:34 +00:00
|
|
|
})));
|
2015-09-28 19:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchBadMissingSelector) {
|
|
|
|
EXPECT_THAT(CompileFailure("OpSwitch"),
|
2022-03-28 14:46:39 +00:00
|
|
|
Eq("Expected operand for OpSwitch instruction, but found the end "
|
|
|
|
"of the stream."));
|
2015-09-28 19:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchBadInvalidSelector) {
|
|
|
|
EXPECT_THAT(CompileFailure("OpSwitch 12"),
|
|
|
|
Eq("Expected id to start with %."));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchBadMissingDefault) {
|
|
|
|
EXPECT_THAT(CompileFailure("OpSwitch %selector"),
|
2022-03-28 14:46:39 +00:00
|
|
|
Eq("Expected operand for OpSwitch instruction, but found the end "
|
|
|
|
"of the stream."));
|
2015-09-28 19:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchBadInvalidDefault) {
|
|
|
|
EXPECT_THAT(CompileFailure("OpSwitch %selector 12"),
|
|
|
|
Eq("Expected id to start with %."));
|
|
|
|
}
|
|
|
|
|
2015-11-05 22:45:09 +00:00
|
|
|
TEST_F(TextToBinaryTest, SwitchBadInvalidLiteral) {
|
2015-09-28 19:02:21 +00:00
|
|
|
// The assembler recognizes "OpSwitch %selector %default" as a complete
|
2015-11-05 22:45:09 +00:00
|
|
|
// instruction. Then it tries to parse "%abc" as the start of a new
|
|
|
|
// instruction, but can't since it hits the end of stream.
|
2015-10-05 17:28:46 +00:00
|
|
|
const auto input = R"(%i32 = OpTypeInt 32 0
|
|
|
|
%selector = OpConstant %i32 42
|
|
|
|
OpSwitch %selector %default %abc)";
|
|
|
|
EXPECT_THAT(CompileFailure(input), Eq("Expected '=', found end of stream."));
|
2015-09-28 19:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, SwitchBadMissingTarget) {
|
2015-09-29 15:28:34 +00:00
|
|
|
EXPECT_THAT(CompileFailure("%1 = OpTypeInt 32 0\n"
|
|
|
|
"%2 = OpConstant %1 52\n"
|
|
|
|
"OpSwitch %2 %default 12"),
|
2022-03-28 14:46:39 +00:00
|
|
|
Eq("Expected operand for OpSwitch instruction, but found the end "
|
|
|
|
"of the stream."));
|
2015-09-28 19:02:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 17:28:46 +00:00
|
|
|
// A test case for an OpSwitch.
|
|
|
|
// It is also parameterized to test encodings OpConstant
|
|
|
|
// integer literals. This can capture both single and multi-word
|
|
|
|
// integer literal tests.
|
2015-10-13 16:46:13 +00:00
|
|
|
struct SwitchTestCase {
|
2015-09-29 15:28:34 +00:00
|
|
|
std::string constant_type_args;
|
2015-10-05 17:28:46 +00:00
|
|
|
std::string constant_value_arg;
|
|
|
|
std::string case_value_arg;
|
2015-09-29 15:28:34 +00:00
|
|
|
std::vector<uint32_t> expected_instructions;
|
|
|
|
};
|
|
|
|
|
2015-10-13 16:46:13 +00:00
|
|
|
using OpSwitchValidTest =
|
2016-04-27 20:22:15 +00:00
|
|
|
spvtest::TextToBinaryTestBase<TestWithParam<SwitchTestCase>>;
|
2015-09-29 15:28:34 +00:00
|
|
|
|
2015-10-05 17:28:46 +00:00
|
|
|
// Tests the encoding of OpConstant literal values, and also
|
|
|
|
// the literal integer cases in an OpSwitch. This can
|
|
|
|
// test both single and multi-word integer literal encodings.
|
2015-10-13 16:46:13 +00:00
|
|
|
TEST_P(OpSwitchValidTest, ValidTypes) {
|
|
|
|
const std::string input = "%1 = OpTypeInt " + GetParam().constant_type_args +
|
|
|
|
"\n"
|
2015-10-05 17:28:46 +00:00
|
|
|
"%2 = OpConstant %1 " +
|
|
|
|
GetParam().constant_value_arg +
|
|
|
|
"\n"
|
|
|
|
"OpSwitch %2 %default " +
|
|
|
|
GetParam().case_value_arg + " %4\n";
|
2015-09-29 15:28:34 +00:00
|
|
|
std::vector<uint32_t> instructions;
|
|
|
|
EXPECT_THAT(CompiledInstructions(input),
|
|
|
|
Eq(GetParam().expected_instructions));
|
|
|
|
}
|
2015-10-05 17:28:46 +00:00
|
|
|
|
|
|
|
// Constructs a SwitchTestCase from the given integer_width, signedness,
|
|
|
|
// constant value string, and expected encoded constant.
|
|
|
|
SwitchTestCase MakeSwitchTestCase(uint32_t integer_width,
|
|
|
|
uint32_t integer_signedness,
|
|
|
|
std::string constant_str,
|
|
|
|
std::vector<uint32_t> encoded_constant,
|
|
|
|
std::string case_value_str,
|
|
|
|
std::vector<uint32_t> encoded_case_value) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << integer_width << " " << integer_signedness;
|
|
|
|
return SwitchTestCase{
|
|
|
|
ss.str(),
|
|
|
|
constant_str,
|
|
|
|
case_value_str,
|
|
|
|
{Concatenate(
|
2022-11-04 21:27:10 +00:00
|
|
|
{MakeInstruction(spv::Op::OpTypeInt,
|
2015-10-05 17:28:46 +00:00
|
|
|
{1, integer_width, integer_signedness}),
|
2022-11-04 21:27:10 +00:00
|
|
|
MakeInstruction(spv::Op::OpConstant,
|
2015-10-05 17:28:46 +00:00
|
|
|
Concatenate({{1, 2}, encoded_constant})),
|
2022-11-04 21:27:10 +00:00
|
|
|
MakeInstruction(spv::Op::OpSwitch,
|
2015-10-05 17:28:46 +00:00
|
|
|
Concatenate({{2, 3}, encoded_case_value, {4}}))})}};
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2015-10-05 17:28:46 +00:00
|
|
|
TextToBinaryOpSwitchValid1Word, OpSwitchValidTest,
|
2016-04-27 20:22:15 +00:00
|
|
|
ValuesIn(std::vector<SwitchTestCase>({
|
2015-10-05 17:28:46 +00:00
|
|
|
MakeSwitchTestCase(32, 0, "42", {42}, "100", {100}),
|
|
|
|
MakeSwitchTestCase(32, 1, "-1", {0xffffffff}, "100", {100}),
|
|
|
|
// SPIR-V 1.0 Rev 1 clarified that for an integer narrower than 32-bits,
|
|
|
|
// its bits will appear in the lower order bits of the 32-bit word, and
|
|
|
|
// a signed integer is sign-extended.
|
|
|
|
MakeSwitchTestCase(7, 0, "127", {127}, "100", {100}),
|
|
|
|
MakeSwitchTestCase(14, 0, "99", {99}, "100", {100}),
|
|
|
|
MakeSwitchTestCase(16, 0, "65535", {65535}, "100", {100}),
|
|
|
|
MakeSwitchTestCase(16, 1, "101", {101}, "100", {100}),
|
|
|
|
// Demonstrate sign extension
|
|
|
|
MakeSwitchTestCase(16, 1, "-2", {0xfffffffe}, "100", {100}),
|
|
|
|
// Hex cases
|
|
|
|
MakeSwitchTestCase(16, 1, "0x7ffe", {0x7ffe}, "0x1234", {0x1234}),
|
|
|
|
MakeSwitchTestCase(16, 1, "0x8000", {0xffff8000}, "0x8100",
|
|
|
|
{0xffff8100}),
|
|
|
|
MakeSwitchTestCase(16, 0, "0x8000", {0x00008000}, "0x8100", {0x8100}),
|
2019-01-29 23:56:52 +00:00
|
|
|
})));
|
2015-10-05 17:28:46 +00:00
|
|
|
|
|
|
|
// NB: The words LOW ORDER bits show up first.
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2015-10-05 17:28:46 +00:00
|
|
|
TextToBinaryOpSwitchValid2Words, OpSwitchValidTest,
|
2016-04-27 20:22:15 +00:00
|
|
|
ValuesIn(std::vector<SwitchTestCase>({
|
2015-10-05 17:28:46 +00:00
|
|
|
MakeSwitchTestCase(33, 0, "101", {101, 0}, "500", {500, 0}),
|
|
|
|
MakeSwitchTestCase(48, 1, "-1", {0xffffffff, 0xffffffff}, "900",
|
|
|
|
{900, 0}),
|
|
|
|
MakeSwitchTestCase(64, 1, "-2", {0xfffffffe, 0xffffffff}, "-5",
|
|
|
|
{0xfffffffb, uint32_t(-1)}),
|
|
|
|
// Hex cases
|
|
|
|
MakeSwitchTestCase(48, 1, "0x7fffffffffff", {0xffffffff, 0x00007fff},
|
|
|
|
"100", {100, 0}),
|
|
|
|
MakeSwitchTestCase(48, 1, "0x800000000000", {0x00000000, 0xffff8000},
|
|
|
|
"0x800000000000", {0x00000000, 0xffff8000}),
|
|
|
|
MakeSwitchTestCase(48, 0, "0x800000000000", {0x00000000, 0x00008000},
|
|
|
|
"0x800000000000", {0x00000000, 0x00008000}),
|
|
|
|
MakeSwitchTestCase(63, 0, "0x500000000", {0, 5}, "12", {12, 0}),
|
2015-10-15 19:20:45 +00:00
|
|
|
MakeSwitchTestCase(64, 0, "0x600000000", {0, 6}, "12", {12, 0}),
|
|
|
|
MakeSwitchTestCase(64, 1, "0x700000123", {0x123, 7}, "12", {12, 0}),
|
2019-01-29 23:56:52 +00:00
|
|
|
})));
|
2015-09-29 15:28:34 +00:00
|
|
|
|
2020-06-10 15:11:46 +00:00
|
|
|
using ControlFlowRoundTripTest = RoundTripTest;
|
|
|
|
|
|
|
|
TEST_P(ControlFlowRoundTripTest, DisassemblyEqualsAssemblyInput) {
|
|
|
|
const std::string assembly = GetParam();
|
|
|
|
EXPECT_THAT(EncodeAndDecodeSuccessfully(assembly), Eq(assembly)) << assembly;
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2020-06-10 15:11:46 +00:00
|
|
|
OpSwitchRoundTripUnsignedIntegers, ControlFlowRoundTripTest,
|
2016-04-27 20:22:15 +00:00
|
|
|
ValuesIn(std::vector<std::string>({
|
2015-10-14 18:40:24 +00:00
|
|
|
// Unsigned 16-bit.
|
|
|
|
"%1 = OpTypeInt 16 0\n%2 = OpConstant %1 65535\nOpSwitch %2 %3\n",
|
|
|
|
// Unsigned 32-bit, three non-default cases.
|
|
|
|
"%1 = OpTypeInt 32 0\n%2 = OpConstant %1 123456\n"
|
|
|
|
"OpSwitch %2 %3 100 %4 102 %5 1000000 %6\n",
|
|
|
|
// Unsigned 48-bit, three non-default cases.
|
|
|
|
"%1 = OpTypeInt 48 0\n%2 = OpConstant %1 5000000000\n"
|
|
|
|
"OpSwitch %2 %3 100 %4 102 %5 6000000000 %6\n",
|
|
|
|
// Unsigned 64-bit, three non-default cases.
|
|
|
|
"%1 = OpTypeInt 64 0\n%2 = OpConstant %1 9223372036854775807\n"
|
|
|
|
"OpSwitch %2 %3 100 %4 102 %5 9000000000000000000 %6\n",
|
2019-01-29 23:56:52 +00:00
|
|
|
})));
|
2015-10-14 18:40:24 +00:00
|
|
|
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2020-06-10 15:11:46 +00:00
|
|
|
OpSwitchRoundTripSignedIntegers, ControlFlowRoundTripTest,
|
2016-04-27 20:22:15 +00:00
|
|
|
ValuesIn(std::vector<std::string>{
|
2015-10-14 18:40:24 +00:00
|
|
|
// Signed 16-bit, with two non-default cases
|
|
|
|
"%1 = OpTypeInt 16 1\n%2 = OpConstant %1 32767\n"
|
|
|
|
"OpSwitch %2 %3 99 %4 -102 %5\n",
|
|
|
|
"%1 = OpTypeInt 16 1\n%2 = OpConstant %1 -32768\n"
|
|
|
|
"OpSwitch %2 %3 99 %4 -102 %5\n",
|
|
|
|
// Signed 32-bit, two non-default cases.
|
|
|
|
"%1 = OpTypeInt 32 1\n%2 = OpConstant %1 -123456\n"
|
2015-11-10 20:43:12 +00:00
|
|
|
"OpSwitch %2 %3 100 %4 -123456 %5\n",
|
2015-10-14 18:40:24 +00:00
|
|
|
"%1 = OpTypeInt 32 1\n%2 = OpConstant %1 123456\n"
|
2015-11-10 20:43:12 +00:00
|
|
|
"OpSwitch %2 %3 100 %4 123456 %5\n",
|
2015-10-14 18:40:24 +00:00
|
|
|
// Signed 48-bit, three non-default cases.
|
|
|
|
"%1 = OpTypeInt 48 1\n%2 = OpConstant %1 5000000000\n"
|
|
|
|
"OpSwitch %2 %3 100 %4 -7000000000 %5 6000000000 %6\n",
|
|
|
|
"%1 = OpTypeInt 48 1\n%2 = OpConstant %1 -5000000000\n"
|
|
|
|
"OpSwitch %2 %3 100 %4 -7000000000 %5 6000000000 %6\n",
|
|
|
|
// Signed 64-bit, three non-default cases.
|
|
|
|
"%1 = OpTypeInt 64 1\n%2 = OpConstant %1 9223372036854775807\n"
|
|
|
|
"OpSwitch %2 %3 100 %4 7000000000 %5 -1000000000000000000 %6\n",
|
|
|
|
"%1 = OpTypeInt 64 1\n%2 = OpConstant %1 -9223372036854775808\n"
|
|
|
|
"OpSwitch %2 %3 100 %4 7000000000 %5 -1000000000000000000 %6\n",
|
2019-01-29 23:56:52 +00:00
|
|
|
}));
|
2015-10-14 18:40:24 +00:00
|
|
|
|
2015-10-13 16:46:13 +00:00
|
|
|
using OpSwitchInvalidTypeTestCase =
|
2016-04-27 20:22:15 +00:00
|
|
|
spvtest::TextToBinaryTestBase<TestWithParam<std::string>>;
|
2015-09-29 15:28:34 +00:00
|
|
|
|
2015-10-13 16:46:13 +00:00
|
|
|
TEST_P(OpSwitchInvalidTypeTestCase, InvalidTypes) {
|
|
|
|
const std::string input =
|
|
|
|
"%1 = " + GetParam() +
|
|
|
|
"\n"
|
|
|
|
"%3 = OpCopyObject %1 %2\n" // We only care the type of the expression
|
2019-09-11 17:15:25 +00:00
|
|
|
" OpSwitch %3 %default 32 %c\n";
|
2015-09-29 15:28:34 +00:00
|
|
|
EXPECT_THAT(CompileFailure(input),
|
2015-10-22 17:24:41 +00:00
|
|
|
Eq("The selector operand for OpSwitch must be the result of an "
|
|
|
|
"instruction that generates an integer scalar"));
|
2015-09-29 15:28:34 +00:00
|
|
|
}
|
2015-10-22 17:24:41 +00:00
|
|
|
|
2015-09-29 15:28:34 +00:00
|
|
|
// clang-format off
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2015-09-29 15:28:34 +00:00
|
|
|
TextToBinaryOpSwitchInvalidTests, OpSwitchInvalidTypeTestCase,
|
2016-04-27 20:22:15 +00:00
|
|
|
ValuesIn(std::vector<std::string>{
|
2015-09-29 15:28:34 +00:00
|
|
|
{"OpTypeVoid",
|
|
|
|
"OpTypeBool",
|
|
|
|
"OpTypeFloat 32",
|
|
|
|
"OpTypeVector %a 32",
|
|
|
|
"OpTypeMatrix %a 32",
|
|
|
|
"OpTypeImage %a 1D 0 0 0 0 Unknown",
|
|
|
|
"OpTypeSampler",
|
|
|
|
"OpTypeSampledImage %a",
|
|
|
|
"OpTypeArray %a %b",
|
|
|
|
"OpTypeRuntimeArray %a",
|
|
|
|
"OpTypeStruct %a",
|
|
|
|
"OpTypeOpaque \"Foo\"",
|
|
|
|
"OpTypePointer UniformConstant %a",
|
|
|
|
"OpTypeFunction %a %b",
|
|
|
|
"OpTypeEvent",
|
|
|
|
"OpTypeDeviceEvent",
|
|
|
|
"OpTypeReserveId",
|
|
|
|
"OpTypeQueue",
|
|
|
|
"OpTypePipe ReadOnly",
|
2019-09-11 17:15:25 +00:00
|
|
|
|
2022-01-26 20:13:08 +00:00
|
|
|
// Skip OpTypeForwardPointer because it doesn't even produce a result
|
2019-09-11 17:15:25 +00:00
|
|
|
// ID.
|
|
|
|
|
|
|
|
// At least one thing that isn't a type at all
|
2015-09-29 15:28:34 +00:00
|
|
|
"OpNot %a %b"
|
|
|
|
},
|
2019-01-29 23:56:52 +00:00
|
|
|
}));
|
2015-09-29 15:28:34 +00:00
|
|
|
// clang-format on
|
2015-09-28 19:02:21 +00:00
|
|
|
|
2020-07-22 15:45:02 +00:00
|
|
|
using OpKillTest = spvtest::TextToBinaryTest;
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(OpKillTest, ControlFlowRoundTripTest,
|
|
|
|
Values("OpKill\n"));
|
|
|
|
|
|
|
|
TEST_F(OpKillTest, ExtraArgsAssemblyError) {
|
|
|
|
const std::string input = "OpKill 1";
|
|
|
|
EXPECT_THAT(CompileFailure(input),
|
|
|
|
Eq("Expected <opcode> or <result-id> at the beginning of an "
|
|
|
|
"instruction, found '1'."));
|
|
|
|
}
|
|
|
|
|
|
|
|
using OpTerminateInvocationTest = spvtest::TextToBinaryTest;
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(OpTerminateInvocationTest, ControlFlowRoundTripTest,
|
|
|
|
Values("OpTerminateInvocation\n"));
|
|
|
|
|
|
|
|
TEST_F(OpTerminateInvocationTest, ExtraArgsAssemblyError) {
|
|
|
|
const std::string input = "OpTerminateInvocation 1";
|
|
|
|
EXPECT_THAT(CompileFailure(input),
|
|
|
|
Eq("Expected <opcode> or <result-id> at the beginning of an "
|
|
|
|
"instruction, found '1'."));
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:28:33 +00:00
|
|
|
// TODO(dneto): OpPhi
|
|
|
|
// TODO(dneto): OpLoopMerge
|
|
|
|
// TODO(dneto): OpLabel
|
|
|
|
// TODO(dneto): OpBranch
|
|
|
|
// TODO(dneto): OpSwitch
|
|
|
|
// TODO(dneto): OpReturn
|
|
|
|
// TODO(dneto): OpReturnValue
|
|
|
|
// TODO(dneto): OpUnreachable
|
|
|
|
// TODO(dneto): OpLifetimeStart
|
|
|
|
// TODO(dneto): OpLifetimeStop
|
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|