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