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
44Align getFunctionArgumentAlignment(const Function *F, Type *Ty, unsigned Idx,
45 const DataLayout &DL);
46
48 Align InitialAlign, const DataLayout &DL);
49
50// PTX ABI requires all scalar argument/return values to have
51// bit-size as a power of two of at least 32 bits.
52inline unsigned promoteScalarArgumentSize(unsigned size) {
53 if (size <= 32)
54 return 32;
55 if (size <= 64)
56 return 64;
57 if (size <= 128)
58 return 128;
59 return size;
60}
61
62bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM);
63
64inline bool shouldPassAsArray(Type *Ty) {
65 return Ty->isAggregateType() || Ty->isVectorTy() ||
66 Ty->getScalarSizeInBits() >= 128 || Ty->isHalfTy() || Ty->isBFloatTy();
67}
68
69namespace NVPTX {
70// Returns a list of vector types that we prefer to fit into a single PTX
71// register. NOTE: This must be kept in sync with the register classes
72// defined in NVPTXRegisterInfo.td.
73inline auto packed_types() {
74 static const auto PackedTypes = {MVT::v4i8, MVT::v2f16, MVT::v2bf16,
75 MVT::v2i16, MVT::v2f32, MVT::v2i32};
76 return PackedTypes;
77}
78
79// Checks if the type VT can fit into a single register.
80inline bool isPackedVectorTy(EVT VT) {
81 return any_of(packed_types(), equal_to(VT));
82}
83
84// Checks if two or more of the type ET can fit into a single register.
85inline bool isPackedElementTy(EVT ET) {
86 return any_of(packed_types(),
87 [ET](EVT OVT) { return OVT.getVectorElementType() == ET; });
88}
89
90inline std::string getValidPTXIdentifier(StringRef Name) {
91 std::string ValidName;
92 ValidName.reserve(Name.size() + 4);
93 for (char C : Name)
94 // While PTX also allows '%' at the start of identifiers, LLVM will throw a
95 // fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
96 if (isAlnum(C) || C == '_' || C == '$')
97 ValidName.push_back(C);
98 else
99 ValidName.append({'_', '$', '_'});
100
101 return ValidName;
102}
103
104inline std::string OrderingToString(Ordering Order) {
105 switch (Order) {
107 return "NotAtomic";
109 return "Relaxed";
111 return "Acquire";
113 return "Release";
115 return "AcquireRelease";
117 return "SequentiallyConsistent";
119 return "Volatile";
121 return "RelaxedMMIO";
122 }
123 report_fatal_error(formatv("Unknown NVPTX::Ordering \"{}\".",
124 static_cast<OrderingUnderlyingType>(Order)));
125}
126
128 O << OrderingToString(Order);
129 return O;
130}
131
132inline std::string ScopeToString(Scope S) {
133 switch (S) {
134 case Scope::Thread:
135 return "Thread";
136 case Scope::System:
137 return "System";
138 case Scope::Block:
139 return "Block";
140 case Scope::Cluster:
141 return "Cluster";
142 case Scope::Device:
143 return "Device";
145 return "DefaultDevice";
146 }
147 report_fatal_error(formatv("Unknown NVPTX::Scope \"{}\".",
148 static_cast<ScopeUnderlyingType>(S)));
149}
150
152 O << ScopeToString(S);
153 return O;
154}
155
157 bool UseParamSubqualifiers = false) {
158 switch (A) {
160 return "generic";
162 return "global";
164 return "const";
166 return "shared";
168 return "shared::cluster";
170 return UseParamSubqualifiers ? "param::entry" : "param";
172 return UseParamSubqualifiers ? "param::func" : "param";
174 return "local";
175 }
176 report_fatal_error(formatv("Unknown NVPTX::AddressSpace \"{}\".",
177 static_cast<AddressSpaceUnderlyingType>(A)));
178}
179
182 return O;
183}
184
185} // namespace NVPTX
186} // namespace llvm
187
188#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
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: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:173
std::string ScopeToString(Scope S)
@ DeviceParam
Definition NVPTX.h:214
@ SharedCluster
Definition NVPTX.h:207
@ EntryParam
Definition NVPTX.h:208
auto packed_types()
std::string OrderingToString(Ordering Order)
unsigned int ScopeUnderlyingType
Definition NVPTX.h:189
bool isPackedVectorTy(EVT VT)
bool isPackedElementTy(EVT ET)
@ DefaultDevice
Definition NVPTX.h:196
@ RelaxedMMIO
Definition NVPTX.h:186
@ AcquireRelease
Definition NVPTX.h:182
@ NotAtomic
Definition NVPTX.h:175
@ SequentiallyConsistent
Definition NVPTX.h:183
unsigned int AddressSpaceUnderlyingType
Definition NVPTX.h:200
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:1669
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 getFunctionByValParamAlign(const Function *F, Type *ArgTy, Align InitialAlign, const DataLayout &DL)
Function * getMaybeBitcastedCallee(const CallBase *CB)
Align getFunctionArgumentAlignment(const Function *F, Type *Ty, unsigned Idx, const DataLayout &DL)
Align getFunctionParamOptimizedAlign(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...
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:336