LLVM 23.0.0git
VFABIDemangler.h
Go to the documentation of this file.
1//===- VFABIDemangler.h - Vector Function ABI demangler ------- -*- 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 defines the VFABI demangling utility.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_IR_VFABIDEMANGLER_H
14#define LLVM_IR_VFABIDEMANGLER_H
15
22
23namespace llvm {
24
25/// Describes the type of Parameters
26enum class VFParamKind {
27 Vector, // No semantic information.
28 OMP_Linear, // declare simd linear(i)
29 OMP_LinearRef, // declare simd linear(ref(i))
30 OMP_LinearVal, // declare simd linear(val(i))
31 OMP_LinearUVal, // declare simd linear(uval(i))
32 OMP_LinearPos, // declare simd linear(i:c) uniform(c)
33 OMP_LinearValPos, // declare simd linear(val(i:c)) uniform(c)
34 OMP_LinearRefPos, // declare simd linear(ref(i:c)) uniform(c)
35 OMP_LinearUValPos, // declare simd linear(uval(i:c)) uniform(c)
36 OMP_Uniform, // declare simd uniform(i)
37 GlobalPredicate, // Global logical predicate that acts on all lanes
38 // of the input and output mask concurrently. For
39 // example, it is implied by the `M` token in the
40 // Vector Function ABI mangled name.
42};
43
44/// Describes the type of Instruction Set Architecture
45enum class VFISAKind {
46 AdvancedSIMD, // AArch64 Advanced SIMD (NEON)
47 SVE, // AArch64 Scalable Vector Extension
48 RVV, // RISC-V Vector Extension
49 SSE, // x86 SSE
50 AVX, // x86 AVX
51 AVX2, // x86 AVX2
52 AVX512, // x86 AVX512
53 LLVM, // LLVM internal ISA for functions that are not
54 // attached to an existing ABI via name mangling.
55 Unknown // Unknown ISA
56};
57
58/// Encapsulates information needed to describe a parameter.
59///
60/// The description of the parameter is not linked directly to
61/// OpenMP or any other vector function description. This structure
62/// is extendible to handle other paradigms that describe vector
63/// functions and their parameters.
65 unsigned ParamPos; // Parameter Position in Scalar Function.
66 VFParamKind ParamKind; // Kind of Parameter.
67 int LinearStepOrPos = 0; // Step or Position of the Parameter.
68 Align Alignment = Align(); // Optional alignment in bytes, defaulted to 1.
69
70 // Comparison operator.
71 bool operator==(const VFParameter &Other) const {
72 return std::tie(ParamPos, ParamKind, LinearStepOrPos, Alignment) ==
73 std::tie(Other.ParamPos, Other.ParamKind, Other.LinearStepOrPos,
74 Other.Alignment);
75 }
76};
77
78/// Contains the information about the kind of vectorization
79/// available.
80///
81/// This object in independent on the paradigm used to
82/// represent vector functions. in particular, it is not attached to
83/// any target-specific ABI.
84struct VFShape {
85 ElementCount VF; // Vectorization factor.
86 SmallVector<VFParameter, 8> Parameters; // List of parameter information.
87 // Comparison operator.
88 bool operator==(const VFShape &Other) const {
89 return std::tie(VF, Parameters) == std::tie(Other.VF, Other.Parameters);
90 }
91
92 /// Update the parameter in position P.ParamPos to P.
94 assert(P.ParamPos < Parameters.size() && "Invalid parameter position.");
95 Parameters[P.ParamPos] = P;
96 assert(hasValidParameterList() && "Invalid parameter list");
97 }
98
99 /// Retrieve the VFShape that can be used to map a scalar function to itself,
100 /// with VF = 1.
103 /*HasGlobalPredicate*/ false);
104 }
105
106 /// Retrieve the basic vectorization shape of the function, where all
107 /// parameters are mapped to VFParamKind::Vector with \p EC lanes. Specifies
108 /// whether the function has a Global Predicate argument via \p HasGlobalPred.
109 static VFShape get(const FunctionType *FTy, ElementCount EC,
110 bool HasGlobalPred) {
112 for (unsigned I = 0; I < FTy->getNumParams(); ++I)
114 if (HasGlobalPred)
115 Parameters.push_back(
117
118 return {EC, Parameters};
119 }
120 /// Validation check on the Parameters in the VFShape.
121 LLVM_ABI bool hasValidParameterList() const;
122};
123
124/// Holds the VFShape for a specific scalar to vector function mapping.
125struct VFInfo {
126 VFShape Shape; /// Classification of the vector function.
127 std::string ScalarName; /// Scalar Function Name.
128 std::string VectorName; /// Vector Function Name associated to this VFInfo.
129 VFISAKind ISA; /// Instruction Set Architecture.
130
131 /// Returns true if the last operand to the vectorized function has the
132 /// kind 'GlobalPredicate'.
133 bool isMasked() const {
134#ifndef NDEBUG
135 unsigned NumMaskParams =
136 llvm::count_if(Shape.Parameters, [](const VFParameter &I) {
137 return I.ParamKind == VFParamKind::GlobalPredicate;
138 });
139 assert(NumMaskParams <= 1 && "Should be at most one mask parameter");
140 assert((!NumMaskParams || Shape.Parameters.back().ParamKind ==
142 "Mask parameter in unexpected position");
143#endif
144 return !Shape.Parameters.empty() &&
145 Shape.Parameters.back().ParamKind == VFParamKind::GlobalPredicate;
146 }
147};
148
149namespace VFABI {
150/// LLVM Internal VFABI ISA token for vector functions.
151static constexpr char const *_LLVM_ = "_LLVM_";
152/// Prefix for internal name redirection for vector function that
153/// tells the compiler to scalarize the call using the scalar name
154/// of the function. For example, a mangled name like
155/// `_ZGV_LLVM_N2v_foo(_LLVM_Scalarize_foo)` would tell the
156/// vectorizer to vectorize the scalar call `foo`, and to scalarize
157/// it once vectorization is done.
158static constexpr char const *_LLVM_Scalarize_ = "_LLVM_Scalarize_";
159
160/// Function to construct a VFInfo out of a mangled names in the
161/// following format:
162///
163/// <VFABI_name>{(<redirection>)}
164///
165/// where <VFABI_name> is the name of the vector function, mangled according
166/// to the rules described in the Vector Function ABI of the target vector
167/// extension (or <isa> from now on). The <VFABI_name> is in the following
168/// format:
169///
170/// _ZGV<isa><mask><vlen><parameters>_<scalarname>[(<redirection>)]
171///
172/// This methods support demangling rules for the following <isa>:
173///
174/// * AArch64: https://developer.arm.com/docs/101129/latest
175///
176/// * x86 (libmvec): https://sourceware.org/glibc/wiki/libmvec and
177/// https://sourceware.org/glibc/wiki/libmvec?action=AttachFile&do=view&target=VectorABI.txt
178///
179/// \param MangledName -> input string in the format
180/// _ZGV<isa><mask><vlen><parameters>_<scalarname>[(<redirection>)].
181/// \param FTy -> FunctionType of the scalar function which we're trying to find
182/// a vectorized variant for. This is required to determine the vectorization
183/// factor for scalable vectors, since the mangled name doesn't encode that;
184/// it needs to be derived from the widest element types of vector arguments
185/// or return values.
186LLVM_ABI std::optional<VFInfo> tryDemangleForVFABI(StringRef MangledName,
187 const FunctionType *FTy);
188
189/// Retrieve the `VFParamKind` from a string token.
191
192// Name of the attribute where the variant mappings are stored.
193static constexpr char const *MappingsAttrName = "vector-function-abi-variant";
194
195/// Populates a set of strings representing the Vector Function ABI variants
196/// associated to the CallInst CI. If the CI does not contain the
197/// vector-function-abi-variant attribute, we return without populating
198/// VariantMappings, i.e. callers of getVectorVariantNames need not check for
199/// the presence of the attribute (see InjectTLIMappings).
200LLVM_ABI void
202 SmallVectorImpl<std::string> &VariantMappings);
203
204/// Constructs a FunctionType by applying vector function information to the
205/// type of a matching scalar function.
206/// \param Info gets the vectorization factor (VF) and the VFParamKind of the
207/// parameters.
208/// \param ScalarFTy gets the Type information of parameters, as it is not
209/// stored in \p Info.
210/// \returns a pointer to a newly created vector FunctionType
212 const FunctionType *ScalarFTy);
213
214/// Overwrite the Vector Function ABI variants attribute with the names provide
215/// in \p VariantMappings.
217 ArrayRef<std::string> VariantMappings);
218
219} // end namespace VFABI
220
221} // namespace llvm
222
223#endif // LLVM_IR_VFABIDEMANGLER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class represents a function call, abstracting a target machine's calling convention.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static constexpr char const * MappingsAttrName
static constexpr char const * _LLVM_Scalarize_
Prefix for internal name redirection for vector function that tells the compiler to scalarize the cal...
LLVM_ABI std::optional< VFInfo > tryDemangleForVFABI(StringRef MangledName, const FunctionType *FTy)
Function to construct a VFInfo out of a mangled names in the following format:
LLVM_ABI FunctionType * createFunctionType(const VFInfo &Info, const FunctionType *ScalarFTy)
Constructs a FunctionType by applying vector function information to the type of a matching scalar fu...
LLVM_ABI void getVectorVariantNames(const CallInst &CI, SmallVectorImpl< std::string > &VariantMappings)
Populates a set of strings representing the Vector Function ABI variants associated to the CallInst C...
LLVM_ABI void setVectorVariantNames(CallInst *CI, ArrayRef< std::string > VariantMappings)
Overwrite the Vector Function ABI variants attribute with the names provide in VariantMappings.
LLVM_ABI VFParamKind getVFParamKindFromString(const StringRef Token)
Retrieve the VFParamKind from a string token.
static constexpr char const * _LLVM_
LLVM Internal VFABI ISA token for vector functions.
This is an optimization pass for GlobalISel generic memory operations.
@ Other
Any other memory.
Definition ModRef.h:68
VFISAKind
Describes the type of Instruction Set Architecture.
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2018
VFParamKind
Describes the type of Parameters.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Holds the VFShape for a specific scalar to vector function mapping.
bool isMasked() const
Instruction Set Architecture.
std::string VectorName
Scalar Function Name.
VFISAKind ISA
Vector Function Name associated to this VFInfo.
std::string ScalarName
Classification of the vector function.
Encapsulates information needed to describe a parameter.
bool operator==(const VFParameter &Other) const
VFParamKind ParamKind
Contains the information about the kind of vectorization available.
LLVM_ABI bool hasValidParameterList() const
Validation check on the Parameters in the VFShape.
ElementCount VF
void updateParam(VFParameter P)
Update the parameter in position P.ParamPos to P.
SmallVector< VFParameter, 8 > Parameters
static VFShape get(const FunctionType *FTy, ElementCount EC, bool HasGlobalPred)
Retrieve the basic vectorization shape of the function, where all parameters are mapped to VFParamKin...
static VFShape getScalarShape(const FunctionType *FTy)
Retrieve the VFShape that can be used to map a scalar function to itself, with VF = 1.
bool operator==(const VFShape &Other) const