Skip to content

Commit c4f3675

Browse files
[Coroutines] Conditional elide coroutines based on hot/cold information (#162276)
Unconditionally eliding all `[[clang::coro_await_elidable]]` coroutines is not good. For example, ``` Task bar(); Task foo(bool b) { if (b) [[unlikely]] { co_await bar(); } } ``` Assume Task is marked with `[[clang::coro_await_elidable]]`, now we will always elide the call to `bar()` into the frame of `foo()`. But this may be a regression instead of an optimization if `b` is always false. This patch tries to mitigate the problem by leveraging hot/cold information. This can be optimized further in the future but at least this patch makes things better. This patch was originally written by ChuanqiXu9 (#145831), but stalled during PR review because the diagnostics were not integrated with the existing optimization remarks. I rebased the original patch, integrated it with the optimization remarks, and did a couple of smaller cosmetic changes (e.g., made the test case expectations more targeted, etc.) Co-Authored-by: Chuanqi Xu <[email protected]>
1 parent a03aef4 commit c4f3675

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
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: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "llvm/IR/Instruction.h"
2525
#include "llvm/IR/Module.h"
2626
#include "llvm/IR/PassManager.h"
27+
#include "llvm/Support/BranchProbability.h"
28+
#include "llvm/Support/CommandLine.h"
29+
#include "llvm/Support/FileSystem.h"
2730
#include "llvm/Transforms/Utils/CallGraphUpdater.h"
2831
#include "llvm/Transforms/Utils/Cloning.h"
2932

@@ -33,6 +36,11 @@ using namespace llvm;
3336

3437
#define DEBUG_TYPE "coro-annotation-elide"
3538

39+
static cl::opt<float> CoroElideBranchRatio(
40+
"coro-elide-branch-ratio", cl::init(0.55), cl::Hidden,
41+
cl::desc("Minimum BranchProbability to consider a elide a coroutine."));
42+
extern cl::opt<unsigned> MinBlockCounterExecution;
43+
3644
static Instruction *getFirstNonAllocaInTheEntryBlock(Function *F) {
3745
for (Instruction &I : F->getEntryBlock())
3846
if (!isa<AllocaInst>(&I))
@@ -145,6 +153,30 @@ PreservedAnalyses CoroAnnotationElidePass::run(LazyCallGraph::SCC &C,
145153
bool IsCallerPresplitCoroutine = Caller->isPresplitCoroutine();
146154
bool HasAttr = CB->hasFnAttr(llvm::Attribute::CoroElideSafe);
147155
if (IsCallerPresplitCoroutine && HasAttr) {
156+
BranchProbability MinBranchProbability(
157+
static_cast<int>(CoroElideBranchRatio * MinBlockCounterExecution),
158+
MinBlockCounterExecution);
159+
160+
auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(*Caller);
161+
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(
169+
DEBUG_TYPE, "CoroAnnotationElideUnlikely", Caller)
170+
<< "'" << ore::NV("callee", Callee->getName())
171+
<< "' not elided in '"
172+
<< ore::NV("caller", Caller->getName())
173+
<< "' because of low probability: "
174+
<< ore::NV("probability", Prob) << " (threshold: "
175+
<< ore::NV("threshold", MinBranchProbability) << ")";
176+
});
177+
continue;
178+
}
179+
148180
auto *CallerN = CG.lookup(*Caller);
149181
auto *CallerC = CallerN ? CG.lookupSCC(*CallerN) : nullptr;
150182
// If CallerC is nullptr, it means LazyCallGraph hasn't visited Caller
@@ -156,7 +188,7 @@ PreservedAnalyses CoroAnnotationElidePass::run(LazyCallGraph::SCC &C,
156188
return OptimizationRemark(DEBUG_TYPE, "CoroAnnotationElide", Caller)
157189
<< "'" << ore::NV("callee", Callee->getName())
158190
<< "' elided in '" << ore::NV("caller", Caller->getName())
159-
<< "'";
191+
<< "' (probability: " << ore::NV("probability", Prob) << ")";
160192
});
161193

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

llvm/lib/Transforms/IPO/PartialInlining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static cl::opt<float> MinRegionSizeRatio(
109109
"outline candidate and original function"));
110110
// Used to tune the minimum number of execution counts needed in the predecessor
111111
// block to the cold edge. ie. confidence interval.
112-
static cl::opt<unsigned>
112+
cl::opt<unsigned>
113113
MinBlockCounterExecution("min-block-execution", cl::init(100), cl::Hidden,
114114
cl::desc("Minimum block executions to consider "
115115
"its BranchProbabilityInfo valid"));

llvm/test/Transforms/Coroutines/coro-transform-must-elide.ll renamed to llvm/test/Transforms/Coroutines/coro-elide-safe.ll

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
; Testing elide performed its job for calls to coroutines marked safe.
1+
; Coroutine calls marked with `coro_elide_safe` should be elided.
2+
; Inside `caller`, we expect the `callee` coroutine to be elided.
3+
; Inside `caller_conditional`, `callee` is only called on an unlikely
4+
; path, hence we expect the `callee` coroutine NOT to be elided.
5+
;
26
; RUN: opt < %s -S -passes='cgscc(coro-annotation-elide)' | FileCheck %s
37

48
%struct.Task = type { ptr }
@@ -57,7 +61,7 @@ define ptr @callee.noalloc(i8 %arg, ptr dereferenceable(32) align(8) %frame) {
5761
; Function Attrs: presplitcoroutine
5862
define ptr @caller() #0 {
5963
entry:
60-
%task = call ptr @callee(i8 0) #1
64+
%task = call ptr @callee(i8 0) coro_elide_safe
6165
ret ptr %task
6266
; CHECK: %[[TASK:.+]] = alloca %struct.Task, align 8
6367
; CHECK-NEXT: %[[FRAME:.+]] = alloca [32 x i8], align 8
@@ -69,11 +73,29 @@ entry:
6973
; CHECK-NEXT: ret ptr %[[TASK]]
7074
}
7175

76+
; CHECK-LABEL: define ptr @caller_conditional(i1 %cond)
77+
; Function Attrs: presplitcoroutine
78+
define ptr @caller_conditional(i1 %cond) #0 {
79+
entry:
80+
br i1 %cond, label %call, label %ret
81+
82+
call:
83+
; CHECK-NOT: alloca
84+
; CHECK-NOT: @llvm.coro.id({{.*}}, ptr @callee, {{.*}})
85+
; CHECK: %task = call ptr @callee(i8 0)
86+
; CHECK-NEXT: br label %ret
87+
%task = call ptr @callee(i8 0) coro_elide_safe
88+
br label %ret
89+
90+
ret:
91+
%retval = phi ptr [ %task, %call ], [ null, %entry ]
92+
ret ptr %retval
93+
}
94+
7295
declare token @llvm.coro.id(i32, ptr, ptr, ptr)
7396
declare ptr @llvm.coro.begin(token, ptr)
7497
declare ptr @llvm.coro.frame()
7598
declare ptr @llvm.coro.subfn.addr(ptr, i8)
7699
declare i1 @llvm.coro.alloc(token)
77100

78101
attributes #0 = { presplitcoroutine }
79-
attributes #1 = { coro_elide_safe }

0 commit comments

Comments
 (0)