blob: 78462043d5ecb2cdf2dc83cd5334d43bff8ddebe [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
David Netob5dc8fc2015-10-06 16:22:00 -040018#include "instruction.h"
Andrey Tuganovaf7d5792017-12-13 17:55:00 -050019#include "latest_version_spirv_header.h"
David Neto5a703352016-02-17 14:44:00 -050020#include "spirv-tools/libspirv.h"
Lei Zhang7a222e42015-11-11 12:40:25 -050021#include "table.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// Gets the name of an instruction, without the "Op" prefix.
Lei Zhang1a0334e2015-11-02 09:41:20 -050060const char* spvOpcodeString(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010061
Lei Zhangaf9906e2015-11-16 10:48:43 -050062// Determine if the given opcode is a scalar type. Returns zero if false,
63// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040064int32_t spvOpcodeIsScalarType(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010065
Alan Baker0a2ee652018-03-23 14:18:54 -040066// Determines if the given opcode is a specialization constant. Returns zero if
67// false, non-zero otherwise.
68int32_t spvOpcodeIsSpecConstant(const SpvOp opcode);
69
Lei Zhangaf9906e2015-11-16 10:48:43 -050070// Determines if the given opcode is a constant. Returns zero if false, non-zero
71// otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040072int32_t spvOpcodeIsConstant(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073
David Neto1f3fb502016-09-14 11:57:20 -040074// Returns true if the given opcode is a constant or undef.
75bool spvOpcodeIsConstantOrUndef(const SpvOp opcode);
76
Aliya Pazylbekovaedb52642017-02-24 20:43:28 -050077// Returns true if the given opcode is a scalar specialization constant.
78bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode);
79
Lei Zhangaf9906e2015-11-16 10:48:43 -050080// Determines if the given opcode is a composite type. Returns zero if false,
81// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040082int32_t spvOpcodeIsComposite(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010083
Florian Ziesche66fcb452016-03-02 22:17:54 +010084// Determines if the given opcode results in a pointer when using the logical
85// addressing model. Returns zero if false, non-zero otherwise.
86int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010087
Ehsan Nasiri23af06c2017-02-01 15:37:39 -050088// Returns whether the given opcode could result in a pointer or a variable
89// pointer when using the logical addressing model.
90bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode);
91
Lei Zhangaf9906e2015-11-16 10:48:43 -050092// Determines if the given opcode generates a type. Returns zero if false,
93// non-zero otherwise.
94int32_t spvOpcodeGeneratesType(SpvOp opcode);
Andrew Woloszyn537e7762015-09-29 11:28:34 -040095
Steven Perron28c41552017-11-10 20:26:55 -050096// Returns true if the opcode adds a decoration to an id.
Steven Perroneb4653a2017-11-13 15:31:43 -050097bool spvOpcodeIsDecoration(const SpvOp opcode);
98
Steven Perron28c41552017-11-10 20:26:55 -050099// Returns true if the opcode is a load from memory into a result id. This
100// function only considers core instructions.
101bool spvOpcodeIsLoad(const SpvOp opcode);
102
103// Returns true if the opcode is an atomic operation.
104bool spvOpcodeIsAtomicOp(const SpvOp opcode);
105
Diego Novillo74327842017-11-17 08:59:25 -0500106// Returns true if the given opcode is a branch instruction.
107bool spvOpcodeIsBranch(SpvOp opcode);
108
109// Returns true if the given opcode is a return instruction.
110bool spvOpcodeIsReturn(SpvOp opcode);
111
Diego Novillo5f100782018-01-03 15:25:03 -0500112// Returns true if the given opcode is a return instruction or it aborts
113// execution.
114bool spvOpcodeIsReturnOrAbort(SpvOp opcode);
115
Diego Novillo74327842017-11-17 08:59:25 -0500116// Returns true if the given opcode is a basic block terminator.
117bool spvOpcodeIsBlockTerminator(SpvOp opcode);
118
Steven Perron79a00642017-12-11 13:10:24 -0500119// Returns true if the given opcode always defines an opaque type.
120bool spvOpcodeIsBaseOpaqueType(SpvOp opcode);
Alan Baker09c206b2018-04-17 10:18:59 -0400121
122// Returns true if the given opcode is a non-uniform group operation.
123bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode);
Steven Perron2c0ce872018-04-23 11:13:07 -0400124
Steven Perronee8cd5c2018-04-23 11:17:07 -0400125// Returns true if the opcode with vector inputs could be divided into a series
126// of independent scalar operations that would give the same result.
Steven Perron2c0ce872018-04-23 11:13:07 -0400127bool spvOpcodeIsScalarizable(SpvOp opcode);
dan sinclair58a68762018-08-03 08:05:33 -0400128#endif // SOURCE_OPCODE_H_