LLVM 20.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"
18
19namespace llvm {
20namespace SPIRV {
22 OperandCategory::OperandCategory Category;
27};
28
30 OperandCategory::OperandCategory Category;
32 Extension::Extension ReqExtension;
33};
34
36 OperandCategory::OperandCategory Category;
38 Capability::Capability ReqCapability;
39};
40
41using namespace OperandCategory;
42using namespace Extension;
43using namespace Capability;
44using namespace InstructionSet;
45#define GET_SymbolicOperands_DECL
46#define GET_SymbolicOperands_IMPL
47#define GET_ExtensionEntries_DECL
48#define GET_ExtensionEntries_IMPL
49#define GET_CapabilityEntries_DECL
50#define GET_CapabilityEntries_IMPL
51#define GET_ExtendedBuiltins_DECL
52#define GET_ExtendedBuiltins_IMPL
53#include "SPIRVGenTables.inc"
54} // namespace SPIRV
55
56std::string
57getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category,
58 int32_t Value) {
60 SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value);
61 // Value that encodes just one enum value.
62 if (Lookup)
63 return Lookup->Mnemonic.str();
64 if (Category != SPIRV::OperandCategory::ImageOperandOperand &&
65 Category != SPIRV::OperandCategory::FPFastMathModeOperand &&
66 Category != SPIRV::OperandCategory::SelectionControlOperand &&
67 Category != SPIRV::OperandCategory::LoopControlOperand &&
68 Category != SPIRV::OperandCategory::FunctionControlOperand &&
69 Category != SPIRV::OperandCategory::MemorySemanticsOperand &&
70 Category != SPIRV::OperandCategory::MemoryOperandOperand &&
71 Category != SPIRV::OperandCategory::KernelProfilingInfoOperand)
72 return "UNKNOWN";
73 // Value that encodes many enum values (one bit per enum value).
74 std::string Name;
75 std::string Separator;
76 const SPIRV::SymbolicOperand *EnumValueInCategory =
77 SPIRV::lookupSymbolicOperandByCategory(Category);
78
79 while (EnumValueInCategory && EnumValueInCategory->Category == Category) {
80 if ((EnumValueInCategory->Value != 0) &&
81 (Value & EnumValueInCategory->Value)) {
82 Name += Separator + EnumValueInCategory->Mnemonic.str();
83 Separator = "|";
84 }
85 ++EnumValueInCategory;
86 }
87
88 return Name;
89}
90
91VersionTuple
92getSymbolicOperandMinVersion(SPIRV::OperandCategory::OperandCategory Category,
95 SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value);
96
97 if (Lookup)
98 return VersionTuple(Lookup->MinVersion / 10, Lookup->MinVersion % 10);
99
100 return VersionTuple(0);
101}
102
103VersionTuple
104getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category,
105 uint32_t Value) {
107 SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value);
108
109 if (Lookup)
110 return VersionTuple(Lookup->MaxVersion / 10, Lookup->MaxVersion % 10);
111
112 return VersionTuple();
113}
114
116getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category,
117 uint32_t Value) {
118 const SPIRV::CapabilityEntry *Capability =
119 SPIRV::lookupCapabilityByCategoryAndValue(Category, Value);
120
121 CapabilityList Capabilities;
122 while (Capability && Capability->Category == Category &&
123 Capability->Value == Value) {
124 Capabilities.push_back(
125 static_cast<SPIRV::Capability::Capability>(Capability->ReqCapability));
126 ++Capability;
127 }
128
129 return Capabilities;
130}
131
133getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension) {
134 const SPIRV::ExtensionEntry *Entry =
135 SPIRV::lookupSymbolicOperandsEnabledByExtension(
136 Extension, SPIRV::OperandCategory::CapabilityOperand);
137
138 CapabilityList Capabilities;
139 while (Entry &&
140 Entry->Category == SPIRV::OperandCategory::CapabilityOperand &&
141 Entry->ReqExtension == Extension) {
142 Capabilities.push_back(
143 static_cast<SPIRV::Capability::Capability>(Entry->Value));
144 ++Entry;
145 }
146
147 return Capabilities;
148}
149
151getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category,
152 uint32_t Value) {
154 SPIRV::lookupExtensionByCategoryAndValue(Category, Value);
155
157 while (Extension && Extension->Category == Category &&
158 Extension->Value == Value) {
159 Extensions.push_back(
160 static_cast<SPIRV::Extension::Extension>(Extension->ReqExtension));
161 ++Extension;
162 }
163
164 return Extensions;
165}
166
167std::string getLinkStringForBuiltIn(SPIRV::BuiltIn::BuiltIn BuiltInValue) {
169 SPIRV::lookupSymbolicOperandByCategoryAndValue(
170 SPIRV::OperandCategory::BuiltInOperand, BuiltInValue);
171
172 if (Lookup)
173 return "__spirv_BuiltIn" + Lookup->Mnemonic.str();
174 return "UNKNOWN_BUILTIN";
175}
176
178 SPIRV::BuiltIn::BuiltIn &BI) {
179 const std::string Prefix = "__spirv_BuiltIn";
180 if (!Name.starts_with(Prefix))
181 return false;
182
184 SPIRV::lookupSymbolicOperandByCategoryAndMnemonic(
185 SPIRV::OperandCategory::BuiltInOperand,
186 Name.drop_front(Prefix.length()));
187
188 if (!Lookup)
189 return false;
190
191 BI = static_cast<SPIRV::BuiltIn::BuiltIn>(Lookup->Value);
192 return true;
193}
194
195std::string getExtInstSetName(SPIRV::InstructionSet::InstructionSet Set) {
196 switch (Set) {
197 case SPIRV::InstructionSet::OpenCL_std:
198 return "OpenCL.std";
199 case SPIRV::InstructionSet::GLSL_std_450:
200 return "GLSL.std.450";
201 case SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100:
202 return "NonSemantic.Shader.DebugInfo.100";
203 case SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax:
204 return "SPV_AMD_shader_trinary_minmax";
205 }
206 return "UNKNOWN_EXT_INST_SET";
207}
208
209SPIRV::InstructionSet::InstructionSet
210getExtInstSetFromString(std::string SetName) {
211 for (auto Set :
212 {SPIRV::InstructionSet::GLSL_std_450, SPIRV::InstructionSet::OpenCL_std,
213 SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100}) {
214 if (SetName == getExtInstSetName(Set))
215 return Set;
216 }
217 llvm_unreachable("UNKNOWN_EXT_INST_SET");
218}
219
220std::string getExtInstName(SPIRV::InstructionSet::InstructionSet Set,
221 uint32_t InstructionNumber) {
223 SPIRV::lookupExtendedBuiltinBySetAndNumber(Set, InstructionNumber);
224
225 if (!Lookup)
226 return "UNKNOWN_EXT_INST";
227
228 return Lookup->Name.str();
229}
230} // namespace llvm
std::string Name
static cl::opt< std::set< SPIRV::Extension::Extension >, 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)
Definition: SmallVector.h:427
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:215
LLVM Value Representation.
Definition: Value.h:74
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
#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.
Definition: AddressRanges.h:18
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)
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)
CapabilityList getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension)
SPIRV::InstructionSet::InstructionSet getExtInstSetFromString(std::string SetName)
std::string getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category, int32_t Value)
SmallVector< SPIRV::Extension::Extension, 8 > ExtensionList
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
Extension::Extension ReqExtension
OperandCategory::OperandCategory Category
OperandCategory::OperandCategory Category