| 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 | #include "llvm/ADT/StringTable.h" |
| 19 | |
| 20 | namespace llvm { |
| 21 | namespace SPIRV { |
| 22 | struct SymbolicOperand { |
| 23 | OperandCategory::OperandCategory Category; |
| 24 | uint32_t Value; |
| 25 | StringTable::Offset Mnemonic; |
| 26 | uint32_t MinVersion; |
| 27 | uint32_t MaxVersion; |
| 28 | }; |
| 29 | |
| 30 | struct ExtensionEntry { |
| 31 | OperandCategory::OperandCategory Category; |
| 32 | uint32_t Value; |
| 33 | Extension::Extension ReqExtension; |
| 34 | }; |
| 35 | |
| 36 | struct CapabilityEntry { |
| 37 | OperandCategory::OperandCategory Category; |
| 38 | uint32_t Value; |
| 39 | Capability::Capability ReqCapability; |
| 40 | }; |
| 41 | |
| 42 | struct EnvironmentEntry { |
| 43 | OperandCategory::OperandCategory Category; |
| 44 | uint32_t Value; |
| 45 | Environment::Environment AllowedEnvironment; |
| 46 | }; |
| 47 | |
| 48 | using namespace OperandCategory; |
| 49 | using namespace Extension; |
| 50 | using namespace Environment; |
| 51 | using namespace Capability; |
| 52 | using 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 | |
| 66 | std::string |
| 67 | getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category, |
| 68 | int32_t Value) { |
| 69 | const SPIRV::SymbolicOperand *Lookup = |
| 70 | SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value); |
| 71 | // Value that encodes just one enum value. |
| 72 | if (Lookup) |
| 73 | return SPIRV::getSymbolicOperandStr(Offset: 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(Offset: EnumValueInCategory->Mnemonic).str(); |
| 96 | Separator = "|" ; |
| 97 | } |
| 98 | if (++EnumValueInCategory == TableEnd) |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | return Name; |
| 103 | } |
| 104 | |
| 105 | // Versions are encoded as in the SPIR-V header: 0x00MMmm00. |
| 106 | static VersionTuple decodeVersion(uint32_t V) { |
| 107 | return VersionTuple((V >> 16) & 0xff, (V >> 8) & 0xff); |
| 108 | } |
| 109 | |
| 110 | VersionTuple |
| 111 | getSymbolicOperandMinVersion(SPIRV::OperandCategory::OperandCategory Category, |
| 112 | uint32_t Value) { |
| 113 | const SPIRV::SymbolicOperand *Lookup = |
| 114 | SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value); |
| 115 | |
| 116 | if (Lookup) |
| 117 | return decodeVersion(V: Lookup->MinVersion); |
| 118 | |
| 119 | return VersionTuple(0); |
| 120 | } |
| 121 | |
| 122 | VersionTuple |
| 123 | getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category, |
| 124 | uint32_t Value) { |
| 125 | const SPIRV::SymbolicOperand *Lookup = |
| 126 | SPIRV::lookupSymbolicOperandByCategoryAndValue(Category, Value); |
| 127 | |
| 128 | if (Lookup) |
| 129 | return decodeVersion(V: Lookup->MaxVersion); |
| 130 | |
| 131 | return VersionTuple(); |
| 132 | } |
| 133 | |
| 134 | CapabilityList |
| 135 | getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category, |
| 136 | uint32_t Value) { |
| 137 | CapabilityList Capabilities; |
| 138 | const SPIRV::CapabilityEntry *Capability = |
| 139 | SPIRV::lookupCapabilityByCategoryAndValue(Category, Value); |
| 140 | auto TableEnd = ArrayRef(SPIRV::CapabilityEntries).end(); |
| 141 | while (Capability && Capability->Category == Category && |
| 142 | Capability->Value == Value) { |
| 143 | Capabilities.push_back( |
| 144 | Elt: static_cast<SPIRV::Capability::Capability>(Capability->ReqCapability)); |
| 145 | if (++Capability == TableEnd) |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | return Capabilities; |
| 150 | } |
| 151 | |
| 152 | EnvironmentList getSymbolicOperandAllowedEnvironments( |
| 153 | SPIRV::OperandCategory::OperandCategory Category, uint32_t Value) { |
| 154 | EnvironmentList Environments; |
| 155 | const SPIRV::EnvironmentEntry *Environment = |
| 156 | SPIRV::lookupEnvironmentByCategoryAndValue(Category, Value); |
| 157 | auto TableEnd = ArrayRef(SPIRV::EnvironmentEntries).end(); |
| 158 | while (Environment && Environment->Category == Category && |
| 159 | Environment->Value == Value) { |
| 160 | Environments.push_back(Elt: static_cast<SPIRV::Environment::Environment>( |
| 161 | Environment->AllowedEnvironment)); |
| 162 | if (++Environment == TableEnd) |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | return Environments; |
| 167 | } |
| 168 | |
| 169 | CapabilityList |
| 170 | getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension) { |
| 171 | const SPIRV::ExtensionEntry *Entry = |
| 172 | SPIRV::lookupSymbolicOperandsEnabledByExtension( |
| 173 | ReqExtension: Extension, Category: SPIRV::OperandCategory::CapabilityOperand); |
| 174 | |
| 175 | CapabilityList Capabilities; |
| 176 | auto TableEnd = ArrayRef(SPIRV::ExtensionEntries).end(); |
| 177 | while (Entry && |
| 178 | Entry->Category == SPIRV::OperandCategory::CapabilityOperand) { |
| 179 | // Some capabilities' codes might go not in order. |
| 180 | if (Entry->ReqExtension == Extension) |
| 181 | Capabilities.push_back( |
| 182 | Elt: static_cast<SPIRV::Capability::Capability>(Entry->Value)); |
| 183 | if (++Entry == TableEnd) |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | return Capabilities; |
| 188 | } |
| 189 | |
| 190 | ExtensionList |
| 191 | getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category, |
| 192 | uint32_t Value) { |
| 193 | const SPIRV::ExtensionEntry *Extension = |
| 194 | SPIRV::lookupExtensionByCategoryAndValue(Category, Value); |
| 195 | |
| 196 | ExtensionList Extensions; |
| 197 | auto TableEnd = ArrayRef(SPIRV::ExtensionEntries).end(); |
| 198 | while (Extension && Extension->Category == Category && |
| 199 | Extension->Value == Value) { |
| 200 | Extensions.push_back( |
| 201 | Elt: static_cast<SPIRV::Extension::Extension>(Extension->ReqExtension)); |
| 202 | if (++Extension == TableEnd) |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | return Extensions; |
| 207 | } |
| 208 | |
| 209 | std::string getLinkStringForBuiltIn(SPIRV::BuiltIn::BuiltIn BuiltInValue) { |
| 210 | const SPIRV::SymbolicOperand *Lookup = |
| 211 | SPIRV::lookupSymbolicOperandByCategoryAndValue( |
| 212 | Category: SPIRV::OperandCategory::BuiltInOperand, Value: BuiltInValue); |
| 213 | |
| 214 | if (Lookup) |
| 215 | return "__spirv_BuiltIn" + |
| 216 | SPIRV::getSymbolicOperandStr(Offset: Lookup->Mnemonic).str(); |
| 217 | return "UNKNOWN_BUILTIN" ; |
| 218 | } |
| 219 | |
| 220 | bool getSpirvBuiltInIdByName(llvm::StringRef Name, |
| 221 | SPIRV::BuiltIn::BuiltIn &BI) { |
| 222 | const std::string Prefix = "__spirv_BuiltIn" ; |
| 223 | if (!Name.starts_with(Prefix)) |
| 224 | return false; |
| 225 | |
| 226 | const SPIRV::SymbolicOperand *Lookup = |
| 227 | SPIRV::lookupSymbolicOperandByCategoryAndMnemonic( |
| 228 | Category: SPIRV::OperandCategory::BuiltInOperand, |
| 229 | Mnemonic: Name.drop_front(N: Prefix.length())); |
| 230 | |
| 231 | if (!Lookup) |
| 232 | return false; |
| 233 | |
| 234 | BI = static_cast<SPIRV::BuiltIn::BuiltIn>(Lookup->Value); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | std::string getExtInstSetName(SPIRV::InstructionSet::InstructionSet Set) { |
| 239 | switch (Set) { |
| 240 | case SPIRV::InstructionSet::OpenCL_std: |
| 241 | return "OpenCL.std" ; |
| 242 | case SPIRV::InstructionSet::GLSL_std_450: |
| 243 | return "GLSL.std.450" ; |
| 244 | case SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100: |
| 245 | return "NonSemantic.Shader.DebugInfo.100" ; |
| 246 | case SPIRV::InstructionSet::NonSemantic_AuxData: |
| 247 | return "NonSemantic.AuxData" ; |
| 248 | case SPIRV::InstructionSet::NonSemantic_DebugBreak: |
| 249 | return "NonSemantic.DebugBreak" ; |
| 250 | case SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax: |
| 251 | return "SPV_AMD_shader_trinary_minmax" ; |
| 252 | } |
| 253 | return "UNKNOWN_EXT_INST_SET" ; |
| 254 | } |
| 255 | |
| 256 | SPIRV::InstructionSet::InstructionSet |
| 257 | getExtInstSetFromString(std::string SetName) { |
| 258 | for (auto Set : |
| 259 | {SPIRV::InstructionSet::GLSL_std_450, SPIRV::InstructionSet::OpenCL_std, |
| 260 | SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100, |
| 261 | SPIRV::InstructionSet::NonSemantic_AuxData, |
| 262 | SPIRV::InstructionSet::NonSemantic_DebugBreak}) { |
| 263 | if (SetName == getExtInstSetName(Set)) |
| 264 | return Set; |
| 265 | } |
| 266 | llvm_unreachable("UNKNOWN_EXT_INST_SET" ); |
| 267 | } |
| 268 | |
| 269 | std::string getExtInstName(SPIRV::InstructionSet::InstructionSet Set, |
| 270 | uint32_t InstructionNumber) { |
| 271 | const SPIRV::ExtendedBuiltin *Lookup = |
| 272 | SPIRV::lookupExtendedBuiltinBySetAndNumber(Set, Number: InstructionNumber); |
| 273 | |
| 274 | if (!Lookup) |
| 275 | return "UNKNOWN_EXT_INST" ; |
| 276 | |
| 277 | return SPIRV::getExtendedBuiltinStr(Offset: Lookup->Name).str(); |
| 278 | } |
| 279 | } // namespace llvm |
| 280 | |