Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 1 | //===- OptimizationRemarkEmitter.cpp - Optimization Diagnostic --*- C++ -*-===// |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 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 | |
Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LazyBlockFrequencyInfo.h" |
| 17 | #include "llvm/Analysis/LoopInfo.h" |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 18 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DiagnosticInfo.h" |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Dominators.h" |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 21 | #include "llvm/IR/LLVMContext.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 22 | #include "llvm/InitializePasses.h" |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Justin Bogner | 8281c81 | 2017-02-22 07:38:17 +0000 | [diff] [blame] | 26 | OptimizationRemarkEmitter::OptimizationRemarkEmitter(const Function *F) |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 27 | : F(F), BFI(nullptr) { |
Brian Gesiak | 44e5f6c | 2017-06-30 18:13:59 +0000 | [diff] [blame] | 28 | if (!F->getContext().getDiagnosticsHotnessRequested()) |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 29 | return; |
| 30 | |
| 31 | // First create a dominator tree. |
| 32 | DominatorTree DT; |
Justin Bogner | 8281c81 | 2017-02-22 07:38:17 +0000 | [diff] [blame] | 33 | DT.recalculate(*const_cast<Function *>(F)); |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 34 | |
| 35 | // Generate LoopInfo from it. |
| 36 | LoopInfo LI; |
| 37 | LI.analyze(DT); |
| 38 | |
| 39 | // Then compute BranchProbabilityInfo. |
Evgeniy Brevnov | 9fb074e | 2020-06-18 16:20:55 +0700 | [diff] [blame] | 40 | BranchProbabilityInfo BPI(*F, LI, nullptr, &DT, nullptr); |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 41 | |
| 42 | // Finally compute BFI. |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 43 | OwnedBFI = std::make_unique<BlockFrequencyInfo>(*F, BPI, LI); |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 44 | BFI = OwnedBFI.get(); |
| 45 | } |
| 46 | |
Chandler Carruth | 1ae34c3 | 2017-01-15 08:20:50 +0000 | [diff] [blame] | 47 | bool OptimizationRemarkEmitter::invalidate( |
| 48 | Function &F, const PreservedAnalyses &PA, |
| 49 | FunctionAnalysisManager::Invalidator &Inv) { |
Alina Sbirlea | 4f33a68 | 2020-01-16 15:32:30 -0800 | [diff] [blame] | 50 | if (OwnedBFI.get()) { |
| 51 | OwnedBFI.reset(); |
| 52 | BFI = nullptr; |
| 53 | } |
Chandler Carruth | 1ae34c3 | 2017-01-15 08:20:50 +0000 | [diff] [blame] | 54 | // This analysis has no state and so can be trivially preserved but it needs |
| 55 | // a fresh view of BFI if it was constructed with one. |
| 56 | if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA)) |
| 57 | return true; |
| 58 | |
| 59 | // Otherwise this analysis result remains valid. |
| 60 | return false; |
| 61 | } |
| 62 | |
Adam Nemet | 6100d16 | 2016-07-20 21:44:22 +0000 | [diff] [blame] | 63 | Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) { |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 64 | if (!BFI) |
| 65 | return None; |
| 66 | |
| 67 | return BFI->getBlockProfileCount(cast<BasicBlock>(V)); |
| 68 | } |
| 69 | |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 70 | void OptimizationRemarkEmitter::computeHotness( |
Adam Nemet | 484f93d | 2017-01-25 23:20:25 +0000 | [diff] [blame] | 71 | DiagnosticInfoIROptimization &OptDiag) { |
Justin Bogner | 8281c81 | 2017-02-22 07:38:17 +0000 | [diff] [blame] | 72 | const Value *V = OptDiag.getCodeRegion(); |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 73 | if (V) |
| 74 | OptDiag.setHotness(computeHotness(V)); |
| 75 | } |
| 76 | |
Adam Nemet | 484f93d | 2017-01-25 23:20:25 +0000 | [diff] [blame] | 77 | void OptimizationRemarkEmitter::emit( |
| 78 | DiagnosticInfoOptimizationBase &OptDiagBase) { |
| 79 | auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase); |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 80 | computeHotness(OptDiag); |
Adam Nemet | 9303f62 | 2017-12-01 20:41:38 +0000 | [diff] [blame] | 81 | |
| 82 | // Only emit it if its hotness meets the threshold. |
| 83 | if (OptDiag.getHotness().getValueOr(0) < |
| 84 | F->getContext().getDiagnosticsHotnessThreshold()) { |
Brian Gesiak | 4ef3daa | 2017-06-30 23:14:53 +0000 | [diff] [blame] | 85 | return; |
| 86 | } |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 87 | |
Adam Nemet | f31b1f3 | 2017-10-04 04:26:23 +0000 | [diff] [blame] | 88 | F->getContext().diagnose(OptDiag); |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 91 | OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass() |
| 92 | : FunctionPass(ID) { |
| 93 | initializeOptimizationRemarkEmitterWrapperPassPass( |
| 94 | *PassRegistry::getPassRegistry()); |
| 95 | } |
| 96 | |
| 97 | bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function &Fn) { |
| 98 | BlockFrequencyInfo *BFI; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 99 | |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 100 | auto &Context = Fn.getContext(); |
| 101 | if (Context.getDiagnosticsHotnessRequested()) { |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 102 | BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI(); |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 103 | // Get hotness threshold from PSI. This should only happen once. |
| 104 | if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) { |
| 105 | if (ProfileSummaryInfo *PSI = |
| 106 | &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI()) |
| 107 | Context.setDiagnosticsHotnessThreshold( |
| 108 | PSI->getOrCompHotCountThreshold()); |
| 109 | } |
| 110 | } else |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 111 | BFI = nullptr; |
| 112 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 113 | ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI); |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 117 | void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage( |
| 118 | AnalysisUsage &AU) const { |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 119 | LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 120 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 121 | AU.setPreservesAll(); |
| 122 | } |
| 123 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 124 | AnalysisKey OptimizationRemarkEmitterAnalysis::Key; |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 125 | |
| 126 | OptimizationRemarkEmitter |
Adam Nemet | 78d1091 | 2016-07-20 21:44:18 +0000 | [diff] [blame] | 127 | OptimizationRemarkEmitterAnalysis::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 128 | FunctionAnalysisManager &AM) { |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 129 | BlockFrequencyInfo *BFI; |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 130 | auto &Context = F.getContext(); |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 131 | |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 132 | if (Context.getDiagnosticsHotnessRequested()) { |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 133 | BFI = &AM.getResult<BlockFrequencyAnalysis>(F); |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 134 | // Get hotness threshold from PSI. This should only happen once. |
| 135 | if (Context.isDiagnosticsHotnessThresholdSetFromPSI()) { |
| 136 | auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F); |
| 137 | if (ProfileSummaryInfo *PSI = |
| 138 | MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent())) |
| 139 | Context.setDiagnosticsHotnessThreshold( |
| 140 | PSI->getOrCompHotCountThreshold()); |
| 141 | } |
| 142 | } else |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 143 | BFI = nullptr; |
| 144 | |
| 145 | return OptimizationRemarkEmitter(&F, BFI); |
| 146 | } |
| 147 | |
| 148 | char OptimizationRemarkEmitterWrapperPass::ID = 0; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 149 | static const char ore_name[] = "Optimization Remark Emitter"; |
| 150 | #define ORE_NAME "opt-remark-emitter" |
| 151 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 152 | INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, |
| 153 | false, true) |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 154 | INITIALIZE_PASS_DEPENDENCY(LazyBFIPass) |
Wei Wang | 93dc1b5 | 2020-11-17 10:43:02 -0800 | [diff] [blame] | 155 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 156 | INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, |
| 157 | false, true) |