OptimizationRemarkEmitter.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //===- OptimizationRemarkEmitter.cpp - Optimization Diagnostic --*- 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. //
  9. // Optimization diagnostic interfaces. It's packaged as an analysis pass so
  10. // that by using this service passes become dependent on BFI as well. BFI is
  11. // used to compute the "hotness" of the diagnostic message.
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  14. #include "llvm/Analysis/BranchProbabilityInfo.h"
  15. #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/Analysis/ProfileSummaryInfo.h"
  18. #include "llvm/IR/DiagnosticInfo.h"
  19. #include "llvm/IR/Dominators.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/InitializePasses.h"
  22. #include <optional>
  23. using namespace llvm;
  24. OptimizationRemarkEmitter::OptimizationRemarkEmitter(const Function *F)
  25. : F(F), BFI(nullptr) {
  26. if (!F->getContext().getDiagnosticsHotnessRequested())
  27. return;
  28. // First create a dominator tree.
  29. DominatorTree DT;
  30. DT.recalculate(*const_cast<Function *>(F));
  31. // Generate LoopInfo from it.
  32. LoopInfo LI;
  33. LI.analyze(DT);
  34. // Then compute BranchProbabilityInfo.
  35. BranchProbabilityInfo BPI(*F, LI, nullptr, &DT, nullptr);
  36. // Finally compute BFI.
  37. OwnedBFI = std::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
  38. BFI = OwnedBFI.get();
  39. }
  40. bool OptimizationRemarkEmitter::invalidate(
  41. Function &F, const PreservedAnalyses &PA,
  42. FunctionAnalysisManager::Invalidator &Inv) {
  43. if (OwnedBFI) {
  44. OwnedBFI.reset();
  45. BFI = nullptr;
  46. }
  47. // This analysis has no state and so can be trivially preserved but it needs
  48. // a fresh view of BFI if it was constructed with one.
  49. if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA))
  50. return true;
  51. // Otherwise this analysis result remains valid.
  52. return false;
  53. }
  54. std::optional<uint64_t>
  55. OptimizationRemarkEmitter::computeHotness(const Value *V) {
  56. if (!BFI)
  57. return std::nullopt;
  58. return BFI->getBlockProfileCount(cast<BasicBlock>(V));
  59. }
  60. void OptimizationRemarkEmitter::computeHotness(
  61. DiagnosticInfoIROptimization &OptDiag) {
  62. const Value *V = OptDiag.getCodeRegion();
  63. if (V)
  64. OptDiag.setHotness(computeHotness(V));
  65. }
  66. void OptimizationRemarkEmitter::emit(
  67. DiagnosticInfoOptimizationBase &OptDiagBase) {
  68. auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
  69. computeHotness(OptDiag);
  70. // Only emit it if its hotness meets the threshold.
  71. if (OptDiag.getHotness().value_or(0) <
  72. F->getContext().getDiagnosticsHotnessThreshold()) {
  73. return;
  74. }
  75. F->getContext().diagnose(OptDiag);
  76. }
  77. OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass()
  78. : FunctionPass(ID) {
  79. initializeOptimizationRemarkEmitterWrapperPassPass(
  80. *PassRegistry::getPassRegistry());
  81. }
  82. bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function &Fn) {
  83. BlockFrequencyInfo *BFI;
  84. auto &Context = Fn.getContext();
  85. if (Context.getDiagnosticsHotnessRequested()) {
  86. BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
  87. // Get hotness threshold from PSI. This should only happen once.
  88. if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) {
  89. if (ProfileSummaryInfo *PSI =
  90. &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI())
  91. Context.setDiagnosticsHotnessThreshold(
  92. PSI->getOrCompHotCountThreshold());
  93. }
  94. } else
  95. BFI = nullptr;
  96. ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
  97. return false;
  98. }
  99. void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage(
  100. AnalysisUsage &AU) const {
  101. LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
  102. AU.addRequired<ProfileSummaryInfoWrapperPass>();
  103. AU.setPreservesAll();
  104. }
  105. AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
  106. OptimizationRemarkEmitter
  107. OptimizationRemarkEmitterAnalysis::run(Function &F,
  108. FunctionAnalysisManager &AM) {
  109. BlockFrequencyInfo *BFI;
  110. auto &Context = F.getContext();
  111. if (Context.getDiagnosticsHotnessRequested()) {
  112. BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
  113. // Get hotness threshold from PSI. This should only happen once.
  114. if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) {
  115. auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
  116. if (ProfileSummaryInfo *PSI =
  117. MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent()))
  118. Context.setDiagnosticsHotnessThreshold(
  119. PSI->getOrCompHotCountThreshold());
  120. }
  121. } else
  122. BFI = nullptr;
  123. return OptimizationRemarkEmitter(&F, BFI);
  124. }
  125. char OptimizationRemarkEmitterWrapperPass::ID = 0;
  126. static const char ore_name[] = "Optimization Remark Emitter";
  127. #define ORE_NAME "opt-remark-emitter"
  128. INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name,
  129. false, true)
  130. INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
  131. INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
  132. INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name,
  133. false, true)