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// Compare version numbers, but allow 0 to mean unspecified.
42static bool isAtLeastVer(VersionTuple Target, VersionTuple VerToCompareTo) {
43 return Target.empty() || Target >= VerToCompareTo;
44}
45
46SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
47 const std::string &FS,
48 const SPIRVTargetMachine &TM)
49 : SPIRVGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
50 PointerSize(TM.getPointerSizeInBits(/* AS= */ 0)), InstrInfo(),
51 FrameLowering(initSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
52 TargetTriple(TT) {
53 switch (TT.getSubArch()) {
55 SPIRVVersion = VersionTuple(1, 0);
56 break;
58 SPIRVVersion = VersionTuple(1, 1);
59 break;
61 SPIRVVersion = VersionTuple(1, 2);
62 break;
64 SPIRVVersion = VersionTuple(1, 3);
65 break;
67 default:
68 SPIRVVersion = VersionTuple(1, 4);
69 break;
71 SPIRVVersion = VersionTuple(1, 5);
72 break;
74 SPIRVVersion = VersionTuple(1, 6);
75 break;
76 }
77 OpenCLVersion = VersionTuple(2, 2);
78
79 // The order of initialization is important.
80 initAvailableExtensions();
81 initAvailableExtInstSets();
82
83 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
84 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
85 InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
86 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
87 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
88 InstSelector.reset(
89 createSPIRVInstructionSelector(TM, *this, *RegBankInfo.get()));
90}
91
93 StringRef FS) {
94 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
95 return *this;
96}
97
98bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
99 return AvailableExtensions.contains(E);
100}
101
103 SPIRV::InstructionSet::InstructionSet E) const {
104 return AvailableExtInstSets.contains(E);
105}
106
108 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
109}
110
112 if (!isOpenCLEnv())
113 return false;
114 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
115}
116
117// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
118// In SPIR-V Translator compatibility mode this feature is not available.
120 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
121}
122
123void SPIRVSubtarget::initAvailableExtensions() {
124 AvailableExtensions.clear();
125 if (!isOpenCLEnv())
126 return;
127
128 AvailableExtensions.insert(Extensions.begin(), Extensions.end());
129}
130
131// TODO: use command line args for this rather than just defaults.
132// Must have called initAvailableExtensions first.
133void SPIRVSubtarget::initAvailableExtInstSets() {
134 AvailableExtInstSets.clear();
135 if (!isOpenCLEnv())
136 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
137 else
138 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
139
140 // Handle extended instruction sets from extensions.
141 if (canUseExtension(
142 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
143 AvailableExtInstSets.insert(
144 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
145 }
146}
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)
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:218
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition: SmallSet.h:236
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:179
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ SPIRVSubArch_v10
Definition: Triple.h:159
@ SPIRVSubArch_v13
Definition: Triple.h:162
@ SPIRVSubArch_v16
Definition: Triple.h:165
@ SPIRVSubArch_v15
Definition: Triple.h:164
@ SPIRVSubArch_v12
Definition: Triple.h:161
@ SPIRVSubArch_v14
Definition: Triple.h:163
@ SPIRVSubArch_v11
Definition: Triple.h:160
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.