LLVM 23.0.0git
SPIRVSubtarget.cpp
Go to the documentation of this file.
1//===-- SPIRVSubtarget.cpp - SPIR-V Subtarget Information ------*- 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 implements the SPIR-V specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SPIRVSubtarget.h"
14
16#include "SPIRV.h"
17#include "SPIRVCommandLine.h"
18#include "SPIRVGlobalRegistry.h"
19#include "SPIRVLegalizerInfo.h"
21#include "SPIRVTargetMachine.h"
22
24
25using namespace llvm;
26
27#define DEBUG_TYPE "spirv-subtarget"
28
29#define GET_SUBTARGETINFO_TARGET_DESC
30#define GET_SUBTARGETINFO_CTOR
31#include "SPIRVGenSubtargetInfo.inc"
32
33static cl::opt<bool>
34 SPVTranslatorCompat("translator-compatibility-mode",
35 cl::desc("SPIR-V Translator compatibility mode"),
36 cl::Optional, cl::init(false));
37
39 Extensions("spirv-ext",
40 cl::desc("Specify list of enabled SPIR-V extensions"));
41
42// Provides access to the cl::opt<...> `Extensions` variable from outside of the
43// module.
45 Extensions.insert(AllowList.begin(), AllowList.end());
46}
47
48// Compare version numbers, but allow 0 to mean unspecified.
49static bool isAtLeastVer(VersionTuple Target, VersionTuple VerToCompareTo) {
50 return Target.empty() || Target >= VerToCompareTo;
51}
52
53SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
54 const std::string &FS,
55 const SPIRVTargetMachine &TM)
56 : SPIRVGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
57 PointerSize(TM.getPointerSizeInBits(/* AS= */ 0)),
58 InstrInfo(initSubtargetDependencies(CPU, FS)), FrameLowering(*this),
59 TLInfo(TM, *this), TargetTriple(TT) {
60 switch (TT.getSubArch()) {
61 case Triple::SPIRVSubArch_v10:
62 SPIRVVersion = VersionTuple(1, 0);
63 break;
64 case Triple::SPIRVSubArch_v11:
65 SPIRVVersion = VersionTuple(1, 1);
66 break;
67 case Triple::SPIRVSubArch_v12:
68 SPIRVVersion = VersionTuple(1, 2);
69 break;
70 case Triple::SPIRVSubArch_v13:
71 SPIRVVersion = VersionTuple(1, 3);
72 break;
73 case Triple::SPIRVSubArch_v14:
74 SPIRVVersion = VersionTuple(1, 4);
75 break;
76 case Triple::SPIRVSubArch_v15:
77 SPIRVVersion = VersionTuple(1, 5);
78 break;
79 case Triple::SPIRVSubArch_v16:
80 SPIRVVersion = VersionTuple(1, 6);
81 break;
82 default:
83 if (TT.getVendor() == Triple::AMD)
84 SPIRVVersion = VersionTuple(1, 6);
85 else
86 SPIRVVersion = VersionTuple(1, 4);
87 }
88 OpenCLVersion = VersionTuple(2, 2);
89
90 // Set the environment based on the target triple.
91 if (TargetTriple.getOS() == Triple::Vulkan)
92 Env = Shader;
93 else if (TargetTriple.getOS() == Triple::OpenCL ||
94 TargetTriple.getVendor() == Triple::AMD)
95 Env = Kernel;
96 else
97 Env = Unknown;
98
99 // Set the default extensions based on the target triple.
100 if (TargetTriple.getVendor() == Triple::Intel) {
101 Extensions.insert(SPIRV::Extension::SPV_INTEL_function_pointers);
102 Extensions.insert(
103 SPIRV::Extension::SPV_EXT_relaxed_printf_string_address_space);
104 }
105 if (TargetTriple.getVendor() == Triple::AMD)
107
108 // The order of initialization is important.
110 initAvailableExtInstSets();
111
112 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
113 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
114 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
115 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
116 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
117 InstSelector.reset(createSPIRVInstructionSelector(TM, *this, *RegBankInfo));
118}
119
121 StringRef FS) {
122 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
123 return *this;
124}
125
126bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
127 return AvailableExtensions.contains(E);
128}
129
131 SPIRV::InstructionSet::InstructionSet E) const {
132 return AvailableExtInstSets.contains(E);
133}
134
135SPIRV::InstructionSet::InstructionSet
137 if (isShader())
138 return SPIRV::InstructionSet::GLSL_std_450;
139 else
140 return SPIRV::InstructionSet::OpenCL_std;
141}
142
144 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
145}
146
148 if (isShader())
149 return false;
150 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
151}
152
153// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
154// In SPIR-V Translator compatibility mode this feature is not available.
156 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
157}
158
159void SPIRVSubtarget::accountForAMDShaderTrinaryMinmax() {
160 if (canUseExtension(
161 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
162 AvailableExtInstSets.insert(
163 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
164 }
165}
166
167// TODO: use command line args for this rather than just defaults.
168// Must have called initAvailableExtensions first.
169void SPIRVSubtarget::initAvailableExtInstSets() {
170 AvailableExtInstSets.clear();
171 if (isShader())
172 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
173 else
174 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
175
176 // Handle extended instruction sets from extensions.
177 accountForAMDShaderTrinaryMinmax();
178}
179
181 if (E == Unknown)
182 report_fatal_error("Unknown environment is not allowed.");
183 if (Env != Unknown && Env != E)
184 report_fatal_error("Environment is already set to a different value.");
185 if (Env == E)
186 return;
187
188 Env = E;
189
190 // Reinitialize Env-dependent state aka ExtInstSet and legalizer info.
191 initAvailableExtInstSets();
192 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
193}
194
196 if (Env != Unknown) {
197 assert(!(isKernel() && any_of(M,
198 [](const Function &F) {
199 return F.hasFnAttribute("hlsl.shader");
200 })) &&
201 "Module has hlsl.shader attributes but environment is Kernel");
202 return;
203 }
204
205 bool HasShaderAttr = false;
206 for (const Function &F : M) {
207 if (F.hasFnAttribute("hlsl.shader")) {
208 HasShaderAttr = true;
209 break;
210 }
211 }
212
213 if (!HasShaderAttr) {
214 if (auto *MemModel = M.getNamedMetadata("spirv.MemoryModel")) {
215 if (MemModel->getNumOperands() == 0)
216 report_fatal_error("Invalid spirv.MemoryModel metadata");
217 auto *MemMD = MemModel->getOperand(0);
218 if (MemMD->getNumOperands() < 2)
219 report_fatal_error("Invalid spirv.MemoryModel operand");
220 unsigned MemModelVal =
221 mdconst::extract<ConstantInt>(MemMD->getOperand(1))->getZExtValue();
222 switch (MemModelVal) {
223 case SPIRV::MemoryModel::Simple:
224 case SPIRV::MemoryModel::GLSL450:
225 HasShaderAttr = true;
226 break;
227 case SPIRV::MemoryModel::VulkanKHR:
228 HasShaderAttr = true;
229 AvailableExtensions.insert(
230 SPIRV::Extension::SPV_KHR_vulkan_memory_model);
231 break;
232 case SPIRV::MemoryModel::OpenCL:
233 break;
234 default:
236 "Unknown memory model in spirv.MemoryModel metadata");
237 }
238 }
239 }
240
241 setEnv(HasShaderAttr ? Shader : Kernel);
242}
243
244// Set available extensions after SPIRVSubtarget is created.
246 const ExtensionSet &AllowedExtIds) {
247 AvailableExtensions.clear();
248 const ExtensionSet &ValidExtensions =
250
251 for (const auto &Ext : AllowedExtIds) {
252 if (ValidExtensions.count(Ext))
253 AvailableExtensions.insert(Ext);
254 }
255
256 accountForAMDShaderTrinaryMinmax();
257}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:54
if(PassOpts->AAPipeline)
static bool isAtLeastVer(VersionTuple Target, VersionTuple VerToCompareTo)
static cl::opt< bool > SPVTranslatorCompat("translator-compatibility-mode", cl::desc("SPIR-V Translator compatibility mode"), cl::Optional, cl::init(false))
static cl::opt< ExtensionSet, false, SPIRVExtensionsParser > Extensions("spirv-ext", cl::desc("Specify list of enabled SPIR-V extensions"))
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
SPIRVSubtarget & initSubtargetDependencies(StringRef CPU, StringRef FS)
bool canDirectlyComparePointers() const
bool isAtLeastSPIRVVer(VersionTuple VerToCompareTo) const
void resolveEnvFromModule(const Module &M)
bool isAtLeastOpenCLVer(VersionTuple VerToCompareTo) const
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
bool canUseExtInstSet(SPIRV::InstructionSet::InstructionSet E) const
static void addExtensionsToClOpt(const ExtensionSet &AllowList)
SPIRVSubtarget(const Triple &TT, const std::string &CPU, const std::string &FS, const SPIRVTargetMachine &TM)
SPIRV::InstructionSet::InstructionSet getPreferredInstructionSet() const
bool canUseExtension(SPIRV::Extension::Extension E) const
void initAvailableExtensions(const ExtensionSet &AllowedExtIds)
void setEnv(SPIRVEnvType E)
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
Represents a version number in the form major[.minor[.subminor[.build]]].
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
DenseSet< SPIRV::Extension::Extension > ExtensionSet
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
InstructionSelector * createSPIRVInstructionSelector(const SPIRVTargetMachine &TM, const SPIRVSubtarget &Subtarget, const RegisterBankInfo &RBI)
static ExtensionSet getValidExtensions(const Triple &TT)
Returns the list of extensions that are valid for a particular target environment (i....