Skip to content

Commit b5cdb03

Browse files
[Coroutines] Better optimization remarks for CoroAnnotationElide pass (#109352)
1 parent f41f6ea commit b5cdb03

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,54 @@ PreservedAnalyses CoroAnnotationElidePass::run(Function &F,
101101

102102
Function *NewCallee =
103103
F.getParent()->getFunction((F.getName() + ".noalloc").str());
104-
if (!NewCallee) {
105-
return PreservedAnalyses::all();
106-
}
107-
108-
auto FramePtrArgPosition = NewCallee->arg_size() - 1;
109-
auto FrameSize =
110-
NewCallee->getParamDereferenceableBytes(FramePtrArgPosition);
111-
auto FrameAlign =
112-
NewCallee->getParamAlign(FramePtrArgPosition).valueOrOne();
113-
114-
SmallVector<CallBase *, 4> Users;
115-
for (auto *U : F.users()) {
116-
if (auto *CB = dyn_cast<CallBase>(U)) {
117-
if (CB->getCalledFunction() == &F)
118-
Users.push_back(CB);
119-
}
120-
}
121104

122-
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
105+
if (!NewCallee)
106+
return PreservedAnalyses::all();
123107

124-
for (auto *CB : Users) {
125-
auto *Caller = CB->getFunction();
126-
if (Caller && Caller->isPresplitCoroutine() &&
127-
CB->hasFnAttr(llvm::Attribute::CoroElideSafe)) {
108+
auto FramePtrArgPosition = NewCallee->arg_size() - 1;
109+
auto FrameSize = NewCallee->getParamDereferenceableBytes(FramePtrArgPosition);
110+
auto FrameAlign = NewCallee->getParamAlign(FramePtrArgPosition).valueOrOne();
128111

129-
processCall(CB, Caller, NewCallee, FrameSize, FrameAlign);
112+
SmallVector<CallBase *, 4> Users;
113+
for (auto *U : F.users()) {
114+
if (auto *CB = dyn_cast<CallBase>(U)) {
115+
if (CB->getCalledFunction() == &F)
116+
Users.push_back(CB);
117+
}
118+
}
130119

131-
ORE.emit([&]() {
132-
return OptimizationRemark(DEBUG_TYPE, "CoroAnnotationElide", Caller)
133-
<< "'" << ore::NV("callee", F.getName()) << "' elided in '"
134-
<< ore::NV("caller", Caller->getName());
135-
});
136-
FAM.invalidate(*Caller, PreservedAnalyses::none());
137-
Changed = true;
138-
}
120+
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
121+
122+
for (auto *CB : Users) {
123+
auto *Caller = CB->getFunction();
124+
if (!Caller)
125+
continue;
126+
127+
bool IsCallerPresplitCoroutine = Caller->isPresplitCoroutine();
128+
bool HasAttr = CB->hasFnAttr(llvm::Attribute::CoroElideSafe);
129+
if (IsCallerPresplitCoroutine && HasAttr) {
130+
processCall(CB, Caller, NewCallee, FrameSize, FrameAlign);
131+
132+
ORE.emit([&]() {
133+
return OptimizationRemark(DEBUG_TYPE, "CoroAnnotationElide", Caller)
134+
<< "'" << ore::NV("callee", F.getName()) << "' elided in '"
135+
<< ore::NV("caller", Caller->getName()) << "'";
136+
});
137+
138+
FAM.invalidate(*Caller, PreservedAnalyses::none());
139+
Changed = true;
140+
} else {
141+
ORE.emit([&]() {
142+
return OptimizationRemarkMissed(DEBUG_TYPE, "CoroAnnotationElide",
143+
Caller)
144+
<< "'" << ore::NV("callee", F.getName()) << "' not elided in '"
145+
<< ore::NV("caller", Caller->getName()) << "' (caller_presplit="
146+
<< ore::NV("caller_presplit", IsCallerPresplitCoroutine)
147+
<< ", elide_safe_attr=" << ore::NV("elide_safe_attr", HasAttr)
148+
<< ")";
149+
});
139150
}
151+
}
140152

141153
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
142154
}

0 commit comments

Comments
 (0)