Skip to content

Commit 6e5d612

Browse files
TylerNowickitnowicki
andauthored
[Coroutines] Move OptimizeFrame out of Shape (llvm#111017)
* OptimizeFrame is not really a part of the Coroutine Shape info, rather it is specifically for the addFieldForAllocas method called indirectly by buildCoroutineFrame. * This patch passes OptimizeFrame directly to buildCoroutineFrame so it can be provided to addFieldForAllocas instead of keeping it in the Shape. Co-authored-by: tnowicki <[email protected]>
1 parent c0a2915 commit 6e5d612

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

llvm/lib/Transforms/Coroutines/ABI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LLVM_LIBRARY_VISIBILITY BaseABI {
4141
virtual void init() = 0;
4242

4343
// Allocate the coroutine frame and do spill/reload as needed.
44-
virtual void buildCoroutineFrame();
44+
virtual void buildCoroutineFrame(bool OptimizeFrame);
4545

4646
// Perform the function splitting according to the ABI.
4747
virtual void splitCoroutine(Function &F, coro::Shape &Shape,

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class FrameTypeBuilder {
234234
/// Side Effects: Because We sort the allocas, the order of allocas in the
235235
/// frame may be different with the order in the source code.
236236
void addFieldForAllocas(const Function &F, FrameDataInfo &FrameData,
237-
coro::Shape &Shape);
237+
coro::Shape &Shape, bool OptimizeFrame);
238238

239239
/// Add a field to this structure.
240240
[[nodiscard]] FieldIDType addField(Type *Ty, MaybeAlign MaybeFieldAlignment,
@@ -336,7 +336,8 @@ void FrameDataInfo::updateLayoutIndex(FrameTypeBuilder &B) {
336336

337337
void FrameTypeBuilder::addFieldForAllocas(const Function &F,
338338
FrameDataInfo &FrameData,
339-
coro::Shape &Shape) {
339+
coro::Shape &Shape,
340+
bool OptimizeFrame) {
340341
using AllocaSetType = SmallVector<AllocaInst *, 4>;
341342
SmallVector<AllocaSetType, 4> NonOverlapedAllocas;
342343

@@ -350,7 +351,7 @@ void FrameTypeBuilder::addFieldForAllocas(const Function &F,
350351
}
351352
});
352353

353-
if (!Shape.OptimizeFrame) {
354+
if (!OptimizeFrame) {
354355
for (const auto &A : FrameData.Allocas) {
355356
AllocaInst *Alloca = A.Alloca;
356357
NonOverlapedAllocas.emplace_back(AllocaSetType(1, Alloca));
@@ -860,7 +861,8 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
860861
// ... spills ...
861862
// };
862863
static StructType *buildFrameType(Function &F, coro::Shape &Shape,
863-
FrameDataInfo &FrameData) {
864+
FrameDataInfo &FrameData,
865+
bool OptimizeFrame) {
864866
LLVMContext &C = F.getContext();
865867
const DataLayout &DL = F.getDataLayout();
866868
StructType *FrameTy = [&] {
@@ -905,7 +907,7 @@ static StructType *buildFrameType(Function &F, coro::Shape &Shape,
905907

906908
// Because multiple allocas may own the same field slot,
907909
// we add allocas to field here.
908-
B.addFieldForAllocas(F, FrameData, Shape);
910+
B.addFieldForAllocas(F, FrameData, Shape, OptimizeFrame);
909911
// Add PromiseAlloca to Allocas list so that
910912
// 1. updateLayoutIndex could update its index after
911913
// `performOptimizedStructLayout`
@@ -2056,7 +2058,7 @@ void coro::normalizeCoroutine(Function &F, coro::Shape &Shape,
20562058
rewritePHIs(F);
20572059
}
20582060

2059-
void coro::BaseABI::buildCoroutineFrame() {
2061+
void coro::BaseABI::buildCoroutineFrame(bool OptimizeFrame) {
20602062
SuspendCrossingInfo Checker(F, Shape.CoroSuspends, Shape.CoroEnds);
20612063
doRematerializations(F, Checker, IsMaterializable);
20622064

@@ -2087,7 +2089,7 @@ void coro::BaseABI::buildCoroutineFrame() {
20872089

20882090
// Build frame
20892091
FrameDataInfo FrameData(Spills, Allocas);
2090-
Shape.FrameTy = buildFrameType(F, Shape, FrameData);
2092+
Shape.FrameTy = buildFrameType(F, Shape, FrameData, OptimizeFrame);
20912093
Shape.FramePtr = Shape.CoroBegin;
20922094
// For now, this works for C++ programs only.
20932095
buildFrameDebugInfo(F, Shape, FrameData);

llvm/lib/Transforms/Coroutines/CoroShape.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ struct LLVM_LIBRARY_VISIBILITY Shape {
112112
Value *FramePtr = nullptr;
113113
BasicBlock *AllocaSpillBlock = nullptr;
114114

115-
/// This would only be true if optimization are enabled.
116-
bool OptimizeFrame;
117-
118115
struct SwitchLoweringStorage {
119116
SwitchInst *ResumeSwitch;
120117
AllocaInst *PromiseAlloca;
@@ -265,8 +262,7 @@ struct LLVM_LIBRARY_VISIBILITY Shape {
265262
void emitDealloc(IRBuilder<> &Builder, Value *Ptr, CallGraph *CG) const;
266263

267264
Shape() = default;
268-
explicit Shape(Function &F, bool OptimizeFrame = false)
269-
: OptimizeFrame(OptimizeFrame) {
265+
explicit Shape(Function &F) {
270266
SmallVector<CoroFrameInst *, 8> CoroFrames;
271267
SmallVector<CoroSaveInst *, 2> UnusedCoroSaves;
272268

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,8 @@ void coro::SwitchABI::splitCoroutine(Function &F, coro::Shape &Shape,
20532053
}
20542054

20552055
static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
2056-
coro::BaseABI &ABI, TargetTransformInfo &TTI) {
2056+
coro::BaseABI &ABI, TargetTransformInfo &TTI,
2057+
bool OptimizeFrame) {
20572058
PrettyStackTraceFunction prettyStackTrace(F);
20582059

20592060
auto &Shape = ABI.Shape;
@@ -2064,7 +2065,7 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
20642065
simplifySuspendPoints(Shape);
20652066

20662067
normalizeCoroutine(F, Shape, TTI);
2067-
ABI.buildCoroutineFrame();
2068+
ABI.buildCoroutineFrame(OptimizeFrame);
20682069
replaceFrameSizeAndAlignment(Shape);
20692070

20702071
bool isNoSuspendCoroutine = Shape.CoroSuspends.empty();
@@ -2273,7 +2274,7 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
22732274
// unreachable blocks before collecting intrinsics into Shape.
22742275
removeUnreachableBlocks(F);
22752276

2276-
coro::Shape Shape(F, OptimizeFrame);
2277+
coro::Shape Shape(F);
22772278
if (!Shape.CoroBegin)
22782279
continue;
22792280

@@ -2283,7 +2284,7 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
22832284

22842285
SmallVector<Function *, 4> Clones;
22852286
auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
2286-
doSplitCoroutine(F, Clones, *ABI, TTI);
2287+
doSplitCoroutine(F, Clones, *ABI, TTI, OptimizeFrame);
22872288
CurrentSCC = &updateCallGraphAfterCoroutineSplit(
22882289
*N, Shape, Clones, *CurrentSCC, CG, AM, UR, FAM);
22892290

0 commit comments

Comments
 (0)