LLVM 24.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 declarations for PTX-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"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/InstrTypes.h"
21#include "llvm/IR/Value.h"
24#include <cstdarg>
25#include <string>
26
27namespace llvm {
28
29class DataLayout;
30class MemSDNode;
31
33
34/// The bit-width of a single element loaded by \p Mem, i.e. the width used for
35/// the ".fromtype" part of the emitted PTX load.
36unsigned getFromTypeWidthForLoad(const MemSDNode *Mem);
37
38/// ABI alignment of \p ArgTy in .param space, capped at the PTX maximum of 128.
40
41/// The .param-space alignment for a byval parameter or call argument: the
42/// (possibly promoted) parameter alignment, raised to the ptxas byval minimum.
43Align getDeviceByValParamAlign(const Function *F, Type *ArgTy, unsigned AttrIdx,
44 const DataLayout &DL);
46 unsigned AttrIdx, const DataLayout &DL);
47
48/// Alignment for a function parameter or return value at AttributeList index
49/// \p AttrIdx (FirstArgIndex + argNo, or ReturnIndex). Prefers an explicit
50/// stackalign, else the ABI type alignment, folding in the byval `align`.
51Align getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx,
52 const DataLayout &DL);
53
54/// Alignment for a call-site argument or return value. Prefers an explicit
55/// stackalign on the call, else resolves the direct callee.
56Align getPTXParamAlign(const CallBase *CB, Type *Ty, unsigned AttrIdx,
57 const DataLayout &DL);
58
59// PTX ABI requires all scalar argument/return values to have
60// bit-size as a power of two of at least 32 bits.
61inline unsigned promoteScalarArgumentSize(unsigned size) {
62 if (size <= 32)
63 return 32;
64 if (size <= 64)
65 return 64;
66 if (size <= 128)
67 return 128;
68 return size;
69}
70
71inline bool shouldPassAsArray(Type *Ty) {
72 return Ty->isAggregateType() || Ty->isVectorTy() ||
73 Ty->getScalarSizeInBits() >= 128 || Ty->isHalfTy() || Ty->isBFloatTy();
74}
75
76namespace NVPTX {
77// Returns a list of vector types that we prefer to fit into a single PTX
78// register. NOTE: This must be kept in sync with the register classes
79// defined in NVPTXRegisterInfo.td.
80inline auto packed_types() {
81 static const auto PackedTypes = {MVT::v4i8, MVT::v2f16, MVT::v2bf16,
82 MVT::v2i16, MVT::v2f32, MVT::v2i32};
83 return PackedTypes;
84}
85
86// Checks if the type VT can fit into a single register.
87inline bool isPackedVectorTy(EVT VT) {
88 return any_of(packed_types(), equal_to(VT));
89}
90
91// Checks if two or more of the type ET can fit into a single register.
92inline bool isPackedElementTy(EVT ET) {
93 return any_of(packed_types(),
94 [ET](EVT OVT) { return OVT.getVectorElementType() == ET; });
95}
96
97inline std::string getValidPTXIdentifier(StringRef Name) {
98 std::string ValidName;
99 ValidName.reserve(Name.size() + 4);
100 for (char C : Name)
101 // While PTX also allows '%' at the start of identifiers, LLVM will throw a
102 // fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
103 if (isAlnum(C) || C == '_' || C == '$')
104 ValidName.push_back(C);
105 else
106 ValidName.append({'_', '$', '_'});
107
108 return ValidName;
109}
110
111inline std::string OrderingToString(Ordering Order) {
112 switch (Order) {
114 return "NotAtomic";
116 return "Relaxed";
118 return "Acquire";
120 return "Release";
122 return "AcquireRelease";
124 return "SequentiallyConsistent";
126 return "Volatile";
128 return "RelaxedMMIO";
129 }
130 report_fatal_error(formatv("Unknown NVPTX::Ordering \"{}\".",
131 static_cast<OrderingUnderlyingType>(Order)));
132}
133
135 O << OrderingToString(Order);
136 return O;
137}
138
139inline std::string ScopeToString(Scope S) {
140 switch (S) {
141 case Scope::Thread:
142 return "Thread";
143 case Scope::System:
144 return "System";
145 case Scope::Block:
146 return "Block";
147 case Scope::Cluster:
148 return "Cluster";
149 case Scope::Device:
150 return "Device";
152 return "DefaultDevice";
153 }
154 report_fatal_error(formatv("Unknown NVPTX::Scope \"{}\".",
155 static_cast<ScopeUnderlyingType>(S)));
156}
157
159 O << ScopeToString(S);
160 return O;
161}
162
164 bool UseParamSubqualifiers = false) {
165 switch (A) {
167 return "generic";
169 return "global";
171 return "const";
173 return "shared";
175 return "shared::cluster";
177 return UseParamSubqualifiers ? "param::entry" : "param";
179 return UseParamSubqualifiers ? "param::func" : "param";
181 return "local";
182 }
183 report_fatal_error(formatv("Unknown NVPTX::AddressSpace \"{}\".",
184 static_cast<AddressSpaceUnderlyingType>(A)));
185}
186
189 return O;
190}
191
192} // namespace NVPTX
193} // namespace llvm
194
195#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define F(x, y, z)
Definition MD5.cpp:54
This file contains some functions that are useful when dealing with strings.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
This is an abstract virtual class for memory operations.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
const char * addressSpaceToString(AddressSpace A, bool UseParamSubqualifiers=false)
raw_ostream & operator<<(raw_ostream &O, Ordering Order)
unsigned int OrderingUnderlyingType
Definition NVPTX.h:176
std::string ScopeToString(Scope S)
@ DeviceParam
Definition NVPTX.h:217
@ SharedCluster
Definition NVPTX.h:210
@ EntryParam
Definition NVPTX.h:211
auto packed_types()
std::string OrderingToString(Ordering Order)
unsigned int ScopeUnderlyingType
Definition NVPTX.h:192
bool isPackedVectorTy(EVT VT)
bool isPackedElementTy(EVT ET)
@ DefaultDevice
Definition NVPTX.h:199
@ RelaxedMMIO
Definition NVPTX.h:189
@ AcquireRelease
Definition NVPTX.h:185
@ NotAtomic
Definition NVPTX.h:178
@ SequentiallyConsistent
Definition NVPTX.h:186
unsigned int AddressSpaceUnderlyingType
Definition NVPTX.h:203
std::string getValidPTXIdentifier(StringRef Name)
This is an optimization pass for GlobalISel generic memory operations.
Align getDeviceByValParamAlign(const Function *F, Type *ArgTy, unsigned AttrIdx, const DataLayout &DL)
The .param-space alignment for a byval parameter or call argument: the (possibly promoted) parameter ...
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
Align getPTXParamTypeAlign(Type *ArgTy, const DataLayout &DL)
ABI alignment of ArgTy in .param space, capped at the PTX maximum of 128.
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
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)
unsigned promoteScalarArgumentSize(unsigned size)
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...
Align getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx, const DataLayout &DL)
Alignment for a function parameter or return value at AttributeList index AttrIdx (FirstArgIndex + ar...
Function * getMaybeBitcastedCallee(const CallBase *CB)
unsigned getFromTypeWidthForLoad(const MemSDNode *Mem)
The bit-width of a single element loaded by Mem, i.e.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351