blob: 9aeba76e0720363aa42884c4dc7f3b948e2ed11b [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
dan sinclair58a68762018-08-03 08:05:33 -040015#ifndef SOURCE_OPCODE_H_
16#define SOURCE_OPCODE_H_
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010017
dan sinclaireda2cfb2018-08-03 15:06:09 -040018#include "source/instruction.h"
19#include "source/latest_version_spirv_header.h"
20#include "source/table.h"
David Neto5a703352016-02-17 14:44:00 -050021#include "spirv-tools/libspirv.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010022
Lei Zhangaf9906e2015-11-16 10:48:43 -050023// Returns the name of a registered SPIR-V generator as a null-terminated
24// string. If the generator is not known, then returns the string "Unknown".
25// The generator parameter should be most significant 16-bits of the generator
26// word in the SPIR-V module header.
27//
28// See the registry at https://www.khronos.org/registry/spir-v/api/spir-v.xml.
Lei Zhang1a0334e2015-11-02 09:41:20 -050029const char* spvGeneratorStr(uint32_t generator);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030
Lei Zhangaf9906e2015-11-16 10:48:43 -050031// Combines word_count and opcode enumerant in single word.
32uint32_t spvOpcodeMake(uint16_t word_count, SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010033
Lei Zhangaf9906e2015-11-16 10:48:43 -050034// Splits word into into two constituent parts: word_count and opcode.
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040035void spvOpcodeSplit(const uint32_t word, uint16_t* word_count,
36 uint16_t* opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010037
Lei Zhangaf9906e2015-11-16 10:48:43 -050038// Finds the named opcode in the given opcode table. On success, returns
39// SPV_SUCCESS and writes a handle of the table entry into *entry.
Lei Zhang1ef6b192018-03-14 13:06:18 -040040spv_result_t spvOpcodeTableNameLookup(spv_target_env,
41 const spv_opcode_table table,
Lei Zhangaf9906e2015-11-16 10:48:43 -050042 const char* name, spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010043
Lei Zhangaf9906e2015-11-16 10:48:43 -050044// Finds the opcode by enumerant in the given opcode table. On success, returns
45// SPV_SUCCESS and writes a handle of the table entry into *entry.
Lei Zhang1ef6b192018-03-14 13:06:18 -040046spv_result_t spvOpcodeTableValueLookup(spv_target_env,
47 const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -040048 const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050049 spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050
Lei Zhangaf9906e2015-11-16 10:48:43 -050051// Copies an instruction's word and fixes the endianness to host native. The
52// source instruction's stream/opcode/endianness is in the words/opcode/endian
53// parameter. The word_count parameter specifies the number of words to copy.
54// Writes copied instruction into *inst.
Lei Zhang1a0334e2015-11-02 09:41:20 -050055void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050056 const uint16_t word_count,
57 const spv_endianness_t endian, spv_instruction_t* inst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010058
Lei Zhangaf9906e2015-11-16 10:48:43 -050059// Determine if the given opcode is a scalar type. Returns zero if false,
60// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040061int32_t spvOpcodeIsScalarType(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010062
Alan Baker0a2ee652018-03-23 14:18:54 -040063// Determines if the given opcode is a specialization constant. Returns zero if
64// false, non-zero otherwise.
65int32_t spvOpcodeIsSpecConstant(const SpvOp opcode);
66
Lei Zhangaf9906e2015-11-16 10:48:43 -050067// Determines if the given opcode is a constant. Returns zero if false, non-zero
68// otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040069int32_t spvOpcodeIsConstant(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010070
David Neto1f3fb502016-09-14 11:57:20 -040071// Returns true if the given opcode is a constant or undef.
72bool spvOpcodeIsConstantOrUndef(const SpvOp opcode);
73
Aliya Pazylbekovaedb52642017-02-24 20:43:28 -050074// Returns true if the given opcode is a scalar specialization constant.
75bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode);
76
Lei Zhangaf9906e2015-11-16 10:48:43 -050077// Determines if the given opcode is a composite type. Returns zero if false,
78// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040079int32_t spvOpcodeIsComposite(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010080
Florian Ziesche66fcb452016-03-02 22:17:54 +010081// Determines if the given opcode results in a pointer when using the logical
82// addressing model. Returns zero if false, non-zero otherwise.
83int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010084
Ehsan Nasiri23af06c2017-02-01 15:37:39 -050085// Returns whether the given opcode could result in a pointer or a variable
86// pointer when using the logical addressing model.
87bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode);
88
Lei Zhangaf9906e2015-11-16 10:48:43 -050089// Determines if the given opcode generates a type. Returns zero if false,
90// non-zero otherwise.
91int32_t spvOpcodeGeneratesType(SpvOp opcode);
Andrew Woloszyn537e7762015-09-29 11:28:34 -040092
Steven Perron28c41552017-11-10 20:26:55 -050093// Returns true if the opcode adds a decoration to an id.
Steven Perroneb4653a2017-11-13 15:31:43 -050094bool spvOpcodeIsDecoration(const SpvOp opcode);
95
Steven Perron28c41552017-11-10 20:26:55 -050096// Returns true if the opcode is a load from memory into a result id. This
97// function only considers core instructions.
98bool spvOpcodeIsLoad(const SpvOp opcode);
99
greg-lunarge5455642018-10-12 06:46:35 -0600100// Returns true if the opcode is an atomic operation that uses the original
101// value.
102bool spvOpcodeIsAtomicWithLoad(const SpvOp opcode);
103
Steven Perron28c41552017-11-10 20:26:55 -0500104// Returns true if the opcode is an atomic operation.
105bool spvOpcodeIsAtomicOp(const SpvOp opcode);
106
Diego Novillo74327842017-11-17 08:59:25 -0500107// Returns true if the given opcode is a branch instruction.
108bool spvOpcodeIsBranch(SpvOp opcode);
109
110// Returns true if the given opcode is a return instruction.
111bool spvOpcodeIsReturn(SpvOp opcode);
112
Diego Novillo5f100782018-01-03 15:25:03 -0500113// Returns true if the given opcode is a return instruction or it aborts
114// execution.
115bool spvOpcodeIsReturnOrAbort(SpvOp opcode);
116
Diego Novillo74327842017-11-17 08:59:25 -0500117// Returns true if the given opcode is a basic block terminator.
118bool spvOpcodeIsBlockTerminator(SpvOp opcode);
119
Steven Perron79a00642017-12-11 13:10:24 -0500120// Returns true if the given opcode always defines an opaque type.
121bool spvOpcodeIsBaseOpaqueType(SpvOp opcode);
Alan Baker09c206b2018-04-17 10:18:59 -0400122
123// Returns true if the given opcode is a non-uniform group operation.
124bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode);
Steven Perron2c0ce872018-04-23 11:13:07 -0400125
Steven Perronee8cd5c2018-04-23 11:17:07 -0400126// Returns true if the opcode with vector inputs could be divided into a series
127// of independent scalar operations that would give the same result.
Steven Perron2c0ce872018-04-23 11:13:07 -0400128bool spvOpcodeIsScalarizable(SpvOp opcode);
Ryan Harrison11c7a9e2018-11-20 16:12:28 -0500129
130// Returns true if the given opcode is a debug instruction.
131bool spvOpcodeIsDebug(SpvOp opcode);
132
Alastair Donaldson66a682b2020-03-05 08:18:39 +0000133// Returns true for opcodes that are binary operators,
134// where the order of the operands is irrelevant.
135bool spvOpcodeIsCommutativeBinaryOperator(SpvOp opcode);
136
André Perez12a4fb32020-06-16 07:20:51 -0300137// Returns true for opcodes that represents linear algebra instructions.
138bool spvOpcodeIsLinearAlgebra(SpvOp opcode);
139
André Perezdaa3b472020-07-08 13:07:04 -0300140// Returns true for opcodes that represents an image sample instruction.
141bool spvOpcodeIsImageSample(SpvOp opcode);
142
Ryan Harrisone5455222019-03-14 13:34:33 -0400143// Returns a vector containing the indices of the memory semantics <id>
144// operands for |opcode|.
145std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode);
146
dan sinclair58a68762018-08-03 08:05:33 -0400147#endif // SOURCE_OPCODE_H_