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"
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/// ABI alignment of \p ArgTy in .param space, capped at the PTX maximum of 128.
37
38/// The .param-space alignment for a byval parameter or call argument: the
39/// (possibly promoted) parameter alignment, raised to the ptxas byval minimum.
40Align getDeviceByValParamAlign(const Function *F, Type *ArgTy, unsigned AttrIdx,
41 const DataLayout &DL);
43 unsigned AttrIdx, const DataLayout &DL);
44
45/// Alignment for a function parameter or return value at AttributeList index
46/// \p AttrIdx (FirstArgIndex + argNo, or ReturnIndex). Prefers an explicit
47/// stackalign, else the ABI type alignment, folding in the byval `align`.
48Align getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx,
49 const DataLayout &DL);
50
51/// Alignment for a call-site argument or return value. Prefers an explicit
52/// stackalign on the call, else resolves the direct callee.
53Align getPTXParamAlign(const CallBase *CB, Type *Ty, unsigned AttrIdx,
54 const DataLayout &DL);
55
56// PTX ABI requires all scalar argument/return values to have
57// bit-size as a power of two of at least 32 bits.
58inline unsigned promoteScalarArgumentSize(unsigned size) {
59 if (size <= 32)
60 return 32;
61 if (size <= 64)
62 return 64;
63 if (size <= 128)
64 return 128;
65 return size;
66}
67
68bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM);
69
70inline bool shouldPassAsArray(Type *Ty) {
71 return Ty->isAggregateType() || Ty->isVectorTy() ||
72 Ty->getScalarSizeInBits() >= 128 || Ty->isHalfTy() || Ty->isBFloatTy();
73}
74
75namespace NVPTX {
76// Returns a list of vector types that we prefer to fit into a single PTX
77// register. NOTE: This must be kept in sync with the register classes
78// defined in NVPTXRegisterInfo.td.
79inline auto packed_types() {
80 static const auto PackedTypes = {MVT::v4i8, MVT::v2f16, MVT::v2bf16,
81 MVT::v2i16, MVT::v2f32, MVT::v2i32};
82 return PackedTypes;
83}
84
85// Checks if the type VT can fit into a single register.
86inline bool isPackedVectorTy(EVT VT) {
87 return any_of(packed_types(), equal_to(VT));
88}
89
90// Checks if two or more of the type ET can fit into a single register.
91inline bool isPackedElementTy(EVT ET) {
92 return any_of(packed_types(),
93 [ET](EVT OVT) { return OVT.getVectorElementType() == ET; });
94}
95
96inline std::string getValidPTXIdentifier(StringRef Name) {
97 std::string ValidName;
98 ValidName.reserve(Name.size() + 4);
99 for (char C : Name)
100 // While PTX also allows '%' at the start of identifiers, LLVM will throw a
101 // fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
102 if (isAlnum(C) || C == '_' || C == '$')
103 ValidName.push_back(C);
104 else
105 ValidName.append({'_', '$', '_'});
106
107 return ValidName;
108}
109
110inline std::string OrderingToString(Ordering Order) {
111 switch (Order) {
113 return "NotAtomic";
115 return "Relaxed";
117 return "Acquire";
119 return "Release";
121 return "AcquireRelease";
123 return "SequentiallyConsistent";
125 return "Volatile";
127 return "RelaxedMMIO";
128 }
129 report_fatal_error(formatv("Unknown NVPTX::Ordering \"{}\".",
130 static_cast<OrderingUnderlyingType>(Order)));
131}
132
134 O << OrderingToString(Order);
135 return O;
136}
137
138inline std::string ScopeToString(Scope S) {
139 switch (S) {
140 case Scope::Thread:
141 return "Thread";
142 case Scope::System:
143 return "System";
144 case Scope::Block:
145 return "Block";
146 case Scope::Cluster:
147 return "Cluster";
148 case Scope::Device:
149 return "Device";
151 return "DefaultDevice";
152 }
153 report_fatal_error(formatv("Unknown NVPTX::Scope \"{}\".",
154 static_cast<ScopeUnderlyingType>(S)));
155}
156
158 O << ScopeToString(S);
159 return O;
160}
161
163 bool UseParamSubqualifiers = false) {
164 switch (A) {
166 return "generic";
168 return "global";
170 return "const";
172 return "shared";
174 return "shared::cluster";
176 return UseParamSubqualifiers ? "param::entry" : "param";
178 return UseParamSubqualifiers ? "param::func" : "param";
180 return "local";
181 }
182 report_fatal_error(formatv("Unknown NVPTX::AddressSpace \"{}\".",
183 static_cast<AddressSpaceUnderlyingType>(A)));
184}
185
188 return O;
189}
190
191} // namespace NVPTX
192} // namespace llvm
193
194#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: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.
bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM)
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)
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