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