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"
14#include "NVPTXSelectionDAGInfo.h"
15#include "NVPTXTargetMachine.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
19#include "llvm/Option/LibraryOptions.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/FormatVariadic.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(C: M.getContext());
40}
41
42const NVPTXOptions &llvm::getNVPTXOptions(const Function &F) {
43 return getNVPTXOptions(C: 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
132StringRef NVPTXSubtarget::getTargetName() const {
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(Feature: 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(Kind: Arch);
148}
149
150NVPTXSubtarget &NVPTXSubtarget::initializeSubtargetDependencies(StringRef CPU,
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(CPU: RequestedCPU, /*TuneCPU=*/RequestedCPU, FS);
155
156 Arch = NVPTX::parseArch(CPU: 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(FS: ("+ptx" + Twine(MinPTX)).str());
168 PTXVersion = MinPTX;
169 } else if (PTXVersion < MinPTX) {
170 // User explicitly requested an insufficient PTX version.
171 reportFatalUsageError(
172 reason: formatv(Fmt: "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 Vals: PTXVersion / 10, Vals: PTXVersion % 10, Vals&: RequestedCPU, Vals: MinPTX / 10,
177 Vals: MinPTX % 10));
178 }
179
180 return *this;
181}
182
183NVPTXSubtarget::NVPTXSubtarget(const Triple &TT, StringRef CPU, StringRef FS,
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
190NVPTXSubtarget::~NVPTXSubtarget() = default;
191
192bool NVPTXSubtarget::allowFP16Math() const {
193 return hasFP16Math() && !CLOpts.no_f16_math;
194}
195
196bool NVPTXSubtarget::hasF32x2Instructions() const {
197 return hasFeature(Feature: 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(Feature: NVPTX::SM90);
221 // Several BF16 instructions are available on sm_80 only.
222 case ISD::FMINNUM:
223 case ISD::FMAXNUM:
224 case ISD::FMAXNUM_IEEE:
225 case ISD::FMINNUM_IEEE:
226 case ISD::FMAXIMUM:
227 case ISD::FMINIMUM:
228 return hasFeature(Feature: NVPTX::SM80);
229 }
230 return true;
231}
232