MLInlineAdvisor.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MLInlineAdvisor.h - ML - based InlineAdvisor factories ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_MLINLINEADVISOR_H
  14. #define LLVM_ANALYSIS_MLINLINEADVISOR_H
  15. #include "llvm/Analysis/InlineAdvisor.h"
  16. #include "llvm/Analysis/LazyCallGraph.h"
  17. #include "llvm/Analysis/MLModelRunner.h"
  18. #include "llvm/IR/PassManager.h"
  19. #include <deque>
  20. #include <map>
  21. #include <memory>
  22. namespace llvm {
  23. class Module;
  24. class MLInlineAdvice;
  25. class MLInlineAdvisor : public InlineAdvisor {
  26. public:
  27. MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM,
  28. std::unique_ptr<MLModelRunner> ModelRunner);
  29. virtual ~MLInlineAdvisor() = default;
  30. void onPassEntry() override;
  31. void onPassExit(LazyCallGraph::SCC *SCC) override;
  32. int64_t getIRSize(const Function &F) const { return F.getInstructionCount(); }
  33. void onSuccessfulInlining(const MLInlineAdvice &Advice,
  34. bool CalleeWasDeleted);
  35. bool isForcedToStop() const { return ForceStop; }
  36. int64_t getLocalCalls(Function &F);
  37. const MLModelRunner &getModelRunner() const { return *ModelRunner.get(); }
  38. protected:
  39. std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
  40. std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
  41. bool Advice) override;
  42. virtual std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB);
  43. virtual std::unique_ptr<MLInlineAdvice>
  44. getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE);
  45. // Get the initial 'level' of the function, or 0 if the function has been
  46. // introduced afterwards.
  47. // TODO: should we keep this updated?
  48. unsigned getInitialFunctionLevel(const Function &F) const;
  49. std::unique_ptr<MLModelRunner> ModelRunner;
  50. private:
  51. int64_t getModuleIRSize() const;
  52. void print(raw_ostream &OS) const override {
  53. OS << "[MLInlineAdvisor] Nodes: " << NodeCount << " Edges: " << EdgeCount
  54. << "\n";
  55. }
  56. LazyCallGraph &CG;
  57. int64_t NodeCount = 0;
  58. int64_t EdgeCount = 0;
  59. int64_t EdgesOfLastSeenNodes = 0;
  60. std::map<const LazyCallGraph::Node *, unsigned> FunctionLevels;
  61. const int32_t InitialIRSize = 0;
  62. int32_t CurrentIRSize = 0;
  63. std::deque<const LazyCallGraph::Node *> NodesInLastSCC;
  64. DenseSet<const LazyCallGraph::Node *> AllNodes;
  65. bool ForceStop = false;
  66. };
  67. /// InlineAdvice that tracks changes post inlining. For that reason, it only
  68. /// overrides the "successful inlining" extension points.
  69. class MLInlineAdvice : public InlineAdvice {
  70. public:
  71. MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB,
  72. OptimizationRemarkEmitter &ORE, bool Recommendation)
  73. : InlineAdvice(Advisor, CB, ORE, Recommendation),
  74. CallerIRSize(Advisor->isForcedToStop() ? 0
  75. : Advisor->getIRSize(*Caller)),
  76. CalleeIRSize(Advisor->isForcedToStop() ? 0
  77. : Advisor->getIRSize(*Callee)),
  78. CallerAndCalleeEdges(Advisor->isForcedToStop()
  79. ? 0
  80. : (Advisor->getLocalCalls(*Caller) +
  81. Advisor->getLocalCalls(*Callee))) {}
  82. virtual ~MLInlineAdvice() = default;
  83. void recordInliningImpl() override;
  84. void recordInliningWithCalleeDeletedImpl() override;
  85. void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
  86. void recordUnattemptedInliningImpl() override;
  87. Function *getCaller() const { return Caller; }
  88. Function *getCallee() const { return Callee; }
  89. const int64_t CallerIRSize;
  90. const int64_t CalleeIRSize;
  91. const int64_t CallerAndCalleeEdges;
  92. private:
  93. void reportContextForRemark(DiagnosticInfoOptimizationBase &OR);
  94. MLInlineAdvisor *getAdvisor() const {
  95. return static_cast<MLInlineAdvisor *>(Advisor);
  96. };
  97. };
  98. } // namespace llvm
  99. #endif // LLVM_ANALYSIS_MLINLINEADVISOR_H
  100. #ifdef __GNUC__
  101. #pragma GCC diagnostic pop
  102. #endif