ReplayInlineAdvisor.cpp 5.6 KB

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