InjectTLIMappings.cpp 7.0 KB

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