25
25
#include " llvm/ADT/PriorityWorklist.h"
26
26
#include " llvm/ADT/SmallPtrSet.h"
27
27
#include " llvm/ADT/SmallVector.h"
28
+ #include " llvm/ADT/StringExtras.h"
28
29
#include " llvm/ADT/StringRef.h"
29
30
#include " llvm/ADT/Twine.h"
30
31
#include " llvm/Analysis/CFG.h"
@@ -1179,6 +1180,14 @@ static void updateAsyncFuncPointerContextSize(coro::Shape &Shape) {
1179
1180
Shape.AsyncLowering .AsyncFuncPointer ->setInitializer (NewFuncPtrStruct);
1180
1181
}
1181
1182
1183
+ static TypeSize getFrameSizeForShape (coro::Shape &Shape) {
1184
+ // In the same function all coro.sizes should have the same result type.
1185
+ auto *SizeIntrin = Shape.CoroSizes .back ();
1186
+ Module *M = SizeIntrin->getModule ();
1187
+ const DataLayout &DL = M->getDataLayout ();
1188
+ return DL.getTypeAllocSize (Shape.FrameTy );
1189
+ }
1190
+
1182
1191
static void replaceFrameSizeAndAlignment (coro::Shape &Shape) {
1183
1192
if (Shape.ABI == coro::ABI::Async)
1184
1193
updateAsyncFuncPointerContextSize (Shape);
@@ -1194,10 +1203,8 @@ static void replaceFrameSizeAndAlignment(coro::Shape &Shape) {
1194
1203
1195
1204
// In the same function all coro.sizes should have the same result type.
1196
1205
auto *SizeIntrin = Shape.CoroSizes .back ();
1197
- Module *M = SizeIntrin->getModule ();
1198
- const DataLayout &DL = M->getDataLayout ();
1199
- auto Size = DL.getTypeAllocSize (Shape.FrameTy );
1200
- auto *SizeConstant = ConstantInt::get (SizeIntrin->getType (), Size );
1206
+ auto *SizeConstant =
1207
+ ConstantInt::get (SizeIntrin->getType (), getFrameSizeForShape (Shape));
1201
1208
1202
1209
for (CoroSizeInst *CS : Shape.CoroSizes ) {
1203
1210
CS->replaceAllUsesWith (SizeConstant);
@@ -1455,6 +1462,73 @@ struct SwitchCoroutineSplitter {
1455
1462
setCoroInfo (F, Shape, Clones);
1456
1463
}
1457
1464
1465
+ // Create a variant of ramp function that does not perform heap allocation
1466
+ // for a switch ABI coroutine.
1467
+ //
1468
+ // The newly split `.noalloc` ramp function has the following differences:
1469
+ // - Has one additional frame pointer parameter in lieu of dynamic allocation.
1470
+ // - Suppressed allocations by replacing coro.alloc and coro.free.
1471
+ static Function *createNoAllocVariant (Function &F, coro::Shape &Shape,
1472
+ SmallVectorImpl<Function *> &Clones) {
1473
+ auto *OrigFnTy = F.getFunctionType ();
1474
+ auto OldParams = OrigFnTy->params ();
1475
+
1476
+ SmallVector<Type *> NewParams;
1477
+ NewParams.reserve (OldParams.size () + 1 );
1478
+ NewParams.append (OldParams.begin (), OldParams.end ());
1479
+ NewParams.push_back (PointerType::getUnqual (Shape.FrameTy ));
1480
+
1481
+ auto *NewFnTy = FunctionType::get (OrigFnTy->getReturnType (), NewParams,
1482
+ OrigFnTy->isVarArg ());
1483
+ Function *NoAllocF =
1484
+ Function::Create (NewFnTy, F.getLinkage (), F.getName () + " .noalloc" );
1485
+
1486
+ ValueToValueMapTy VMap;
1487
+ unsigned int Idx = 0 ;
1488
+ for (const auto &I : F.args ()) {
1489
+ VMap[&I] = NoAllocF->getArg (Idx++);
1490
+ }
1491
+ SmallVector<ReturnInst *, 4 > Returns;
1492
+ CloneFunctionInto (NoAllocF, &F, VMap,
1493
+ CloneFunctionChangeType::LocalChangesOnly, Returns);
1494
+
1495
+ if (Shape.CoroBegin ) {
1496
+ auto *NewCoroBegin =
1497
+ cast_if_present<CoroBeginInst>(VMap[Shape.CoroBegin ]);
1498
+ auto *NewCoroId = cast<CoroIdInst>(NewCoroBegin->getId ());
1499
+ coro::replaceCoroFree (NewCoroId, /* Elide=*/ true );
1500
+ coro::suppressCoroAllocs (NewCoroId);
1501
+ NewCoroBegin->replaceAllUsesWith (NoAllocF->getArg (Idx));
1502
+ NewCoroBegin->eraseFromParent ();
1503
+ }
1504
+
1505
+ Module *M = F.getParent ();
1506
+ M->getFunctionList ().insert (M->end (), NoAllocF);
1507
+
1508
+ removeUnreachableBlocks (*NoAllocF);
1509
+ auto NewAttrs = NoAllocF->getAttributes ();
1510
+ // We just appended the frame pointer as the last argument of the new
1511
+ // function.
1512
+ auto FrameIdx = NoAllocF->arg_size () - 1 ;
1513
+ // When we elide allocation, we read these attributes to determine the
1514
+ // frame size and alignment.
1515
+ addFramePointerAttrs (NewAttrs, NoAllocF->getContext (), FrameIdx,
1516
+ Shape.FrameSize , Shape.FrameAlign ,
1517
+ /* NoAlias=*/ false );
1518
+
1519
+ NoAllocF->setAttributes (NewAttrs);
1520
+
1521
+ Clones.push_back (NoAllocF);
1522
+ // Reset the original function's coro info, make the new noalloc variant
1523
+ // connected to the original ramp function.
1524
+ setCoroInfo (F, Shape, Clones);
1525
+ // After copying, set the linkage to internal linkage. Original function
1526
+ // may have different linkage, but optimization dependent on this function
1527
+ // generally relies on LTO.
1528
+ NoAllocF->setLinkage (llvm::GlobalValue::InternalLinkage);
1529
+ return NoAllocF;
1530
+ }
1531
+
1458
1532
private:
1459
1533
// Create a resume clone by cloning the body of the original function, setting
1460
1534
// new entry block and replacing coro.suspend an appropriate value to force
@@ -1913,6 +1987,21 @@ class PrettyStackTraceFunction : public PrettyStackTraceEntry {
1913
1987
};
1914
1988
} // namespace
1915
1989
1990
+ // / Remove calls to llvm.coro.end in the original function.
1991
+ static void removeCoroEndsFromRampFunction (const coro::Shape &Shape) {
1992
+ if (Shape.ABI != coro::ABI::Switch) {
1993
+ for (auto *End : Shape.CoroEnds ) {
1994
+ replaceCoroEnd (End, Shape, Shape.FramePtr , /* in resume*/ false , nullptr );
1995
+ }
1996
+ } else {
1997
+ for (llvm::AnyCoroEndInst *End : Shape.CoroEnds ) {
1998
+ auto &Context = End->getContext ();
1999
+ End->replaceAllUsesWith (ConstantInt::getFalse (Context));
2000
+ End->eraseFromParent ();
2001
+ }
2002
+ }
2003
+ }
2004
+
1916
2005
static coro::Shape
1917
2006
splitCoroutine (Function &F, SmallVectorImpl<Function *> &Clones,
1918
2007
TargetTransformInfo &TTI, bool OptimizeFrame,
@@ -1932,10 +2021,10 @@ splitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
1932
2021
simplifySuspendPoints (Shape);
1933
2022
buildCoroutineFrame (F, Shape, TTI, MaterializableCallback);
1934
2023
replaceFrameSizeAndAlignment (Shape);
1935
-
2024
+ bool isNoSuspendCoroutine = Shape. CoroSuspends . empty ();
1936
2025
// If there are no suspend points, no split required, just remove
1937
2026
// the allocation and deallocation blocks, they are not needed.
1938
- if (Shape. CoroSuspends . empty () ) {
2027
+ if (isNoSuspendCoroutine ) {
1939
2028
handleNoSuspendCoroutine (Shape);
1940
2029
} else {
1941
2030
switch (Shape.ABI ) {
@@ -1967,22 +2056,13 @@ splitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
1967
2056
for (DbgVariableRecord *DVR : DbgVariableRecords)
1968
2057
coro::salvageDebugInfo (ArgToAllocaMap, *DVR, Shape.OptimizeFrame ,
1969
2058
false /* UseEntryValue*/ );
1970
- return Shape;
1971
- }
1972
2059
1973
- // / Remove calls to llvm.coro.end in the original function.
1974
- static void removeCoroEndsFromRampFunction (const coro::Shape &Shape) {
1975
- if (Shape.ABI != coro::ABI::Switch) {
1976
- for (auto *End : Shape.CoroEnds ) {
1977
- replaceCoroEnd (End, Shape, Shape.FramePtr , /* in resume*/ false , nullptr );
1978
- }
1979
- } else {
1980
- for (llvm::AnyCoroEndInst *End : Shape.CoroEnds ) {
1981
- auto &Context = End->getContext ();
1982
- End->replaceAllUsesWith (ConstantInt::getFalse (Context));
1983
- End->eraseFromParent ();
1984
- }
2060
+ removeCoroEndsFromRampFunction (Shape);
2061
+
2062
+ if (!isNoSuspendCoroutine && Shape.ABI == coro::ABI::Switch) {
2063
+ SwitchCoroutineSplitter::createNoAllocVariant (F, Shape, Clones);
1985
2064
}
2065
+ return Shape;
1986
2066
}
1987
2067
1988
2068
static void updateCallGraphAfterCoroutineSplit (
@@ -2108,18 +2188,18 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2108
2188
// Split all the coroutines.
2109
2189
for (LazyCallGraph::Node *N : Coroutines) {
2110
2190
Function &F = N->getFunction ();
2191
+
2111
2192
LLVM_DEBUG (dbgs () << " CoroSplit: Processing coroutine '" << F.getName ()
2112
2193
<< " \n " );
2113
2194
F.setSplittedCoroutine ();
2114
2195
2115
2196
SmallVector<Function *, 4 > Clones;
2116
- auto &ORE = FAM.getResult <OptimizationRemarkEmitterAnalysis>(F);
2117
- const coro::Shape Shape =
2197
+ coro::Shape Shape =
2118
2198
splitCoroutine (F, Clones, FAM.getResult <TargetIRAnalysis>(F),
2119
2199
OptimizeFrame, MaterializableCallback);
2120
- removeCoroEndsFromRampFunction (Shape);
2121
2200
updateCallGraphAfterCoroutineSplit (*N, Shape, Clones, C, CG, AM, UR, FAM);
2122
2201
2202
+ auto &ORE = FAM.getResult <OptimizationRemarkEmitterAnalysis>(F);
2123
2203
ORE.emit ([&]() {
2124
2204
return OptimizationRemark (DEBUG_TYPE, " CoroSplit" , &F)
2125
2205
<< " Split '" << ore::NV (" function" , F.getName ())
@@ -2135,9 +2215,9 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2135
2215
}
2136
2216
}
2137
2217
2138
- for (auto *PrepareFn : PrepareFns) {
2139
- replaceAllPrepares (PrepareFn, CG, C);
2140
- }
2218
+ for (auto *PrepareFn : PrepareFns) {
2219
+ replaceAllPrepares (PrepareFn, CG, C);
2220
+ }
2141
2221
2142
2222
return PreservedAnalyses::none ();
2143
2223
}
0 commit comments