1 //===- InjectTLIMAppings.cpp - TLI to VFABI attribute injection ----------===// 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 // Populates the VFABI attribute with the scalar-to-vector mappings 10 // from the TargetLibraryInfo. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/InjectTLIMappings.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/Analysis/DemandedBits.h" 17 #include "llvm/Analysis/GlobalsModRef.h" 18 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 19 #include "llvm/Analysis/TargetLibraryInfo.h" 20 #include "llvm/Analysis/VectorUtils.h" 21 #include "llvm/IR/InstIterator.h" 22 #include "llvm/Transforms/Utils/ModuleUtils.h" 23 24 using namespace llvm; 25 26 #define DEBUG_TYPE "inject-tli-mappings" 27 28 STATISTIC(NumCallInjected, 29 "Number of calls in which the mappings have been injected."); 30 31 STATISTIC(NumVFDeclAdded, 32 "Number of function declarations that have been added."); 33 STATISTIC(NumCompUsedAdded, 34 "Number of `@llvm.compiler.used` operands that have been added."); 35 36 /// A helper function that adds the vector variant declaration for vectorizing 37 /// the CallInst \p CI with a vectorization factor of \p VF lanes. For each 38 /// mapping, TLI provides a VABI prefix, which contains all information required 39 /// to create vector function declaration. 40 static void addVariantDeclaration(CallInst &CI, const ElementCount &VF, 41 const VecDesc *VD) { 42 Module *M = CI.getModule(); 43 FunctionType *ScalarFTy = CI.getFunctionType(); 44 45 assert(!ScalarFTy->isVarArg() && "VarArg functions are not supported."); 46 47 const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI( 48 VD->getVectorFunctionABIVariantString(), ScalarFTy); 49 50 assert(Info && "Failed to demangle vector variant"); 51 assert(Info->Shape.VF == VF && "Mangled name does not match VF"); 52 53 const StringRef VFName = VD->getVectorFnName(); 54 FunctionType *VectorFTy = VFABI::createFunctionType(*Info, ScalarFTy); 55 Function *VecFunc = 56 Function::Create(VectorFTy, Function::ExternalLinkage, VFName, M); 57 VecFunc->copyAttributesFrom(CI.getCalledFunction()); 58 ++NumVFDeclAdded; 59 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName 60 << "` of type " << *VectorFTy << "\n"); 61 62 // Make function declaration (without a body) "sticky" in the IR by 63 // listing it in the @llvm.compiler.used intrinsic. 64 assert(!VecFunc->size() && "VFABI attribute requires `@llvm.compiler.used` " 65 "only on declarations."); 66 appendToCompilerUsed(*M, {VecFunc}); 67 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName 68 << "` to `@llvm.compiler.used`.\n"); 69 ++NumCompUsedAdded; 70 } 71 72 static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) { 73 // This is needed to make sure we don't query the TLI for calls to 74 // bitcast of function pointers, like `%call = call i32 (i32*, ...) 75 // bitcast (i32 (...)* @goo to i32 (i32*, ...)*)(i32* nonnull %i)`, 76 // as such calls make the `isFunctionVectorizable` raise an 77 // exception. 78 if (CI.isNoBuiltin() || !CI.getCalledFunction()) 79 return; 80 81 StringRef ScalarName = CI.getCalledFunction()->getName(); 82 83 // Nothing to be done if the TLI thinks the function is not 84 // vectorizable. 85 if (!TLI.isFunctionVectorizable(ScalarName)) 86 return; 87 SmallVector<std::string, 8> Mappings; 88 VFABI::getVectorVariantNames(CI, Mappings); 89 Module *M = CI.getModule(); 90 const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(), 91 Mappings.end()); 92 93 auto AddVariantDecl = [&](const ElementCount &VF, bool Predicate) { 94 const VecDesc *VD = TLI.getVectorMappingInfo(ScalarName, VF, Predicate); 95 if (VD && !VD->getVectorFnName().empty()) { 96 std::string MangledName = VD->getVectorFunctionABIVariantString(); 97 if (!OriginalSetOfMappings.count(MangledName)) { 98 Mappings.push_back(MangledName); 99 ++NumCallInjected; 100 } 101 Function *VariantF = M->getFunction(VD->getVectorFnName()); 102 if (!VariantF) 103 addVariantDeclaration(CI, VF, VD); 104 } 105 }; 106 107 // All VFs in the TLI are powers of 2. 108 ElementCount WidestFixedVF, WidestScalableVF; 109 TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF); 110 111 for (bool Predicated : {false, true}) { 112 for (ElementCount VF = ElementCount::getFixed(2); 113 ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2) 114 AddVariantDecl(VF, Predicated); 115 116 for (ElementCount VF = ElementCount::getScalable(2); 117 ElementCount::isKnownLE(VF, WidestScalableVF); VF *= 2) 118 AddVariantDecl(VF, Predicated); 119 } 120 121 VFABI::setVectorVariantNames(&CI, Mappings); 122 } 123 124 static bool runImpl(const TargetLibraryInfo &TLI, Function &F) { 125 for (auto &I : instructions(F)) 126 if (auto CI = dyn_cast<CallInst>(&I)) 127 addMappingsFromTLI(TLI, *CI); 128 // Even if the pass adds IR attributes, the analyses are preserved. 129 return false; 130 } 131 132 //////////////////////////////////////////////////////////////////////////////// 133 // New pass manager implementation. 134 //////////////////////////////////////////////////////////////////////////////// 135 PreservedAnalyses InjectTLIMappings::run(Function &F, 136 FunctionAnalysisManager &AM) { 137 const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F); 138 runImpl(TLI, F); 139 // Even if the pass adds IR attributes, the analyses are preserved. 140 return PreservedAnalyses::all(); 141 } 142