LLVM 19.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(uint32_t Target, uint32_t VerToCompareTo) {
43 return Target == 0 || Target >= VerToCompareTo;
44}
45
46SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
47 const std::string &FS,
49 : SPIRVGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
50 PointerSize(TM.getPointerSizeInBits(/* AS= */ 0)), SPIRVVersion(0),
51 OpenCLVersion(0), InstrInfo(),
52 FrameLowering(initSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
53 TargetTriple(TT) {
54 // The order of initialization is important.
55 initAvailableExtensions();
56 initAvailableExtInstSets();
57
58 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
59 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
60 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
61 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
62 InstSelector.reset(
63 createSPIRVInstructionSelector(TM, *this, *RegBankInfo.get()));
64}
65
67 StringRef FS) {
68 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
69 if (SPIRVVersion == 0)
70 SPIRVVersion = 14;
71 if (OpenCLVersion == 0)
72 OpenCLVersion = 22;
73 return *this;
74}
75
76bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
77 return AvailableExtensions.contains(E);
78}
79
81 SPIRV::InstructionSet::InstructionSet E) const {
82 return AvailableExtInstSets.contains(E);
83}
84
85bool SPIRVSubtarget::isAtLeastSPIRVVer(uint32_t VerToCompareTo) const {
86 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
87}
88
89bool SPIRVSubtarget::isAtLeastOpenCLVer(uint32_t VerToCompareTo) const {
90 if (!isOpenCLEnv())
91 return false;
92 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
93}
94
95// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
96// In SPIR-V Translator compatibility mode this feature is not available.
98 return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, 14);
99}
100
101void SPIRVSubtarget::initAvailableExtensions() {
102 AvailableExtensions.clear();
103 if (!isOpenCLEnv())
104 return;
105
106 AvailableExtensions.insert(Extensions.begin(), Extensions.end());
107}
108
109// TODO: use command line args for this rather than just defaults.
110// Must have called initAvailableExtensions first.
111void SPIRVSubtarget::initAvailableExtInstSets() {
112 AvailableExtInstSets.clear();
113 if (!isOpenCLEnv())
114 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
115 else
116 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
117
118 // Handle extended instruction sets from extensions.
119 if (canUseExtension(
120 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
121 AvailableExtInstSets.insert(
122 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
123 }
124}
const char LLVMTargetMachineRef TM
static bool isAtLeastVer(uint32_t Target, uint32_t 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 isAtLeastOpenCLVer(uint32_t VerToCompareTo) const
bool canDirectlyComparePointers() const
bool isAtLeastSPIRVVer(uint32_t 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
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
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.