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
This commit is contained in:
David Neto 2015-08-27 13:03:52 -04:00
parent 6c03f87490
commit 78c3b43774
23 changed files with 1311 additions and 1837 deletions

View File

@ -166,6 +166,7 @@ if (TARGET gmock)
${CMAKE_CURRENT_SOURCE_DIR}/test/ImmediateInt.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/LibspirvMacros.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/NamedId.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/OperandPattern.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeIsVariable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeMake.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeRequiresCapabilities.cpp
@ -175,6 +176,7 @@ if (TARGET gmock)
${CMAKE_CURRENT_SOURCE_DIR}/test/TextAdvance.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/TextDestroy.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/TextLiteral.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/TextStartsNewInst.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/TextWordGet.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.cpp

View File

@ -0,0 +1,94 @@
//
//Copyright (C) 2014-2015 LunarG, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
// Author: John Kessenich, LunarG
//
// The contents of this file were extracted from the SPIR-V spec-generating
// code.
namespace spv {
// Classify the kinds of logical operands of an instruction.
enum OperandClass {
OperandNone,
OperandId,
OperandOptionalId,
OperandOptionalImage,
OperandVariableIds,
OperandOptionalLiteral,
OperandOptionalLiteralString,
OperandVariableLiterals,
OperandVariableIdLiteral,
OperandVariableLiteralId, // zero or more pairs of (Literal, Id)
OperandLiteralNumber,
OperandLiteralString,
OperandSource,
OperandExecutionModel,
OperandAddressing,
OperandMemory,
OperandExecutionMode,
OperandStorage,
OperandDimensionality,
OperandSamplerAddressingMode,
OperandSamplerFilterMode,
OperandSamplerImageFormat,
OperandImageChannelOrder,
OperandImageChannelDataType,
OperandImageOperands,
OperandFPFastMath,
OperandFPRoundingMode,
OperandLinkageType,
OperandAccessQualifier,
OperandFuncParamAttr,
OperandDecoration,
OperandBuiltIn,
OperandSelect,
OperandLoop,
OperandFunction,
OperandMemorySemantics,
OperandMemoryAccess,
OperandScope,
OperandGroupOperation,
OperandKernelEnqueueFlags,
OperandKernelProfilingInfo,
OperandCapability,
OperandOpcode,
OperandCount
};
} // namespace spv

View File

@ -28,6 +28,7 @@
#define _CODEPLAY_SPIRV_SPIRV_H_
#include <headers/spirv.hpp>
#include <headers/spirv_operands.hpp>
#include <headers/GLSL.std.450.h>
#include <headers/OpenCLLib.h>
@ -139,11 +140,25 @@ typedef enum spv_opcode_flags_t {
SPV_FORCE_32_BIT_ENUM(spv_opcode_flags_t)
} spv_opcode_flags_t;
// The kinds of operands that an instruction may have.
//
// Sometimes an operand kind is very specific, e.g. SPV_OPERAND_TYPE_RESULT_ID
// for a result ID in a value-generating instruction.
// Other times they represent several options, e.g. a SPV_OPERAND_TYPE_LITERAL
// could either be a literal number or a literal string, depending on context.
//
// In addition to determining what kind of value an operand may be, certain
// enums capture the fact that an operand might be optional (may be absent,
// or present exactly once), or might occure zero or more times.
//
// Sometimes we also need to be able to express the fact that an operand
// is a member of an optional tuple of values. In that case the first member
// would be optional, and the subsequent members would be required.
typedef enum spv_operand_type_t {
SPV_OPERAND_TYPE_NONE,
SPV_OPERAND_TYPE_NONE = 0,
SPV_OPERAND_TYPE_ID,
SPV_OPERAND_TYPE_RESULT_ID,
SPV_OPERAND_TYPE_LITERAL,
SPV_OPERAND_TYPE_LITERAL, // Either a literal number or literal string
SPV_OPERAND_TYPE_LITERAL_NUMBER,
SPV_OPERAND_TYPE_LITERAL_STRING,
SPV_OPERAND_TYPE_SOURCE_LANGUAGE,
@ -166,13 +181,52 @@ typedef enum spv_operand_type_t {
SPV_OPERAND_TYPE_LOOP_CONTROL,
SPV_OPERAND_TYPE_FUNCTION_CONTROL,
SPV_OPERAND_TYPE_MEMORY_SEMANTICS,
SPV_OPERAND_TYPE_MEMORY_ACCESS,
SPV_OPERAND_TYPE_EXECUTION_SCOPE,
SPV_OPERAND_TYPE_GROUP_OPERATION,
SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS,
SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO,
SPV_OPERAND_TYPE_CAPABILITY,
// An optional operand represents zero or one logical operands.
// In an instruction definition, this may only appear at the end of the
// operand types.
SPV_OPERAND_TYPE_OPTIONAL_ID,
SPV_OPERAND_TYPE_OPTIONAL_IMAGE,
// A literal number or string, but optional.
SPV_OPERAND_TYPE_OPTIONAL_LITERAL,
// An optional literal string.
SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
// An optional memory access qualifier, e.g. Volatile
SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
// An optional execution mode
SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE,
// A variable operand represents zero or more logical operands.
// In an instruction definition, this may only appear at the end of the
// operand types.
SPV_OPERAND_TYPE_VARIABLE_ID,
SPV_OPERAND_TYPE_VARIABLE_LITERAL,
// A sequence of zero or more pairs of (Literal, Id)
SPV_OPERAND_TYPE_VARIABLE_LITERAL_ID,
// A sequence of zero or more pairs of (Id, Literal)
SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL,
// A sequence of zero or more memory access operands
SPV_OPERAND_TYPE_VARIABLE_MEMORY_ACCESS,
// A sequence of zero or more execution modes
SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE,
// An Id that is second or later in an optional tuple of operands.
// This must be present if the first operand in the tuple is present.
SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE,
// A Literal that is second or later in an optional tuple of operands.
// This must be present if the first operand in the tuple is present.
SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE,
// This is a sentinel value, and does not represent an operand type.
// It should come last.
SPV_OPERAND_TYPE_NUM_OPERAND_TYPES,
// TODO(dneto): Remove this, as it's covered by above optional and
// variable cases.
SPV_OPERAND_TYPE_ELLIPSIS, // NOTE: Unspecified variable operands
SPV_FORCE_32_BIT_ENUM(spv_operand_type_t)
} spv_operand_type_t;
@ -217,11 +271,19 @@ typedef struct spv_header_t {
typedef struct spv_opcode_desc_t {
const char *name;
const uint16_t wordCount;
const Op opcode;
const uint32_t flags; // Bitfield of spv_opcode_flags_t
const uint32_t capabilities; // spv_language_capabilities_t
const spv_operand_type_t operandTypes[16]; // TODO: Smaller/larger?
// operandTypes[0..numTypes-1] describe logical operands for the instruction.
// The operand types include result id and result-type id, followed by
// the types of arguments.
uint16_t numTypes;
spv_operand_type_t operandTypes[16]; // TODO: Smaller/larger?
const bool hasResult; // Does the instruction have a result ID operand?
const bool hasType; // Does the instruction have a type ID operand?
// The operand class for each logical argument. This does *not* include
// the result Id or type ID. The list is terminated by SPV_OPERAND_TYPE_NONE.
const OperandClass operandClass[16];
} spv_opcode_desc_t;
typedef struct spv_opcode_table_t {
@ -275,6 +337,15 @@ typedef struct spv_text_t {
uint64_t length;
} spv_text_t;
// Describes an instruction.
//
// The wordCount and words[0..wordCount-1] always contain valid data.
//
// Normally, both opcode and extInstType contain valid data.
// However, when the assembler parses !<number> as the first word in
// an instruction, then opcode and extInstType are invalid, and
// wordCount == 1
// words[0] == <number>
typedef struct spv_instruction_t {
uint16_t wordCount;
Op opcode;

View File

@ -162,13 +162,17 @@ spv_result_t spvBinaryEncodeString(const char *str, spv_instruction_t *pInst,
return SPV_SUCCESS;
}
// TODO(dneto): This API is not powerful enough in the case that the
// number and type of operands are not known until partway through parsing
// the operation. This happens when enum operands might have different number
// of operands, or with extended instructions.
spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
const uint16_t operandIndex,
const spv_opcode_desc opcodeEntry,
const spv_operand_table operandTable,
spv_operand_desc *pOperandEntry) {
spv_operand_type_t type;
if (operandIndex < opcodeEntry->wordCount) {
if (operandIndex < opcodeEntry->numTypes) {
// NOTE: Do operand table lookup to set operandEntry if successful
uint16_t index = operandIndex - 1;
type = opcodeEntry->operandTypes[index];
@ -180,7 +184,7 @@ spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
}
} else if (*pOperandEntry) {
// NOTE: Use specified operand entry operand type for this word
uint16_t index = operandIndex - opcodeEntry->wordCount;
uint16_t index = operandIndex - opcodeEntry->numTypes;
type = (*pOperandEntry)->operandTypes[index];
} else if (OpSwitch == opcodeEntry->opcode) {
// NOTE: OpSwitch is a special case which expects a list of paired extra
@ -188,12 +192,12 @@ spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
assert(0 &&
"This case is previously untested, remove this assert and ensure it "
"is behaving correctly!");
uint16_t lastIndex = opcodeEntry->wordCount - 1;
uint16_t lastIndex = opcodeEntry->numTypes - 1;
uint16_t index = lastIndex + ((operandIndex - lastIndex) % 2);
type = opcodeEntry->operandTypes[index];
} else {
// NOTE: Default to last operand type in opcode entry
uint16_t index = opcodeEntry->wordCount - 1;
uint16_t index = opcodeEntry->numTypes - 1;
type = opcodeEntry->operandTypes[index];
}
return type;
@ -203,8 +207,8 @@ spv_result_t spvBinaryDecodeOperand(
const Op opcode, const spv_operand_type_t type, const uint32_t *words,
const spv_endianness_t endian, const uint32_t options,
const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
spv_ext_inst_type_t *pExtInstType, out_stream &stream,
spv_position position, spv_diagnostic *pDiagnostic) {
spv_operand_pattern_t *pExpectedOperands, spv_ext_inst_type_t *pExtInstType,
out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic) {
spvCheck(!words || !position, return SPV_ERROR_INVALID_POINTER);
spvCheck(!pDiagnostic, return SPV_ERROR_INVALID_DIAGNOSTIC);
@ -214,21 +218,22 @@ spv_result_t spvBinaryDecodeOperand(
uint64_t index = 0;
switch (type) {
case SPV_OPERAND_TYPE_ID: {
stream.get() << ((color) ? clr::yellow() : "");
case SPV_OPERAND_TYPE_ID:
case SPV_OPERAND_TYPE_RESULT_ID:
case SPV_OPERAND_TYPE_OPTIONAL_ID:
case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: {
if (color) {
stream.get() << (type == SPV_OPERAND_TYPE_RESULT_ID ? clr::blue()
: clr::yellow());
}
stream.get() << "%" << spvFixWord(words[index], endian);
stream.get() << ((color) ? clr::reset() : "");
index++;
position->index++;
} break;
case SPV_OPERAND_TYPE_RESULT_ID: {
stream.get() << (color ? clr::blue() : "");
stream.get() << "%" << spvFixWord(words[index], endian);
stream.get() << (color ? clr::reset() : "");
index++;
position->index++;
} break;
case SPV_OPERAND_TYPE_LITERAL: {
case SPV_OPERAND_TYPE_LITERAL:
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL:
case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE: {
// TODO: Need to support multiple word literals
stream.get() << (color ? clr::red() : "");
stream.get() << spvFixWord(words[index], endian);
@ -245,6 +250,7 @@ spv_result_t spvBinaryDecodeOperand(
DIAGNOSTIC << "Invalid extended instruction '" << words[0]
<< "'.";
return SPV_ERROR_INVALID_BINARY);
spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
stream.get() << (color ? clr::red() : "");
stream.get() << extInst->name;
stream.get() << (color ? clr::reset() : "");
@ -256,7 +262,8 @@ spv_result_t spvBinaryDecodeOperand(
index++;
position->index++;
} break;
case SPV_OPERAND_TYPE_LITERAL_STRING: {
case SPV_OPERAND_TYPE_LITERAL_STRING:
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
const char *string = (const char *)&words[index];
uint64_t stringOperandCount = (strlen(string) / 4) + 1;
@ -283,6 +290,7 @@ spv_result_t spvBinaryDecodeOperand(
case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
case SPV_OPERAND_TYPE_MEMORY_MODEL:
case SPV_OPERAND_TYPE_EXECUTION_MODE:
case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
case SPV_OPERAND_TYPE_STORAGE_CLASS:
case SPV_OPERAND_TYPE_DIMENSIONALITY:
case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
@ -298,7 +306,7 @@ spv_result_t spvBinaryDecodeOperand(
case SPV_OPERAND_TYPE_LOOP_CONTROL:
case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
case SPV_OPERAND_TYPE_MEMORY_ACCESS:
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
case SPV_OPERAND_TYPE_GROUP_OPERATION:
case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
@ -311,6 +319,8 @@ spv_result_t spvBinaryDecodeOperand(
<< words[index] << "'.";
return SPV_ERROR_INVALID_TEXT);
stream.get() << entry->name;
// Prepare to accept operands to this operand, if needed.
spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
index++;
position->index++;
} break;
@ -333,6 +343,8 @@ spv_result_t spvBinaryDecodeOpcode(
return SPV_ERROR_INVALID_TABLE);
spvCheck(!pDiagnostic, return SPV_ERROR_INVALID_DIAGNOSTIC);
spv_position_t instructionStart = *position;
uint16_t wordCount;
Op opcode;
spvOpcodeSplit(spvFixWord(pInst->words[0], endian), &wordCount, &opcode);
@ -342,11 +354,21 @@ spv_result_t spvBinaryDecodeOpcode(
DIAGNOSTIC << "Invalid Opcode '" << opcode << "'.";
return SPV_ERROR_INVALID_BINARY);
spvCheck(opcodeEntry->wordCount > wordCount,
DIAGNOSTIC << "Invalid instruction word count '" << wordCount
<< "', expected at least '" << opcodeEntry->wordCount
<< "'.";
return SPV_ERROR_INVALID_BINARY);
// See if there are enough required words.
// Some operands in the operand types are optional or could be zero length.
// The optional and zero length opeands must be at the end of the list.
if (opcodeEntry->numTypes > wordCount &&
!spvOperandIsOptional(opcodeEntry->operandTypes[wordCount])) {
uint16_t numRequired;
for (numRequired = 0; numRequired < opcodeEntry->numTypes &&
!spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]) ; numRequired++ )
;
DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
<< " word count '" << wordCount
<< "', expected at least '" << numRequired
<< "'.";
return SPV_ERROR_INVALID_BINARY;
}
std::stringstream no_result_id_strstream;
out_stream no_result_id_stream(no_result_id_strstream);
@ -355,23 +377,50 @@ spv_result_t spvBinaryDecodeOpcode(
position->index++;
spv_operand_desc operandEntry = nullptr;
// Maintains the ordered list of expected operand types.
// For many instructions we only need the {numTypes, operandTypes}
// entries in opcodeEntry. However, sometimes we need to modify
// the list as we parse the operands. This occurs when an operand
// has its own logical operands (such as the LocalSize operand for
// ExecutionMode), or for extended instructions that may have their
// own operands depending on the selected extended instruction.
spv_operand_pattern_t expectedOperands(
opcodeEntry->operandTypes,
opcodeEntry->operandTypes + opcodeEntry->numTypes);
for (uint16_t index = 1; index < wordCount; ++index) {
const uint32_t word = spvFixWord(pInst->words[index], endian);
const uint64_t currentPosIndex = position->index;
spvCheck(expectedOperands.empty(),
DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
<< " starting at word " << instructionStart.index
<< ": "
<< " expected no more operands after " << index
<< " words, but word count is " << wordCount << ".";
return SPV_ERROR_INVALID_BINARY;);
spv_operand_type_t type = spvTakeFirstMatchableOperand(&expectedOperands);
if (result_id_index != index - 1) no_result_id_strstream << " ";
spv_operand_type_t type = spvBinaryOperandInfo(word, index, opcodeEntry,
operandTable, &operandEntry);
spvCheck(spvBinaryDecodeOperand(
opcodeEntry->opcode, type, pInst->words + index, endian,
options, operandTable, extInstTable, &pInst->extInstType,
(result_id_index == index - 1 ? stream : no_result_id_stream),
position, pDiagnostic),
return SPV_ERROR_INVALID_BINARY);
spvCheck(
spvBinaryDecodeOperand(
opcodeEntry->opcode, type, pInst->words + index, endian, options,
operandTable, extInstTable, &expectedOperands, &pInst->extInstType,
(result_id_index == index - 1 ? stream : no_result_id_stream),
position, pDiagnostic),
DIAGNOSTIC << "UNEXPLAINED ERROR";
return SPV_ERROR_INVALID_BINARY);
if (result_id_index == index - 1) stream.get() << " = ";
index += (uint16_t)(position->index - currentPosIndex - 1);
}
// TODO(dneto): There's an opportunity for a more informative message.
spvCheck(!expectedOperands.empty() &&
!spvOperandIsOptional(expectedOperands.front()),
DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
<< " starting at word " << instructionStart.index << ": "
<< " expected more operands after " << wordCount
<< " words.";
return SPV_ERROR_INVALID_BINARY;);
stream.get() << no_result_id_strstream.str();

View File

@ -28,6 +28,7 @@
#define _LIBSPIRV_UTIL_BINARY_H_
#include <libspirv/libspirv.h>
#include "operand.h"
#include "print.h"
// Functions
@ -131,6 +132,7 @@ spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
/// @param[in] endian the endianness of the stream
/// @param[in] options bitfield of spv_binary_to_text_options_t values
/// @param[in] operandTable table of specified operands
/// @param[in,out] pExpectedOperands the expected operand types
/// @param[in,out] pExtInstType type of extended instruction library
/// @param[in,out] stream the text output stream
/// @param[in,out] position position in the binary stream
@ -141,8 +143,8 @@ spv_result_t spvBinaryDecodeOperand(
const Op opcode, const spv_operand_type_t type, const uint32_t *words,
const spv_endianness_t endian, const uint32_t options,
const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
spv_ext_inst_type_t *pExtInstType, out_stream &stream,
spv_position position, spv_diagnostic *pDiag);
spv_operand_pattern_t *pExpectedOperands, spv_ext_inst_type_t *pExtInstType,
out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic);
/// @brief Translate binary Opcode stream to textual form
///

File diff suppressed because it is too large Load Diff

294
source/opcode.inc Normal file
View File

@ -0,0 +1,294 @@
// Instruction fields are:
// name - skips the "Op" prefix
// {0|1} - whether the instruction generates a result Id
// {0|1} - whether the instruction encodes the type of the result Id
// numLogicalOperands - does not include result id or type id
// numCapabilities - we only handle 0 or 1 required capabilities
// Capability(<capability-name>) - capability required to use this instruction. Might be None.
// {0|1} - whether the instruction is variable number of words
// EmptyList or List(...) - list of classes of logical operands
// Example use:
// #define EmptyList {}
// #define List(...) {__VA_ARGS__}
// #define Capability(C) Capability##C
// #define NoCapability
// #define Instruction(Name,HasResult,HasType,NumLogicalOperands,CapabiltyRequired,IsVariable,LogicalArgsList)
Instruction(Nop, 0, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(Undef, 1, 1, 0, 0, Capability(None), 0, EmptyList)
Instruction(SourceContinued, 0, 0, 1, 0, Capability(None), 1, List(OperandLiteralString))
Instruction(Source, 0, 0, 4, 0, Capability(None), 1, List(OperandSource, OperandLiteralNumber, OperandOptionalId, OperandOptionalLiteralString))
Instruction(SourceExtension, 0, 0, 1, 0, Capability(None), 1, List(OperandLiteralString))
Instruction(Name, 0, 0, 2, 0, Capability(None), 1, List(OperandId, OperandLiteralString))
Instruction(MemberName, 0, 0, 3, 0, Capability(None), 1, List(OperandId, OperandLiteralNumber, OperandLiteralString))
Instruction(String, 1, 0, 1, 0, Capability(None), 1, List(OperandLiteralString))
// Rev32+ has: Instruction(Line, 0, 0, 3, 0, Capability(None), 0, List(OperandId, OperandLiteralNumber, OperandLiteralNumber))
// TODO(dneto): Remove the following hack for OpLine. It was put in to match Rev31.
Instruction(Line, 0, 0, 3, 0, Capability(None), 0, List(OperandId, OperandId, OperandLiteralNumber, OperandLiteralNumber))
Instruction(Extension, 0, 0, 1, 0, Capability(None), 1, List(OperandLiteralString))
Instruction(ExtInstImport, 1, 0, 1, 0, Capability(None), 1, List(OperandLiteralString))
Instruction(ExtInst, 1, 1, 3, 0, Capability(None), 1, List(OperandId, OperandLiteralNumber, OperandVariableIds))
Instruction(MemoryModel, 0, 0, 2, 0, Capability(None), 0, List(OperandAddressing, OperandMemory))
Instruction(EntryPoint, 0, 0, 3, 0, Capability(None), 1, List(OperandExecutionModel, OperandId, OperandLiteralString))
Instruction(ExecutionMode, 0, 0, 3, 0, Capability(None), 1, List(OperandId, OperandExecutionMode, OperandOptionalLiteral))
Instruction(Capability, 0, 0, 1, 0, Capability(None), 0, List(OperandCapability))
Instruction(TypeVoid, 1, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(TypeBool, 1, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(TypeInt, 1, 0, 2, 0, Capability(None), 0, List(OperandLiteralNumber, OperandLiteralNumber))
Instruction(TypeFloat, 1, 0, 1, 0, Capability(None), 0, List(OperandLiteralNumber))
Instruction(TypeVector, 1, 0, 2, 0, Capability(None), 0, List(OperandId, OperandLiteralNumber))
Instruction(TypeMatrix, 1, 0, 2, 1, Capability(Matrix), 0, List(OperandId, OperandLiteralNumber))
Instruction(TypeImage, 1, 0, 8, 0, Capability(None), 1, List(OperandId, OperandDimensionality, OperandLiteralNumber, OperandLiteralNumber, OperandLiteralNumber, OperandLiteralNumber, OperandSamplerImageFormat, OperandOptionalLiteral))
Instruction(TypeSampler, 1, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(TypeSampledImage, 1, 0, 1, 0, Capability(None), 0, List(OperandId))
Instruction(TypeArray, 1, 0, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(TypeRuntimeArray, 1, 0, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(TypeStruct, 1, 0, 1, 0, Capability(None), 1, List(OperandVariableIds))
Instruction(TypeOpaque, 1, 0, 1, 1, Capability(Kernel), 1, List(OperandLiteralString))
Instruction(TypePointer, 1, 0, 2, 0, Capability(None), 0, List(OperandStorage, OperandId))
Instruction(TypeFunction, 1, 0, 2, 0, Capability(None), 1, List(OperandId, OperandVariableIds))
Instruction(TypeEvent, 1, 0, 0, 1, Capability(Kernel), 0, EmptyList)
Instruction(TypeDeviceEvent, 1, 0, 0, 1, Capability(DeviceEnqueue), 0, EmptyList)
Instruction(TypeReserveId, 1, 0, 0, 1, Capability(Pipes), 0, EmptyList)
Instruction(TypeQueue, 1, 0, 0, 1, Capability(DeviceEnqueue), 0, EmptyList)
Instruction(TypePipe, 1, 0, 2, 1, Capability(Pipes), 0, List(OperandId, OperandAccessQualifier))
Instruction(TypeForwardPointer, 0, 0, 2, 1, Capability(Addresses), 0, List(OperandId, OperandStorage))
Instruction(ConstantTrue, 1, 1, 0, 0, Capability(None), 0, EmptyList)
Instruction(ConstantFalse, 1, 1, 0, 0, Capability(None), 0, EmptyList)
Instruction(Constant, 1, 1, 1, 0, Capability(None), 1, List(OperandVariableLiterals))
Instruction(ConstantComposite, 1, 1, 1, 0, Capability(None), 1, List(OperandVariableIds))
Instruction(ConstantSampler, 1, 1, 3, 1, Capability(LiteralSampler), 0, List(OperandSamplerAddressingMode, OperandLiteralNumber, OperandSamplerFilterMode))
Instruction(ConstantNull, 1, 1, 0, 0, Capability(None), 0, EmptyList)
Instruction(SpecConstantTrue, 1, 1, 0, 0, Capability(None), 0, EmptyList)
Instruction(SpecConstantFalse, 1, 1, 0, 0, Capability(None), 0, EmptyList)
Instruction(SpecConstant, 1, 1, 1, 0, Capability(None), 1, List(OperandVariableLiterals))
Instruction(SpecConstantComposite, 1, 1, 1, 0, Capability(None), 1, List(OperandVariableIds))
Instruction(SpecConstantOp, 1, 1, 2, 0, Capability(None), 1, List(OperandLiteralNumber, OperandVariableIds))
Instruction(Function, 1, 1, 2, 0, Capability(None), 0, List(OperandFunction, OperandId))
Instruction(FunctionParameter, 1, 1, 0, 0, Capability(None), 0, EmptyList)
Instruction(FunctionEnd, 0, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(FunctionCall, 1, 1, 2, 0, Capability(None), 1, List(OperandId, OperandVariableIds))
Instruction(Variable, 1, 1, 2, 0, Capability(None), 1, List(OperandStorage, OperandOptionalId))
Instruction(ImageTexelPointer, 1, 1, 3, 0, Capability(None), 0, List(OperandId, OperandId, OperandId))
Instruction(Load, 1, 1, 2, 0, Capability(None), 1, List(OperandId, OperandOptionalLiteral))
Instruction(Store, 0, 0, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandOptionalLiteral))
Instruction(CopyMemory, 0, 0, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandOptionalLiteral))
Instruction(CopyMemorySized, 0, 0, 4, 1, Capability(Addresses), 1, List(OperandId, OperandId, OperandId, OperandOptionalLiteral))
Instruction(AccessChain, 1, 1, 2, 0, Capability(None), 1, List(OperandId, OperandVariableIds))
Instruction(InBoundsAccessChain, 1, 1, 2, 0, Capability(None), 1, List(OperandId, OperandVariableIds))
Instruction(PtrAccessChain, 1, 1, 3, 1, Capability(Addresses), 1, List(OperandId, OperandId, OperandVariableIds))
Instruction(ArrayLength, 1, 1, 2, 1, Capability(Shader), 0, List(OperandId, OperandLiteralNumber))
Instruction(GenericPtrMemSemantics, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(InBoundsPtrAccessChain, 1, 1, 3, 1, Capability(Addresses), 1, List(OperandId, OperandId, OperandVariableIds))
Instruction(Decorate, 0, 0, 3, 0, Capability(None), 1, List(OperandId, OperandDecoration, OperandVariableLiterals))
Instruction(MemberDecorate, 0, 0, 4, 0, Capability(None), 1, List(OperandId, OperandLiteralNumber, OperandDecoration, OperandVariableLiterals))
Instruction(DecorationGroup, 1, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(GroupDecorate, 0, 0, 2, 0, Capability(None), 1, List(OperandId, OperandVariableIds))
Instruction(GroupMemberDecorate, 0, 0, 2, 0, Capability(None), 1, List(OperandId, OperandVariableIdLiteral))
Instruction(VectorExtractDynamic, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(VectorInsertDynamic, 1, 1, 3, 0, Capability(None), 0, List(OperandId, OperandId, OperandId))
Instruction(VectorShuffle, 1, 1, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandVariableLiterals))
Instruction(CompositeConstruct, 1, 1, 1, 0, Capability(None), 1, List(OperandVariableIds))
Instruction(CompositeExtract, 1, 1, 2, 0, Capability(None), 1, List(OperandId, OperandVariableLiterals))
Instruction(CompositeInsert, 1, 1, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandVariableLiterals))
Instruction(CopyObject, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(Transpose, 1, 1, 1, 1, Capability(Matrix), 0, List(OperandId))
Instruction(SampledImage, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ImageSampleImplicitLod, 1, 1, 3, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandOptionalImage))
Instruction(ImageSampleExplicitLod, 1, 1, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandOptionalImage))
Instruction(ImageSampleDrefImplicitLod, 1, 1, 4, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandId, OperandOptionalImage))
Instruction(ImageSampleDrefExplicitLod, 1, 1, 4, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandId, OperandOptionalImage))
Instruction(ImageSampleProjImplicitLod, 1, 1, 3, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandOptionalImage))
Instruction(ImageSampleProjExplicitLod, 1, 1, 3, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandOptionalImage))
Instruction(ImageSampleProjDrefImplicitLod, 1, 1, 4, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandId, OperandOptionalImage))
Instruction(ImageSampleProjDrefExplicitLod, 1, 1, 4, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandId, OperandOptionalImage))
Instruction(ImageFetch, 1, 1, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandOptionalImage))
Instruction(ImageGather, 1, 1, 4, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandId, OperandOptionalImage))
Instruction(ImageDrefGather, 1, 1, 4, 1, Capability(Shader), 1, List(OperandId, OperandId, OperandId, OperandOptionalImage))
Instruction(ImageRead, 1, 1, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandOptionalImage))
Instruction(ImageWrite, 0, 0, 4, 0, Capability(None), 1, List(OperandId, OperandId, OperandId, OperandOptionalImage))
Instruction(ImageQueryFormat, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(ImageQueryOrder, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(ImageQuerySizeLod, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ImageQuerySize, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(ImageQueryLod, 1, 1, 2, 1, Capability(Shader), 0, List(OperandId, OperandId))
Instruction(ImageQueryLevels, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(ImageQuerySamples, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(ConvertFToU, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(ConvertFToS, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(ConvertSToF, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(ConvertUToF, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(UConvert, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(SConvert, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(FConvert, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(QuantizeToF16, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(ConvertPtrToU, 1, 1, 1, 1, Capability(Addresses), 0, List(OperandId))
Instruction(SatConvertSToU, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(SatConvertUToS, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(ConvertUToPtr, 1, 1, 1, 1, Capability(Addresses), 0, List(OperandId))
Instruction(PtrCastToGeneric, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(GenericCastToPtr, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(GenericCastToPtrExplicit, 1, 1, 2, 1, Capability(Kernel), 0, List(OperandId, OperandStorage))
Instruction(Bitcast, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(SNegate, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(FNegate, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(IAdd, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FAdd, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ISub, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FSub, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(IMul, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FMul, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(UDiv, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SDiv, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FDiv, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(UMod, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SRem, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SMod, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FRem, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FMod, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(VectorTimesScalar, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(MatrixTimesScalar, 1, 1, 2, 1, Capability(Matrix), 0, List(OperandId, OperandId))
Instruction(VectorTimesMatrix, 1, 1, 2, 1, Capability(Matrix), 0, List(OperandId, OperandId))
Instruction(MatrixTimesVector, 1, 1, 2, 1, Capability(Matrix), 0, List(OperandId, OperandId))
Instruction(MatrixTimesMatrix, 1, 1, 2, 1, Capability(Matrix), 0, List(OperandId, OperandId))
Instruction(OuterProduct, 1, 1, 2, 1, Capability(Matrix), 0, List(OperandId, OperandId))
Instruction(Dot, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(IAddCarry, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ISubBorrow, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(UMulExtended, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SMulExtended, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(Any, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(All, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(IsNan, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(IsInf, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(IsFinite, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(IsNormal, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(SignBitSet, 1, 1, 1, 1, Capability(Kernel), 0, List(OperandId))
Instruction(LessOrGreater, 1, 1, 2, 1, Capability(Kernel), 0, List(OperandId, OperandId))
Instruction(Ordered, 1, 1, 2, 1, Capability(Kernel), 0, List(OperandId, OperandId))
Instruction(Unordered, 1, 1, 2, 1, Capability(Kernel), 0, List(OperandId, OperandId))
Instruction(LogicalEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(LogicalNotEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(LogicalOr, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(LogicalAnd, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(LogicalNot, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(Select, 1, 1, 3, 0, Capability(None), 0, List(OperandId, OperandId, OperandId))
Instruction(IEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(INotEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(UGreaterThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SGreaterThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(UGreaterThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SGreaterThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ULessThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SLessThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ULessThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(SLessThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FOrdEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FUnordEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FOrdNotEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FUnordNotEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FOrdLessThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FUnordLessThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FOrdGreaterThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FUnordGreaterThan, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FOrdLessThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FUnordLessThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FOrdGreaterThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(FUnordGreaterThanEqual, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ShiftRightLogical, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ShiftRightArithmetic, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(ShiftLeftLogical, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(BitwiseOr, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(BitwiseXor, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(BitwiseAnd, 1, 1, 2, 0, Capability(None), 0, List(OperandId, OperandId))
Instruction(Not, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(BitFieldInsert, 1, 1, 4, 1, Capability(Shader), 0, List(OperandId, OperandId, OperandId, OperandId))
Instruction(BitFieldSExtract, 1, 1, 3, 1, Capability(Shader), 0, List(OperandId, OperandId, OperandId))
Instruction(BitFieldUExtract, 1, 1, 3, 1, Capability(Shader), 0, List(OperandId, OperandId, OperandId))
Instruction(BitReverse, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(BitCount, 1, 1, 1, 0, Capability(None), 0, List(OperandId))
Instruction(DPdx, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(DPdy, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(Fwidth, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(DPdxFine, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(DPdyFine, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(FwidthFine, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(DPdxCoarse, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(DPdyCoarse, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(FwidthCoarse, 1, 1, 1, 1, Capability(Shader), 0, List(OperandId))
Instruction(EmitVertex, 0, 0, 0, 1, Capability(Geometry), 0, EmptyList)
Instruction(EndPrimitive, 0, 0, 0, 1, Capability(Geometry), 0, EmptyList)
Instruction(EmitStreamVertex, 0, 0, 1, 1, Capability(Geometry), 0, List(OperandId))
Instruction(EndStreamPrimitive, 0, 0, 1, 1, Capability(Geometry), 0, List(OperandId))
Instruction(ControlBarrier, 0, 0, 3, 0, Capability(None), 0, List(OperandScope, OperandScope, OperandMemorySemantics))
Instruction(MemoryBarrier, 0, 0, 2, 0, Capability(None), 0, List(OperandScope, OperandMemorySemantics))
Instruction(AtomicLoad, 1, 1, 3, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics))
Instruction(AtomicStore, 0, 0, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicExchange, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicCompareExchange, 1, 1, 6, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandMemorySemantics, OperandId, OperandId))
Instruction(AtomicCompareExchangeWeak, 1, 1, 6, 1, Capability(Kernel), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandMemorySemantics, OperandId, OperandId))
Instruction(AtomicIIncrement, 1, 1, 3, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics))
Instruction(AtomicIDecrement, 1, 1, 3, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics))
Instruction(AtomicIAdd, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicISub, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicSMin, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicUMin, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicSMax, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicUMax, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicAnd, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicOr, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(AtomicXor, 1, 1, 4, 0, Capability(None), 0, List(OperandId, OperandScope, OperandMemorySemantics, OperandId))
Instruction(Phi, 1, 1, 1, 0, Capability(None), 1, List(OperandVariableIds))
Instruction(LoopMerge, 0, 0, 2, 0, Capability(None), 0, List(OperandId, OperandLoop))
Instruction(SelectionMerge, 0, 0, 2, 0, Capability(None), 0, List(OperandId, OperandSelect))
Instruction(Label, 1, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(Branch, 0, 0, 1, 0, Capability(None), 0, List(OperandId))
Instruction(BranchConditional, 0, 0, 4, 0, Capability(None), 1, List(OperandId, OperandId, OperandId, OperandVariableLiterals))
Instruction(Switch, 0, 0, 3, 0, Capability(None), 1, List(OperandId, OperandId, OperandVariableLiteralId))
Instruction(Kill, 0, 0, 0, 1, Capability(Shader), 0, EmptyList)
Instruction(Return, 0, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(ReturnValue, 0, 0, 1, 0, Capability(None), 0, List(OperandId))
Instruction(Unreachable, 0, 0, 0, 0, Capability(None), 0, EmptyList)
Instruction(LifetimeStart, 0, 0, 2, 0, Capability(None), 0, List(OperandId, OperandLiteralNumber))
Instruction(LifetimeStop, 0, 0, 2, 0, Capability(None), 0, List(OperandId, OperandLiteralNumber))
Instruction(AsyncGroupCopy, 1, 1, 6, 1, Capability(Kernel), 0, List(OperandScope, OperandId, OperandId, OperandId, OperandId, OperandId))
Instruction(WaitGroupEvents, 0, 0, 3, 1, Capability(Kernel), 0, List(OperandScope, OperandId, OperandId))
Instruction(GroupAll, 1, 1, 2, 1, Capability(Groups), 0, List(OperandScope, OperandId))
Instruction(GroupAny, 1, 1, 2, 1, Capability(Groups), 0, List(OperandScope, OperandId))
Instruction(GroupBroadcast, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandId, OperandId))
Instruction(GroupIAdd, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(GroupFAdd, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(GroupFMin, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(GroupUMin, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(GroupSMin, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(GroupFMax, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(GroupUMax, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(GroupSMax, 1, 1, 3, 1, Capability(Groups), 0, List(OperandScope, OperandGroupOperation, OperandId))
Instruction(ReadPipe, 1, 1, 2, 1, Capability(Pipes), 0, List(OperandId, OperandId))
Instruction(WritePipe, 1, 1, 2, 1, Capability(Pipes), 0, List(OperandId, OperandId))
Instruction(ReservedReadPipe, 1, 1, 4, 1, Capability(Pipes), 0, List(OperandId, OperandId, OperandId, OperandId))
Instruction(ReservedWritePipe, 1, 1, 4, 1, Capability(Pipes), 0, List(OperandId, OperandId, OperandId, OperandId))
Instruction(ReserveReadPipePackets, 1, 1, 2, 1, Capability(Pipes), 0, List(OperandId, OperandId))
Instruction(ReserveWritePipePackets, 1, 1, 2, 1, Capability(Pipes), 0, List(OperandId, OperandId))
Instruction(CommitReadPipe, 0, 0, 2, 1, Capability(Pipes), 0, List(OperandId, OperandId))
Instruction(CommitWritePipe, 0, 0, 2, 1, Capability(Pipes), 0, List(OperandId, OperandId))
Instruction(IsValidReserveId, 1, 1, 1, 1, Capability(Pipes), 0, List(OperandId))
Instruction(GetNumPipePackets, 1, 1, 1, 1, Capability(Pipes), 0, List(OperandId))
Instruction(GetMaxPipePackets, 1, 1, 1, 1, Capability(Pipes), 0, List(OperandId))
Instruction(GroupReserveReadPipePackets, 1, 1, 3, 1, Capability(Pipes), 0, List(OperandScope, OperandId, OperandId))
Instruction(GroupReserveWritePipePackets, 1, 1, 3, 1, Capability(Pipes), 0, List(OperandScope, OperandId, OperandId))
Instruction(GroupCommitReadPipe, 0, 0, 3, 1, Capability(Pipes), 0, List(OperandScope, OperandId, OperandId))
Instruction(GroupCommitWritePipe, 0, 0, 3, 1, Capability(Pipes), 0, List(OperandScope, OperandId, OperandId))
Instruction(EnqueueMarker, 1, 1, 4, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId, OperandId, OperandId))
Instruction(EnqueueKernel, 1, 1, 11, 1, Capability(DeviceEnqueue), 1, List(OperandId, OperandId, OperandId, OperandId, OperandId, OperandId, OperandId, OperandId, OperandId, OperandId, OperandVariableIds))
Instruction(GetKernelNDrangeSubGroupCount, 1, 1, 5, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId, OperandId, OperandId, OperandId))
Instruction(GetKernelNDrangeMaxSubGroupSize, 1, 1, 5, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId, OperandId, OperandId, OperandId))
Instruction(GetKernelWorkGroupSize, 1, 1, 4, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId, OperandId, OperandId))
Instruction(GetKernelPreferredWorkGroupSizeMultiple, 1, 1, 4, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId, OperandId, OperandId))
Instruction(RetainEvent, 0, 0, 1, 1, Capability(DeviceEnqueue), 0, List(OperandId))
Instruction(ReleaseEvent, 0, 0, 1, 1, Capability(DeviceEnqueue), 0, List(OperandId))
Instruction(CreateUserEvent, 1, 1, 0, 1, Capability(DeviceEnqueue), 0, EmptyList)
Instruction(IsValidEvent, 1, 1, 1, 1, Capability(DeviceEnqueue), 0, List(OperandId))
Instruction(SetUserEventStatus, 0, 0, 2, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId))
Instruction(CaptureEventProfilingInfo, 0, 0, 3, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId, OperandId))
Instruction(GetDefaultQueue, 1, 1, 0, 1, Capability(DeviceEnqueue), 0, EmptyList)
Instruction(BuildNDRange, 1, 1, 3, 1, Capability(DeviceEnqueue), 0, List(OperandId, OperandId, OperandId))

View File

@ -1418,7 +1418,7 @@ static const spv_operand_desc_group_t opcodeEntryTypes[] = {
{SPV_OPERAND_TYPE_MEMORY_SEMANTICS,
sizeof(memorySemanticsEntries) / sizeof(spv_operand_desc_t),
memorySemanticsEntries},
{SPV_OPERAND_TYPE_MEMORY_ACCESS,
{SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
sizeof(memoryAccessEntries) / sizeof(spv_operand_desc_t),
memoryAccessEntries},
{SPV_OPERAND_TYPE_EXECUTION_SCOPE,
@ -1549,7 +1549,7 @@ const char *spvOperandTypeStr(spv_operand_type_t type) {
return "function control";
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
return "memory semantics";
case SPV_OPERAND_TYPE_MEMORY_ACCESS:
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
return "memory access";
case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
return "execution scope";
@ -1561,9 +1561,98 @@ const char *spvOperandTypeStr(spv_operand_type_t type) {
return "kernel profiling info";
case SPV_OPERAND_TYPE_CAPABILITY:
return "capability";
case SPV_OPERAND_TYPE_NONE:
return "NONE";
default:
assert(0 && "Unhandled operand type!");
break;
}
return "unknown";
}
void spvPrependOperandTypes(const spv_operand_type_t* types,
spv_operand_pattern_t* pattern) {
const spv_operand_type_t* endTypes;
for (endTypes = types ; *endTypes != SPV_OPERAND_TYPE_NONE ; ++endTypes)
;
pattern->insert(pattern->begin(), types, endTypes);
}
bool spvOperandIsOptional(spv_operand_type_t type) {
// Variable means zero or more times.
if (spvOperandIsVariable(type))
return true;
switch (type) {
case SPV_OPERAND_TYPE_OPTIONAL_ID:
case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL:
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING:
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
return true;
default:
break;
}
return false;
}
bool spvOperandIsVariable(spv_operand_type_t type) {
switch (type) {
case SPV_OPERAND_TYPE_VARIABLE_ID:
case SPV_OPERAND_TYPE_VARIABLE_LITERAL:
case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL:
case SPV_OPERAND_TYPE_VARIABLE_LITERAL_ID:
case SPV_OPERAND_TYPE_VARIABLE_MEMORY_ACCESS:
case SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE:
return true;
default:
break;
}
return false;
}
bool spvExpandOperandSequenceOnce(spv_operand_type_t type,
spv_operand_pattern_t* pattern) {
switch (type) {
case SPV_OPERAND_TYPE_VARIABLE_ID:
pattern->insert(pattern->begin(), {SPV_OPERAND_TYPE_OPTIONAL_ID, type});
return true;
case SPV_OPERAND_TYPE_VARIABLE_LITERAL:
pattern->insert(pattern->begin(),
{SPV_OPERAND_TYPE_OPTIONAL_LITERAL, type});
return true;
case SPV_OPERAND_TYPE_VARIABLE_LITERAL_ID:
// Represents Zero or more (Literal, Id) pairs.
pattern->insert(pattern->begin(),
{SPV_OPERAND_TYPE_OPTIONAL_LITERAL,
SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE, type});
return true;
case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL:
// Represents Zero or more (Id, Literal) pairs.
pattern->insert(pattern->begin(),
{SPV_OPERAND_TYPE_OPTIONAL_ID,
SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE, type});
return true;
case SPV_OPERAND_TYPE_VARIABLE_MEMORY_ACCESS:
pattern->insert(pattern->begin(), {SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS, type});
return true;
case SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE:
pattern->insert(pattern->begin(), {SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE, type});
return true;
default:
break;
}
return false;
}
spv_operand_type_t spvTakeFirstMatchableOperand(spv_operand_pattern_t* pattern) {
assert(!pattern->empty());
spv_operand_type_t result;
do {
result = pattern->front();
pattern->pop_front();
} while(spvExpandOperandSequenceOnce(result, pattern));
return result;
}

View File

@ -27,8 +27,19 @@
#ifndef _CODEPLAY_SPIRV_OPERAND_H_
#define _CODEPLAY_SPIRV_OPERAND_H_
#include <deque>
#include <libspirv/libspirv.h>
/// @brief A sequence of operand types.
///
/// A SPIR-V parser uses an operand pattern to describe what is expected
/// next on the input.
///
/// As we parse an instruction in text or binary form from left to right,
/// we pull and push from the front of the pattern.
using spv_operand_pattern_t = std::deque<spv_operand_type_t>;
/// @brief Find the named operand in the table
///
/// @param[in] table to lookup
@ -62,4 +73,63 @@ spv_result_t spvOperandTableValueLookup(const spv_operand_table table,
/// @return the string name of the operand
const char *spvOperandTypeStr(spv_operand_type_t type);
/// @brief Returns true if an operand of the given type is optional.
///
/// @param[in] type The operand type
///
/// @return bool
bool spvOperandIsOptional(spv_operand_type_t type);
/// @brief Returns true if an operand type represents zero or more
/// logical operands.
///
/// Note that a single logical operand may still be a variable number
/// of words. For example, a literal string may be many words, but
/// is just one logical operand.
///
/// @param[in] type The operand type
///
/// @return bool
bool spvOperandIsVariable(spv_operand_type_t type);
/// @brief Inserts a list of operand types into the front of the given pattern.
///
/// @param[in] types source array of types, ending with SPV_OPERAND_TYPE_NONE.
/// @param[in,out] pattern the destination sequence
void spvPrependOperandTypes(const spv_operand_type_t *types,
spv_operand_pattern_t *pattern);
/// @brief Expands an operand type representing zero or more logical operands,
/// exactly once.
///
/// If the given type represents potentially several logical operands,
/// then prepend the given pattern with the first expansion of the logical
/// operands, followed by original type. Otherwise, don't modify the pattern.
///
/// For example, the SPV_OPERAND_TYPE_VARIABLE_ID represents zero or more
/// IDs. In that case we would prepend the pattern with SPV_OPERAND_TYPE_ID
/// followed by SPV_OPERAND_TYPE_VARIABLE_ID again.
///
/// This also applies to zero or more tuples of logical operands. In that case
/// we prepend pattern with for the members of the tuple, followed by the
/// original type argument. The pattern must encode the fact that if any part
/// of the tuple is present, then all tuple members should be. So the first
/// member of the tuple must be optional, and the remaining members
/// non-optional.
///
/// @param [in] type an operand type, maybe representing a sequence of operands
/// @param [in,out] pattern the list of operand types
///
/// @return true if we modified the pattern
bool spvExpandOperandSequenceOnce(spv_operand_type_t type,
spv_operand_pattern_t* pattern);
/// Expands the first element in the pattern until it is a matchable operand
/// type, then pops it off the front and returns it. The pattern must not be
/// empty.
///
/// A matchable operand type is anything other than a zero-or-more-items
/// operand type.
spv_operand_type_t spvTakeFirstMatchableOperand(spv_operand_pattern_t* pattern);
#endif

View File

@ -188,20 +188,22 @@ spv_result_t spvTextWordGet(const spv_text text,
// Returns true if the string at the given position in text starts with "Op".
static bool spvStartsWithOp(const spv_text text, const spv_position position) {
if (text->length < position->index + 2) return false;
return ('O' == text->str[position->index] &&
'p' == text->str[position->index + 1]);
if (text->length < position->index + 3) return false;
char ch0 = text->str[position->index];
char ch1 = text->str[position->index + 1];
char ch2 = text->str[position->index + 2];
return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
}
// Returns true if a new instruction begins at the given position in text.
static bool spvTextIsStartOfNewInst(const spv_text text,
const spv_position position) {
bool spvTextIsStartOfNewInst(const spv_text text,
const spv_position position) {
spv_position_t nextPosition = *position;
if (spvTextAdvance(text, &nextPosition)) return false;
if (spvStartsWithOp(text, position)) return true;
if (spvStartsWithOp(text, &nextPosition)) return true;
std::string word;
spv_position_t startPosition = nextPosition;
spv_position_t startPosition = *position;
if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
if ('%' != word.front()) return false;
@ -348,7 +350,7 @@ spv_result_t spvTextEncodeOperand(
const spv_operand_type_t type, const char *textValue,
const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
spv_named_id_table namedIdTable, spv_instruction_t *pInst,
const spv_operand_type_t **ppExtraOperands, uint32_t *pBound,
spv_operand_pattern_t* pExpectedOperands, uint32_t *pBound,
const spv_position position, spv_diagnostic *pDiagnostic) {
// NOTE: Handle immediate int in the stream
if ('!' == textValue[0]) {
@ -369,6 +371,8 @@ spv_result_t spvTextEncodeOperand(
switch (type) {
case SPV_OPERAND_TYPE_ID:
case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
case SPV_OPERAND_TYPE_OPTIONAL_ID:
case SPV_OPERAND_TYPE_RESULT_ID: {
if ('%' == textValue[0]) {
textValue++;
@ -379,10 +383,12 @@ spv_result_t spvTextEncodeOperand(
id = spvNamedIdAssignOrGet(namedIdTable, textValue, pBound);
} else {
spvCheck(spvTextToUInt32(textValue, &id),
DIAGNOSTIC << "Invalid "
<< ((type == SPV_OPERAND_TYPE_RESULT_ID) ? "result " : "")
<< "ID '" << textValue << "'.";
return SPV_ERROR_INVALID_TEXT);
DIAGNOSTIC
<< "Invalid "
<< ((type == SPV_OPERAND_TYPE_RESULT_ID) ? "result " : "")
<< "ID '" << textValue << "'.";
return (spvOperandIsOptional(type) ? SPV_FAILED_MATCH
: SPV_ERROR_INVALID_TEXT));
}
pInst->words[pInst->wordCount++] = id;
if (*pBound <= id) {
@ -399,18 +405,26 @@ spv_result_t spvTextEncodeOperand(
<< textValue << "'.";
return SPV_ERROR_INVALID_TEXT);
pInst->words[pInst->wordCount++] = extInst->ext_inst;
*ppExtraOperands = extInst->operandTypes;
// Prepare to parse the operands for the extended instructions.
spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
return SPV_SUCCESS;
}
// TODO: Literal numbers can be any number up to 64 bits wide. This
// includes integers and floating point numbers.
// TODO(dneto): Suggest using spvTextToLiteral and looking for an
// appropriate result type.
spvCheck(spvTextToUInt32(textValue, &pInst->words[pInst->wordCount++]),
DIAGNOSTIC << "Invalid literal number '" << textValue << "'.";
return SPV_ERROR_INVALID_TEXT);
} break;
case SPV_OPERAND_TYPE_LITERAL: {
case SPV_OPERAND_TYPE_LITERAL:
case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE:
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL: {
spv_literal_t literal = {};
// TODO(dneto): Is return code different for optional operands?
spvCheck(spvTextToLiteral(textValue, &literal),
DIAGNOSTIC << "Invalid literal '" << textValue << "'.";
return SPV_ERROR_INVALID_TEXT);
@ -455,12 +469,15 @@ spv_result_t spvTextEncodeOperand(
return SPV_ERROR_INVALID_TEXT;
}
} break;
case SPV_OPERAND_TYPE_LITERAL_STRING: {
case SPV_OPERAND_TYPE_LITERAL_STRING:
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
size_t len = strlen(textValue);
spvCheck('"' != textValue[0] && '"' != textValue[len - 1],
if (spvOperandIsOptional(type))
return SPV_FAILED_MATCH;
DIAGNOSTIC << "Invalid literal string '" << textValue
<< "', expected quotes.";
return SPV_ERROR_INVALID_TEXT);
return SPV_ERROR_INVALID_TEXT;);
// NOTE: Strip quotes
std::string text(textValue + 1, len - 2);
@ -473,6 +490,9 @@ spv_result_t spvTextEncodeOperand(
spvBinaryEncodeString(text.c_str(), pInst, position, pDiagnostic),
return SPV_ERROR_INVALID_TEXT);
} break;
case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
assert(0 && " Handle optional optional image operands");
break;
default: {
// NOTE: All non literal operands are handled here using the operand
// table.
@ -485,9 +505,9 @@ spv_result_t spvTextEncodeOperand(
DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '"
<< textValue << "'.";
return SPV_ERROR_INVALID_TEXT;);
if (ppExtraOperands && entry->operandTypes[0] != SPV_OPERAND_TYPE_NONE) {
*ppExtraOperands = entry->operandTypes;
}
// Prepare to parse the operands for this logical operand.
spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
} break;
}
return SPV_SUCCESS;
@ -574,98 +594,74 @@ spv_result_t spvTextEncodeOpcode(
*position = nextPosition;
pInst->wordCount++;
// Get the arugment index for <result-id>. Used for handling the <result-id>
// for value generating instructions below.
const int16_t result_id_index = spvOpcodeResultIdIndex(opcodeEntry);
// Maintains the ordered list of expected operand types.
// For many instructions we only need the {numTypes, operandTypes}
// entries in opcodeEntry. However, sometimes we need to modify
// the list as we parse the operands. This occurs when an operand
// has its own logical operands (such as the LocalSize operand for
// ExecutionMode), or for extended instructions that may have their
// own operands depending on the selected extended instruction.
spv_operand_pattern_t expectedOperands(
opcodeEntry->operandTypes,
opcodeEntry->operandTypes + opcodeEntry->numTypes);
// NOTE: Process the fixed size operands
const spv_operand_type_t *extraOperandTypes = nullptr;
for (int32_t operandIndex = 0; operandIndex < (opcodeEntry->wordCount - 1);
++operandIndex) {
if (operandIndex == result_id_index && !result_id.empty()) {
// Handling the <result-id> for value generating instructions.
while (!expectedOperands.empty()) {
const spv_operand_type_t type = expectedOperands.front();
expectedOperands.pop_front();
// Expand optional tuples lazily.
if (spvExpandOperandSequenceOnce(type, &expectedOperands))
continue;
if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
// Handle the <result-id> for value generating instructions.
// We've already consumed it from the text stream. Here
// we inject its words into the instruction.
error = spvTextEncodeOperand(
SPV_OPERAND_TYPE_RESULT_ID, result_id.c_str(), operandTable,
extInstTable, namedIdTable, pInst, &extraOperandTypes, pBound,
extInstTable, namedIdTable, pInst, nullptr, pBound,
&result_id_position, pDiagnostic);
spvCheck(error, return error);
continue;
}
spvCheck(spvTextAdvance(text, position),
DIAGNOSTIC << "Expected operand, found end of stream.";
return SPV_ERROR_INVALID_TEXT);
std::string operandValue;
error = spvTextWordGet(text, position, operandValue, &nextPosition);
spvCheck(error, return error);
error = spvTextEncodeOperand(
opcodeEntry->operandTypes[operandIndex], operandValue.c_str(),
operandTable, extInstTable, namedIdTable, pInst, &extraOperandTypes,
pBound, position, pDiagnostic);
spvCheck(error, return error);
*position = nextPosition;
}
if (spvOpcodeIsVariable(opcodeEntry)) {
if (!extraOperandTypes) {
// NOTE: Handle variable length not defined by an immediate previously
// encountered in the Opcode.
spv_operand_type_t type =
opcodeEntry->operandTypes[opcodeEntry->wordCount - 1];
while (!spvTextAdvance(text, position)) {
// NOTE: If this is the end of the current instruction stream and we
// break out of this loop.
if (spvTextIsStartOfNewInst(text, position)) break;
std::string textValue;
spvTextWordGet(text, position, textValue, &nextPosition);
if (SPV_OPERAND_TYPE_LITERAL_STRING == type) {
spvCheck(spvTextAdvance(text, position),
DIAGNOSTIC << "Invalid string, found end of stream.";
return SPV_ERROR_INVALID_TEXT);
std::string string;
spvCheck(spvTextStringGet(text, position, string, &nextPosition),
DIAGNOSTIC << "Invalid string, new line or end of stream.";
return SPV_ERROR_INVALID_TEXT);
spvCheck(spvTextEncodeOperand(type, string.c_str(), operandTable,
extInstTable, namedIdTable, pInst,
nullptr, pBound, position, pDiagnostic),
return SPV_ERROR_INVALID_TEXT);
} else {
spvCheck(spvTextEncodeOperand(type, textValue.c_str(), operandTable,
extInstTable, namedIdTable, pInst,
nullptr, pBound, position, pDiagnostic),
return SPV_ERROR_INVALID_TEXT);
}
*position = nextPosition;
}
} else {
// NOTE: Process the variable size operands defined by an immediate
// previously encountered in the Opcode.
uint64_t extraOperandsIndex = 0;
while (extraOperandTypes[extraOperandsIndex]) {
spvCheck(spvTextAdvance(text, position),
DIAGNOSTIC << "Expected operand, found end of stream.";
return SPV_ERROR_INVALID_TEXT);
std::string operandValue;
error = spvTextWordGet(text, position, operandValue, &nextPosition);
error = spvTextEncodeOperand(extraOperandTypes[extraOperandsIndex],
operandValue.c_str(), operandTable,
extInstTable, namedIdTable, pInst, nullptr,
pBound, position, pDiagnostic);
spvCheck(error, return error);
*position = nextPosition;
extraOperandsIndex++;
// Find the next word.
error = spvTextAdvance(text, position);
if (error == SPV_END_OF_STREAM) {
if (spvOperandIsOptional(type)) {
// This would have been the last potential operand for the instruction,
// and we didn't find one. We're finished parsing this instruction.
break;
} else {
DIAGNOSTIC << "Expected operand, found end of stream.";
return SPV_ERROR_INVALID_TEXT;
}
}
assert(error == SPV_SUCCESS && "Somebody added another way to fail");
if (spvTextIsStartOfNewInst(text, position)) {
if (spvOperandIsOptional(type)) {
break;
} else {
DIAGNOSTIC << "Expected operand, found next instruction instead.";
return SPV_ERROR_INVALID_TEXT;
}
}
std::string operandValue;
error = spvTextWordGet(text, position, operandValue, &nextPosition);
spvCheck(error, return error);
error = spvTextEncodeOperand(
type, operandValue.c_str(),
operandTable, extInstTable, namedIdTable, pInst, &expectedOperands,
pBound, position, pDiagnostic);
if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
return SPV_SUCCESS;
spvCheck(error, return error);
*position = nextPosition;
}
}

View File

@ -28,6 +28,7 @@
#define _LIBSPIRV_UTIL_TEXT_H_
#include <libspirv/libspirv.h>
#include "operand.h"
#include <string>
@ -77,7 +78,7 @@ spv_result_t spvTextAdvanceLine(const spv_text text, spv_position_t *pPosition);
///
/// If a null terminator is found during the text advance SPV_END_OF_STREAM is
/// returned, SPV_SUCCESS otherwise. No error checking is performed on the
/// parameters, its the users responsispvity to ensure these are non null.
/// parameters, its the users responsibility to ensure these are non null.
///
/// @param[in] text to be parsed
/// @param[in,out] pPosition position text has been advanced to
@ -100,6 +101,15 @@ spv_result_t spvTextWordGet(const spv_text text,
const spv_position startPosition, std::string &word,
spv_position endPosition);
/// @brief Returns true if the given text can start a new instruction.
///
/// @param[in] text stream to read from
/// @param[in] startPosition current position in text stream
///
/// @return result code
bool spvTextIsStartOfNewInst(const spv_text text,
const spv_position startPosition);
/// @brief Fetch a string, including quotes, from the text stream
///
/// @param[in] text stream to read from
@ -165,7 +175,7 @@ int32_t spvTextIsNamedId(const char *textValue);
/// @param[in] operandTable operand lookup table
/// @param[in,out] namedIdTable table of named ID's
/// @param[out] pInst return binary Opcode
/// @param[out] ppExtraOperands list of extra variable operands, if any
/// @param[in,out] pExpectedOperands the operand types expected
/// @param[in,out] pBound current highest defined ID value
/// @param[in] pPosition used in diagnostic on error
/// @param[out] pDiagnostic populated on error
@ -175,7 +185,7 @@ spv_result_t spvTextEncodeOperand(
const spv_operand_type_t type, const char *textValue,
const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
spv_named_id_table namedIdTable, spv_instruction_t *pInst,
const spv_operand_type_t **ppExtraOperands, uint32_t *pBound,
spv_operand_pattern_t* pExpectedOperands, uint32_t *pBound,
const spv_position_t *pPosition, spv_diagnostic *pDiagnostic);
/// @brief Translate single Opcode and operands to binary form

View File

@ -99,7 +99,7 @@ spv_result_t spvValidateOperandValue(const spv_operand_type_t type,
case SPV_OPERAND_TYPE_LOOP_CONTROL:
case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
case SPV_OPERAND_TYPE_MEMORY_ACCESS:
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
case SPV_OPERAND_TYPE_GROUP_OPERATION:
case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
@ -135,16 +135,23 @@ spv_result_t spvValidateBasic(const spv_instruction_t *pInsts,
return SPV_ERROR_INVALID_BINARY);
position->index++;
spvCheck(opcodeEntry->wordCount > wordCount,
spvCheck(opcodeEntry->numTypes > wordCount,
DIAGNOSTIC << "Instruction word count '" << wordCount
<< "' is not small, expected at least '"
<< opcodeEntry->wordCount << "'.";
<< opcodeEntry->numTypes << "'.";
return SPV_ERROR_INVALID_BINARY);
spv_operand_desc operandEntry = nullptr;
for (uint16_t index = 1; index < pInsts[instIndex].wordCount;
++index, position->index++) {
const uint32_t word = words[index];
// TODO(dneto): This strategy is inadequate for dealing with operations
// with varying kinds or numbers of logical operands. See the definition
// of spvBinaryOperandInfo for more.
// We should really parse the instruction and capture and use
// the elaborated list of logical operands generated as a side effect
// of the parse.
spv_operand_type_t type = spvBinaryOperandInfo(
word, index, opcodeEntry, operandTable, &operandEntry);
if (SPV_OPERAND_TYPE_LITERAL_STRING == type) {

View File

@ -359,20 +359,9 @@ bool idUsage::isValid<OpTypeMatrix>(const spv_instruction_t *inst,
}
template <>
bool idUsage::isValid<OpTypeSampler>(const spv_instruction_t *inst,
bool idUsage::isValid<OpTypeSampler>(const spv_instruction_t *,
const spv_opcode_desc) {
auto sampledTypeIndex = 2;
auto sampledType = find(inst->words[sampledTypeIndex]);
spvCheck(!found(sampledType), DIAG(sampledTypeIndex)
<< "OpTypeSampler Sampled Type <id> '"
<< inst->words[sampledTypeIndex]
<< "' is not defined.";
return false);
spvCheck(!spvOpcodeIsScalarType(sampledType->second.opcode),
DIAG(sampledTypeIndex) << "OpTypeSampler Sampled Type <id> '"
<< inst->words[sampledTypeIndex]
<< "' is not a scalar type.";
return false);
// OpTypeSampler takes no arguments in Rev31 and beyond.
return true;
}
@ -957,7 +946,7 @@ bool idUsage::isValid<OpVariable>(const spv_instruction_t *inst,
<< inst->words[resultTypeIndex]
<< "' is not a pointer type.";
return false);
if (opcodeEntry->wordCount < inst->wordCount) {
if (opcodeEntry->numTypes < inst->wordCount) {
auto initialiserIndex = 4;
auto initialiser = find(inst->words[initialiserIndex]);
spvCheck(!found(initialiser), DIAG(initialiserIndex)

View File

@ -42,7 +42,7 @@ class BinaryToText : public ::testing::Test {
OpSource OpenCL 12
OpMemoryModel Physical64 OpenCL
OpSourceExtension "PlaceholderExtensionName"
OpEntryPoint Kernel %1
OpEntryPoint Kernel %1 "foo"
OpExecutionMode %1 LocalSizeHint 1 1 1
%2 = OpTypeVoid
%3 = OpTypeBool

View File

@ -32,7 +32,7 @@ TEST(NamedId, Default) {
const char *spirv = R"(
OpCapability Shader
OpMemoryModel Logical Simple
OpEntryPoint Vertex %main
OpEntryPoint Vertex %main "foo"
%void = OpTypeVoid
%fnMain = OpTypeFunction %void
%main = OpFunction %void None %fnMain

View File

@ -26,12 +26,23 @@
#include "UnitSPIRV.h"
// TODO(dneto): This function is dead anyway. We should remove its implementation
// and tests.
namespace {
TEST(OpcodeIsVariable, Default) {
TEST(OpcodeIsVariable, IsVariable) {
spv_opcode_desc_t entry = {
nullptr, 0, (Op)0, SPV_OPCODE_FLAGS_VARIABLE, 0, {}};
ASSERT_NE(0, spvOpcodeIsVariable(&entry));
nullptr, Op(0), SPV_OPCODE_FLAGS_VARIABLE, 0, 0, {}, false, false, {}};
EXPECT_TRUE(spvOpcodeIsVariable(&entry));
}
TEST(OpcodeIsVariable, NotVariable) {
spv_opcode_desc_t entryNone = {
nullptr, Op(0), SPV_OPCODE_FLAGS_NONE, 0, 0, {}, true, false, {}};
EXPECT_FALSE(spvOpcodeIsVariable(&entryNone));
spv_opcode_desc_t entryCap = {
nullptr, Op(0), SPV_OPCODE_FLAGS_CAPABILITIES, 0, 0, {}, true, false, {}};
EXPECT_FALSE(spvOpcodeIsVariable(&entryCap));
}
} // anonymous namespace

View File

@ -32,11 +32,10 @@ class Requires : public ::testing::TestWithParam<Capability> {
public:
Requires()
: entry({nullptr,
0,
(Op)0,
SPV_OPCODE_FLAGS_CAPABILITIES,
GetParam(),
{}}) {}
0, {}, false, false, {}}) {}
virtual void SetUp() {}
@ -57,7 +56,12 @@ INSTANTIATE_TEST_CASE_P(Op, Requires,
CapabilityLinkage, CapabilityKernel));
TEST(OpcodeRequiresCapabilityaspvities, None) {
spv_opcode_desc_t entry = {nullptr, 0, (Op)0, SPV_OPCODE_FLAGS_NONE, 0, {}};
spv_opcode_desc_t entry = {nullptr, (Op)0, SPV_OPCODE_FLAGS_NONE, 0, 0, {}, false, false, {}};
ASSERT_EQ(0, spvOpcodeRequiresCapabilities(&entry));
}
TEST(OpcodeRequiresCapabilityaspvities, Variable) {
spv_opcode_desc_t entry = {nullptr, (Op)0, SPV_OPCODE_FLAGS_VARIABLE, 0, 0, {}, false, false, {}};
ASSERT_EQ(0, spvOpcodeRequiresCapabilities(&entry));
}

118
test/OperandPattern.cpp Normal file
View File

@ -0,0 +1,118 @@
// 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"
#include "gmock/gmock.h"
#include "../source/operand.h"
using ::testing::Eq;
namespace {
TEST(OperandPattern, InitiallyEmpty) {
spv_operand_pattern_t empty;
EXPECT_THAT(empty, Eq(spv_operand_pattern_t{}));
EXPECT_EQ(0u, empty.size());
EXPECT_TRUE(empty.empty());
}
TEST(OperandPattern, PushFrontsAreOnTheLeft) {
spv_operand_pattern_t pattern;
pattern.push_front(SPV_OPERAND_TYPE_ID);
EXPECT_THAT(pattern, Eq(spv_operand_pattern_t{SPV_OPERAND_TYPE_ID}));
EXPECT_EQ(1u, pattern.size());
EXPECT_TRUE(!pattern.empty());
EXPECT_EQ(SPV_OPERAND_TYPE_ID, pattern.front());
pattern.push_front(SPV_OPERAND_TYPE_NONE);
EXPECT_THAT(pattern, Eq(spv_operand_pattern_t{SPV_OPERAND_TYPE_NONE,
SPV_OPERAND_TYPE_ID}));
EXPECT_EQ(2u, pattern.size());
EXPECT_TRUE(!pattern.empty());
EXPECT_EQ(SPV_OPERAND_TYPE_NONE, pattern.front());
}
TEST(OperandPattern, PopFrontsAreOnTheLeft) {
spv_operand_pattern_t pattern{SPV_OPERAND_TYPE_LITERAL_NUMBER,
SPV_OPERAND_TYPE_ID};
pattern.pop_front();
EXPECT_THAT(pattern, Eq(spv_operand_pattern_t{SPV_OPERAND_TYPE_ID}));
pattern.pop_front();
EXPECT_THAT(pattern, Eq(spv_operand_pattern_t{}));
}
// Returns a vector of all operand types that can be used in a pattern.
std::vector<spv_operand_type_t> allOperandTypes() {
std::vector<spv_operand_type_t> result;
for (int i = 0; i < SPV_OPERAND_TYPE_NUM_OPERAND_TYPES; i++) {
result.push_back(spv_operand_type_t(i));
}
return result;
}
using MatchableOperandExpansionTest =
::testing::TestWithParam<spv_operand_type_t>;
TEST_P(MatchableOperandExpansionTest, MatchableOperandsDontExpand) {
const spv_operand_type_t type = GetParam();
if (!spvOperandIsVariable(type)) {
spv_operand_pattern_t pattern;
const bool did_expand = spvExpandOperandSequenceOnce(type, &pattern);
EXPECT_EQ(false, did_expand);
EXPECT_THAT(pattern, Eq(spv_operand_pattern_t{}));
}
}
INSTANTIATE_TEST_CASE_P(MatchableOperandExpansion,
MatchableOperandExpansionTest,
::testing::ValuesIn(allOperandTypes()));
using VariableOperandExpansionTest =
::testing::TestWithParam<spv_operand_type_t>;
TEST_P(VariableOperandExpansionTest, NonMatchableOperandsExpand) {
const spv_operand_type_t type = GetParam();
if (spvOperandIsVariable(type)) {
spv_operand_pattern_t pattern;
const bool did_expand = spvExpandOperandSequenceOnce(type, &pattern);
EXPECT_EQ(true, did_expand);
EXPECT_FALSE(pattern.empty());
// For the existing rules, the first expansion of a zero-or-more operand
// type yields a matchable operand type. This isn't strictly necessary.
EXPECT_FALSE(spvOperandIsVariable(pattern.front()));
}
}
INSTANTIATE_TEST_CASE_P(NonMatchableOperandExpansion,
VariableOperandExpansionTest,
::testing::ValuesIn(allOperandTypes()));
} // anonymous namespace

View File

@ -38,26 +38,27 @@ TEST(TextDestroy, Default) {
spv_ext_inst_table extInstTable;
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
char textStr[] =
"OpSource OpenCL 12\n"
"OpMemoryModel Physical64 OpenCL\n"
"OpSourceExtension \"PlaceholderExtensionName\"\n"
"OpEntryPoint Kernel 0\n"
"OpExecutionMode 0 LocalSizeHint 1 1 1\n"
"OpTypeVoid 1\n"
"OpTypeBool 2\n"
"OpTypeInt 3 8 0\n"
"OpTypeInt 4 8 1\n"
"OpTypeInt 5 16 0\n"
"OpTypeInt 6 16 1\n"
"OpTypeInt 7 32 0\n"
"OpTypeInt 8 32 1\n"
"OpTypeInt 9 64 0\n"
"OpTypeInt 10 64 1\n"
"OpTypeFloat 11 16\n"
"OpTypeFloat 12 32\n"
"OpTypeFloat 13 64\n"
"OpTypeVector 14 3 2\n";
char textStr[] = R"(
OpSource OpenCL 12
OpMemoryModel Physical64 OpenCL
OpSourceExtension "PlaceholderExtensionName"
OpEntryPoint Kernel 0 ""
OpExecutionMode 0 LocalSizeHint 1 1 1
OpTypeVoid 1
OpTypeBool 2
OpTypeInt 3 8 0
OpTypeInt 4 8 1
OpTypeInt 5 16 0
OpTypeInt 6 16 1
OpTypeInt 7 32 0
OpTypeInt 8 32 1
OpTypeInt 9 64 0
OpTypeInt 10 64 1
OpTypeFloat 11 16
OpTypeFloat 12 32
OpTypeFloat 13 64
OpTypeVector 14 3 2
)";
spv_text_t text = {textStr, strlen(textStr)};
spv_binary binary = nullptr;
spv_diagnostic diagnostic = nullptr;

View File

@ -0,0 +1,70 @@
// 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"
#include <string>
TEST(TextStartsWithOp, YesAtStart) {
spv_position_t startPosition = {};
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("OpFoo"), &startPosition));
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("OpFoo"), &startPosition));
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("OpEnCL"), &startPosition));
}
TEST(TextStartsWithOp, YesAtMiddle) {
spv_position_t startPosition = {};
startPosition.index = 2;
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText(" OpFoo"), &startPosition));
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText(" OpFoo"), &startPosition));
}
TEST(TextStartsWithOp, NoIfTooFar) {
spv_position_t startPosition = {};
startPosition.index = 3;
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText(" OpFoo"), &startPosition));
}
TEST(TextStartsWithOp, NoRegular) {
spv_position_t startPosition = {};
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("Fee Fi Fo Fum"), &startPosition));
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("123456"), &startPosition));
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("123456"), &startPosition));
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("OpenCL"), &startPosition));
}
TEST(TextStartsWithOp, YesForValueGenerationForm) {
spv_position_t startPosition = {};
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("\%foo = OpAdd"), &startPosition));
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("\%foo = OpAdd"), &startPosition));
}
TEST(TextStartsWithOp, NoForNearlyValueGeneration) {
spv_position_t startPosition = {};
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("\%foo = "), &startPosition));
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("\%foo "), &startPosition));
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("\%foo"), &startPosition));
}

View File

@ -49,7 +49,7 @@ TEST(TextToBinary, Default) {
OpSource OpenCL 12
OpMemoryModel Physical64 OpenCL
OpSourceExtension "PlaceholderExtensionName"
OpEntryPoint Kernel %1
OpEntryPoint Kernel %1 "foo"
OpExecutionMode %1 LocalSizeHint 1 1 1
%2 = OpTypeVoid
%3 = OpTypeBool
@ -128,9 +128,11 @@ TEST(TextToBinary, Default) {
ASSERT_EQ(spvFixWord(cw.u, endian), binary->code[instIndex++]);
ASSERT_EQ(0, binary->code[instIndex++]);
ASSERT_EQ(spvOpcodeMake(3, OpEntryPoint), binary->code[instIndex++]);
ASSERT_EQ(spvOpcodeMake(4, OpEntryPoint), binary->code[instIndex++]);
ASSERT_EQ(ExecutionModelKernel, binary->code[instIndex++]);
ASSERT_EQ(1, binary->code[instIndex++]);
cw = {{'f', 'o', 'o', 0}};
ASSERT_EQ(spvFixWord(cw.u, endian), binary->code[instIndex++]);
ASSERT_EQ(spvOpcodeMake(6, OpExecutionMode), binary->code[instIndex++]);
ASSERT_EQ(1, binary->code[instIndex++]);
@ -212,7 +214,7 @@ TEST_F(TextToBinaryTest, InvalidText) {
}
TEST_F(TextToBinaryTest, InvalidTable) {
SetText("OpEntryPoint Kernel 0\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
ASSERT_EQ(SPV_ERROR_INVALID_TABLE,
spvTextToBinary(&text, nullptr, operandTable, extInstTable, &binary,
&diagnostic));
@ -225,14 +227,14 @@ TEST_F(TextToBinaryTest, InvalidTable) {
}
TEST_F(TextToBinaryTest, InvalidPointer) {
SetText("OpEntryPoint Kernel 0\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
spvTextToBinary(&text, opcodeTable, operandTable, extInstTable,
nullptr, &diagnostic));
}
TEST_F(TextToBinaryTest, InvalidDiagnostic) {
SetText("OpEntryPoint Kernel 0\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
spv_binary binary;
ASSERT_EQ(SPV_ERROR_INVALID_DIAGNOSTIC,
spvTextToBinary(&text, opcodeTable, operandTable, extInstTable,
@ -294,7 +296,7 @@ TEST_F(TextToBinaryTest, InstructionTwoFormats) {
}
}
TEST_F(TextToBinaryTest, UnknownBeginningOfInsruction) {
TEST_F(TextToBinaryTest, UnknownBeginningOfInstruction) {
SetText(R"(
OpSource OpenCL 12
OpMemoryModel Physical64 OpenCL
@ -361,4 +363,53 @@ TEST_F(TextToBinaryTest, WrongOpCode) {
if (binary) spvBinaryDestroy(binary);
}
TEST_F(TextToBinaryTest, GoodSwitch) {
const SpirvVector code = CompileSuccessfully(
R"(
%i32 = OpTypeInt 32 0
%fortytwo = OpConstant %i32 42
%twelve = OpConstant %i32 12
%entry = OpLabel
OpSwitch %fortytwo %default 42 %go42 12 %go12
%go42 = OpLabel
OpBranch %default
%go12 = OpLabel
OpBranch %default
%default = OpLabel
)");
// Minimal check: The OpSwitch opcode word is correct.
EXPECT_EQ(int(spv::OpSwitch) || (7<<16) , code[14]);
}
TEST_F(TextToBinaryTest, GoodSwitchZeroCasesOneDefault) {
const SpirvVector code = CompileSuccessfully(R"(
%i32 = OpTypeInt 32 0
%fortytwo = OpConstant %i32 42
%entry = OpLabel
OpSwitch %fortytwo %default
%default = OpLabel
)");
// Minimal check: The OpSwitch opcode word is correct.
EXPECT_EQ(int(spv::OpSwitch) || (3<<16) , code[10]);
}
TEST_F(TextToBinaryTest, BadSwitchTruncatedCase) {
SetText(R"(
%i32 = OpTypeInt 32 0
%fortytwo = OpConstant %i32 42
%entry = OpLabel
OpSwitch %fortytwo %default 42 ; missing target!
%default = OpLabel
)");
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
spvTextToBinary(&text, opcodeTable, operandTable, extInstTable,
&binary, &diagnostic));
EXPECT_EQ(6, diagnostic->position.line + 1);
EXPECT_EQ(1, diagnostic->position.column + 1);
EXPECT_STREQ("Expected operand, found next instruction instead.", diagnostic->error);
}
} // anonymous namespace

View File

@ -49,7 +49,7 @@ class Validate : public ::testing::Test {
TEST_F(Validate, DISABLED_Default) {
char str[] = R"(
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute 1
OpEntryPoint GLCompute 1 ""
OpExecutionMode 1 LocalSize 1 1 1
OpTypeVoid 2
OpTypeFunction 3 2
@ -74,7 +74,7 @@ OpFunctionEnd
TEST_F(Validate, DISABLED_InvalidIdUndefined) {
char str[] = R"(
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute 1
OpEntryPoint GLCompute 1 ""
OpExecutionMode 5 LocalSize 1 1 1
OpTypeVoid 2
OpTypeFunction 3 2
@ -98,7 +98,7 @@ OpFunctionEnd
TEST_F(Validate, DISABLED_InvalidIdRedefined) {
char str[] = R"(
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute 1
OpEntryPoint GLCompute 1 ""
OpExecutionMode 1 LocalSize 1 1 1
OpTypeVoid 2
OpTypeFunction 2 2

View File

@ -104,6 +104,8 @@ TEST_F(ValidateID, OpMemberNameMemberBad) {
}
TEST_F(ValidateID, OpLineGood) {
// TODO(dneto): OpLine changed after Rev31. It no longer has a first argument.
// The following is the Rev31 form.
const char *spirv = R"(
%1 = OpString "/path/to/source.file"
OpLine %4 %1 0 0
@ -113,6 +115,8 @@ TEST_F(ValidateID, OpLineGood) {
CHECK(spirv, SPV_SUCCESS);
}
TEST_F(ValidateID, OpLineFileBad) {
// TODO(dneto): OpLine changed after Rev31. It no longer has a first argument.
// The following is the Rev31 form.
const char *spirv = R"(
OpLine %4 %2 0 0
%2 = OpTypeInt 32 0
@ -189,7 +193,7 @@ TEST_F(ValidateID, OpGroupDecorateTargetBad) {
TEST_F(ValidateID, OpEntryPointGood) {
const char *spirv = R"(
OpEntryPoint GLCompute %3
OpEntryPoint GLCompute %3 ""
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpFunction %1 None %2
@ -201,13 +205,13 @@ TEST_F(ValidateID, OpEntryPointGood) {
}
TEST_F(ValidateID, OpEntryPointFunctionBad) {
const char *spirv = R"(
OpEntryPoint GLCompute %1
OpEntryPoint GLCompute %1 ""
%1 = OpTypeVoid)";
CHECK(spirv, SPV_ERROR_INVALID_ID);
}
TEST_F(ValidateID, OpEntryPointParameterCountBad) {
const char *spirv = R"(
OpEntryPoint GLCompute %3
OpEntryPoint GLCompute %3 ""
%1 = OpTypeVoid
%2 = OpTypeFunction %1 %1
%3 = OpFunction %1 None %2
@ -218,7 +222,7 @@ TEST_F(ValidateID, OpEntryPointParameterCountBad) {
}
TEST_F(ValidateID, OpEntryPointReturnTypeBad) {
const char *spirv = R"(
OpEntryPoint GLCompute %3
OpEntryPoint GLCompute %3 ""
%1 = OpTypeInt 32 0
%2 = OpTypeFunction %1
%3 = OpFunction %1 None %2
@ -230,7 +234,7 @@ TEST_F(ValidateID, OpEntryPointReturnTypeBad) {
TEST_F(ValidateID, OpExecutionModeGood) {
const char *spirv = R"(
OpEntryPoint GLCompute %3
OpEntryPoint GLCompute %3 ""
OpExecutionMode %3 LocalSize 1 1 1
%1 = OpTypeVoid
%2 = OpTypeFunction %1
@ -281,17 +285,11 @@ TEST_F(ValidateID, OpTypeMatrixColumnTypeBad) {
}
TEST_F(ValidateID, OpTypeSamplerGood) {
// In Rev31, OpTypeSampler takes no arguments.
const char *spirv = R"(
%1 = OpTypeFloat 32
%2 = OpTypeSampler %1 2D 0 0 0 0)";
%s = OpTypeSampler)";
CHECK(spirv, SPV_SUCCESS);
}
TEST_F(ValidateID, OpTypeSamplerSampledTypeBad) {
const char *spirv = R"(
%1 = OpTypeVoid
%2 = OpTypeSampler %1 2D 0 0 0 0)";
CHECK(spirv, SPV_ERROR_INVALID_ID);
}
TEST_F(ValidateID, OpTypeArrayGood) {
const char *spirv = R"(
@ -556,9 +554,9 @@ TEST_F(ValidateID, OpConstantCompositeStructMemberBad) {
TEST_F(ValidateID, OpConstantSamplerGood) {
const char *spirv = R"(
%1 = OpTypeFloat 32
%2 = OpTypeSampler %1 2D 1 0 1 0
%3 = OpConstantSampler %2 ClampToEdge 0 Nearest)";
%float = OpTypeFloat 32
%samplerType = OpTypeSampler
%3 = OpConstantSampler %samplerType ClampToEdge 0 Nearest)";
CHECK(spirv, SPV_SUCCESS);
}
TEST_F(ValidateID, OpConstantSamplerResultTypeBad) {
@ -606,9 +604,8 @@ TEST_F(ValidateID, OpConstantNullBasicBad) {
}
TEST_F(ValidateID, OpConstantNullArrayBad) {
const char *spirv = R"(
%1 = OpTypeInt 8 0
%2 = OpTypeInt 32 0
%3 = OpTypeSampler %1 2D 0 0 0 0
%3 = OpTypeSampler
%4 = OpConstant %2 4
%5 = OpTypeArray %3 %4
%6 = OpConstantNull %5)";
@ -616,8 +613,7 @@ TEST_F(ValidateID, OpConstantNullArrayBad) {
}
TEST_F(ValidateID, OpConstantNullStructBad) {
const char *spirv = R"(
%1 = OpTypeInt 8 0
%2 = OpTypeSampler %1 2D 0 0 0 0
%2 = OpTypeSampler
%3 = OpTypeStruct %2 %2
%4 = OpConstantNull %3)";
CHECK(spirv, SPV_ERROR_INVALID_ID);
@ -1110,7 +1106,8 @@ TEST_F(ValidateID, OpFunctionCallArgumentCountBar) {
}
#endif
// TODO: OpSampler
// TODO: OpSampledImage
// TODO: The many things that changed with how images are used.
// TODO: OpTextureSample
// TODO: OpTextureSampleDref
// TODO: OpTextureSampleLod