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