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 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"
20#include "llvm/IR/Function.h"
22#include "llvm/IR/Value.h"
25#include <cstdarg>
26#include <string>
27
28namespace llvm {
29
30class DataLayout;
31class TargetMachine;
32
34
35/// Since function arguments are passed via .param space, we may want to
36/// increase their alignment in a way that ensures that we can effectively
37/// vectorize their loads & stores. We can increase alignment only if the
38/// function has internal or private linkage as for other linkage types callers
39/// may already rely on default alignment. To allow using 128-bit vectorized
40/// loads/stores, this function ensures that alignment is 16 or greater.
42 const DataLayout &DL);
43
45 Align InitialAlign, const DataLayout &DL);
46
47/// Get the alignment for a function parameter or return value.
48/// \p AttrIdx is the AttributeList index (e.g. FirstArgIndex + argNo, or
49/// ReturnIndex for return values). Checks for an explicit alignment attribute,
50/// then falls back to getPromotedParamTypeAlign, incorporating byval param
51/// alignment when applicable.
52Align getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx,
53 const DataLayout &DL);
54
55/// Get the alignment for a call-site argument or return value. Resolves the
56/// callee and delegates to the Function overload of getParamAlign. For
57/// indirect calls with no resolvable callee, falls back to
58/// getPromotedParamTypeAlign.
59Align getPTXParamAlign(const CallBase *CB, Type *Ty, unsigned AttrIdx,
60 const DataLayout &DL);
61
62// PTX ABI requires all scalar argument/return values to have
63// bit-size as a power of two of at least 32 bits.
64inline unsigned promoteScalarArgumentSize(unsigned size) {
65 if (size <= 32)
66 return 32;
67 if (size <= 64)
68 return 64;
69 if (size <= 128)
70 return 128;
71 return size;
72}
73
74bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM);
75
76inline bool shouldPassAsArray(Type *Ty) {
77 return Ty->isAggregateType() || Ty->isVectorTy() ||
78 Ty->getScalarSizeInBits() >= 128 || Ty->isHalfTy() || Ty->isBFloatTy();
79}
80
81namespace NVPTX {
82// Returns a list of vector types that we prefer to fit into a single PTX
83// register. NOTE: This must be kept in sync with the register classes
84// defined in NVPTXRegisterInfo.td.
85inline auto packed_types() {
86 static const auto PackedTypes = {MVT::v4i8, MVT::v2f16, MVT::v2bf16,
87 MVT::v2i16, MVT::v2f32, MVT::v2i32};
88 return PackedTypes;
89}
90
91// Checks if the type VT can fit into a single register.
92inline bool isPackedVectorTy(EVT VT) {
93 return any_of(packed_types(), equal_to(VT));
94}
95
96// Checks if two or more of the type ET can fit into a single register.
97inline bool isPackedElementTy(EVT ET) {
98 return any_of(packed_types(),
99 [ET](EVT OVT) { return OVT.getVectorElementType() == ET; });
100}
101
102inline std::string getValidPTXIdentifier(StringRef Name) {
103 std::string ValidName;
104 ValidName.reserve(Name.size() + 4);
105 for (char C : Name)
106 // While PTX also allows '%' at the start of identifiers, LLVM will throw a
107 // fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
108 if (isAlnum(C) || C == '_' || C == '$')
109 ValidName.push_back(C);
110 else
111 ValidName.append({'_', '$', '_'});
112
113 return ValidName;
114}
115
116inline std::string OrderingToString(Ordering Order) {
117 switch (Order) {
119 return "NotAtomic";
121 return "Relaxed";
123 return "Acquire";
125 return "Release";
127 return "AcquireRelease";
129 return "SequentiallyConsistent";
131 return "Volatile";
133 return "RelaxedMMIO";
134 }
135 report_fatal_error(formatv("Unknown NVPTX::Ordering \"{}\".",
136 static_cast<OrderingUnderlyingType>(Order)));
137}
138
140 O << OrderingToString(Order);
141 return O;
142}
143
144inline std::string ScopeToString(Scope S) {
145 switch (S) {
146 case Scope::Thread:
147 return "Thread";
148 case Scope::System:
149 return "System";
150 case Scope::Block:
151 return "Block";
152 case Scope::Cluster:
153 return "Cluster";
154 case Scope::Device:
155 return "Device";
157 return "DefaultDevice";
158 }
159 report_fatal_error(formatv("Unknown NVPTX::Scope \"{}\".",
160 static_cast<ScopeUnderlyingType>(S)));
161}
162
164 O << ScopeToString(S);
165 return O;
166}
167
169 bool UseParamSubqualifiers = false) {
170 switch (A) {
172 return "generic";
174 return "global";
176 return "const";
178 return "shared";
180 return "shared::cluster";
182 return UseParamSubqualifiers ? "param::entry" : "param";
184 return UseParamSubqualifiers ? "param::func" : "param";
186 return "local";
187 }
188 report_fatal_error(formatv("Unknown NVPTX::AddressSpace \"{}\".",
189 static_cast<AddressSpaceUnderlyingType>(A)));
190}
191
194 return O;
195}
196
197} // namespace NVPTX
198} // namespace llvm
199
200#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.
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:46
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
const char * addressSpaceToString(AddressSpace A, bool UseParamSubqualifiers=false)
raw_ostream & operator<<(raw_ostream &O, Ordering Order)
unsigned int OrderingUnderlyingType
Definition NVPTX.h:174
std::string ScopeToString(Scope S)
@ DeviceParam
Definition NVPTX.h:215
@ SharedCluster
Definition NVPTX.h:208
@ EntryParam
Definition NVPTX.h:209
auto packed_types()
std::string OrderingToString(Ordering Order)
unsigned int ScopeUnderlyingType
Definition NVPTX.h:190
bool isPackedVectorTy(EVT VT)
bool isPackedElementTy(EVT ET)
@ DefaultDevice
Definition NVPTX.h:197
@ RelaxedMMIO
Definition NVPTX.h:187
@ AcquireRelease
Definition NVPTX.h:183
@ NotAtomic
Definition NVPTX.h:176
@ SequentiallyConsistent
Definition NVPTX.h:184
unsigned int AddressSpaceUnderlyingType
Definition NVPTX.h:201
std::string getValidPTXIdentifier(StringRef Name)
This is an optimization pass for GlobalISel generic memory operations.
bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM)
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:1668
Align getPTXPromotedParamTypeAlign(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...
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:2172
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:1745
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)
Get the alignment for a function parameter or return value.
Align getDeviceByValParamAlign(const Function *F, Type *ArgTy, Align InitialAlign, const DataLayout &DL)
Function * getMaybeBitcastedCallee(const CallBase *CB)
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