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)),
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 default:
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 }
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 // Set the default extensions based on the target triple.
94 if (TargetTriple.getVendor() == Triple::Intel)
95 Extensions.insert(SPIRV::Extension::SPV_INTEL_function_pointers);
96
97 // The order of initialization is important.
99 initAvailableExtInstSets();
100
101 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
102 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
103 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
104 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
105 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
106 InstSelector.reset(createSPIRVInstructionSelector(TM, *this, *RegBankInfo));
107}
108
110 StringRef FS) {
111 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
112 return *this;
113}
114
115bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
116 return AvailableExtensions.contains(E);
117}
118
120 SPIRV::InstructionSet::InstructionSet E) const {
121 return AvailableExtInstSets.contains(E);
122}
123
124SPIRV::InstructionSet::InstructionSet
126 if (isShader())
127 return SPIRV::InstructionSet::GLSL_std_450;
128 else
129 return SPIRV::InstructionSet::OpenCL_std;
130}
131
133 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
134}
135
137 if (isShader())
138 return false;
139 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
140}
141
142// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
143// In SPIR-V Translator compatibility mode this feature is not available.
145 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
146}
147
148void SPIRVSubtarget::accountForAMDShaderTrinaryMinmax() {
149 if (canUseExtension(
150 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
151 AvailableExtInstSets.insert(
152 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
153 }
154}
155
156// TODO: use command line args for this rather than just defaults.
157// Must have called initAvailableExtensions first.
158void SPIRVSubtarget::initAvailableExtInstSets() {
159 AvailableExtInstSets.clear();
160 if (isShader())
161 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
162 else
163 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
164
165 // Handle extended instruction sets from extensions.
166 accountForAMDShaderTrinaryMinmax();
167}
168
169// Set available extensions after SPIRVSubtarget is created.
171 const std::set<SPIRV::Extension::Extension> &AllowedExtIds) {
172 AvailableExtensions.clear();
173 const std::set<SPIRV::Extension::Extension> &ValidExtensions =
175
176 for (const auto &Ext : AllowedExtIds) {
177 if (ValidExtensions.count(Ext))
178 AvailableExtensions.insert(Ext);
179 }
180
181 accountForAMDShaderTrinaryMinmax();
182}
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"))
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
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:181
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.
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....