LLVM 23.0.0git
NVPTXUtilities.h
Go to the documentation of this file.
1//===-- NVPTXUtilities - Utilities -----------------------------*- 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 contains the declaration of the NVVM specific utility functions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_NVPTX_NVPTXUTILITIES_H
14#define LLVM_LIB_TARGET_NVPTX_NVPTXUTILITIES_H
15
16#include "NVPTX.h"
20#include "llvm/IR/CallingConv.h"
21#include "llvm/IR/Function.h"
24#include "llvm/IR/Value.h"
27#include <cstdarg>
28#include <string>
29
30namespace llvm {
31
32class DataLayout;
33class TargetMachine;
34
35void clearAnnotationCache(const Module *);
36
37bool isTexture(const Value &);
38bool isSurface(const Value &);
39bool isSampler(const Value &);
40bool isImage(const Value &);
41bool isImageReadOnly(const Value &);
42bool isImageWriteOnly(const Value &);
43bool isImageReadWrite(const Value &);
44bool isManaged(const Value &);
45
49
53
54std::optional<uint64_t> getOverallMaxNTID(const Function &);
55std::optional<uint64_t> getOverallReqNTID(const Function &);
56std::optional<uint64_t> getOverallClusterRank(const Function &);
57
58std::optional<unsigned> getMaxClusterRank(const Function &);
59std::optional<unsigned> getMinCTASm(const Function &);
60std::optional<unsigned> getMaxNReg(const Function &);
61
62bool hasBlocksAreClusters(const Function &);
63
64inline bool isKernelFunction(const Function &F) {
65 return F.getCallingConv() == CallingConv::PTX_Kernel;
66}
67
68bool isParamGridConstant(const Argument &);
69
70inline MaybeAlign getAlign(const Function &F, unsigned Index) {
71 return F.getAttributes().getAttributes(Index).getStackAlignment();
72}
73
74MaybeAlign getAlign(const CallInst &, unsigned);
75Function *getMaybeBitcastedCallee(const CallBase *CB);
76
77/// Since function arguments are passed via .param space, we may want to
78/// increase their alignment in a way that ensures that we can effectively
79/// vectorize their loads & stores. We can increase alignment only if the
80/// function has internal or private linkage as for other linkage types callers
81/// may already rely on default alignment. To allow using 128-bit vectorized
82/// loads/stores, this function ensures that alignment is 16 or greater.
83Align getFunctionParamOptimizedAlign(const Function *F, Type *ArgTy,
84 const DataLayout &DL);
85
86Align getFunctionArgumentAlignment(const Function *F, Type *Ty, unsigned Idx,
87 const DataLayout &DL);
88
89Align getFunctionByValParamAlign(const Function *F, Type *ArgTy,
90 Align InitialAlign, const DataLayout &DL);
91
92// PTX ABI requires all scalar argument/return values to have
93// bit-size as a power of two of at least 32 bits.
94inline unsigned promoteScalarArgumentSize(unsigned size) {
95 if (size <= 32)
96 return 32;
97 if (size <= 64)
98 return 64;
99 if (size <= 128)
100 return 128;
101 return size;
102}
103
104bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM);
105
106inline bool shouldPassAsArray(Type *Ty) {
107 return Ty->isAggregateType() || Ty->isVectorTy() ||
108 Ty->getScalarSizeInBits() >= 128 || Ty->isHalfTy() || Ty->isBFloatTy();
109}
110
111namespace NVPTX {
112// Returns a list of vector types that we prefer to fit into a single PTX
113// register. NOTE: This must be kept in sync with the register classes
114// defined in NVPTXRegisterInfo.td.
115inline auto packed_types() {
116 static const auto PackedTypes = {MVT::v4i8, MVT::v2f16, MVT::v2bf16,
117 MVT::v2i16, MVT::v2f32, MVT::v2i32};
118 return PackedTypes;
119}
120
121// Checks if the type VT can fit into a single register.
122inline bool isPackedVectorTy(EVT VT) {
123 return any_of(packed_types(), equal_to(VT));
124}
125
126// Checks if two or more of the type ET can fit into a single register.
127inline bool isPackedElementTy(EVT ET) {
128 return any_of(packed_types(),
129 [ET](EVT OVT) { return OVT.getVectorElementType() == ET; });
130}
131
132inline std::string getValidPTXIdentifier(StringRef Name) {
133 std::string ValidName;
134 ValidName.reserve(Name.size() + 4);
135 for (char C : Name)
136 // While PTX also allows '%' at the start of identifiers, LLVM will throw a
137 // fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
138 if (isAlnum(C) || C == '_' || C == '$')
139 ValidName.push_back(C);
140 else
141 ValidName.append({'_', '$', '_'});
142
143 return ValidName;
144}
145
146inline std::string OrderingToString(Ordering Order) {
147 switch (Order) {
149 return "NotAtomic";
151 return "Relaxed";
153 return "Acquire";
155 return "Release";
157 return "AcquireRelease";
159 return "SequentiallyConsistent";
161 return "Volatile";
163 return "RelaxedMMIO";
164 }
165 report_fatal_error(formatv("Unknown NVPTX::Ordering \"{}\".",
166 static_cast<OrderingUnderlyingType>(Order)));
167}
168
170 O << OrderingToString(Order);
171 return O;
172}
173
174inline std::string ScopeToString(Scope S) {
175 switch (S) {
176 case Scope::Thread:
177 return "Thread";
178 case Scope::System:
179 return "System";
180 case Scope::Block:
181 return "Block";
182 case Scope::Cluster:
183 return "Cluster";
184 case Scope::Device:
185 return "Device";
187 return "DefaultDevice";
188 }
189 report_fatal_error(formatv("Unknown NVPTX::Scope \"{}\".",
190 static_cast<ScopeUnderlyingType>(S)));
191}
192
194 O << ScopeToString(S);
195 return O;
196}
197
199 switch (A) {
201 return "generic";
203 return "global";
205 return "const";
207 return "shared";
209 return "shared::cluster";
211 return "param";
213 return "local";
214 }
215 report_fatal_error(formatv("Unknown NVPTX::AddressSpace \"{}\".",
216 static_cast<AddressSpaceUnderlyingType>(A)));
217}
218
221 return O;
222}
223
224} // namespace NVPTX
225} // namespace llvm
226
227#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define F(x, y, z)
Definition MD5.cpp:54
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Primary interface to the complete machine description for the target machine.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ PTX_Kernel
Call to a PTX kernel. Passes all arguments in parameter space.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
raw_ostream & operator<<(raw_ostream &O, Ordering Order)
unsigned int OrderingUnderlyingType
Definition NVPTX.h:165
std::string ScopeToString(Scope S)
@ SharedCluster
Definition NVPTX.h:199
auto packed_types()
std::string OrderingToString(Ordering Order)
unsigned int ScopeUnderlyingType
Definition NVPTX.h:181
bool isPackedVectorTy(EVT VT)
bool isPackedElementTy(EVT ET)
@ DefaultDevice
Definition NVPTX.h:188
std::string AddressSpaceToString(AddressSpace A)
@ RelaxedMMIO
Definition NVPTX.h:178
@ AcquireRelease
Definition NVPTX.h:174
@ NotAtomic
Definition NVPTX.h:167
@ SequentiallyConsistent
Definition NVPTX.h:175
unsigned int AddressSpaceUnderlyingType
Definition NVPTX.h:192
std::string getValidPTXIdentifier(StringRef Name)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
bool isManaged(const Value &V)
std::optional< uint64_t > getOverallClusterRank(const Function &F)
bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM)
MaybeAlign getAlign(const CallInst &I, unsigned Index)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
std::optional< unsigned > getMaxNReg(const Function &F)
StringRef getSamplerName(const Value &V)
bool isImageReadWrite(const Value &V)
bool isImageReadOnly(const Value &V)
std::optional< unsigned > getMinCTASm(const Function &F)
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
Definition STLExtras.h:2173
SmallVector< unsigned, 3 > getReqNTID(const Function &F)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
bool isImage(const Value &V)
bool isSampler(const Value &V)
unsigned promoteScalarArgumentSize(unsigned size)
void clearAnnotationCache(const Module *Mod)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool shouldPassAsArray(Type *Ty)
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
bool isSurface(const Value &V)
std::optional< unsigned > getMaxClusterRank(const Function &F)
StringRef getTextureName(const Value &V)
Align getFunctionByValParamAlign(const Function *F, Type *ArgTy, Align InitialAlign, const DataLayout &DL)
SmallVector< unsigned, 3 > getMaxNTID(const Function &F)
bool isParamGridConstant(const Argument &Arg)
StringRef getSurfaceName(const Value &V)
std::optional< uint64_t > getOverallReqNTID(const Function &F)
bool isKernelFunction(const Function &F)
bool isTexture(const Value &V)
Function * getMaybeBitcastedCallee(const CallBase *CB)
bool isImageWriteOnly(const Value &V)
std::optional< uint64_t > getOverallMaxNTID(const Function &F)
Align getFunctionArgumentAlignment(const Function *F, Type *Ty, unsigned Idx, const DataLayout &DL)
bool hasBlocksAreClusters(const Function &F)
SmallVector< unsigned, 3 > getClusterDim(const Function &F)
Align getFunctionParamOptimizedAlign(const Function *F, Type *ArgTy, const DataLayout &DL)
Since function arguments are passed via .param space, we may want to increase their alignment in a wa...
Extended Value Type.
Definition ValueTypes.h:35
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:336
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106