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 <set>
29#include <string>
30#include <vector>
31
32namespace llvm {
33
34class TargetMachine;
35
36void clearAnnotationCache(const Module *);
37
38bool isTexture(const Value &);
39bool isSurface(const Value &);
40bool isSampler(const Value &);
41bool isImage(const Value &);
42bool isImageReadOnly(const Value &);
43bool isImageWriteOnly(const Value &);
44bool isImageReadWrite(const Value &);
45bool isManaged(const Value &);
46
47StringRef getTextureName(const Value &);
48StringRef getSurfaceName(const Value &);
49StringRef getSamplerName(const Value &);
50
51SmallVector<unsigned, 3> getMaxNTID(const Function &);
52SmallVector<unsigned, 3> getReqNTID(const Function &);
53SmallVector<unsigned, 3> getClusterDim(const Function &);
54
55std::optional<uint64_t> getOverallMaxNTID(const Function &);
56std::optional<uint64_t> getOverallReqNTID(const Function &);
57std::optional<uint64_t> getOverallClusterRank(const Function &);
58
59std::optional<unsigned> getMaxClusterRank(const Function &);
60std::optional<unsigned> getMinCTASm(const Function &);
61std::optional<unsigned> getMaxNReg(const Function &);
62
63bool hasBlocksAreClusters(const Function &);
64
65inline bool isKernelFunction(const Function &F) {
66 return F.getCallingConv() == CallingConv::PTX_Kernel;
67}
68
69bool isParamGridConstant(const Argument &);
70
71inline MaybeAlign getAlign(const Function &F, unsigned Index) {
72 return F.getAttributes().getAttributes(Index).getStackAlignment();
73}
74
75MaybeAlign getAlign(const CallInst &, unsigned);
76Function *getMaybeBitcastedCallee(const CallBase *CB);
77
78// PTX ABI requires all scalar argument/return values to have
79// bit-size as a power of two of at least 32 bits.
80inline unsigned promoteScalarArgumentSize(unsigned size) {
81 if (size <= 32)
82 return 32;
83 if (size <= 64)
84 return 64;
85 if (size <= 128)
86 return 128;
87 return size;
88}
89
90bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM);
91
92inline bool shouldPassAsArray(Type *Ty) {
93 return Ty->isAggregateType() || Ty->isVectorTy() ||
94 Ty->getScalarSizeInBits() >= 128 || Ty->isHalfTy() || Ty->isBFloatTy();
95}
96
97namespace NVPTX {
98// Returns a list of vector types that we prefer to fit into a single PTX
99// register. NOTE: This must be kept in sync with the register classes
100// defined in NVPTXRegisterInfo.td.
101inline auto packed_types() {
102 static const auto PackedTypes = {MVT::v4i8, MVT::v2f16, MVT::v2bf16,
103 MVT::v2i16, MVT::v2f32};
104 return PackedTypes;
105}
106
107// Checks if the type VT can fit into a single register.
108inline bool isPackedVectorTy(EVT VT) {
109 return any_of(packed_types(), [VT](EVT OVT) { return OVT == VT; });
110}
111
112// Checks if two or more of the type ET can fit into a single register.
113inline bool isPackedElementTy(EVT ET) {
114 return any_of(packed_types(),
115 [ET](EVT OVT) { return OVT.getVectorElementType() == ET; });
116}
117
119 std::string ValidName;
120 ValidName.reserve(Name.size() + 4);
121 for (char C : Name)
122 // While PTX also allows '%' at the start of identifiers, LLVM will throw a
123 // fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
124 if (isAlnum(C) || C == '_' || C == '$')
125 ValidName.push_back(C);
126 else
127 ValidName.append({'_', '$', '_'});
128
129 return ValidName;
130}
131
132inline std::string OrderingToString(Ordering Order) {
133 switch (Order) {
135 return "NotAtomic";
137 return "Relaxed";
139 return "Acquire";
141 return "Release";
143 return "AcquireRelease";
145 return "SequentiallyConsistent";
147 return "Volatile";
149 return "RelaxedMMIO";
150 }
151 report_fatal_error(formatv("Unknown NVPTX::Ordering \"{}\".",
152 static_cast<OrderingUnderlyingType>(Order)));
153}
154
156 O << OrderingToString(Order);
157 return O;
158}
159
160inline std::string ScopeToString(Scope S) {
161 switch (S) {
162 case Scope::Thread:
163 return "Thread";
164 case Scope::System:
165 return "System";
166 case Scope::Block:
167 return "Block";
168 case Scope::Cluster:
169 return "Cluster";
170 case Scope::Device:
171 return "Device";
173 return "DefaultDevice";
174 }
175 report_fatal_error(formatv("Unknown NVPTX::Scope \"{}\".",
176 static_cast<ScopeUnderlyingType>(S)));
177}
178
180 O << ScopeToString(S);
181 return O;
182}
183
185 switch (A) {
187 return "generic";
189 return "global";
191 return "const";
193 return "shared";
195 return "shared::cluster";
197 return "param";
199 return "local";
200 }
201 report_fatal_error(formatv("Unknown NVPTX::AddressSpace \"{}\".",
202 static_cast<AddressSpaceUnderlyingType>(A)));
203}
204
207 return O;
208}
209
210} // namespace NVPTX
211} // namespace llvm
212
213#endif
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
std::string Name
uint32_t Index
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition: Type.h:145
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition: Type.h:304
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:142
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
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.
Definition: CallingConv.h:125
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
raw_ostream & operator<<(raw_ostream &O, Ordering Order)
std::string ScopeToString(Scope S)
AddressSpace
Definition: NVPTX.h:181
@ Global
Definition: NVPTX.h:183
@ SharedCluster
Definition: NVPTX.h:187
@ Shared
Definition: NVPTX.h:184
@ Generic
Definition: NVPTX.h:182
auto packed_types()
unsigned int AddressSpaceUnderlyingType
Definition: NVPTX.h:180
std::string OrderingToString(Ordering Order)
bool isPackedVectorTy(EVT VT)
unsigned int OrderingUnderlyingType
Definition: NVPTX.h:153
bool isPackedElementTy(EVT ET)
unsigned int ScopeUnderlyingType
Definition: NVPTX.h:169
@ System
Definition: NVPTX.h:175
@ Cluster
Definition: NVPTX.h:173
@ Thread
Definition: NVPTX.h:171
@ Device
Definition: NVPTX.h:174
@ DefaultDevice
Definition: NVPTX.h:176
std::string AddressSpaceToString(AddressSpace A)
@ RelaxedMMIO
Definition: NVPTX.h:166
@ Acquire
Definition: NVPTX.h:160
@ Relaxed
Definition: NVPTX.h:158
@ AcquireRelease
Definition: NVPTX.h:162
@ NotAtomic
Definition: NVPTX.h:155
@ Volatile
Definition: NVPTX.h:165
@ Release
Definition: NVPTX.h:161
@ SequentiallyConsistent
Definition: NVPTX.h:163
std::string getValidPTXIdentifier(StringRef Name)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1702
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:1751
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 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:323
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117