LLVM 22.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 TargetMachine;
33
34void clearAnnotationCache(const Module *);
35
36bool isTexture(const Value &);
37bool isSurface(const Value &);
38bool isSampler(const Value &);
39bool isImage(const Value &);
40bool isImageReadOnly(const Value &);
41bool isImageWriteOnly(const Value &);
42bool isImageReadWrite(const Value &);
43bool isManaged(const Value &);
44
48
52
53std::optional<uint64_t> getOverallMaxNTID(const Function &);
54std::optional<uint64_t> getOverallReqNTID(const Function &);
55std::optional<uint64_t> getOverallClusterRank(const Function &);
56
57std::optional<unsigned> getMaxClusterRank(const Function &);
58std::optional<unsigned> getMinCTASm(const Function &);
59std::optional<unsigned> getMaxNReg(const Function &);
60
61bool hasBlocksAreClusters(const Function &);
62
63inline bool isKernelFunction(const Function &F) {
64 return F.getCallingConv() == CallingConv::PTX_Kernel;
65}
66
67bool isParamGridConstant(const Argument &);
68
69inline MaybeAlign getAlign(const Function &F, unsigned Index) {
70 return F.getAttributes().getAttributes(Index).getStackAlignment();
71}
72
73MaybeAlign getAlign(const CallInst &, unsigned);
74Function *getMaybeBitcastedCallee(const CallBase *CB);
75
76// PTX ABI requires all scalar argument/return values to have
77// bit-size as a power of two of at least 32 bits.
78inline unsigned promoteScalarArgumentSize(unsigned size) {
79 if (size <= 32)
80 return 32;
81 if (size <= 64)
82 return 64;
83 if (size <= 128)
84 return 128;
85 return size;
86}
87
88bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM);
89
90inline bool shouldPassAsArray(Type *Ty) {
91 return Ty->isAggregateType() || Ty->isVectorTy() ||
92 Ty->getScalarSizeInBits() >= 128 || Ty->isHalfTy() || Ty->isBFloatTy();
93}
94
95namespace NVPTX {
96// Returns a list of vector types that we prefer to fit into a single PTX
97// register. NOTE: This must be kept in sync with the register classes
98// defined in NVPTXRegisterInfo.td.
99inline auto packed_types() {
100 static const auto PackedTypes = {MVT::v4i8, MVT::v2f16, MVT::v2bf16,
101 MVT::v2i16, MVT::v2f32, MVT::v2i32};
102 return PackedTypes;
103}
104
105// Checks if the type VT can fit into a single register.
106inline bool isPackedVectorTy(EVT VT) {
107 return any_of(packed_types(), [VT](EVT OVT) { return OVT == VT; });
108}
109
110// Checks if two or more of the type ET can fit into a single register.
111inline bool isPackedElementTy(EVT ET) {
112 return any_of(packed_types(),
113 [ET](EVT OVT) { return OVT.getVectorElementType() == ET; });
114}
115
116inline std::string getValidPTXIdentifier(StringRef Name) {
117 std::string ValidName;
118 ValidName.reserve(Name.size() + 4);
119 for (char C : Name)
120 // While PTX also allows '%' at the start of identifiers, LLVM will throw a
121 // fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
122 if (isAlnum(C) || C == '_' || C == '$')
123 ValidName.push_back(C);
124 else
125 ValidName.append({'_', '$', '_'});
126
127 return ValidName;
128}
129
130inline std::string OrderingToString(Ordering Order) {
131 switch (Order) {
133 return "NotAtomic";
135 return "Relaxed";
137 return "Acquire";
139 return "Release";
141 return "AcquireRelease";
143 return "SequentiallyConsistent";
145 return "Volatile";
147 return "RelaxedMMIO";
148 }
149 report_fatal_error(formatv("Unknown NVPTX::Ordering \"{}\".",
150 static_cast<OrderingUnderlyingType>(Order)));
151}
152
154 O << OrderingToString(Order);
155 return O;
156}
157
158inline std::string ScopeToString(Scope S) {
159 switch (S) {
160 case Scope::Thread:
161 return "Thread";
162 case Scope::System:
163 return "System";
164 case Scope::Block:
165 return "Block";
166 case Scope::Cluster:
167 return "Cluster";
168 case Scope::Device:
169 return "Device";
171 return "DefaultDevice";
172 }
173 report_fatal_error(formatv("Unknown NVPTX::Scope \"{}\".",
174 static_cast<ScopeUnderlyingType>(S)));
175}
176
178 O << ScopeToString(S);
179 return O;
180}
181
183 switch (A) {
185 return "generic";
187 return "global";
189 return "const";
191 return "shared";
193 return "shared::cluster";
195 return "param";
197 return "local";
198 }
199 report_fatal_error(formatv("Unknown NVPTX::AddressSpace \"{}\".",
200 static_cast<AddressSpaceUnderlyingType>(A)));
201}
202
205 return O;
206}
207
208} // namespace NVPTX
209} // namespace llvm
210
211#endif
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 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:152
std::string ScopeToString(Scope S)
@ SharedCluster
Definition NVPTX.h:186
auto packed_types()
std::string OrderingToString(Ordering Order)
unsigned int ScopeUnderlyingType
Definition NVPTX.h:168
bool isPackedVectorTy(EVT VT)
bool isPackedElementTy(EVT ET)
@ DefaultDevice
Definition NVPTX.h:175
std::string AddressSpaceToString(AddressSpace A)
@ RelaxedMMIO
Definition NVPTX.h:165
@ AcquireRelease
Definition NVPTX.h:161
@ NotAtomic
Definition NVPTX.h:154
@ SequentiallyConsistent
Definition NVPTX.h:162
unsigned int AddressSpaceUnderlyingType
Definition NVPTX.h:179
std::string getValidPTXIdentifier(StringRef Name)
This is an optimization pass for GlobalISel generic memory operations.
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:1655
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)
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:1732
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:167
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)
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)
bool hasBlocksAreClusters(const Function &F)
SmallVector< unsigned, 3 > getClusterDim(const Function &F)
Extended Value Type.
Definition ValueTypes.h:35
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106