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 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/IR/DerivedTypes.h" 18 #include "llvm/IR/Instructions.h" 19 #include "llvm/Support/Alignment.h" 20 #include "llvm/Support/TypeSize.h" 21 22 namespace llvm { 23 24 /// Describes the type of Parameters 25 enum 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. 40 Unknown 41 }; 42 43 /// Describes the type of Instruction Set Architecture 44 enum 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. 63 struct VFParameter { 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. 83 struct 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. 92 void updateParam(VFParameter 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. 100 static VFShape getScalarShape(const FunctionType *FTy) { 101 return VFShape::get(FTy, ElementCount::getFixed(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) { 110 SmallVector<VFParameter, 8> Parameters; 111 for (unsigned I = 0; I < FTy->getNumParams(); ++I) 112 Parameters.push_back(VFParameter({I, VFParamKind::Vector})); 113 if (HasGlobalPred) 114 Parameters.push_back( 115 VFParameter({FTy->getNumParams(), VFParamKind::GlobalPredicate})); 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. 124 struct 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) 135 if (Shape.Parameters[i].ParamKind == VFParamKind::GlobalPredicate) 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 146 namespace VFABI { 147 /// LLVM Internal VFABI ISA token for vector functions. 148 static 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. 155 static 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. 183 std::optional<VFInfo> tryDemangleForVFABI(StringRef MangledName, 184 const FunctionType *FTy); 185 186 /// Retrieve the `VFParamKind` from a string token. 187 VFParamKind getVFParamKindFromString(const StringRef Token); 188 189 // Name of the attribute where the variant mappings are stored. 190 static 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). 197 void 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 207 FunctionType *createFunctionType(const VFInfo &Info, 208 const FunctionType *ScalarFTy); 209 210 /// Overwrite the Vector Function ABI variants attribute with the names provide 211 /// in \p VariantMappings. 212 void setVectorVariantNames(CallInst *CI, ArrayRef<std::string> VariantMappings); 213 214 } // end namespace VFABI 215 216 } // namespace llvm 217 218 #endif // LLVM_IR_VFABIDEMANGLER_H 219