InjectTLIMappings.cpp 7.0 KB

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