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#include "SPIRV.h"
15#include "SPIRVCommandLine.h"
16#include "SPIRVGlobalRegistry.h"
17#include "SPIRVLegalizerInfo.h"
19#include "SPIRVTargetMachine.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "spirv-subtarget"
25
26#define GET_SUBTARGETINFO_TARGET_DESC
27#define GET_SUBTARGETINFO_CTOR
28#include "SPIRVGenSubtargetInfo.inc"
29
30static cl::opt<bool>
31 SPVTranslatorCompat("translator-compatibility-mode",
32 cl::desc("SPIR-V Translator compatibility mode"),
33 cl::Optional, cl::init(false));
34
36 Extensions("spirv-ext",
37 cl::desc("Specify list of enabled SPIR-V extensions"));
38
39// Provides access to the cl::opt<...> `Extensions` variable from outside of the
40// module.
42 Extensions.insert(AllowList.begin(), AllowList.end());
43}
44
45// Compare version numbers, but allow 0 to mean unspecified.
46static bool isAtLeastVer(VersionTuple Target, VersionTuple VerToCompareTo) {
47 return Target.empty() || Target >= VerToCompareTo;
48}
49
50SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
51 const std::string &FS,
52 const SPIRVTargetMachine &TM)
53 : SPIRVGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
54 PointerSize(TM.getPointerSizeInBits(/* AS= */ 0)),
55 InstrInfo(initSubtargetDependencies(CPU, FS)), FrameLowering(*this),
56 TLInfo(TM, *this), TargetTriple(TT) {
57 switch (TT.getSubArch()) {
58 case Triple::SPIRVSubArch_v10:
59 SPIRVVersion = VersionTuple(1, 0);
60 break;
61 case Triple::SPIRVSubArch_v11:
62 SPIRVVersion = VersionTuple(1, 1);
63 break;
64 case Triple::SPIRVSubArch_v12:
65 SPIRVVersion = VersionTuple(1, 2);
66 break;
67 case Triple::SPIRVSubArch_v13:
68 SPIRVVersion = VersionTuple(1, 3);
69 break;
70 case Triple::SPIRVSubArch_v14:
71 SPIRVVersion = VersionTuple(1, 4);
72 break;
73 case Triple::SPIRVSubArch_v15:
74 SPIRVVersion = VersionTuple(1, 5);
75 break;
76 case Triple::SPIRVSubArch_v16:
77 SPIRVVersion = VersionTuple(1, 6);
78 break;
79 default:
80 if (TT.getVendor() == Triple::AMD)
81 SPIRVVersion = VersionTuple(1, 6);
82 else
83 SPIRVVersion = VersionTuple(1, 4);
84 }
85 OpenCLVersion = VersionTuple(2, 2);
86
87 // Set the environment based on the target triple.
88 if (TargetTriple.getOS() == Triple::Vulkan)
89 Env = Shader;
90 else if (TargetTriple.getOS() == Triple::OpenCL ||
91 TargetTriple.getVendor() == Triple::AMD)
92 Env = Kernel;
93 else
94 Env = Unknown;
95
96 // Set the default extensions based on the target triple.
97 if (TargetTriple.getVendor() == Triple::Intel) {
98 Extensions.insert(SPIRV::Extension::SPV_INTEL_function_pointers);
99 Extensions.insert(
100 SPIRV::Extension::SPV_EXT_relaxed_printf_string_address_space);
101 }
102 if (TargetTriple.getVendor() == Triple::AMD)
104
105 // The order of initialization is important.
107 initAvailableExtInstSets();
108
109 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
110 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
111 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
112 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
113 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
114 InstSelector.reset(createSPIRVInstructionSelector(TM, *this, *RegBankInfo));
115}
116
118 StringRef FS) {
119 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
120 return *this;
121}
122
123bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
124 return AvailableExtensions.contains(E);
125}
126
128 SPIRV::InstructionSet::InstructionSet E) const {
129 return AvailableExtInstSets.contains(E);
130}
131
132SPIRV::InstructionSet::InstructionSet
134 if (isShader())
135 return SPIRV::InstructionSet::GLSL_std_450;
136 else
137 return SPIRV::InstructionSet::OpenCL_std;
138}
139
141 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
142}
143
145 if (isShader())
146 return false;
147 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
148}
149
150// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
151// In SPIR-V Translator compatibility mode this feature is not available.
153 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
154}
155
156void SPIRVSubtarget::accountForAMDShaderTrinaryMinmax() {
157 if (canUseExtension(
158 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
159 AvailableExtInstSets.insert(
160 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
161 }
162}
163
164// TODO: use command line args for this rather than just defaults.
165// Must have called initAvailableExtensions first.
166void SPIRVSubtarget::initAvailableExtInstSets() {
167 AvailableExtInstSets.clear();
168 if (isShader())
169 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
170 else
171 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
172
173 // Handle extended instruction sets from extensions.
174 accountForAMDShaderTrinaryMinmax();
175}
176
178 if (E == Unknown)
179 report_fatal_error("Unknown environment is not allowed.");
180 if (Env != Unknown && Env != E)
181 report_fatal_error("Environment is already set to a different value.");
182 if (Env == E)
183 return;
184
185 Env = E;
186
187 // Reinitialize Env-dependent state aka ExtInstSet and legalizer info.
188 initAvailableExtInstSets();
189 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
190}
191
193 if (Env != Unknown) {
194 assert(!(isKernel() && any_of(M,
195 [](const Function &F) {
196 return F.hasFnAttribute("hlsl.shader");
197 })) &&
198 "Module has hlsl.shader attributes but environment is Kernel");
199 return;
200 }
201
202 bool HasShaderAttr = false;
203 for (const Function &F : M) {
204 if (F.hasFnAttribute("hlsl.shader")) {
205 HasShaderAttr = true;
206 break;
207 }
208 }
209
210 setEnv(HasShaderAttr ? Shader : Kernel);
211}
212
213// Set available extensions after SPIRVSubtarget is created.
215 const ExtensionSet &AllowedExtIds) {
216 AvailableExtensions.clear();
217 const ExtensionSet &ValidExtensions =
219
220 for (const auto &Ext : AllowedExtIds) {
221 if (ValidExtensions.count(Ext))
222 AvailableExtensions.insert(Ext);
223 }
224
225 accountForAMDShaderTrinaryMinmax();
226}
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)
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....