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"
@@ -1177,6 +1178,14 @@ static void updateAsyncFuncPointerContextSize(coro::Shape &Shape) {
1177
1178
Shape.AsyncLowering .AsyncFuncPointer ->setInitializer (NewFuncPtrStruct);
1178
1179
}
1179
1180
1181
+ static TypeSize getFrameSizeForShape (coro::Shape &Shape) {
1182
+ // In the same function all coro.sizes should have the same result type.
1183
+ auto *SizeIntrin = Shape.CoroSizes .back ();
1184
+ Module *M = SizeIntrin->getModule ();
1185
+ const DataLayout &DL = M->getDataLayout ();
1186
+ return DL.getTypeAllocSize (Shape.FrameTy );
1187
+ }
1188
+
1180
1189
static void replaceFrameSizeAndAlignment (coro::Shape &Shape) {
1181
1190
if (Shape.ABI == coro::ABI::Async)
1182
1191
updateAsyncFuncPointerContextSize (Shape);
@@ -1192,10 +1201,8 @@ static void replaceFrameSizeAndAlignment(coro::Shape &Shape) {
1192
1201
1193
1202
// In the same function all coro.sizes should have the same result type.
1194
1203
auto *SizeIntrin = Shape.CoroSizes .back ();
1195
- Module *M = SizeIntrin->getModule ();
1196
- const DataLayout &DL = M->getDataLayout ();
1197
- auto Size = DL.getTypeAllocSize (Shape.FrameTy );
1198
- auto *SizeConstant = ConstantInt::get (SizeIntrin->getType (), Size);
1204
+ auto *SizeConstant =
1205
+ ConstantInt::get (SizeIntrin->getType (), getFrameSizeForShape (Shape));
1199
1206
1200
1207
for (CoroSizeInst *CS : Shape.CoroSizes ) {
1201
1208
CS->replaceAllUsesWith (SizeConstant);
@@ -1452,6 +1459,75 @@ struct SwitchCoroutineSplitter {
1452
1459
setCoroInfo (F, Shape, Clones);
1453
1460
}
1454
1461
1462
+ // Create a variant of ramp function that does not perform heap allocation
1463
+ // for a switch ABI coroutine.
1464
+ //
1465
+ // The newly split `.noalloc` ramp function has the following differences:
1466
+ // - Has one additional frame pointer parameter in lieu of dynamic
1467
+ // allocation.
1468
+ // - Suppressed allocations by replacing coro.alloc and coro.free.
1469
+ static Function *createNoAllocVariant (Function &F, coro::Shape &Shape,
1470
+ SmallVectorImpl<Function *> &Clones) {
1471
+ assert (Shape.ABI == coro::ABI::Switch);
1472
+ auto *OrigFnTy = F.getFunctionType ();
1473
+ auto OldParams = OrigFnTy->params ();
1474
+
1475
+ SmallVector<Type *> NewParams;
1476
+ NewParams.reserve (OldParams.size () + 1 );
1477
+ NewParams.append (OldParams.begin (), OldParams.end ());
1478
+ NewParams.push_back (PointerType::getUnqual (Shape.FrameTy ));
1479
+
1480
+ auto *NewFnTy = FunctionType::get (OrigFnTy->getReturnType (), NewParams,
1481
+ OrigFnTy->isVarArg ());
1482
+ Function *NoAllocF =
1483
+ Function::Create (NewFnTy, F.getLinkage (), F.getName () + " .noalloc" );
1484
+
1485
+ ValueToValueMapTy VMap;
1486
+ unsigned int Idx = 0 ;
1487
+ for (const auto &I : F.args ()) {
1488
+ VMap[&I] = NoAllocF->getArg (Idx++);
1489
+ }
1490
+ // We just appended the frame pointer as the last argument of the new
1491
+ // function.
1492
+ auto FrameIdx = NoAllocF->arg_size () - 1 ;
1493
+ SmallVector<ReturnInst *, 4 > Returns;
1494
+ CloneFunctionInto (NoAllocF, &F, VMap,
1495
+ CloneFunctionChangeType::LocalChangesOnly, Returns);
1496
+
1497
+ if (Shape.CoroBegin ) {
1498
+ auto *NewCoroBegin =
1499
+ cast_if_present<CoroBeginInst>(VMap[Shape.CoroBegin ]);
1500
+ auto *NewCoroId = cast<CoroIdInst>(NewCoroBegin->getId ());
1501
+ coro::replaceCoroFree (NewCoroId, /* Elide=*/ true );
1502
+ coro::suppressCoroAllocs (NewCoroId);
1503
+ NewCoroBegin->replaceAllUsesWith (NoAllocF->getArg (FrameIdx));
1504
+ NewCoroBegin->eraseFromParent ();
1505
+ }
1506
+
1507
+ Module *M = F.getParent ();
1508
+ M->getFunctionList ().insert (M->end (), NoAllocF);
1509
+
1510
+ removeUnreachableBlocks (*NoAllocF);
1511
+ auto NewAttrs = NoAllocF->getAttributes ();
1512
+ // When we elide allocation, we read these attributes to determine the
1513
+ // frame size and alignment.
1514
+ addFramePointerAttrs (NewAttrs, NoAllocF->getContext (), FrameIdx,
1515
+ Shape.FrameSize , Shape.FrameAlign ,
1516
+ /* NoAlias=*/ false );
1517
+
1518
+ NoAllocF->setAttributes (NewAttrs);
1519
+
1520
+ Clones.push_back (NoAllocF);
1521
+ // Reset the original function's coro info, make the new noalloc variant
1522
+ // connected to the original ramp function.
1523
+ setCoroInfo (F, Shape, Clones);
1524
+ // After copying, set the linkage to internal linkage. Original function
1525
+ // may have different linkage, but optimization dependent on this function
1526
+ // generally relies on LTO.
1527
+ NoAllocF->setLinkage (llvm::GlobalValue::InternalLinkage);
1528
+ return NoAllocF;
1529
+ }
1530
+
1455
1531
private:
1456
1532
// Create a resume clone by cloning the body of the original function, setting
1457
1533
// new entry block and replacing coro.suspend an appropriate value to force
@@ -1910,6 +1986,33 @@ class PrettyStackTraceFunction : public PrettyStackTraceEntry {
1910
1986
};
1911
1987
} // namespace
1912
1988
1989
+ // / Remove calls to llvm.coro.end in the original function.
1990
+ static void removeCoroEndsFromRampFunction (const coro::Shape &Shape) {
1991
+ if (Shape.ABI != coro::ABI::Switch) {
1992
+ for (auto *End : Shape.CoroEnds ) {
1993
+ replaceCoroEnd (End, Shape, Shape.FramePtr , /* in resume*/ false , nullptr );
1994
+ }
1995
+ } else {
1996
+ for (llvm::AnyCoroEndInst *End : Shape.CoroEnds ) {
1997
+ auto &Context = End->getContext ();
1998
+ End->replaceAllUsesWith (ConstantInt::getFalse (Context));
1999
+ End->eraseFromParent ();
2000
+ }
2001
+ }
2002
+ }
2003
+
2004
+ static bool hasSafeElideCaller (Function &F) {
2005
+ for (auto *U : F.users ()) {
2006
+ if (auto *CB = dyn_cast<CallBase>(U)) {
2007
+ auto *Caller = CB->getFunction ();
2008
+ if (Caller && Caller->isPresplitCoroutine () &&
2009
+ CB->hasFnAttr (llvm::Attribute::CoroElideSafe))
2010
+ return true ;
2011
+ }
2012
+ }
2013
+ return false ;
2014
+ }
2015
+
1913
2016
static coro::Shape
1914
2017
splitCoroutine (Function &F, SmallVectorImpl<Function *> &Clones,
1915
2018
TargetTransformInfo &TTI, bool OptimizeFrame,
@@ -1929,10 +2032,15 @@ splitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
1929
2032
simplifySuspendPoints (Shape);
1930
2033
buildCoroutineFrame (F, Shape, TTI, MaterializableCallback);
1931
2034
replaceFrameSizeAndAlignment (Shape);
2035
+ bool isNoSuspendCoroutine = Shape.CoroSuspends .empty ();
2036
+
2037
+ bool shouldCreateNoAllocVariant = !isNoSuspendCoroutine &&
2038
+ Shape.ABI == coro::ABI::Switch &&
2039
+ hasSafeElideCaller (F);
1932
2040
1933
2041
// If there are no suspend points, no split required, just remove
1934
2042
// the allocation and deallocation blocks, they are not needed.
1935
- if (Shape. CoroSuspends . empty () ) {
2043
+ if (isNoSuspendCoroutine ) {
1936
2044
handleNoSuspendCoroutine (Shape);
1937
2045
} else {
1938
2046
switch (Shape.ABI ) {
@@ -1962,22 +2070,13 @@ splitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
1962
2070
coro::salvageDebugInfo (ArgToAllocaMap, *DDI, false /* UseEntryValue*/ );
1963
2071
for (DbgVariableRecord *DVR : DbgVariableRecords)
1964
2072
coro::salvageDebugInfo (ArgToAllocaMap, *DVR, false /* UseEntryValue*/ );
1965
- return Shape;
1966
- }
1967
2073
1968
- // / Remove calls to llvm.coro.end in the original function.
1969
- static void removeCoroEndsFromRampFunction (const coro::Shape &Shape) {
1970
- if (Shape.ABI != coro::ABI::Switch) {
1971
- for (auto *End : Shape.CoroEnds ) {
1972
- replaceCoroEnd (End, Shape, Shape.FramePtr , /* in resume*/ false , nullptr );
1973
- }
1974
- } else {
1975
- for (llvm::AnyCoroEndInst *End : Shape.CoroEnds ) {
1976
- auto &Context = End->getContext ();
1977
- End->replaceAllUsesWith (ConstantInt::getFalse (Context));
1978
- End->eraseFromParent ();
1979
- }
1980
- }
2074
+ removeCoroEndsFromRampFunction (Shape);
2075
+
2076
+ if (shouldCreateNoAllocVariant)
2077
+ SwitchCoroutineSplitter::createNoAllocVariant (F, Shape, Clones);
2078
+
2079
+ return Shape;
1981
2080
}
1982
2081
1983
2082
static void updateCallGraphAfterCoroutineSplit (
@@ -2108,13 +2207,12 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2108
2207
F.setSplittedCoroutine ();
2109
2208
2110
2209
SmallVector<Function *, 4 > Clones;
2111
- auto &ORE = FAM.getResult <OptimizationRemarkEmitterAnalysis>(F);
2112
- const coro::Shape Shape =
2210
+ coro::Shape Shape =
2113
2211
splitCoroutine (F, Clones, FAM.getResult <TargetIRAnalysis>(F),
2114
2212
OptimizeFrame, MaterializableCallback);
2115
- removeCoroEndsFromRampFunction (Shape);
2116
2213
updateCallGraphAfterCoroutineSplit (*N, Shape, Clones, C, CG, AM, UR, FAM);
2117
2214
2215
+ auto &ORE = FAM.getResult <OptimizationRemarkEmitterAnalysis>(F);
2118
2216
ORE.emit ([&]() {
2119
2217
return OptimizationRemark (DEBUG_TYPE, " CoroSplit" , &F)
2120
2218
<< " Split '" << ore::NV (" function" , F.getName ())
@@ -2130,9 +2228,9 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2130
2228
}
2131
2229
}
2132
2230
2133
- for (auto *PrepareFn : PrepareFns) {
2134
- replaceAllPrepares (PrepareFn, CG, C);
2135
- }
2231
+ for (auto *PrepareFn : PrepareFns) {
2232
+ replaceAllPrepares (PrepareFn, CG, C);
2233
+ }
2136
2234
2137
2235
return PreservedAnalyses::none ();
2138
2236
}
0 commit comments