LLVM 20.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"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "spirv-subtarget"
26
27#define GET_SUBTARGETINFO_TARGET_DESC
28#define GET_SUBTARGETINFO_CTOR
29#include "SPIRVGenSubtargetInfo.inc"
30
31static cl::opt<bool>
32 SPVTranslatorCompat("translator-compatibility-mode",
33 cl::desc("SPIR-V Translator compatibility mode"),
34 cl::Optional, cl::init(false));
35
38 Extensions("spirv-ext",
39 cl::desc("Specify list of enabled SPIR-V extensions"));
40
41// Provides access to the cl::opt<...> `Extensions` variable from outside of the
42// module.
44 const std::set<SPIRV::Extension::Extension> &AllowList) {
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)), InstrInfo(),
58 FrameLowering(initSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
59 TargetTriple(TT) {
60 switch (TT.getSubArch()) {
62 SPIRVVersion = VersionTuple(1, 0);
63 break;
65 SPIRVVersion = VersionTuple(1, 1);
66 break;
68 SPIRVVersion = VersionTuple(1, 2);
69 break;
71 SPIRVVersion = VersionTuple(1, 3);
72 break;
74 default:
75 SPIRVVersion = VersionTuple(1, 4);
76 break;
78 SPIRVVersion = VersionTuple(1, 5);
79 break;
81 SPIRVVersion = VersionTuple(1, 6);
82 break;
83 }
84 OpenCLVersion = VersionTuple(2, 2);
85
86 // The order of initialization is important.
87 initAvailableExtensions();
88 initAvailableExtInstSets();
89
90 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
91 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
92 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
93 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
94 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
95 InstSelector.reset(
96 createSPIRVInstructionSelector(TM, *this, *RegBankInfo.get()));
97}
98
100 StringRef FS) {
101 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
102 return *this;
103}
104
105bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
106 return AvailableExtensions.contains(E);
107}
108
110 SPIRV::InstructionSet::InstructionSet E) const {
111 return AvailableExtInstSets.contains(E);
112}
113
115 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
116}
117
119 if (!isOpenCLEnv())
120 return false;
121 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
122}
123
124// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
125// In SPIR-V Translator compatibility mode this feature is not available.
127 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
128}
129
130void SPIRVSubtarget::initAvailableExtensions() {
131 AvailableExtensions.clear();
132 AvailableExtensions.insert(Extensions.begin(), Extensions.end());
133}
134
135// TODO: use command line args for this rather than just defaults.
136// Must have called initAvailableExtensions first.
137void SPIRVSubtarget::initAvailableExtInstSets() {
138 AvailableExtInstSets.clear();
139 if (!isOpenCLEnv())
140 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
141 else
142 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
143
144 // Handle extended instruction sets from extensions.
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}
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
bool isOpenCLEnv() const
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
bool canUseExtInstSet(SPIRV::InstructionSet::InstructionSet E) const
SPIRVSubtarget(const Triple &TT, const std::string &CPU, const std::string &FS, const SPIRVTargetMachine &TM)
bool canUseExtension(SPIRV::Extension::Extension E) const
void clear()
Definition: SmallSet.h:204
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition: SmallSet.h:222
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:51
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ SPIRVSubArch_v10
Definition: Triple.h:160
@ SPIRVSubArch_v13
Definition: Triple.h:163
@ SPIRVSubArch_v16
Definition: Triple.h:166
@ SPIRVSubArch_v15
Definition: Triple.h:165
@ SPIRVSubArch_v12
Definition: Triple.h:162
@ SPIRVSubArch_v14
Definition: Triple.h:164
@ SPIRVSubArch_v11
Definition: Triple.h:161
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
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.