LLVM 24.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 TargetTriple.getOS() == Triple::ChipStar)
96 Env = Kernel;
97 else
98 Env = Unknown;
99
100 // Set the default extensions based on the target triple.
101 if (TargetTriple.getVendor() == Triple::Intel) {
102 Extensions.insert(SPIRV::Extension::SPV_INTEL_function_pointers);
103 Extensions.insert(
104 SPIRV::Extension::SPV_EXT_relaxed_printf_string_address_space);
105 }
106 if (TargetTriple.getVendor() == Triple::AMD)
108
109 // The order of initialization is important.
111 initAvailableExtInstSets();
112
113 GR = std::make_unique<SPIRVGlobalRegistry>(TM.createDataLayout());
114 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
115 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
116 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
117 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
118 InstSelector.reset(createSPIRVInstructionSelector(TM, *this, *RegBankInfo));
119}
120
122 StringRef FS) {
123 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
124 return *this;
125}
126
127bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
128 return AvailableExtensions.contains(E);
129}
130
132 SPIRV::InstructionSet::InstructionSet E) const {
133 return AvailableExtInstSets.contains(E);
134}
135
136SPIRV::InstructionSet::InstructionSet
138 if (isShader())
139 return SPIRV::InstructionSet::GLSL_std_450;
140 else
141 return SPIRV::InstructionSet::OpenCL_std;
142}
143
145 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
146}
147
149 if (isShader())
150 return false;
151 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
152}
153
154// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
155// In SPIR-V Translator compatibility mode this feature is not available.
157 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
158}
159
160void SPIRVSubtarget::accountForAMDShaderTrinaryMinmax() {
161 if (canUseExtension(
162 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
163 AvailableExtInstSets.insert(
164 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
165 }
166}
167
168// TODO: use command line args for this rather than just defaults.
169// Must have called initAvailableExtensions first.
170void SPIRVSubtarget::initAvailableExtInstSets() {
171 AvailableExtInstSets.clear();
172 if (isShader())
173 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
174 else
175 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
176
177 // Handle extended instruction sets from extensions.
178 accountForAMDShaderTrinaryMinmax();
179}
180
182 if (E == Unknown)
183 report_fatal_error("Unknown environment is not allowed.");
184 if (Env != Unknown && Env != E)
185 report_fatal_error("Environment is already set to a different value.");
186 if (Env == E)
187 return;
188
189 Env = E;
190
191 // Reinitialize Env-dependent state aka ExtInstSet and legalizer info.
192 initAvailableExtInstSets();
193 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
194}
195
197 *GR = SPIRVGlobalRegistry(M.getDataLayout());
198
199 if (Env != Unknown) {
200 assert(!(isKernel() && any_of(M,
201 [](const Function &F) {
202 return F.hasFnAttribute("hlsl.shader");
203 })) &&
204 "Module has hlsl.shader attributes but environment is Kernel");
205 return;
206 }
207
208 bool HasShaderAttr = any_of(
209 M, [](const Function &F) { return F.hasFnAttribute("hlsl.shader"); });
210
211 if (!HasShaderAttr) {
212 if (auto *MemModel = M.getNamedMetadata("spirv.MemoryModel")) {
213 if (MemModel->getNumOperands() == 0)
214 report_fatal_error("Invalid spirv.MemoryModel metadata");
215 auto *MemMD = MemModel->getOperand(0);
216 if (MemMD->getNumOperands() < 2)
217 report_fatal_error("Invalid spirv.MemoryModel operand");
218 unsigned MemModelVal =
219 mdconst::extract<ConstantInt>(MemMD->getOperand(1))->getZExtValue();
220 switch (MemModelVal) {
221 case SPIRV::MemoryModel::Simple:
222 case SPIRV::MemoryModel::GLSL450:
223 HasShaderAttr = true;
224 break;
225 case SPIRV::MemoryModel::VulkanKHR:
226 HasShaderAttr = true;
227 AvailableExtensions.insert(
228 SPIRV::Extension::SPV_KHR_vulkan_memory_model);
229 break;
230 case SPIRV::MemoryModel::OpenCL:
231 break;
232 default:
234 "Unknown memory model in spirv.MemoryModel metadata");
235 }
236 }
237 }
238
239 setEnv(HasShaderAttr ? Shader : Kernel);
240}
241
242// Set available extensions after SPIRVSubtarget is created.
244 const ExtensionSet &AllowedExtIds) {
245 AvailableExtensions.clear();
246 const ExtensionSet &ValidExtensions =
248
249 for (const auto &Ext : AllowedExtIds) {
250 if (ValidExtensions.count(Ext))
251 AvailableExtensions.insert(Ext);
252 }
253
254 accountForAMDShaderTrinaryMinmax();
255}
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
const DataLayout createDataLayout() const
Create a DataLayout.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
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:187
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.
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....