LLVM 22.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)), InstrInfo(),
57 FrameLowering(initSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
58 TargetTriple(TT) {
59 switch (TT.getSubArch()) {
61 SPIRVVersion = VersionTuple(1, 0);
62 break;
64 SPIRVVersion = VersionTuple(1, 1);
65 break;
67 SPIRVVersion = VersionTuple(1, 2);
68 break;
70 SPIRVVersion = VersionTuple(1, 3);
71 break;
73 default:
74 SPIRVVersion = VersionTuple(1, 4);
75 break;
77 SPIRVVersion = VersionTuple(1, 5);
78 break;
80 SPIRVVersion = VersionTuple(1, 6);
81 break;
82 }
83 OpenCLVersion = VersionTuple(2, 2);
84
85 // Set the environment based on the target triple.
86 if (TargetTriple.getOS() == Triple::Vulkan)
87 Env = Shader;
88 else if (TargetTriple.getEnvironment() == Triple::OpenCL)
89 Env = Kernel;
90 else
91 Env = Unknown;
92
93 // The order of initialization is important.
95 initAvailableExtInstSets();
96
97 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
98 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
99 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
100 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
101 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
102 InstSelector.reset(createSPIRVInstructionSelector(TM, *this, *RegBankInfo));
103}
104
106 StringRef FS) {
107 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
108 return *this;
109}
110
111bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
112 return AvailableExtensions.contains(E);
113}
114
116 SPIRV::InstructionSet::InstructionSet E) const {
117 return AvailableExtInstSets.contains(E);
118}
119
120SPIRV::InstructionSet::InstructionSet
122 if (isShader())
123 return SPIRV::InstructionSet::GLSL_std_450;
124 else
125 return SPIRV::InstructionSet::OpenCL_std;
126}
127
129 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
130}
131
133 if (isShader())
134 return false;
135 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
136}
137
138// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
139// In SPIR-V Translator compatibility mode this feature is not available.
141 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
142}
143
144void SPIRVSubtarget::accountForAMDShaderTrinaryMinmax() {
145 if (canUseExtension(
146 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
147 AvailableExtInstSets.insert(
148 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
149 }
150}
151
152// TODO: use command line args for this rather than just defaults.
153// Must have called initAvailableExtensions first.
154void SPIRVSubtarget::initAvailableExtInstSets() {
155 AvailableExtInstSets.clear();
156 if (isShader())
157 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
158 else
159 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
160
161 // Handle extended instruction sets from extensions.
162 accountForAMDShaderTrinaryMinmax();
163}
164
165// Set available extensions after SPIRVSubtarget is created.
167 const std::set<SPIRV::Extension::Extension> &AllowedExtIds) {
168 AvailableExtensions.clear();
169 const std::set<SPIRV::Extension::Extension> &ValidExtensions =
171
172 for (const auto &Ext : AllowedExtIds) {
173 if (ValidExtensions.count(Ext))
174 AvailableExtensions.insert(Ext);
175 }
176
177 accountForAMDShaderTrinaryMinmax();
178}
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"))
bool isShader() const
SPIRVSubtarget & initSubtargetDependencies(StringRef CPU, StringRef FS)
static void addExtensionsToClOpt(const std::set< SPIRV::Extension::Extension > &AllowList)
bool canDirectlyComparePointers() const
bool isAtLeastSPIRVVer(VersionTuple VerToCompareTo) const
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 clear()
Definition: SmallSet.h:209
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition: SmallSet.h:227
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:182
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
OSType getOS() const
Get the parsed operating system type of this triple.
Definition: Triple.h:417
EnvironmentType getEnvironment() const
Get the parsed environment type of this triple.
Definition: Triple.h:425
@ SPIRVSubArch_v10
Definition: Triple.h:165
@ SPIRVSubArch_v13
Definition: Triple.h:168
@ SPIRVSubArch_v16
Definition: Triple.h:171
@ SPIRVSubArch_v15
Definition: Triple.h:170
@ SPIRVSubArch_v12
Definition: Triple.h:167
@ SPIRVSubArch_v14
Definition: Triple.h:169
@ SPIRVSubArch_v11
Definition: Triple.h:166
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:30
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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....