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 "SPIRVGlobalRegistry.h"
16#include "SPIRVLegalizerInfo.h"
18#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
31 "spirv-extensions", cl::desc("SPIR-V extensions"), cl::ZeroOrMore,
34 clEnumValN(SPIRV::Extension::SPV_EXT_shader_atomic_float_add,
35 "SPV_EXT_shader_atomic_float_add",
36 "Adds atomic add instruction on floating-point numbers."),
38 SPIRV::Extension::SPV_EXT_shader_atomic_float16_add,
39 "SPV_EXT_shader_atomic_float16_add",
40 "Extends the SPV_EXT_shader_atomic_float_add extension to support "
41 "atomically adding to 16-bit floating-point numbers in memory."),
43 SPIRV::Extension::SPV_EXT_shader_atomic_float_min_max,
44 "SPV_EXT_shader_atomic_float_min_max",
45 "Adds atomic min and max instruction on floating-point numbers."),
46 clEnumValN(SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers,
47 "SPV_INTEL_arbitrary_precision_integers",
48 "Allows generating arbitrary width integer types."),
49 clEnumValN(SPIRV::Extension::SPV_INTEL_optnone, "SPV_INTEL_optnone",
50 "Adds OptNoneINTEL value for Function Control mask that "
51 "indicates a request to not optimize the function."),
52 clEnumValN(SPIRV::Extension::SPV_INTEL_subgroups, "SPV_INTEL_subgroups",
53 "Allows work items in a subgroup to share data without the "
54 "use of local memory and work group barriers, and to "
55 "utilize specialized hardware to load and store blocks of "
56 "data from images or buffers."),
57 clEnumValN(SPIRV::Extension::SPV_KHR_uniform_group_instructions,
58 "SPV_KHR_uniform_group_instructions",
59 "Allows support for additional group operations within "
60 "uniform control flow."),
61 clEnumValN(SPIRV::Extension::SPV_KHR_no_integer_wrap_decoration,
62 "SPV_KHR_no_integer_wrap_decoration",
63 "Adds decorations to indicate that a given instruction does "
64 "not cause integer wrapping."),
65 clEnumValN(SPIRV::Extension::SPV_KHR_expect_assume,
66 "SPV_KHR_expect_assume",
67 "Provides additional information to a compiler, similar to "
68 "the llvm.assume and llvm.expect intrinsics."),
69 clEnumValN(SPIRV::Extension::SPV_KHR_bit_instructions,
70 "SPV_KHR_bit_instructions",
71 "This enables bit instructions to be used by SPIR-V modules "
72 "without requiring the Shader capability."),
74 SPIRV::Extension::SPV_KHR_linkonce_odr, "SPV_KHR_linkonce_odr",
75 "Allows to use the LinkOnceODR linkage type that is to let "
76 "a function or global variable to be merged with other functions "
77 "or global variables of the same name when linkage occurs."),
78 clEnumValN(SPIRV::Extension::SPV_INTEL_function_pointers,
79 "SPV_INTEL_function_pointers",
80 "Allows translation of function pointers.")));
81
82// Compare version numbers, but allow 0 to mean unspecified.
83static bool isAtLeastVer(uint32_t Target, uint32_t VerToCompareTo) {
84 return Target == 0 || Target >= VerToCompareTo;
85}
86
87SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
88 const std::string &FS,
90 : SPIRVGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
91 PointerSize(TM.getPointerSizeInBits(/* AS= */ 0)), SPIRVVersion(0),
92 OpenCLVersion(0), InstrInfo(),
93 FrameLowering(initSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
94 TargetTriple(TT) {
95 // The order of initialization is important.
96 initAvailableExtensions();
97 initAvailableExtInstSets();
98
99 GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
100 CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
101 Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
102 RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
103 InstSelector.reset(
104 createSPIRVInstructionSelector(TM, *this, *RegBankInfo.get()));
105}
106
108 StringRef FS) {
109 ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
110 if (SPIRVVersion == 0)
111 SPIRVVersion = 14;
112 if (OpenCLVersion == 0)
113 OpenCLVersion = 22;
114 return *this;
115}
116
117bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
118 return AvailableExtensions.contains(E);
119}
120
122 SPIRV::InstructionSet::InstructionSet E) const {
123 return AvailableExtInstSets.contains(E);
124}
125
126bool SPIRVSubtarget::isAtLeastSPIRVVer(uint32_t VerToCompareTo) const {
127 return isAtLeastVer(SPIRVVersion, VerToCompareTo);
128}
129
131 if (!isOpenCLEnv())
132 return false;
133 return isAtLeastVer(OpenCLVersion, VerToCompareTo);
134}
135
136// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
138 return isAtLeastVer(SPIRVVersion, 14);
139}
140
141void SPIRVSubtarget::initAvailableExtensions() {
142 AvailableExtensions.clear();
143 if (!isOpenCLEnv())
144 return;
145
146 for (auto Extension : Extensions)
147 AvailableExtensions.insert(Extension);
148}
149
150// TODO: use command line args for this rather than just defaults.
151// Must have called initAvailableExtensions first.
152void SPIRVSubtarget::initAvailableExtInstSets() {
153 AvailableExtInstSets.clear();
154 if (!isOpenCLEnv())
155 AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
156 else
157 AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
158
159 // Handle extended instruction sets from extensions.
160 if (canUseExtension(
161 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
162 AvailableExtInstSets.insert(
163 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
164 }
165}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:693
const char LLVMTargetMachineRef TM
static bool isAtLeastVer(uint32_t Target, uint32_t VerToCompareTo)
cl::list< SPIRV::Extension::Extension > Extensions("spirv-extensions", cl::desc("SPIR-V extensions"), cl::ZeroOrMore, cl::Hidden, cl::values(clEnumValN(SPIRV::Extension::SPV_EXT_shader_atomic_float_add, "SPV_EXT_shader_atomic_float_add", "Adds atomic add instruction on floating-point numbers."), clEnumValN(SPIRV::Extension::SPV_EXT_shader_atomic_float16_add, "SPV_EXT_shader_atomic_float16_add", "Extends the SPV_EXT_shader_atomic_float_add extension to support " "atomically adding to 16-bit floating-point numbers in memory."), clEnumValN(SPIRV::Extension::SPV_EXT_shader_atomic_float_min_max, "SPV_EXT_shader_atomic_float_min_max", "Adds atomic min and max instruction on floating-point numbers."), clEnumValN(SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers, "SPV_INTEL_arbitrary_precision_integers", "Allows generating arbitrary width integer types."), clEnumValN(SPIRV::Extension::SPV_INTEL_optnone, "SPV_INTEL_optnone", "Adds OptNoneINTEL value for Function Control mask that " "indicates a request to not optimize the function."), clEnumValN(SPIRV::Extension::SPV_INTEL_subgroups, "SPV_INTEL_subgroups", "Allows work items in a subgroup to share data without the " "use of local memory and work group barriers, and to " "utilize specialized hardware to load and store blocks of " "data from images or buffers."), clEnumValN(SPIRV::Extension::SPV_KHR_uniform_group_instructions, "SPV_KHR_uniform_group_instructions", "Allows support for additional group operations within " "uniform control flow."), clEnumValN(SPIRV::Extension::SPV_KHR_no_integer_wrap_decoration, "SPV_KHR_no_integer_wrap_decoration", "Adds decorations to indicate that a given instruction does " "not cause integer wrapping."), clEnumValN(SPIRV::Extension::SPV_KHR_expect_assume, "SPV_KHR_expect_assume", "Provides additional information to a compiler, similar to " "the llvm.assume and llvm.expect intrinsics."), clEnumValN(SPIRV::Extension::SPV_KHR_bit_instructions, "SPV_KHR_bit_instructions", "This enables bit instructions to be used by SPIR-V modules " "without requiring the Shader capability."), clEnumValN(SPIRV::Extension::SPV_KHR_linkonce_odr, "SPV_KHR_linkonce_odr", "Allows to use the LinkOnceODR linkage type that is to let " "a function or global variable to be merged with other functions " "or global variables of the same name when linkage occurs."), clEnumValN(SPIRV::Extension::SPV_INTEL_function_pointers, "SPV_INTEL_function_pointers", "Allows translation of function pointers.")))
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
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:718
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)