MachineOptimizationRemarkEmitter.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <optional>
  21. using namespace llvm;
  22. DiagnosticInfoMIROptimization::MachineArgument::MachineArgument(
  23. StringRef MKey, const MachineInstr &MI) {
  24. Key = std::string(MKey);
  25. raw_string_ostream OS(Val);
  26. MI.print(OS, /*IsStandalone=*/true, /*SkipOpers=*/false,
  27. /*SkipDebugLoc=*/true);
  28. }
  29. std::optional<uint64_t>
  30. MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
  31. if (!MBFI)
  32. return std::nullopt;
  33. return MBFI->getBlockProfileCount(&MBB);
  34. }
  35. void MachineOptimizationRemarkEmitter::computeHotness(
  36. DiagnosticInfoMIROptimization &Remark) {
  37. const MachineBasicBlock *MBB = Remark.getBlock();
  38. if (MBB)
  39. Remark.setHotness(computeHotness(*MBB));
  40. }
  41. void MachineOptimizationRemarkEmitter::emit(
  42. DiagnosticInfoOptimizationBase &OptDiagCommon) {
  43. auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
  44. computeHotness(OptDiag);
  45. LLVMContext &Ctx = MF.getFunction().getContext();
  46. // Only emit it if its hotness meets the threshold.
  47. if (OptDiag.getHotness().value_or(0) < Ctx.getDiagnosticsHotnessThreshold())
  48. return;
  49. Ctx.diagnose(OptDiag);
  50. }
  51. MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass()
  52. : MachineFunctionPass(ID) {
  53. initializeMachineOptimizationRemarkEmitterPassPass(
  54. *PassRegistry::getPassRegistry());
  55. }
  56. bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
  57. MachineFunction &MF) {
  58. MachineBlockFrequencyInfo *MBFI;
  59. if (MF.getFunction().getContext().getDiagnosticsHotnessRequested())
  60. MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
  61. else
  62. MBFI = nullptr;
  63. ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
  64. return false;
  65. }
  66. void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
  67. AnalysisUsage &AU) const {
  68. AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
  69. AU.setPreservesAll();
  70. MachineFunctionPass::getAnalysisUsage(AU);
  71. }
  72. char MachineOptimizationRemarkEmitterPass::ID = 0;
  73. static const char ore_name[] = "Machine Optimization Remark Emitter";
  74. #define ORE_NAME "machine-opt-remark-emitter"
  75. INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
  76. true, true)
  77. INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass)
  78. INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
  79. true, true)