@@ -41,44 +41,6 @@ static cl::opt<float> CoroElideBranchRatio(
4141 cl::desc(" Minimum BranchProbability to consider a elide a coroutine." ));
4242extern 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-
8244static 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