2015-08-31 18:07:22 +00:00
|
|
|
//
|
2015-05-22 17:26:19 +00:00
|
|
|
// Copyright (c) 2015 The Khronos Group Inc.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and/or associated documentation files (the
|
|
|
|
// "Materials"), to deal in the Materials without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Materials, and to
|
|
|
|
// permit persons to whom the Materials are furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Materials.
|
|
|
|
//
|
|
|
|
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
|
|
|
|
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
|
|
|
|
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
|
|
|
|
// https://www.khronos.org/registry/
|
|
|
|
//
|
|
|
|
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
|
|
|
|
|
|
|
#include "UnitSPIRV.h"
|
|
|
|
|
2015-08-31 19:24:32 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
class BinaryToText : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
BinaryToText() : binary(), opcodeTable(nullptr), operandTable(nullptr) {}
|
|
|
|
|
|
|
|
virtual void SetUp() {
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
|
|
|
|
|
2015-08-24 19:52:26 +00:00
|
|
|
const char* textStr = R"(
|
2015-08-21 15:52:29 +00:00
|
|
|
OpSource OpenCL 12
|
|
|
|
OpMemoryModel Physical64 OpenCL
|
|
|
|
OpSourceExtension "PlaceholderExtensionName"
|
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
|
|
|
OpEntryPoint Kernel %1 "foo"
|
2015-08-21 15:52:29 +00:00
|
|
|
OpExecutionMode %1 LocalSizeHint 1 1 1
|
|
|
|
%2 = OpTypeVoid
|
|
|
|
%3 = OpTypeBool
|
|
|
|
%4 = OpTypeInt 8 0
|
|
|
|
%5 = OpTypeInt 8 1
|
|
|
|
%6 = OpTypeInt 16 0
|
|
|
|
%7 = OpTypeInt 16 1
|
|
|
|
%8 = OpTypeInt 32 0
|
|
|
|
%9 = OpTypeInt 32 1
|
|
|
|
%10 = OpTypeInt 64 0
|
|
|
|
%11 = OpTypeInt 64 1
|
|
|
|
%12 = OpTypeFloat 16
|
|
|
|
%13 = OpTypeFloat 32
|
|
|
|
%14 = OpTypeFloat 64
|
|
|
|
%15 = OpTypeVector 4 2
|
2015-05-22 17:26:19 +00:00
|
|
|
)";
|
|
|
|
spv_text_t text = {textStr, strlen(textStr)};
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
2015-09-09 17:04:32 +00:00
|
|
|
spv_result_t error =
|
|
|
|
spvTextToBinary(text.str, text.length, opcodeTable, operandTable,
|
|
|
|
extInstTable, &binary, &diagnostic);
|
2015-05-22 17:26:19 +00:00
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() { spvBinaryDestroy(binary); }
|
|
|
|
|
|
|
|
spv_binary binary;
|
|
|
|
spv_opcode_table opcodeTable;
|
|
|
|
spv_operand_table operandTable;
|
|
|
|
spv_ext_inst_table extInstTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(BinaryToText, Default) {
|
|
|
|
spv_text text = nullptr;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2015-09-09 17:04:32 +00:00
|
|
|
spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
2015-05-22 17:26:19 +00:00
|
|
|
operandTable, extInstTable, &text, &diagnostic));
|
|
|
|
printf("%s", text->str);
|
|
|
|
spvTextDestroy(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryToText, InvalidCode) {
|
|
|
|
spv_binary_t binary = {nullptr, 42};
|
|
|
|
spv_text text;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
2015-09-14 14:05:37 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
SPV_ERROR_INVALID_BINARY,
|
|
|
|
spvBinaryToText(nullptr, 42, SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
|
|
|
operandTable, extInstTable, &text, &diagnostic));
|
2015-05-22 17:26:19 +00:00
|
|
|
if (diagnostic) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryToText, InvalidTable) {
|
|
|
|
spv_text text;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_TABLE,
|
2015-09-09 17:04:32 +00:00
|
|
|
spvBinaryToText(binary->code, binary->wordCount, 0, nullptr,
|
|
|
|
operandTable, extInstTable, &text, &diagnostic));
|
2015-05-22 17:26:19 +00:00
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_TABLE,
|
2015-09-09 17:04:32 +00:00
|
|
|
spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
2015-05-22 17:26:19 +00:00
|
|
|
nullptr, extInstTable, &text, &diagnostic));
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_TABLE,
|
2015-09-09 17:04:32 +00:00
|
|
|
spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
2015-05-22 17:26:19 +00:00
|
|
|
operandTable, nullptr, &text, &diagnostic));
|
|
|
|
if (diagnostic) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryToText, InvalidDiagnostic) {
|
|
|
|
spv_text text;
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_DIAGNOSTIC,
|
2015-09-09 17:04:32 +00:00
|
|
|
spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
2015-05-22 17:26:19 +00:00
|
|
|
operandTable, extInstTable, &text, nullptr));
|
|
|
|
}
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2015-08-31 17:34:28 +00:00
|
|
|
TEST(BinaryToTextSmall, OneInstruction) {
|
|
|
|
// TODO(dneto): This test could/should be refactored.
|
|
|
|
spv_opcode_table opcodeTable;
|
|
|
|
spv_operand_table operandTable;
|
|
|
|
spv_ext_inst_table extInstTable;
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
|
|
|
|
spv_binary binary;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
2015-09-09 17:04:32 +00:00
|
|
|
const char* input = "OpSource OpenCL 12";
|
2015-08-31 17:34:28 +00:00
|
|
|
spv_result_t error =
|
2015-09-09 17:04:32 +00:00
|
|
|
spvTextToBinary(input, strlen(input), opcodeTable, operandTable,
|
2015-08-31 17:34:28 +00:00
|
|
|
extInstTable, &binary, &diagnostic);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, error);
|
2015-09-08 19:38:55 +00:00
|
|
|
spv_text text = nullptr;
|
2015-09-09 17:04:32 +00:00
|
|
|
error = spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
2015-08-31 17:34:28 +00:00
|
|
|
operandTable, extInstTable, &text, &diagnostic);
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, error);
|
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
spvTextDestroy(text);
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:07:22 +00:00
|
|
|
// Exercise the case where an operand itself has operands.
|
|
|
|
// This could detect problems in updating the expected-set-of-operands
|
|
|
|
// list.
|
|
|
|
TEST(BinaryToTextSmall, OperandWithOperands) {
|
|
|
|
spv_opcode_table opcodeTable;
|
|
|
|
spv_operand_table operandTable;
|
|
|
|
spv_ext_inst_table extInstTable;
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
|
|
|
|
spv_binary binary;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
|
|
|
|
|
|
|
AutoText input(R"(OpEntryPoint Kernel %fn "foo"
|
|
|
|
OpExecutionMode %fn LocalSizeHint 100 200 300
|
|
|
|
%void = OpTypeVoid
|
|
|
|
%fnType = OpTypeFunction %void
|
|
|
|
%fn = OpFunction %void None %fnType
|
|
|
|
)");
|
|
|
|
spv_result_t error =
|
2015-09-09 17:04:32 +00:00
|
|
|
spvTextToBinary(input.str.c_str(), input.str.length(), opcodeTable,
|
|
|
|
operandTable, extInstTable, &binary, &diagnostic);
|
2015-08-31 18:07:22 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, error);
|
2015-09-08 19:38:55 +00:00
|
|
|
spv_text text = nullptr;
|
2015-09-09 17:04:32 +00:00
|
|
|
error = spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
2015-08-31 18:07:22 +00:00
|
|
|
operandTable, extInstTable, &text, &diagnostic);
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, error);
|
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
spvTextDestroy(text);
|
|
|
|
}
|
|
|
|
|
2015-09-14 19:22:23 +00:00
|
|
|
TEST(BinaryToTextSmall, LiteralInt64) {
|
|
|
|
spv_opcode_table opcodeTable;
|
|
|
|
spv_operand_table operandTable;
|
|
|
|
spv_ext_inst_table extInstTable;
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
|
|
|
|
spv_binary binary;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
|
|
|
|
|
|
|
AutoText input("%1 = OpTypeInt 64 0\n%2 = OpConstant %1 123456789021\n");
|
|
|
|
spv_result_t error =
|
|
|
|
spvTextToBinary(input.str.c_str(), input.str.length(), opcodeTable,
|
|
|
|
operandTable, extInstTable, &binary, &diagnostic);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, error);
|
|
|
|
spv_text text = nullptr;
|
|
|
|
error = spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
|
|
|
operandTable, extInstTable, &text, &diagnostic);
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, error);
|
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
const std::string header =
|
|
|
|
"; SPIR-V\n; Version: 99\n; Generator: Khronos\n; "
|
|
|
|
"Bound: 3\n; Schema: 0\n";
|
|
|
|
EXPECT_EQ(header + input.str, text->str);
|
|
|
|
spvTextDestroy(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BinaryToTextSmall, LiteralDouble) {
|
|
|
|
spv_opcode_table opcodeTable;
|
|
|
|
spv_operand_table operandTable;
|
|
|
|
spv_ext_inst_table extInstTable;
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
|
|
|
|
spv_binary binary;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
|
|
|
|
|
|
|
// Pi: 3.1415926535897930 => 0x400921fb54442d18 => 4614256656552045848
|
|
|
|
AutoText input(
|
|
|
|
"%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 3.1415926535897930");
|
|
|
|
spv_result_t error =
|
|
|
|
spvTextToBinary(input.str.c_str(), input.str.length(), opcodeTable,
|
|
|
|
operandTable, extInstTable, &binary, &diagnostic);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, error);
|
|
|
|
spv_text text = nullptr;
|
|
|
|
error = spvBinaryToText(binary->code, binary->wordCount,
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, opcodeTable,
|
|
|
|
operandTable, extInstTable, &text, &diagnostic);
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, error);
|
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
const std::string output =
|
|
|
|
R"(; SPIR-V
|
|
|
|
; Version: 99
|
|
|
|
; Generator: Khronos
|
|
|
|
; Bound: 3
|
|
|
|
; Schema: 0
|
|
|
|
%1 = OpTypeFloat 64
|
|
|
|
%2 = OpSpecConstant %1 4614256656552045848
|
|
|
|
)";
|
|
|
|
EXPECT_EQ(output, text->str);
|
|
|
|
spvTextDestroy(text);
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:24:32 +00:00
|
|
|
} // anonymous namespace
|