2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 17:26:19 +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-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 17:26:19 +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-05-22 17:26:19 +00:00
|
|
|
|
2015-11-06 23:08:49 +00:00
|
|
|
#include <algorithm>
|
2016-03-31 16:16:51 +00:00
|
|
|
#include <cstring>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2015-11-06 23:08:49 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-11-02 16:40:59 +00:00
|
|
|
#include "gmock/gmock.h"
|
2015-12-22 20:15:46 +00:00
|
|
|
#include "source/spirv_constant.h"
|
2016-02-17 19:44:00 +00:00
|
|
|
#include "source/util/bitutils.h"
|
|
|
|
#include "source/util/hex_float.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "test/test_fixture.h"
|
|
|
|
#include "test/unit_spirv.h"
|
2015-10-23 13:53:58 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2015-08-31 19:24:32 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-10-01 20:58:17 +00:00
|
|
|
using spvtest::AutoText;
|
2015-11-06 16:23:57 +00:00
|
|
|
using spvtest::Concatenate;
|
|
|
|
using spvtest::MakeInstruction;
|
2016-04-21 19:46:08 +00:00
|
|
|
using spvtest::ScopedContext;
|
2015-11-06 16:23:57 +00:00
|
|
|
using spvtest::TextToBinaryTest;
|
2015-11-02 16:40:59 +00:00
|
|
|
using testing::Eq;
|
2016-03-31 16:16:51 +00:00
|
|
|
using testing::IsNull;
|
|
|
|
using testing::NotNull;
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2015-09-16 22:32:54 +00:00
|
|
|
// An mask parsing test case.
|
|
|
|
struct MaskCase {
|
2015-09-18 15:50:54 +00:00
|
|
|
spv_operand_type_t which_enum;
|
|
|
|
uint32_t expected_value;
|
2015-09-18 16:08:36 +00:00
|
|
|
const char* expression;
|
2015-09-16 22:32:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using GoodMaskParseTest = ::testing::TestWithParam<MaskCase>;
|
|
|
|
|
|
|
|
TEST_P(GoodMaskParseTest, GoodMaskExpressions) {
|
2016-03-31 16:16:51 +00:00
|
|
|
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
|
2015-09-16 22:32:54 +00:00
|
|
|
|
|
|
|
uint32_t value;
|
2015-11-12 18:48:30 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS,
|
|
|
|
AssemblyGrammar(context).parseMaskOperand(
|
|
|
|
GetParam().which_enum, GetParam().expression, &value));
|
2015-09-16 22:32:54 +00:00
|
|
|
EXPECT_EQ(GetParam().expected_value, value);
|
2015-11-12 18:48:30 +00:00
|
|
|
|
|
|
|
spvContextDestroy(context);
|
2015-09-16 22:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
|
|
ParseMask, GoodMaskParseTest,
|
|
|
|
::testing::ValuesIn(std::vector<MaskCase>{
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 1, "NotNaN"},
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 2, "NotInf"},
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotNaN|NotInf"},
|
|
|
|
// Mask experssions are symmetric.
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN"},
|
|
|
|
// Repeating a value has no effect.
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN|NotInf"},
|
|
|
|
// Using 3 operands still works.
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0x13, "NotInf|NotNaN|Fast"},
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 1, "Flatten"},
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 2, "DontFlatten"},
|
|
|
|
// Weirdly, you can specify to flatten and don't flatten a selection.
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 3, "Flatten|DontFlatten"},
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 1, "Unroll"},
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 2, "DontUnroll"},
|
|
|
|
// Weirdly, you can specify to unroll and don't unroll a loop.
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 3, "Unroll|DontUnroll"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 1, "Inline"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 2, "DontInline"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 4, "Pure"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 8, "Const"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0xd, "Inline|Const|Pure"},
|
2016-03-31 16:16:51 +00:00
|
|
|
}), );
|
2015-09-16 22:32:54 +00:00
|
|
|
|
|
|
|
using BadFPFastMathMaskParseTest = ::testing::TestWithParam<const char*>;
|
|
|
|
|
|
|
|
TEST_P(BadFPFastMathMaskParseTest, BadMaskExpressions) {
|
2016-03-31 16:16:51 +00:00
|
|
|
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
|
2015-09-16 22:32:54 +00:00
|
|
|
|
|
|
|
uint32_t value;
|
2015-09-24 14:26:51 +00:00
|
|
|
EXPECT_NE(SPV_SUCCESS,
|
2015-11-12 18:48:30 +00:00
|
|
|
AssemblyGrammar(context).parseMaskOperand(
|
|
|
|
SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, GetParam(), &value));
|
|
|
|
|
|
|
|
spvContextDestroy(context);
|
2015-09-16 22:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(ParseMask, BadFPFastMathMaskParseTest,
|
|
|
|
::testing::ValuesIn(std::vector<const char*>{
|
|
|
|
nullptr, "", "NotValidEnum", "|", "NotInf|",
|
|
|
|
"|NotInf", "NotInf||NotNaN",
|
|
|
|
"Unroll" // A good word, but for the wrong enum
|
2016-03-31 16:16:51 +00:00
|
|
|
}), );
|
2015-09-16 22:32:54 +00:00
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
TEST_F(TextToBinaryTest, InvalidText) {
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_TEXT,
|
2016-04-19 02:25:35 +00:00
|
|
|
spvTextToBinary(ScopedContext().context, nullptr, 0, &binary,
|
|
|
|
&diagnostic));
|
2015-11-02 16:40:59 +00:00
|
|
|
EXPECT_NE(nullptr, diagnostic);
|
|
|
|
EXPECT_THAT(diagnostic->error, Eq(std::string("Missing assembly text.")));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, InvalidPointer) {
|
2015-09-10 21:07:21 +00:00
|
|
|
SetText(
|
|
|
|
"OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
|
2016-04-19 02:25:35 +00:00
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
|
|
|
|
spvTextToBinary(ScopedContext().context, text.str, text.length,
|
|
|
|
nullptr, &diagnostic));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, InvalidPrefix) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
"Expected <opcode> or <result-id> at the beginning of an instruction, "
|
|
|
|
"found 'Invalid'.",
|
|
|
|
CompileFailure("Invalid"));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 16:40:59 +00:00
|
|
|
TEST_F(TextToBinaryTest, EmptyAssemblyString) {
|
|
|
|
// An empty assembly module is valid!
|
|
|
|
// It should produce a valid module with zero instructions.
|
|
|
|
EXPECT_THAT(CompiledInstructions(""), Eq(std::vector<uint32_t>{}));
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
TEST_F(TextToBinaryTest, StringSpace) {
|
2015-11-12 15:45:36 +00:00
|
|
|
const std::string code = ("OpSourceExtension \"string with spaces\"\n");
|
|
|
|
EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
2015-08-21 15:50:55 +00:00
|
|
|
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
TEST_F(TextToBinaryTest, UnknownBeginningOfInstruction) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ(
|
2015-08-21 15:50:55 +00:00
|
|
|
"Expected <opcode> or <result-id> at the beginning of an instruction, "
|
|
|
|
"found 'Google'.",
|
2015-11-12 15:45:36 +00:00
|
|
|
CompileFailure(
|
|
|
|
"\nOpSource OpenCL_C 12\nOpMemoryModel Physical64 OpenCL\nGoogle\n"));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(4u, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(1u, diagnostic->position.column + 1);
|
2015-08-21 15:50:55 +00:00
|
|
|
}
|
2015-08-21 15:52:03 +00:00
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, NoEqualSign) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ("Expected '=', found end of stream.",
|
|
|
|
CompileFailure("\nOpSource OpenCL_C 12\n"
|
|
|
|
"OpMemoryModel Physical64 OpenCL\n%2\n"));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(5u, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(1u, diagnostic->position.column + 1);
|
2015-08-21 15:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, NoOpCode) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ("Expected opcode, found end of stream.",
|
|
|
|
CompileFailure("\nOpSource OpenCL_C 12\n"
|
|
|
|
"OpMemoryModel Physical64 OpenCL\n%2 =\n"));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(5u, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(1u, diagnostic->position.column + 1);
|
2015-08-21 15:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, WrongOpCode) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ("Invalid Opcode prefix 'Wahahaha'.",
|
|
|
|
CompileFailure("\nOpSource OpenCL_C 12\n"
|
|
|
|
"OpMemoryModel Physical64 OpenCL\n%2 = Wahahaha\n"));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(4u, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(6u, diagnostic->position.column + 1);
|
2015-08-21 15:52:03 +00:00
|
|
|
}
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2016-04-01 04:47:02 +00:00
|
|
|
TEST_F(TextToBinaryTest, CRLF) {
|
|
|
|
const std::string input =
|
|
|
|
"%i32 = OpTypeInt 32 1\r\n%c = OpConstant %i32 123\r\n";
|
|
|
|
EXPECT_THAT(CompiledInstructions(input),
|
|
|
|
Eq(Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
|
|
|
|
MakeInstruction(SpvOpConstant, {1, 2, 123})})));
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
using TextToBinaryFloatValueTest = spvtest::TextToBinaryTestBase<
|
2015-09-09 14:36:48 +00:00
|
|
|
::testing::TestWithParam<std::pair<std::string, uint32_t>>>;
|
|
|
|
|
2015-11-06 16:23:57 +00:00
|
|
|
TEST_P(TextToBinaryFloatValueTest, Samples) {
|
|
|
|
const std::string input =
|
|
|
|
"%1 = OpTypeFloat 32\n%2 = OpConstant %1 " + GetParam().first;
|
|
|
|
EXPECT_THAT(CompiledInstructions(input),
|
|
|
|
Eq(Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
|
|
|
|
MakeInstruction(SpvOpConstant,
|
|
|
|
{1, 2, GetParam().second})})));
|
2015-09-04 18:55:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:36:48 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
|
|
FloatValues, TextToBinaryFloatValueTest,
|
|
|
|
::testing::ValuesIn(std::vector<std::pair<std::string, uint32_t>>{
|
|
|
|
{"0.0", 0x00000000}, // +0
|
|
|
|
{"!0x00000001", 0x00000001}, // +denorm
|
|
|
|
{"!0x00800000", 0x00800000}, // +norm
|
|
|
|
{"1.5", 0x3fc00000},
|
|
|
|
{"!0x7f800000", 0x7f800000}, // +inf
|
|
|
|
{"!0x7f800001", 0x7f800001}, // NaN
|
|
|
|
|
|
|
|
{"-0.0", 0x80000000}, // -0
|
|
|
|
{"!0x80000001", 0x80000001}, // -denorm
|
|
|
|
{"!0x80800000", 0x80800000}, // -norm
|
|
|
|
{"-2.5", 0xc0200000},
|
|
|
|
{"!0xff800000", 0xff800000}, // -inf
|
|
|
|
{"!0xff800001", 0xff800001}, // NaN
|
2016-03-31 16:16:51 +00:00
|
|
|
}), );
|
2015-09-04 18:55:28 +00:00
|
|
|
|
2016-01-08 14:54:42 +00:00
|
|
|
using TextToBinaryHalfValueTest = spvtest::TextToBinaryTestBase<
|
|
|
|
::testing::TestWithParam<std::pair<std::string, uint32_t>>>;
|
|
|
|
|
|
|
|
TEST_P(TextToBinaryHalfValueTest, Samples) {
|
|
|
|
const std::string input =
|
|
|
|
"%1 = OpTypeFloat 16\n%2 = OpConstant %1 " + GetParam().first;
|
|
|
|
EXPECT_THAT(CompiledInstructions(input),
|
|
|
|
Eq(Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 16}),
|
|
|
|
MakeInstruction(SpvOpConstant,
|
|
|
|
{1, 2, GetParam().second})})));
|
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
|
|
HalfValues, TextToBinaryHalfValueTest,
|
|
|
|
::testing::ValuesIn(std::vector<std::pair<std::string, uint32_t>>{
|
|
|
|
{"0.0", 0x00000000},
|
|
|
|
{"1.0", 0x00003c00},
|
2016-02-06 18:48:05 +00:00
|
|
|
{"1.000844", 0x00003c00}, // Truncate to 1.0
|
|
|
|
{"1.000977", 0x00003c01}, // Don't have to truncate
|
|
|
|
{"1.001465", 0x00003c01}, // Truncate to 1.0000977
|
2016-01-08 14:54:42 +00:00
|
|
|
{"1.5", 0x00003e00},
|
|
|
|
{"-1.0", 0x0000bc00},
|
|
|
|
{"2.0", 0x00004000},
|
|
|
|
{"-2.0", 0x0000c000},
|
|
|
|
{"0x1p1", 0x00004000},
|
|
|
|
{"-0x1p1", 0x0000c000},
|
|
|
|
{"0x1.8p1", 0x00004200},
|
|
|
|
{"0x1.8p4", 0x00004e00},
|
|
|
|
{"0x1.801p4", 0x00004e00},
|
|
|
|
{"0x1.804p4", 0x00004e01},
|
2016-03-31 16:16:51 +00:00
|
|
|
}), );
|
2016-01-08 14:54:42 +00:00
|
|
|
|
2016-03-31 16:16:51 +00:00
|
|
|
TEST(CreateContext, InvalidEnvironment) {
|
|
|
|
spv_target_env env;
|
|
|
|
std::memset(&env, 99, sizeof(env));
|
|
|
|
EXPECT_THAT(spvContextCreate(env), IsNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CreateContext, UniversalEnvironment) {
|
|
|
|
auto c = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
|
|
|
|
EXPECT_THAT(c, NotNull());
|
|
|
|
spvContextDestroy(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CreateContext, VulkanEnvironment) {
|
|
|
|
auto c = spvContextCreate(SPV_ENV_VULKAN_1_0);
|
|
|
|
EXPECT_THAT(c, NotNull());
|
|
|
|
spvContextDestroy(c);
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|