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 // FIXME: The GlobalRegistry does not belong in the subtarget, see issue
114 // #223774. The DataLayout is a property of the module and in principle
115 // depends on program state. It just happens SPIRV currently doesn't use
116 // target-abi names.
117 GR = std::make_unique<SPIRVGlobalRegistry>(
118 DataLayout(TargetTriple.computeDataLayout()));
119 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
120 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
121 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
122 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
123 InstSelector.reset(createSPIRVInstructionSelector(TM, *this, *RegBankInfo));
124}
125
127 StringRef FS) {
128 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
129 return *this;
130}
131
132bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
133 return AvailableExtensions.contains(E);
134}
135
137 SPIRV::InstructionSet::InstructionSet E) const {
138 return AvailableExtInstSets.contains(E);
139}
140
141SPIRV::InstructionSet::InstructionSet
143 if (isShader())
144 return SPIRV::InstructionSet::GLSL_std_450;
145 else
146 return SPIRV::InstructionSet::OpenCL_std;
147}
148
150 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
151}
152
154 if (isShader())
155 return false;
156 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
157}
158
159// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
160// In SPIR-V Translator compatibility mode this feature is not available.
162 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
163}
164
165void SPIRVSubtarget::accountForAMDShaderTrinaryMinmax() {
166 if (canUseExtension(
167 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
168 AvailableExtInstSets.insert(
169 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
170 }
171}
172
173// TODO: use command line args for this rather than just defaults.
174// Must have called initAvailableExtensions first.
175void SPIRVSubtarget::initAvailableExtInstSets() {
176 AvailableExtInstSets.clear();
177 if (isShader())
178 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
179 else
180 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
181
182 // Handle extended instruction sets from extensions.
183 accountForAMDShaderTrinaryMinmax();
184}
185
187 if (E == Unknown)
188 report_fatal_error("Unknown environment is not allowed.");
189 if (Env != Unknown && Env != E)
190 report_fatal_error("Environment is already set to a different value.");
191 if (Env == E)
192 return;
193
194 Env = E;
195
196 // Reinitialize Env-dependent state aka ExtInstSet and legalizer info.
197 initAvailableExtInstSets();
198 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
199}
200
202 *GR = SPIRVGlobalRegistry(M.getDataLayout());
203
204 if (Env != Unknown) {
205 assert(!(isKernel() && any_of(M,
206 [](const Function &F) {
207 return F.hasFnAttribute("hlsl.shader");
208 })) &&
209 "Module has hlsl.shader attributes but environment is Kernel");
210 return;
211 }
212
213 bool HasShaderAttr = any_of(
214 M, [](const Function &F) { return F.hasFnAttribute("hlsl.shader"); });
215
216 if (!HasShaderAttr) {
217 if (auto *MemModel = M.getNamedMetadata("spirv.MemoryModel")) {
218 if (MemModel->getNumOperands() == 0)
219 report_fatal_error("Invalid spirv.MemoryModel metadata");
220 auto *MemMD = MemModel->getOperand(0);
221 if (MemMD->getNumOperands() < 2)
222 report_fatal_error("Invalid spirv.MemoryModel operand");
223 unsigned MemModelVal =
224 mdconst::extract<ConstantInt>(MemMD->getOperand(1))->getZExtValue();
225 switch (MemModelVal) {
226 case SPIRV::MemoryModel::Simple:
227 case SPIRV::MemoryModel::GLSL450:
228 HasShaderAttr = true;
229 break;
230 case SPIRV::MemoryModel::VulkanKHR:
231 HasShaderAttr = true;
232 AvailableExtensions.insert(
233 SPIRV::Extension::SPV_KHR_vulkan_memory_model);
234 break;
235 case SPIRV::MemoryModel::OpenCL:
236 break;
237 default:
239 "Unknown memory model in spirv.MemoryModel metadata");
240 }
241 }
242 }
243
244 setEnv(HasShaderAttr ? Shader : Kernel);
245}
246
247// Set available extensions after SPIRVSubtarget is created.
249 const ExtensionSet &AllowedExtIds) {
250 AvailableExtensions.clear();
251 const ExtensionSet &ValidExtensions =
253
254 for (const auto &Ext : AllowedExtIds) {
255 if (ValidExtensions.count(Ext))
256 AvailableExtensions.insert(Ext);
257 }
258
259 accountForAMDShaderTrinaryMinmax();
260}
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 parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
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
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:679
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:1762
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....