Skip to content

Commit c1cb5d9

Browse files
committed
Integrate with Optimization Remarks
1 parent 8a0904b commit c1cb5d9

File tree

3 files changed

+26
-65
lines changed

3 files changed

+26
-65
lines changed

llvm/include/llvm/IR/DiagnosticInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/StringRef.h"
2121
#include "llvm/ADT/Twine.h"
2222
#include "llvm/IR/DebugLoc.h"
23+
#include "llvm/Support/BranchProbability.h"
2324
#include "llvm/Support/CBindingWrapping.h"
2425
#include "llvm/Support/Compiler.h"
2526
#include "llvm/Support/ErrorHandling.h"
@@ -555,6 +556,7 @@ class LLVM_ABI DiagnosticInfoOptimizationBase
555556
Argument(StringRef Key, bool B) : Key(Key), Val(B ? "true" : "false") {}
556557
LLVM_ABI Argument(StringRef Key, DebugLoc dl);
557558
LLVM_ABI Argument(StringRef Key, InstructionCost C);
559+
LLVM_ABI Argument(StringRef Key, BranchProbability P);
558560
};
559561

560562
/// \p PassName is the name of the pass emitting this diagnostic. \p

llvm/lib/IR/DiagnosticInfo.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
273273
C.print(OS);
274274
}
275275

276+
DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
277+
BranchProbability P)
278+
: Key(std::string(Key)) {
279+
raw_string_ostream OS(Val);
280+
P.print(OS);
281+
}
282+
276283
DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, DebugLoc Loc)
277284
: Key(std::string(Key)), Loc(Loc) {
278285
if (Loc) {

llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp

Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,6 @@ static cl::opt<float> CoroElideBranchRatio(
4141
cl::desc("Minimum BranchProbability to consider a elide a coroutine."));
4242
extern cl::opt<unsigned> MinBlockCounterExecution;
4343

44-
static cl::opt<bool>
45-
PrintElidedCoroutine("print-elided-coroutine-stats", cl::init(false),
46-
cl::Hidden,
47-
cl::desc("Print stats for elided coroutine"));
48-
49-
static cl::opt<std::string>
50-
ElideStatOutput("coro-elide-stat-output", cl::init(""), cl::Hidden,
51-
cl::desc("Output file for -print-elided-coroutine-stats. "
52-
"Defaults to standard error output."));
53-
54-
// The return value is used to indicate the owner of the resources. The users
55-
// should use the output parameter.
56-
static std::unique_ptr<llvm::raw_ostream>
57-
getCoroElidedStatsOStream(llvm::raw_ostream *&OS) {
58-
if (!PrintElidedCoroutine) {
59-
OS = &llvm::nulls();
60-
return nullptr;
61-
}
62-
63-
if (ElideStatOutput.empty()) {
64-
OS = &llvm::errs();
65-
return nullptr;
66-
}
67-
68-
std::error_code EC;
69-
auto ret = std::make_unique<llvm::raw_fd_ostream>(ElideStatOutput, EC,
70-
sys::fs::OF_Append);
71-
72-
if (EC) {
73-
llvm::errs() << "llvm cannot open file: " << EC.message() << "\n";
74-
OS = &llvm::nulls();
75-
return nullptr;
76-
}
77-
78-
OS = ret.get();
79-
return ret;
80-
}
81-
8244
static Instruction *getFirstNonAllocaInTheEntryBlock(Function *F) {
8345
for (Instruction &I : F->getEntryBlock())
8446
if (!isa<AllocaInst>(&I))
@@ -191,37 +153,27 @@ PreservedAnalyses CoroAnnotationElidePass::run(LazyCallGraph::SCC &C,
191153
bool IsCallerPresplitCoroutine = Caller->isPresplitCoroutine();
192154
bool HasAttr = CB->hasFnAttr(llvm::Attribute::CoroElideSafe);
193155
if (IsCallerPresplitCoroutine && HasAttr) {
194-
195-
llvm::raw_ostream *OS = nullptr;
196-
auto _ = getCoroElidedStatsOStream(OS);
197-
assert(OS && "At least we should able to get access to standard error");
156+
static BranchProbability MinBranchProbability(
157+
static_cast<int>(CoroElideBranchRatio * MinBlockCounterExecution),
158+
MinBlockCounterExecution);
198159

199160
auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(*Caller);
200-
if (BFI.getBlockFreq(CB->getParent()) <
201-
BFI.getEntryFreq()) {
202-
static BranchProbability MinBranchProbability(
203-
static_cast<int>(CoroElideBranchRatio * MinBlockCounterExecution),
204-
MinBlockCounterExecution);
205161

206-
auto Prob = BranchProbability::getBranchProbability(
207-
BFI.getBlockFreq(CB->getParent()).getFrequency(),
208-
BFI.getEntryFreq().getFrequency());
209-
210-
if (Prob < MinBranchProbability) {
211-
*OS << "Not eliding " << *CB
212-
<< " with estimated probability: " << Prob << "\n";
213-
continue;
214-
}
215-
216-
*OS << "BB Prob: \t" << Prob << "\n";
217-
} else {
218-
*OS << "BB Freq: \t"
219-
<< BFI.getBlockFreq(CB->getParent()).getFrequency() << "\n";
220-
*OS << "Entry Freq: \t" << BFI.getEntryFreq().getFrequency() << "\n";
162+
auto Prob = BranchProbability::getBranchProbability(
163+
BFI.getBlockFreq(CB->getParent()).getFrequency(),
164+
BFI.getEntryFreq().getFrequency());
165+
166+
if (Prob < MinBranchProbability) {
167+
ORE.emit([&]() {
168+
return OptimizationRemarkMissed(DEBUG_TYPE, "CoroAnnotationElideUnlikely", Caller)
169+
<< "'" << ore::NV("callee", Callee->getName())
170+
<< "' not elided in '" << ore::NV("caller", Caller->getName())
171+
<< "' because of low probability: " << ore::NV("probability", Prob)
172+
<< " (threshold: " << ore::NV("threshold", MinBranchProbability) << ")";
173+
});
174+
continue;
221175
}
222176

223-
*OS << "eliding " << *CB << "\n";
224-
225177
auto *CallerN = CG.lookup(*Caller);
226178
auto *CallerC = CallerN ? CG.lookupSCC(*CallerN) : nullptr;
227179
// If CallerC is nullptr, it means LazyCallGraph hasn't visited Caller
@@ -233,7 +185,7 @@ PreservedAnalyses CoroAnnotationElidePass::run(LazyCallGraph::SCC &C,
233185
return OptimizationRemark(DEBUG_TYPE, "CoroAnnotationElide", Caller)
234186
<< "'" << ore::NV("callee", Callee->getName())
235187
<< "' elided in '" << ore::NV("caller", Caller->getName())
236-
<< "'";
188+
<< "' (probability: " << ore::NV("probability", Prob) << ")";
237189
});
238190

239191
FAM.invalidate(*Caller, PreservedAnalyses::none());

0 commit comments

Comments
 (0)