ReplayInlineAdvisor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===- ReplayInlineAdvisor.cpp - Replay InlineAdvisor ---------------------===//
  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. // This file implements ReplayInlineAdvisor that replays inline decisions based
  10. // on previous inline remarks from optimization remark log. This is a best
  11. // effort approach useful for testing compiler/source changes while holding
  12. // inlining steady.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/ReplayInlineAdvisor.h"
  16. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  17. #include "llvm/Support/LineIterator.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include <memory>
  20. using namespace llvm;
  21. #define DEBUG_TYPE "replay-inline"
  22. ReplayInlineAdvisor::ReplayInlineAdvisor(
  23. Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context,
  24. std::unique_ptr<InlineAdvisor> OriginalAdvisor,
  25. const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks,
  26. InlineContext IC)
  27. : InlineAdvisor(M, FAM, IC), OriginalAdvisor(std::move(OriginalAdvisor)),
  28. ReplaySettings(ReplaySettings), EmitRemarks(EmitRemarks) {
  29. auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(ReplaySettings.ReplayFile);
  30. std::error_code EC = BufferOrErr.getError();
  31. if (EC) {
  32. Context.emitError("Could not open remarks file: " + EC.message());
  33. return;
  34. }
  35. // Example for inline remarks to parse:
  36. // main:3:1.1: '_Z3subii' inlined into 'main' at callsite sum:1 @
  37. // main:3:1.1;
  38. // We use the callsite string after `at callsite` to replay inlining.
  39. line_iterator LineIt(*BufferOrErr.get(), /*SkipBlanks=*/true);
  40. const std::string PositiveRemark = "' inlined into '";
  41. const std::string NegativeRemark = "' will not be inlined into '";
  42. for (; !LineIt.is_at_eof(); ++LineIt) {
  43. StringRef Line = *LineIt;
  44. auto Pair = Line.split(" at callsite ");
  45. bool IsPositiveRemark = true;
  46. if (Pair.first.contains(NegativeRemark))
  47. IsPositiveRemark = false;
  48. auto CalleeCaller =
  49. Pair.first.split(IsPositiveRemark ? PositiveRemark : NegativeRemark);
  50. StringRef Callee = CalleeCaller.first.rsplit(": '").second;
  51. StringRef Caller = CalleeCaller.second.rsplit("'").first;
  52. auto CallSite = Pair.second.split(";").first;
  53. if (Callee.empty() || Caller.empty() || CallSite.empty()) {
  54. Context.emitError("Invalid remark format: " + Line);
  55. return;
  56. }
  57. std::string Combined = (Callee + CallSite).str();
  58. InlineSitesFromRemarks[Combined] = IsPositiveRemark;
  59. if (ReplaySettings.ReplayScope == ReplayInlinerSettings::Scope::Function)
  60. CallersToReplay.insert(Caller);
  61. }
  62. HasReplayRemarks = true;
  63. }
  64. std::unique_ptr<InlineAdvisor>
  65. llvm::getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
  66. LLVMContext &Context,
  67. std::unique_ptr<InlineAdvisor> OriginalAdvisor,
  68. const ReplayInlinerSettings &ReplaySettings,
  69. bool EmitRemarks, InlineContext IC) {
  70. auto Advisor = std::make_unique<ReplayInlineAdvisor>(
  71. M, FAM, Context, std::move(OriginalAdvisor), ReplaySettings, EmitRemarks,
  72. IC);
  73. if (!Advisor->areReplayRemarksLoaded())
  74. Advisor.reset();
  75. return Advisor;
  76. }
  77. std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdviceImpl(CallBase &CB) {
  78. assert(HasReplayRemarks);
  79. Function &Caller = *CB.getCaller();
  80. auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
  81. // Decision not made by replay system
  82. if (!hasInlineAdvice(*CB.getFunction())) {
  83. // If there's a registered original advisor, return its decision
  84. if (OriginalAdvisor)
  85. return OriginalAdvisor->getAdvice(CB);
  86. // If no decision is made above, return non-decision
  87. return {};
  88. }
  89. std::string CallSiteLoc =
  90. formatCallSiteLocation(CB.getDebugLoc(), ReplaySettings.ReplayFormat);
  91. StringRef Callee = CB.getCalledFunction()->getName();
  92. std::string Combined = (Callee + CallSiteLoc).str();
  93. // Replay decision, if it has one
  94. auto Iter = InlineSitesFromRemarks.find(Combined);
  95. if (Iter != InlineSitesFromRemarks.end()) {
  96. if (InlineSitesFromRemarks[Combined]) {
  97. LLVM_DEBUG(dbgs() << "Replay Inliner: Inlined " << Callee << " @ "
  98. << CallSiteLoc << "\n");
  99. return std::make_unique<DefaultInlineAdvice>(
  100. this, CB, llvm::InlineCost::getAlways("previously inlined"), ORE,
  101. EmitRemarks);
  102. } else {
  103. LLVM_DEBUG(dbgs() << "Replay Inliner: Not Inlined " << Callee << " @ "
  104. << CallSiteLoc << "\n");
  105. // A negative inline is conveyed by "None" std::optional<InlineCost>
  106. return std::make_unique<DefaultInlineAdvice>(this, CB, std::nullopt, ORE,
  107. EmitRemarks);
  108. }
  109. }
  110. // Fallback decisions
  111. if (ReplaySettings.ReplayFallback ==
  112. ReplayInlinerSettings::Fallback::AlwaysInline)
  113. return std::make_unique<DefaultInlineAdvice>(
  114. this, CB, llvm::InlineCost::getAlways("AlwaysInline Fallback"), ORE,
  115. EmitRemarks);
  116. else if (ReplaySettings.ReplayFallback ==
  117. ReplayInlinerSettings::Fallback::NeverInline)
  118. // A negative inline is conveyed by "None" std::optional<InlineCost>
  119. return std::make_unique<DefaultInlineAdvice>(this, CB, std::nullopt, ORE,
  120. EmitRemarks);
  121. else {
  122. assert(ReplaySettings.ReplayFallback ==
  123. ReplayInlinerSettings::Fallback::Original);
  124. // If there's a registered original advisor, return its decision
  125. if (OriginalAdvisor)
  126. return OriginalAdvisor->getAdvice(CB);
  127. }
  128. // If no decision is made above, return non-decision
  129. return {};
  130. }