SpeculateAnalyses.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //===-- SpeculateAnalyses.cpp --*- C++ -*-===//
  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. #include "llvm/ExecutionEngine/Orc/SpeculateAnalyses.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/ADT/SmallPtrSet.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/Analysis/BlockFrequencyInfo.h"
  15. #include "llvm/Analysis/BranchProbabilityInfo.h"
  16. #include "llvm/Analysis/CFG.h"
  17. #include "llvm/IR/PassManager.h"
  18. #include "llvm/Passes/PassBuilder.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include <algorithm>
  21. namespace {
  22. using namespace llvm;
  23. SmallVector<const BasicBlock *, 8> findBBwithCalls(const Function &F,
  24. bool IndirectCall = false) {
  25. SmallVector<const BasicBlock *, 8> BBs;
  26. auto findCallInst = [&IndirectCall](const Instruction &I) {
  27. if (auto Call = dyn_cast<CallBase>(&I))
  28. return Call->isIndirectCall() ? IndirectCall : true;
  29. else
  30. return false;
  31. };
  32. for (auto &BB : F)
  33. if (findCallInst(*BB.getTerminator()) ||
  34. llvm::any_of(BB.instructionsWithoutDebug(), findCallInst))
  35. BBs.emplace_back(&BB);
  36. return BBs;
  37. }
  38. } // namespace
  39. // Implementations of Queries shouldn't need to lock the resources
  40. // such as LLVMContext, each argument (function) has a non-shared LLVMContext
  41. // Plus, if Queries contain states necessary locking scheme should be provided.
  42. namespace llvm {
  43. namespace orc {
  44. // Collect direct calls only
  45. void SpeculateQuery::findCalles(const BasicBlock *BB,
  46. DenseSet<StringRef> &CallesNames) {
  47. assert(BB != nullptr && "Traversing Null BB to find calls?");
  48. auto getCalledFunction = [&CallesNames](const CallBase *Call) {
  49. auto CalledValue = Call->getCalledOperand()->stripPointerCasts();
  50. if (auto DirectCall = dyn_cast<Function>(CalledValue))
  51. CallesNames.insert(DirectCall->getName());
  52. };
  53. for (auto &I : BB->instructionsWithoutDebug())
  54. if (auto CI = dyn_cast<CallInst>(&I))
  55. getCalledFunction(CI);
  56. if (auto II = dyn_cast<InvokeInst>(BB->getTerminator()))
  57. getCalledFunction(II);
  58. }
  59. bool SpeculateQuery::isStraightLine(const Function &F) {
  60. return llvm::all_of(F, [](const BasicBlock &BB) {
  61. return BB.getSingleSuccessor() != nullptr;
  62. });
  63. }
  64. // BlockFreqQuery Implementations
  65. size_t BlockFreqQuery::numBBToGet(size_t numBB) {
  66. // small CFG
  67. if (numBB < 4)
  68. return numBB;
  69. // mid-size CFG
  70. else if (numBB < 20)
  71. return (numBB / 2);
  72. else
  73. return (numBB / 2) + (numBB / 4);
  74. }
  75. BlockFreqQuery::ResultTy BlockFreqQuery::operator()(Function &F) {
  76. DenseMap<StringRef, DenseSet<StringRef>> CallerAndCalles;
  77. DenseSet<StringRef> Calles;
  78. SmallVector<std::pair<const BasicBlock *, uint64_t>, 8> BBFreqs;
  79. PassBuilder PB;
  80. FunctionAnalysisManager FAM;
  81. PB.registerFunctionAnalyses(FAM);
  82. auto IBBs = findBBwithCalls(F);
  83. if (IBBs.empty())
  84. return std::nullopt;
  85. auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
  86. for (const auto I : IBBs)
  87. BBFreqs.push_back({I, BFI.getBlockFreq(I).getFrequency()});
  88. assert(IBBs.size() == BBFreqs.size() && "BB Count Mismatch");
  89. llvm::sort(BBFreqs, [](decltype(BBFreqs)::const_reference BBF,
  90. decltype(BBFreqs)::const_reference BBS) {
  91. return BBF.second > BBS.second ? true : false;
  92. });
  93. // ignoring number of direct calls in a BB
  94. auto Topk = numBBToGet(BBFreqs.size());
  95. for (size_t i = 0; i < Topk; i++)
  96. findCalles(BBFreqs[i].first, Calles);
  97. assert(!Calles.empty() && "Running Analysis on Function with no calls?");
  98. CallerAndCalles.insert({F.getName(), std::move(Calles)});
  99. return CallerAndCalles;
  100. }
  101. // SequenceBBQuery Implementation
  102. std::size_t SequenceBBQuery::getHottestBlocks(std::size_t TotalBlocks) {
  103. if (TotalBlocks == 1)
  104. return TotalBlocks;
  105. return TotalBlocks / 2;
  106. }
  107. // FIXME : find good implementation.
  108. SequenceBBQuery::BlockListTy
  109. SequenceBBQuery::rearrangeBB(const Function &F, const BlockListTy &BBList) {
  110. BlockListTy RearrangedBBSet;
  111. for (auto &Block : F)
  112. if (llvm::is_contained(BBList, &Block))
  113. RearrangedBBSet.push_back(&Block);
  114. assert(RearrangedBBSet.size() == BBList.size() &&
  115. "BasicBlock missing while rearranging?");
  116. return RearrangedBBSet;
  117. }
  118. void SequenceBBQuery::traverseToEntryBlock(const BasicBlock *AtBB,
  119. const BlockListTy &CallerBlocks,
  120. const BackEdgesInfoTy &BackEdgesInfo,
  121. const BranchProbabilityInfo *BPI,
  122. VisitedBlocksInfoTy &VisitedBlocks) {
  123. auto Itr = VisitedBlocks.find(AtBB);
  124. if (Itr != VisitedBlocks.end()) { // already visited.
  125. if (!Itr->second.Upward)
  126. return;
  127. Itr->second.Upward = false;
  128. } else {
  129. // Create hint for newly discoverd blocks.
  130. WalkDirection BlockHint;
  131. BlockHint.Upward = false;
  132. // FIXME: Expensive Check
  133. if (llvm::is_contained(CallerBlocks, AtBB))
  134. BlockHint.CallerBlock = true;
  135. VisitedBlocks.insert(std::make_pair(AtBB, BlockHint));
  136. }
  137. const_pred_iterator PIt = pred_begin(AtBB), EIt = pred_end(AtBB);
  138. // Move this check to top, when we have code setup to launch speculative
  139. // compiles for function in entry BB, this triggers the speculative compiles
  140. // before running the program.
  141. if (PIt == EIt) // No Preds.
  142. return;
  143. DenseSet<const BasicBlock *> PredSkipNodes;
  144. // Since we are checking for predecessor's backedges, this Block
  145. // occurs in second position.
  146. for (auto &I : BackEdgesInfo)
  147. if (I.second == AtBB)
  148. PredSkipNodes.insert(I.first);
  149. // Skip predecessors which source of back-edges.
  150. for (; PIt != EIt; ++PIt)
  151. // checking EdgeHotness is cheaper
  152. if (BPI->isEdgeHot(*PIt, AtBB) && !PredSkipNodes.count(*PIt))
  153. traverseToEntryBlock(*PIt, CallerBlocks, BackEdgesInfo, BPI,
  154. VisitedBlocks);
  155. }
  156. void SequenceBBQuery::traverseToExitBlock(const BasicBlock *AtBB,
  157. const BlockListTy &CallerBlocks,
  158. const BackEdgesInfoTy &BackEdgesInfo,
  159. const BranchProbabilityInfo *BPI,
  160. VisitedBlocksInfoTy &VisitedBlocks) {
  161. auto Itr = VisitedBlocks.find(AtBB);
  162. if (Itr != VisitedBlocks.end()) { // already visited.
  163. if (!Itr->second.Downward)
  164. return;
  165. Itr->second.Downward = false;
  166. } else {
  167. // Create hint for newly discoverd blocks.
  168. WalkDirection BlockHint;
  169. BlockHint.Downward = false;
  170. // FIXME: Expensive Check
  171. if (llvm::is_contained(CallerBlocks, AtBB))
  172. BlockHint.CallerBlock = true;
  173. VisitedBlocks.insert(std::make_pair(AtBB, BlockHint));
  174. }
  175. const_succ_iterator PIt = succ_begin(AtBB), EIt = succ_end(AtBB);
  176. if (PIt == EIt) // No succs.
  177. return;
  178. // If there are hot edges, then compute SuccSkipNodes.
  179. DenseSet<const BasicBlock *> SuccSkipNodes;
  180. // Since we are checking for successor's backedges, this Block
  181. // occurs in first position.
  182. for (auto &I : BackEdgesInfo)
  183. if (I.first == AtBB)
  184. SuccSkipNodes.insert(I.second);
  185. for (; PIt != EIt; ++PIt)
  186. if (BPI->isEdgeHot(AtBB, *PIt) && !SuccSkipNodes.count(*PIt))
  187. traverseToExitBlock(*PIt, CallerBlocks, BackEdgesInfo, BPI,
  188. VisitedBlocks);
  189. }
  190. // Get Block frequencies for blocks and take most frquently executed block,
  191. // walk towards the entry block from those blocks and discover the basic blocks
  192. // with call.
  193. SequenceBBQuery::BlockListTy
  194. SequenceBBQuery::queryCFG(Function &F, const BlockListTy &CallerBlocks) {
  195. BlockFreqInfoTy BBFreqs;
  196. VisitedBlocksInfoTy VisitedBlocks;
  197. BackEdgesInfoTy BackEdgesInfo;
  198. PassBuilder PB;
  199. FunctionAnalysisManager FAM;
  200. PB.registerFunctionAnalyses(FAM);
  201. auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
  202. llvm::FindFunctionBackedges(F, BackEdgesInfo);
  203. for (const auto I : CallerBlocks)
  204. BBFreqs.push_back({I, BFI.getBlockFreq(I).getFrequency()});
  205. llvm::sort(BBFreqs, [](decltype(BBFreqs)::const_reference Bbf,
  206. decltype(BBFreqs)::const_reference Bbs) {
  207. return Bbf.second > Bbs.second;
  208. });
  209. ArrayRef<std::pair<const BasicBlock *, uint64_t>> HotBlocksRef(BBFreqs);
  210. HotBlocksRef =
  211. HotBlocksRef.drop_back(BBFreqs.size() - getHottestBlocks(BBFreqs.size()));
  212. BranchProbabilityInfo *BPI =
  213. FAM.getCachedResult<BranchProbabilityAnalysis>(F);
  214. // visit NHotBlocks,
  215. // traverse upwards to entry
  216. // traverse downwards to end.
  217. for (auto I : HotBlocksRef) {
  218. traverseToEntryBlock(I.first, CallerBlocks, BackEdgesInfo, BPI,
  219. VisitedBlocks);
  220. traverseToExitBlock(I.first, CallerBlocks, BackEdgesInfo, BPI,
  221. VisitedBlocks);
  222. }
  223. BlockListTy MinCallerBlocks;
  224. for (auto &I : VisitedBlocks)
  225. if (I.second.CallerBlock)
  226. MinCallerBlocks.push_back(std::move(I.first));
  227. return rearrangeBB(F, MinCallerBlocks);
  228. }
  229. SpeculateQuery::ResultTy SequenceBBQuery::operator()(Function &F) {
  230. // reduce the number of lists!
  231. DenseMap<StringRef, DenseSet<StringRef>> CallerAndCalles;
  232. DenseSet<StringRef> Calles;
  233. BlockListTy SequencedBlocks;
  234. BlockListTy CallerBlocks;
  235. CallerBlocks = findBBwithCalls(F);
  236. if (CallerBlocks.empty())
  237. return std::nullopt;
  238. if (isStraightLine(F))
  239. SequencedBlocks = rearrangeBB(F, CallerBlocks);
  240. else
  241. SequencedBlocks = queryCFG(F, CallerBlocks);
  242. for (const auto *BB : SequencedBlocks)
  243. findCalles(BB, Calles);
  244. CallerAndCalles.insert({F.getName(), std::move(Calles)});
  245. return CallerAndCalles;
  246. }
  247. } // namespace orc
  248. } // namespace llvm