MachineOptimizationRemarkEmitter.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ///===- MachineOptimizationRemarkEmitter.cpp - Opt 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. /// \file
  9. /// Optimization diagnostic interfaces for machine passes. It's packaged as an
  10. /// analysis pass so that by using this service passes become dependent on MBFI
  11. /// as well. MBFI is used to compute the "hotness" of the diagnostic message.
  12. ///
  13. ///===---------------------------------------------------------------------===//
  14. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  15. #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
  16. #include "llvm/CodeGen/MachineInstr.h"
  17. #include "llvm/IR/DiagnosticInfo.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/InitializePasses.h"
  20. using namespace llvm;
  21. DiagnosticInfoMIROptimization::MachineArgument::MachineArgument(
  22. StringRef MKey, const MachineInstr &MI) {
  23. Key = std::string(MKey);
  24. raw_string_ostream OS(Val);
  25. MI.print(OS, /*IsStandalone=*/true, /*SkipOpers=*/false,
  26. /*SkipDebugLoc=*/true);
  27. }
  28. Optional<uint64_t>
  29. MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
  30. if (!MBFI)
  31. return None;
  32. return MBFI->getBlockProfileCount(&MBB);
  33. }
  34. void MachineOptimizationRemarkEmitter::computeHotness(
  35. DiagnosticInfoMIROptimization &Remark) {
  36. const MachineBasicBlock *MBB = Remark.getBlock();
  37. if (MBB)
  38. Remark.setHotness(computeHotness(*MBB));
  39. }
  40. void MachineOptimizationRemarkEmitter::emit(
  41. DiagnosticInfoOptimizationBase &OptDiagCommon) {
  42. auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
  43. computeHotness(OptDiag);
  44. LLVMContext &Ctx = MF.getFunction().getContext();
  45. // Only emit it if its hotness meets the threshold.
  46. if (OptDiag.getHotness().getValueOr(0) <
  47. Ctx.getDiagnosticsHotnessThreshold()) {
  48. return;
  49. }
  50. Ctx.diagnose(OptDiag);
  51. }
  52. MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass()
  53. : MachineFunctionPass(ID) {
  54. initializeMachineOptimizationRemarkEmitterPassPass(
  55. *PassRegistry::getPassRegistry());
  56. }
  57. bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
  58. MachineFunction &MF) {
  59. MachineBlockFrequencyInfo *MBFI;
  60. if (MF.getFunction().getContext().getDiagnosticsHotnessRequested())
  61. MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
  62. else
  63. MBFI = nullptr;
  64. ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
  65. return false;
  66. }
  67. void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
  68. AnalysisUsage &AU) const {
  69. AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
  70. AU.setPreservesAll();
  71. MachineFunctionPass::getAnalysisUsage(AU);
  72. }
  73. char MachineOptimizationRemarkEmitterPass::ID = 0;
  74. static const char ore_name[] = "Machine Optimization Remark Emitter";
  75. #define ORE_NAME "machine-opt-remark-emitter"
  76. INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
  77. true, true)
  78. INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass)
  79. INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
  80. true, true)