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/OptimizationRemarkEmitter.h" 18 #include "llvm/Analysis/VectorUtils.h" 19 #include "llvm/IR/InstIterator.h" 20 #include "llvm/IR/IntrinsicInst.h" 21 #include "llvm/Transforms/Utils.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 /// Helper function to map the TLI name to a strings that holds 37 /// scalar-to-vector mapping. 38 /// 39 /// _ZGV<isa><mask><vlen><vparams>_<scalarname>(<vectorname>) 40 /// 41 /// where: 42 /// 43 /// <isa> = "_LLVM_" 44 /// <mask> = "N". Note: TLI does not support masked interfaces. 45 /// <vlen> = Number of concurrent lanes, stored in the `VectorizationFactor` 46 /// field of the `VecDesc` struct. 47 /// <vparams> = "v", as many as are the number of parameters of CI. 48 /// <scalarname> = the name of the scalar function called by CI. 49 /// <vectorname> = the name of the vector function mapped by the TLI. 50 static std::string mangleTLIName(StringRef VectorName, const CallInst &CI, 51 unsigned VF) { 52 SmallString<256> Buffer; 53 llvm::raw_svector_ostream Out(Buffer); 54 Out << "_ZGV" << VFABI::_LLVM_ << "N" << VF; 55 for (unsigned I = 0; I < CI.getNumArgOperands(); ++I) 56 Out << "v"; 57 Out << "_" << CI.getCalledFunction()->getName() << "(" << VectorName << ")"; 58 return std::string(Out.str()); 59 } 60 61 /// A helper function that adds the vector function declaration that 62 /// vectorizes the CallInst CI with a vectorization factor of VF 63 /// lanes. The TLI assumes that all parameters and the return type of 64 /// CI (other than void) need to be widened to a VectorType of VF 65 /// lanes. 66 static void addVariantDeclaration(CallInst &CI, const unsigned VF, 67 const StringRef VFName) { 68 Module *M = CI.getModule(); 69 70 // Add function declaration. 71 Type *RetTy = ToVectorTy(CI.getType(), VF); 72 SmallVector<Type *, 4> Tys; 73 for (Value *ArgOperand : CI.arg_operands()) 74 Tys.push_back(ToVectorTy(ArgOperand->getType(), VF)); 75 assert(!CI.getFunctionType()->isVarArg() && 76 "VarArg functions are not supported."); 77 FunctionType *FTy = FunctionType::get(RetTy, Tys, /*isVarArg=*/false); 78 Function *VectorF = 79 Function::Create(FTy, Function::ExternalLinkage, VFName, M); 80 VectorF->copyAttributesFrom(CI.getCalledFunction()); 81 ++NumVFDeclAdded; 82 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Added to the module: `" << VFName 83 << "` of type " << *(VectorF->getType()) << "\n"); 84 85 // Make function declaration (without a body) "sticky" in the IR by 86 // listing it in the @llvm.compiler.used intrinsic. 87 assert(!VectorF->size() && "VFABI attribute requires `@llvm.compiler.used` " 88 "only on declarations."); 89 appendToCompilerUsed(*M, {VectorF}); 90 LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Adding `" << VFName 91 << "` to `@llvm.compiler.used`.\n"); 92 ++NumCompUsedAdded; 93 } 94 95 static void addMappingsFromTLI(const TargetLibraryInfo &TLI, CallInst &CI) { 96 // This is needed to make sure we don't query the TLI for calls to 97 // bitcast of function pointers, like `%call = call i32 (i32*, ...) 98 // bitcast (i32 (...)* @goo to i32 (i32*, ...)*)(i32* nonnull %i)`, 99 // as such calls make the `isFunctionVectorizable` raise an 100 // exception. 101 if (CI.isNoBuiltin() || !CI.getCalledFunction()) 102 return; 103 104 const std::string ScalarName = std::string(CI.getCalledFunction()->getName()); 105 // Nothing to be done if the TLI thinks the function is not 106 // vectorizable. 107 if (!TLI.isFunctionVectorizable(ScalarName)) 108 return; 109 SmallVector<std::string, 8> Mappings; 110 VFABI::getVectorVariantNames(CI, Mappings); 111 Module *M = CI.getModule(); 112 const SetVector<StringRef> OriginalSetOfMappings(Mappings.begin(), 113 Mappings.end()); 114 // All VFs in the TLI are powers of 2. 115 for (unsigned VF = 2, WidestVF = TLI.getWidestVF(ScalarName); VF <= WidestVF; 116 VF *= 2) { 117 const std::string TLIName = 118 std::string(TLI.getVectorizedFunction(ScalarName, VF)); 119 if (!TLIName.empty()) { 120 std::string MangledName = mangleTLIName(TLIName, CI, VF); 121 if (!OriginalSetOfMappings.count(MangledName)) { 122 Mappings.push_back(MangledName); 123 ++NumCallInjected; 124 } 125 Function *VariantF = M->getFunction(TLIName); 126 if (!VariantF) 127 addVariantDeclaration(CI, VF, TLIName); 128 } 129 } 130 131 VFABI::setVectorVariantNames(&CI, Mappings); 132 } 133 134 static bool runImpl(const TargetLibraryInfo &TLI, Function &F) { 135 for (auto &I : instructions(F)) 136 if (auto CI = dyn_cast<CallInst>(&I)) 137 addMappingsFromTLI(TLI, *CI); 138 // Even if the pass adds IR attributes, the analyses are preserved. 139 return false; 140 } 141 142 //////////////////////////////////////////////////////////////////////////////// 143 // New pass manager implementation. 144 //////////////////////////////////////////////////////////////////////////////// 145 PreservedAnalyses InjectTLIMappings::run(Function &F, 146 FunctionAnalysisManager &AM) { 147 const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F); 148 runImpl(TLI, F); 149 // Even if the pass adds IR attributes, the analyses are preserved. 150 return PreservedAnalyses::all(); 151 } 152 153 //////////////////////////////////////////////////////////////////////////////// 154 // Legacy PM Implementation. 155 //////////////////////////////////////////////////////////////////////////////// 156 bool InjectTLIMappingsLegacy::runOnFunction(Function &F) { 157 const TargetLibraryInfo &TLI = 158 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 159 return runImpl(TLI, F); 160 } 161 162 void InjectTLIMappingsLegacy::getAnalysisUsage(AnalysisUsage &AU) const { 163 AU.setPreservesCFG(); 164 AU.addRequired<TargetLibraryInfoWrapperPass>(); 165 AU.addPreserved<TargetLibraryInfoWrapperPass>(); 166 AU.addPreserved<ScalarEvolutionWrapperPass>(); 167 AU.addPreserved<AAResultsWrapperPass>(); 168 AU.addPreserved<LoopAccessLegacyAnalysis>(); 169 AU.addPreserved<DemandedBitsWrapperPass>(); 170 AU.addPreserved<OptimizationRemarkEmitterWrapperPass>(); 171 } 172 173 //////////////////////////////////////////////////////////////////////////////// 174 // Legacy Pass manager initialization 175 //////////////////////////////////////////////////////////////////////////////// 176 char InjectTLIMappingsLegacy::ID = 0; 177 178 INITIALIZE_PASS_BEGIN(InjectTLIMappingsLegacy, DEBUG_TYPE, 179 "Inject TLI Mappings", false, false) 180 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 181 INITIALIZE_PASS_END(InjectTLIMappingsLegacy, DEBUG_TYPE, "Inject TLI Mappings", 182 false, false) 183 184 FunctionPass *llvm::createInjectTLIMappingsLegacyPass() { 185 return new InjectTLIMappingsLegacy(); 186 } 187