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