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,62 @@ struct SwitchCoroutineSplitter {
1455
1462
setCoroInfo (F, Shape, Clones);
1456
1463
}
1457
1464
1465
+ static Function *createNoAllocVariant (Function &F, coro::Shape &Shape,
1466
+ SmallVectorImpl<Function *> &Clones) {
1467
+ auto *OrigFnTy = F.getFunctionType ();
1468
+ auto OldParams = OrigFnTy->params ();
1469
+
1470
+ SmallVector<Type *> NewParams;
1471
+ NewParams.reserve (OldParams.size () + 1 );
1472
+ NewParams.append (OldParams.begin (), OldParams.end ());
1473
+ NewParams.push_back (PointerType::getUnqual (Shape.FrameTy ));
1474
+
1475
+ auto *NewFnTy = FunctionType::get (OrigFnTy->getReturnType (), NewParams,
1476
+ OrigFnTy->isVarArg ());
1477
+ Function *NoAllocF =
1478
+ Function::Create (NewFnTy, F.getLinkage (), F.getName () + " .noalloc" );
1479
+ ValueToValueMapTy VMap;
1480
+ unsigned int Idx = 0 ;
1481
+ for (const auto &I : F.args ()) {
1482
+ VMap[&I] = NoAllocF->getArg (Idx++);
1483
+ }
1484
+ SmallVector<ReturnInst *, 4 > Returns;
1485
+ CloneFunctionInto (NoAllocF, &F, VMap,
1486
+ CloneFunctionChangeType::LocalChangesOnly, Returns);
1487
+
1488
+ if (Shape.CoroBegin ) {
1489
+ auto *NewCoroBegin =
1490
+ cast_if_present<CoroBeginInst>(VMap[Shape.CoroBegin ]);
1491
+ auto *NewCoroId = cast<CoroIdInst>(NewCoroBegin->getId ());
1492
+ coro::replaceCoroFree (NewCoroId, /* Elide=*/ true );
1493
+ coro::suppressCoroAllocs (NewCoroId);
1494
+ NewCoroBegin->replaceAllUsesWith (NoAllocF->getArg (Idx));
1495
+ NewCoroBegin->eraseFromParent ();
1496
+ }
1497
+
1498
+ Module *M = F.getParent ();
1499
+ M->getFunctionList ().insert (M->end (), NoAllocF);
1500
+
1501
+ removeUnreachableBlocks (*NoAllocF);
1502
+ auto NewAttrs = NoAllocF->getAttributes ();
1503
+ // We just appended the frame pointer as the last argument of the new
1504
+ // function.
1505
+ auto FrameIdx = NoAllocF->arg_size () - 1 ;
1506
+ // When we elide allocation, we read these attributes to determine the
1507
+ // frame size and alignment.
1508
+ addFramePointerAttrs (NewAttrs, NoAllocF->getContext (), FrameIdx,
1509
+ Shape.FrameSize , Shape.FrameAlign ,
1510
+ /* NoAlias=*/ false );
1511
+
1512
+ NoAllocF->setAttributes (NewAttrs);
1513
+
1514
+ Clones.push_back (NoAllocF);
1515
+ // Reset the original function's coro info, make the new noalloc variant
1516
+ // connected to the original ramp function.
1517
+ setCoroInfo (F, Shape, Clones);
1518
+ return NoAllocF;
1519
+ }
1520
+
1458
1521
private:
1459
1522
// Create a resume clone by cloning the body of the original function, setting
1460
1523
// new entry block and replacing coro.suspend an appropriate value to force
@@ -1913,6 +1976,21 @@ class PrettyStackTraceFunction : public PrettyStackTraceEntry {
1913
1976
};
1914
1977
} // namespace
1915
1978
1979
+ // / Remove calls to llvm.coro.end in the original function.
1980
+ static void removeCoroEndsFromRampFunction (const coro::Shape &Shape) {
1981
+ if (Shape.ABI != coro::ABI::Switch) {
1982
+ for (auto *End : Shape.CoroEnds ) {
1983
+ replaceCoroEnd (End, Shape, Shape.FramePtr , /* in resume*/ false , nullptr );
1984
+ }
1985
+ } else {
1986
+ for (llvm::AnyCoroEndInst *End : Shape.CoroEnds ) {
1987
+ auto &Context = End->getContext ();
1988
+ End->replaceAllUsesWith (ConstantInt::getFalse (Context));
1989
+ End->eraseFromParent ();
1990
+ }
1991
+ }
1992
+ }
1993
+
1916
1994
static coro::Shape
1917
1995
splitCoroutine (Function &F, SmallVectorImpl<Function *> &Clones,
1918
1996
TargetTransformInfo &TTI, bool OptimizeFrame,
@@ -1932,10 +2010,10 @@ splitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
1932
2010
simplifySuspendPoints (Shape);
1933
2011
buildCoroutineFrame (F, Shape, TTI, MaterializableCallback);
1934
2012
replaceFrameSizeAndAlignment (Shape);
1935
-
2013
+ bool isNoSuspendCoroutine = Shape. CoroSuspends . empty ();
1936
2014
// If there are no suspend points, no split required, just remove
1937
2015
// the allocation and deallocation blocks, they are not needed.
1938
- if (Shape. CoroSuspends . empty () ) {
2016
+ if (isNoSuspendCoroutine ) {
1939
2017
handleNoSuspendCoroutine (Shape);
1940
2018
} else {
1941
2019
switch (Shape.ABI ) {
@@ -1967,22 +2045,13 @@ splitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
1967
2045
for (DbgVariableRecord *DVR : DbgVariableRecords)
1968
2046
coro::salvageDebugInfo (ArgToAllocaMap, *DVR, Shape.OptimizeFrame ,
1969
2047
false /* UseEntryValue*/ );
1970
- return Shape;
1971
- }
1972
2048
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
- }
2049
+ removeCoroEndsFromRampFunction (Shape);
2050
+
2051
+ if (!isNoSuspendCoroutine && Shape.ABI == coro::ABI::Switch) {
2052
+ SwitchCoroutineSplitter::createNoAllocVariant (F, Shape, Clones);
1985
2053
}
2054
+ return Shape;
1986
2055
}
1987
2056
1988
2057
static void updateCallGraphAfterCoroutineSplit (
@@ -2108,18 +2177,18 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2108
2177
// Split all the coroutines.
2109
2178
for (LazyCallGraph::Node *N : Coroutines) {
2110
2179
Function &F = N->getFunction ();
2180
+
2111
2181
LLVM_DEBUG (dbgs () << " CoroSplit: Processing coroutine '" << F.getName ()
2112
2182
<< " \n " );
2113
2183
F.setSplittedCoroutine ();
2114
2184
2115
2185
SmallVector<Function *, 4 > Clones;
2116
- auto &ORE = FAM.getResult <OptimizationRemarkEmitterAnalysis>(F);
2117
- const coro::Shape Shape =
2186
+ coro::Shape Shape =
2118
2187
splitCoroutine (F, Clones, FAM.getResult <TargetIRAnalysis>(F),
2119
2188
OptimizeFrame, MaterializableCallback);
2120
- removeCoroEndsFromRampFunction (Shape);
2121
2189
updateCallGraphAfterCoroutineSplit (*N, Shape, Clones, C, CG, AM, UR, FAM);
2122
2190
2191
+ auto &ORE = FAM.getResult <OptimizationRemarkEmitterAnalysis>(F);
2123
2192
ORE.emit ([&]() {
2124
2193
return OptimizationRemark (DEBUG_TYPE, " CoroSplit" , &F)
2125
2194
<< " Split '" << ore::NV (" function" , F.getName ())
@@ -2135,9 +2204,9 @@ PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C,
2135
2204
}
2136
2205
}
2137
2206
2138
- for (auto *PrepareFn : PrepareFns) {
2139
- replaceAllPrepares (PrepareFn, CG, C);
2140
- }
2207
+ for (auto *PrepareFn : PrepareFns) {
2208
+ replaceAllPrepares (PrepareFn, CG, C);
2209
+ }
2141
2210
2142
2211
return PreservedAnalyses::none ();
2143
2212
}
0 commit comments