InjectTLIMappings.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 unsigned 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.arg_operands())
  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. // All VFs in the TLI are powers of 2.
  82. for (unsigned VF = 2, WidestVF = TLI.getWidestVF(ScalarName); VF <= WidestVF;
  83. VF *= 2) {
  84. const std::string TLIName =
  85. std::string(TLI.getVectorizedFunction(ScalarName, VF));
  86. if (!TLIName.empty()) {
  87. std::string MangledName = VFABI::mangleTLIVectorName(
  88. TLIName, ScalarName, CI.getNumArgOperands(), VF);
  89. if (!OriginalSetOfMappings.count(MangledName)) {
  90. Mappings.push_back(MangledName);
  91. ++NumCallInjected;
  92. }
  93. Function *VariantF = M->getFunction(TLIName);
  94. if (!VariantF)
  95. addVariantDeclaration(CI, VF, TLIName);
  96. }
  97. }
  98. VFABI::setVectorVariantNames(&CI, Mappings);
  99. }
  100. static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
  101. for (auto &I : instructions(F))
  102. if (auto CI = dyn_cast<CallInst>(&I))
  103. addMappingsFromTLI(TLI, *CI);
  104. // Even if the pass adds IR attributes, the analyses are preserved.
  105. return false;
  106. }
  107. ////////////////////////////////////////////////////////////////////////////////
  108. // New pass manager implementation.
  109. ////////////////////////////////////////////////////////////////////////////////
  110. PreservedAnalyses InjectTLIMappings::run(Function &F,
  111. FunctionAnalysisManager &AM) {
  112. const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F);
  113. runImpl(TLI, F);
  114. // Even if the pass adds IR attributes, the analyses are preserved.
  115. return PreservedAnalyses::all();
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. // Legacy PM Implementation.
  119. ////////////////////////////////////////////////////////////////////////////////
  120. bool InjectTLIMappingsLegacy::runOnFunction(Function &F) {
  121. const TargetLibraryInfo &TLI =
  122. getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  123. return runImpl(TLI, F);
  124. }
  125. void InjectTLIMappingsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
  126. AU.setPreservesCFG();
  127. AU.addRequired<TargetLibraryInfoWrapperPass>();
  128. AU.addPreserved<TargetLibraryInfoWrapperPass>();
  129. AU.addPreserved<ScalarEvolutionWrapperPass>();
  130. AU.addPreserved<AAResultsWrapperPass>();
  131. AU.addPreserved<LoopAccessLegacyAnalysis>();
  132. AU.addPreserved<DemandedBitsWrapperPass>();
  133. AU.addPreserved<OptimizationRemarkEmitterWrapperPass>();
  134. AU.addPreserved<GlobalsAAWrapperPass>();
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////
  137. // Legacy Pass manager initialization
  138. ////////////////////////////////////////////////////////////////////////////////
  139. char InjectTLIMappingsLegacy::ID = 0;
  140. INITIALIZE_PASS_BEGIN(InjectTLIMappingsLegacy, DEBUG_TYPE,
  141. "Inject TLI Mappings", false, false)
  142. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  143. INITIALIZE_PASS_END(InjectTLIMappingsLegacy, DEBUG_TYPE, "Inject TLI Mappings",
  144. false, false)
  145. FunctionPass *llvm::createInjectTLIMappingsLegacyPass() {
  146. return new InjectTLIMappingsLegacy();
  147. }