LLVM 23.0.0git
SPIRVBaseInfo.cpp
Go to the documentation of this file.
1//===-- SPIRVBaseInfo.cpp - Top level SPIRV definitions ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation for helper mnemonic lookup functions,
10// versioning/capabilities/extensions getters for symbolic/named operands used
11// in various SPIR-V instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SPIRVBaseInfo.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringRef.h"
19
20namespace llvm {
21namespace SPIRV {
29
31 OperandCategory::OperandCategory Category;
33 Extension::Extension ReqExtension;
34};
35
37 OperandCategory::OperandCategory Category;
39 Capability::Capability ReqCapability;
40};
41
43 OperandCategory::OperandCategory Category;
45 Environment::Environment AllowedEnvironment;
46};
47
48using namespace OperandCategory;
49using namespace Extension;
50using namespace Environment;
51using namespace Capability;
52using namespace InstructionSet;
53#define GET_SymbolicOperands_DECL
54#define GET_SymbolicOperands_IMPL
55#define GET_ExtensionEntries_DECL
56#define GET_ExtensionEntries_IMPL
57#define GET_CapabilityEntries_DECL
58#define GET_CapabilityEntries_IMPL
59#define GET_EnvironmentEntries_DECL
60#define GET_EnvironmentEntries_IMPL
61#define GET_ExtendedBuiltins_DECL
62#define GET_ExtendedBuiltins_IMPL
63#include "SPIRVGenTables.inc"
64} // namespace SPIRV
65
66std::string
67getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category,
68 int32_t Value) {
70 SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value);
71 // Value that encodes just one enum value.
72 if (Lookup)
73 return SPIRV::getSymbolicOperandStr(Lookup->Mnemonic).str();
74 if (Category != SPIRV::OperandCategory::ImageOperandOperand &&
75 Category != SPIRV::OperandCategory::FPFastMathModeOperand &&
76 Category != SPIRV::OperandCategory::SelectionControlOperand &&
77 Category != SPIRV::OperandCategory::LoopControlOperand &&
78 Category != SPIRV::OperandCategory::FunctionControlOperand &&
79 Category != SPIRV::OperandCategory::MemorySemanticsOperand &&
80 Category != SPIRV::OperandCategory::MemoryOperandOperand &&
81 Category != SPIRV::OperandCategory::KernelProfilingInfoOperand &&
82 Category != SPIRV::OperandCategory::SpecConstantOpOperandsOperand)
83 return "UNKNOWN";
84 // Value that encodes many enum values (one bit per enum value).
85 std::string Name;
86 std::string Separator;
87 const SPIRV::SymbolicOperand *EnumValueInCategory =
88 SPIRV::lookupSymbolicOperandByCategory(Category);
89
90 auto TableEnd = ArrayRef(SPIRV::SymbolicOperands).end();
91 while (EnumValueInCategory && EnumValueInCategory->Category == Category) {
92 if ((EnumValueInCategory->Value != 0) &&
93 (Value & EnumValueInCategory->Value)) {
94 Name += Separator +
95 SPIRV::getSymbolicOperandStr(EnumValueInCategory->Mnemonic).str();
96 Separator = "|";
97 }
98 if (++EnumValueInCategory == TableEnd)
99 break;
100 }
101
102 return Name;
103}
104
105VersionTuple
106getSymbolicOperandMinVersion(SPIRV::OperandCategory::OperandCategory Category,
107 uint32_t Value) {
109 SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value);
110
111 if (Lookup)
112 return VersionTuple(Lookup->MinVersion / 10, Lookup->MinVersion % 10);
113
114 return VersionTuple(0);
115}
116
117VersionTuple
118getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category,
119 uint32_t Value) {
121 SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value);
122
123 if (Lookup)
124 return VersionTuple(Lookup->MaxVersion / 10, Lookup->MaxVersion % 10);
125
126 return VersionTuple();
127}
128
130getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category,
131 uint32_t Value) {
132 CapabilityList Capabilities;
133 const SPIRV::CapabilityEntry *Capability =
134 SPIRV::lookupCapabilityByCategoryAndValue(Category, Value);
135 auto TableEnd = ArrayRef(SPIRV::CapabilityEntries).end();
136 while (Capability && Capability->Category == Category &&
137 Capability->Value == Value) {
138 Capabilities.push_back(
139 static_cast<SPIRV::Capability::Capability>(Capability->ReqCapability));
140 if (++Capability == TableEnd)
141 break;
142 }
143
144 return Capabilities;
145}
146
148 SPIRV::OperandCategory::OperandCategory Category, uint32_t Value) {
149 EnvironmentList Environments;
150 const SPIRV::EnvironmentEntry *Environment =
151 SPIRV::lookupEnvironmentByCategoryAndValue(Category, Value);
152 auto TableEnd = ArrayRef(SPIRV::EnvironmentEntries).end();
153 while (Environment && Environment->Category == Category &&
154 Environment->Value == Value) {
155 Environments.push_back(static_cast<SPIRV::Environment::Environment>(
156 Environment->AllowedEnvironment));
157 if (++Environment == TableEnd)
158 break;
159 }
160
161 return Environments;
162}
163
165getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension) {
166 const SPIRV::ExtensionEntry *Entry =
167 SPIRV::lookupSymbolicOperandsEnabledByExtension(
168 Extension, SPIRV::OperandCategory::CapabilityOperand);
169
170 CapabilityList Capabilities;
171 auto TableEnd = ArrayRef(SPIRV::ExtensionEntries).end();
172 while (Entry &&
173 Entry->Category == SPIRV::OperandCategory::CapabilityOperand) {
174 // Some capabilities' codes might go not in order.
175 if (Entry->ReqExtension == Extension)
176 Capabilities.push_back(
177 static_cast<SPIRV::Capability::Capability>(Entry->Value));
178 if (++Entry == TableEnd)
179 break;
180 }
181
182 return Capabilities;
183}
184
186getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category,
187 uint32_t Value) {
188 const SPIRV::ExtensionEntry *Extension =
189 SPIRV::lookupExtensionByCategoryAndValue(Category, Value);
190
192 auto TableEnd = ArrayRef(SPIRV::ExtensionEntries).end();
193 while (Extension && Extension->Category == Category &&
194 Extension->Value == Value) {
195 Extensions.push_back(
196 static_cast<SPIRV::Extension::Extension>(Extension->ReqExtension));
197 if (++Extension == TableEnd)
198 break;
199 }
200
201 return Extensions;
202}
203
204std::string getLinkStringForBuiltIn(SPIRV::BuiltIn::BuiltIn BuiltInValue) {
206 SPIRV::lookupSymbolicOperandByCategoryAndValue(
207 SPIRV::OperandCategory::BuiltInOperand, BuiltInValue);
208
209 if (Lookup)
210 return "__spirv_BuiltIn" +
211 SPIRV::getSymbolicOperandStr(Lookup->Mnemonic).str();
212 return "UNKNOWN_BUILTIN";
213}
214
216 SPIRV::BuiltIn::BuiltIn &BI) {
217 const std::string Prefix = "__spirv_BuiltIn";
218 if (!Name.starts_with(Prefix))
219 return false;
220
222 SPIRV::lookupSymbolicOperandByCategoryAndMnemonic(
223 SPIRV::OperandCategory::BuiltInOperand,
224 Name.drop_front(Prefix.length()));
225
226 if (!Lookup)
227 return false;
228
229 BI = static_cast<SPIRV::BuiltIn::BuiltIn>(Lookup->Value);
230 return true;
231}
232
233std::string getExtInstSetName(SPIRV::InstructionSet::InstructionSet Set) {
234 switch (Set) {
235 case SPIRV::InstructionSet::OpenCL_std:
236 return "OpenCL.std";
237 case SPIRV::InstructionSet::GLSL_std_450:
238 return "GLSL.std.450";
239 case SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100:
240 return "NonSemantic.Shader.DebugInfo.100";
241 case SPIRV::InstructionSet::NonSemantic_AuxData:
242 return "NonSemantic.AuxData";
243 case SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax:
244 return "SPV_AMD_shader_trinary_minmax";
245 }
246 return "UNKNOWN_EXT_INST_SET";
247}
248
249SPIRV::InstructionSet::InstructionSet
250getExtInstSetFromString(std::string SetName) {
251 for (auto Set :
252 {SPIRV::InstructionSet::GLSL_std_450, SPIRV::InstructionSet::OpenCL_std,
253 SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100,
254 SPIRV::InstructionSet::NonSemantic_AuxData}) {
255 if (SetName == getExtInstSetName(Set))
256 return Set;
257 }
258 llvm_unreachable("UNKNOWN_EXT_INST_SET");
259}
260
261std::string getExtInstName(SPIRV::InstructionSet::InstructionSet Set,
262 uint32_t InstructionNumber) {
264 SPIRV::lookupExtendedBuiltinBySetAndNumber(Set, InstructionNumber);
265
266 if (!Lookup)
267 return "UNKNOWN_EXT_INST";
268
269 return SPIRV::getExtendedBuiltinStr(Lookup->Name).str();
270}
271} // namespace llvm
static cl::opt< ExtensionSet, false, SPIRVExtensionsParser > Extensions("spirv-ext", cl::desc("Specify list of enabled SPIR-V extensions"))
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
Represents a version number in the form major[.minor[.subminor[.build]]].
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
SmallVector< SPIRV::Environment::Environment, 8 > EnvironmentList
ExtensionList getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
std::string getExtInstName(SPIRV::InstructionSet::InstructionSet Set, uint32_t InstructionNumber)
CapabilityList getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
SmallVector< SPIRV::Extension::Extension, 8 > ExtensionList
std::string getExtInstSetName(SPIRV::InstructionSet::InstructionSet Set)
bool getSpirvBuiltInIdByName(llvm::StringRef Name, SPIRV::BuiltIn::BuiltIn &BI)
VersionTuple getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
EnvironmentList getSymbolicOperandAllowedEnvironments(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
CapabilityList getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension)
SPIRV::InstructionSet::InstructionSet getExtInstSetFromString(std::string SetName)
std::string getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category, int32_t Value)
ArrayRef(const T &OneElt) -> ArrayRef< T >
VersionTuple getSymbolicOperandMinVersion(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
SmallVector< SPIRV::Capability::Capability, 8 > CapabilityList
std::string getLinkStringForBuiltIn(SPIRV::BuiltIn::BuiltIn BuiltInValue)
Capability::Capability ReqCapability
OperandCategory::OperandCategory Category
Environment::Environment AllowedEnvironment
OperandCategory::OperandCategory Category
Extension::Extension ReqExtension
OperandCategory::OperandCategory Category
OperandCategory::OperandCategory Category
StringTable::Offset Mnemonic