LLVM 24.0.0git
NVPTXSubtarget.cpp
Go to the documentation of this file.
1//===- NVPTXSubtarget.cpp - NVPTX Subtarget Information -------------------===//
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 NVPTX specific subclass of TargetSubtarget.
10//
11//===----------------------------------------------------------------------===//
12
13#include "NVPTXSubtarget.h"
15#include "NVPTXTargetMachine.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "nvptx-subtarget"
26
27#define GET_SUBTARGETINFO_TARGET_DESC
28#define GET_SUBTARGETINFO_CTOR
29#include "NVPTXGenSubtargetInfo.inc"
30
31#define OPTIONS_STRUCT_DEFS
32#include "NVPTXOptions.inc"
33
34const NVPTXOptions &llvm::getNVPTXOptions(const LLVMContext &C) {
35 return C.getLibraryOptions().get<NVPTXOptions>();
36}
37
38const NVPTXOptions &llvm::getNVPTXOptions(const Module &M) {
39 return getNVPTXOptions(M.getContext());
40}
41
42const NVPTXOptions &llvm::getNVPTXOptions(const Function &F) {
43 return getNVPTXOptions(F.getContext());
44}
45
46// Pin the vtable to this file.
47void NVPTXSubtarget::anchor() {}
48
49// Returns the minimum PTX version required for a given SM target.
50// This must be kept in sync with the "Supported Targets" column of the
51// "PTX Release History" table in the PTX ISA documentation:
52// https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#release-notes-ptx-release-history
53//
54// Note: LLVM's minimum supported PTX version is 3.2 (see FeaturePTX in
55// NVPTX.td), so older SMs that supported earlier PTX versions instead use 3.2
56// as their effective minimum.
57//
58// Note: sm_110* is the PTX ISA 9.0 spelling of sm_101*, which was introduced in
59// PTX ISA 8.6 (sm_101f in 8.8) and renamed in 9.0. Since getTargetName()
60// rewrites the new spelling back to the old one when emitting older PTX, the
61// sm_110* names work with any PTX version that supported sm_101*, so they take
62// sm_101*'s minimum PTX version rather than the 9.0 of the rename.
63static unsigned minPTXVersion(NVPTX::GPUKind Arch) {
64 switch (Arch) {
65 case NVPTX::GK_NONE:
66 llvm_unreachable("architecture is resolved before this is reached");
67 case NVPTX::GK_SM_20:
68 case NVPTX::GK_SM_21:
69 case NVPTX::GK_SM_30:
70 case NVPTX::GK_SM_35:
71 return 32;
72 case NVPTX::GK_SM_32_:
73 case NVPTX::GK_SM_50:
74 return 40;
75 case NVPTX::GK_SM_37:
76 case NVPTX::GK_SM_52:
77 return 41;
78 case NVPTX::GK_SM_53:
79 return 42;
80 case NVPTX::GK_SM_60:
81 case NVPTX::GK_SM_61:
82 case NVPTX::GK_SM_62:
83 return 50;
84 case NVPTX::GK_SM_70:
85 return 60;
86 case NVPTX::GK_SM_72:
87 return 61;
88 case NVPTX::GK_SM_75:
89 return 63;
90 case NVPTX::GK_SM_80:
91 return 70;
92 case NVPTX::GK_SM_86:
93 return 71;
94 case NVPTX::GK_SM_87:
95 return 74;
96 case NVPTX::GK_SM_89:
97 case NVPTX::GK_SM_90:
98 return 78;
99 case NVPTX::GK_SM_90a:
100 return 80;
101 case NVPTX::GK_SM_100:
102 case NVPTX::GK_SM_100a:
103 case NVPTX::GK_SM_101:
104 case NVPTX::GK_SM_101a:
105 case NVPTX::GK_SM_110:
106 case NVPTX::GK_SM_110a:
107 return 86;
108 case NVPTX::GK_SM_120:
109 case NVPTX::GK_SM_120a:
110 return 87;
111 case NVPTX::GK_SM_100f:
112 case NVPTX::GK_SM_101f:
113 case NVPTX::GK_SM_103:
114 case NVPTX::GK_SM_103f:
115 case NVPTX::GK_SM_103a:
116 case NVPTX::GK_SM_110f:
117 case NVPTX::GK_SM_120f:
118 case NVPTX::GK_SM_121:
119 case NVPTX::GK_SM_121f:
120 case NVPTX::GK_SM_121a:
121 return 88;
122 case NVPTX::GK_SM_88:
123 return 90;
124 case NVPTX::GK_SM_107:
125 case NVPTX::GK_SM_107f:
126 case NVPTX::GK_SM_107a:
127 return 94;
128 }
129 llvm_unreachable("invalid NVPTX GPUKind");
130}
131
133 // Spell sm_110* as sm_101* when emitting PTX older than 9.0, which does not
134 // know the new names. See the note above minPTXVersion().
135 if (!hasFeature(NVPTX::PTX90))
136 switch (Arch) {
137 case NVPTX::GK_SM_110:
138 return "sm_101";
139 case NVPTX::GK_SM_110f:
140 return "sm_101f";
141 case NVPTX::GK_SM_110a:
142 return "sm_101a";
143 default:
144 break;
145 }
146
147 return NVPTX::getArchName(Arch);
148}
149
151 StringRef FS) {
152 // If the user did not provide a target we default to the `sm_75` target.
153 StringRef RequestedCPU = CPU.empty() ? StringRef("sm_75") : CPU;
154 ParseSubtargetFeatures(RequestedCPU, /*TuneCPU=*/RequestedCPU, FS);
155
156 Arch = NVPTX::parseArch(RequestedCPU);
157
158 // An unrecognized name has already been diagnosed and its features dropped,
159 // leaving the subtarget at the oldest architecture, so name it that.
160 if (Arch == NVPTX::GK_NONE)
161 Arch = NVPTX::GK_SM_20;
162
163 unsigned MinPTX = minPTXVersion(Arch);
164
165 if (PTXVersion == 0) {
166 // User didn't request a specific PTX version; use the minimum for this SM.
167 ApplyFeatureFlag(("+ptx" + Twine(MinPTX)).str());
168 PTXVersion = MinPTX;
169 } else if (PTXVersion < MinPTX) {
170 // User explicitly requested an insufficient PTX version.
172 formatv("PTX version {0}.{1} does not support target '{2}'. "
173 "Minimum required PTX version is {3}.{4}. "
174 "Either remove the PTX version to use the default, "
175 "or increase it to at least {3}.{4}.",
176 PTXVersion / 10, PTXVersion % 10, RequestedCPU, MinPTX / 10,
177 MinPTX % 10));
178 }
179
180 return *this;
181}
182
184 const NVPTXTargetMachine &TM)
185 : NVPTXGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
186 CLOpts(TM.getCLOpts()), PTXVersion(0),
187 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
188 TSInfo(std::make_unique<NVPTXSelectionDAGInfo>()) {}
189
191
193 return hasFP16Math() && !CLOpts.no_f16_math;
194}
195
197 return hasFeature(NVPTX::SM100) && !CLOpts.no_f32x2;
198}
199
200bool NVPTXSubtarget::hasNativeBF16Support(unsigned Opcode) const {
201 if (!hasBF16Math())
202 return false;
203
204 switch (Opcode) {
205 // Several BF16 instructions are available on sm_90 only.
206 case ISD::FADD:
207 case ISD::FMUL:
208 case ISD::FSUB:
209 case ISD::SELECT:
210 case ISD::SELECT_CC:
211 case ISD::SETCC:
212 case ISD::FEXP2:
213 case ISD::FTANH:
214 case ISD::FCEIL:
215 case ISD::FFLOOR:
216 case ISD::FNEARBYINT:
217 case ISD::FRINT:
218 case ISD::FROUNDEVEN:
219 case ISD::FTRUNC:
220 return hasFeature(NVPTX::SM90);
221 // Several BF16 instructions are available on sm_80 only.
222 case ISD::FMINNUM:
223 case ISD::FMAXNUM:
226 case ISD::FMAXIMUM:
227 case ISD::FMINIMUM:
228 return hasFeature(NVPTX::SM80);
229 }
230 return true;
231}
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
Module.h This file contains the declarations for the Module class.
static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature, ArrayRef< SubtargetFeatureKV > FeatureTable)
static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits, ArrayRef< SubtargetFeatureKV > ProcFeatures)
#define F(x, y, z)
Definition MD5.cpp:54
static unsigned minPTXVersion(NVPTX::GPUKind Arch)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
const NVPTXOptions & getCLOpts() const
bool hasNativeBF16Support(unsigned Opcode) const
StringRef getTargetName() const
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
~NVPTXSubtarget() override
bool hasF32x2Instructions() const
NVPTXSubtarget & initializeSubtargetDependencies(StringRef CPU, StringRef FS)
NVPTXSubtarget(const Triple &TT, StringRef CPU, StringRef FS, const NVPTXTargetMachine &TM)
This constructor initializes the data members to match that of the specified module.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:845
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:420
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:822
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Same as FMINNUM/FMAXNUM, except that a signaling NaN operand deterministi...
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:837
@ FMINNUM
FMINNUM/FMAXNUM - Same semantics as the llvm.minnum/maxnum intrinsics.
@ FMINIMUM
FMINIMUM/FMAXIMUM - Same semantics as the llvm.minimum/maximum intrinsics.
LLVM_ABI GPUKind parseArch(StringRef CPU)
Parse CPU (e.g. "sm_90") into a GPUKind, or GK_NONE if unrecognized.
GPUKind
GPU kinds supported by the NVPTX target.
LLVM_ABI StringRef getArchName(GPUKind Kind)
Return the canonical processor name (e.g.
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
const NVPTXOptions & getNVPTXOptions(const LLVMContext &C)
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878