6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
// This file contains classes used to discover if for a particular value
9
- // there from sue to definition that crosses a suspend block.
9
+ // its definition preceeds and its uses follow a suspend block. This is
10
+ // referred to as a suspend crossing value.
10
11
//
11
12
// Using the information discovered we form a Coroutine Frame structure to
12
13
// contain those values. All uses of those values are replaced with appropriate
@@ -52,6 +53,16 @@ extern cl::opt<bool> UseNewDbgInfoFormat;
52
53
53
54
enum { SmallVectorThreshold = 32 };
54
55
56
+ static std::string getBasicBlockLabel (const BasicBlock *BB) {
57
+ if (BB->hasName ())
58
+ return BB->getName ().str ();
59
+
60
+ std::string S;
61
+ raw_string_ostream OS (S);
62
+ BB->printAsOperand (OS, false );
63
+ return OS.str ().substr (1 );
64
+ }
65
+
55
66
// Provides two way mapping between the blocks and numbers.
56
67
namespace {
57
68
class BlockToIndexMapping {
@@ -123,8 +134,9 @@ class SuspendCrossingInfo {
123
134
124
135
public:
125
136
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
126
- void dump () const ;
127
- void dump (StringRef Label, BitVector const &BV) const ;
137
+ void dump (const ReversePostOrderTraversal<Function *> &RPOT) const ;
138
+ void dump (StringRef Label, BitVector const &BV,
139
+ const ReversePostOrderTraversal<Function *> &RPOT) const ;
128
140
#endif
129
141
130
142
SuspendCrossingInfo (Function &F, coro::Shape &Shape);
@@ -207,21 +219,25 @@ class SuspendCrossingInfo {
207
219
} // end anonymous namespace
208
220
209
221
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
210
- LLVM_DUMP_METHOD void SuspendCrossingInfo::dump (StringRef Label,
211
- BitVector const &BV) const {
222
+ LLVM_DUMP_METHOD void SuspendCrossingInfo::dump (
223
+ StringRef Label, BitVector const &BV,
224
+ const ReversePostOrderTraversal<Function *> &RPOT) const {
212
225
dbgs () << Label << " :" ;
213
- for (size_t I = 0 , N = BV.size (); I < N; ++I)
214
- if (BV[I])
215
- dbgs () << " " << Mapping.indexToBlock (I)->getName ();
226
+ for (const BasicBlock *BB : RPOT) {
227
+ auto BBNo = Mapping.blockToIndex (BB);
228
+ if (BV[BBNo])
229
+ dbgs () << " " << getBasicBlockLabel (BB);
230
+ }
216
231
dbgs () << " \n " ;
217
232
}
218
233
219
- LLVM_DUMP_METHOD void SuspendCrossingInfo::dump () const {
220
- for (size_t I = 0 , N = Block.size (); I < N; ++I) {
221
- BasicBlock *const B = Mapping.indexToBlock (I);
222
- dbgs () << B->getName () << " :\n " ;
223
- dump (" Consumes" , Block[I].Consumes );
224
- dump (" Kills" , Block[I].Kills );
234
+ LLVM_DUMP_METHOD void SuspendCrossingInfo::dump (
235
+ const ReversePostOrderTraversal<Function *> &RPOT) const {
236
+ for (const BasicBlock *BB : RPOT) {
237
+ auto BBNo = Mapping.blockToIndex (BB);
238
+ dbgs () << getBasicBlockLabel (BB) << " :\n " ;
239
+ dump (" Consumes" , Block[BBNo].Consumes , RPOT);
240
+ dump (" Kills" , Block[BBNo].Kills , RPOT);
225
241
}
226
242
dbgs () << " \n " ;
227
243
}
@@ -335,7 +351,7 @@ SuspendCrossingInfo::SuspendCrossingInfo(Function &F, coro::Shape &Shape)
335
351
while (computeBlockData</* Initialize*/ false >(RPOT))
336
352
;
337
353
338
- LLVM_DEBUG (dump ());
354
+ LLVM_DEBUG (dump (RPOT ));
339
355
}
340
356
341
357
namespace {
@@ -419,7 +435,7 @@ struct RematGraph {
419
435
void dump () const {
420
436
dbgs () << " Entry (" ;
421
437
if (EntryNode->Node ->getParent ()->hasName ())
422
- dbgs () << EntryNode->Node ->getParent ()-> getName ( );
438
+ dbgs () << getBasicBlockLabel ( EntryNode->Node ->getParent ());
423
439
else
424
440
EntryNode->Node ->getParent ()->printAsOperand (dbgs (), false );
425
441
dbgs () << " ) : " << *EntryNode->Node << " \n " ;
@@ -551,7 +567,7 @@ struct FrameDataInfo {
551
567
552
568
#ifndef NDEBUG
553
569
static void dumpSpills (StringRef Title, const SpillInfo &Spills) {
554
- dbgs () << " ------------- " << Title << " --------------\n " ;
570
+ dbgs () << " ------------- " << Title << " --------------\n " ;
555
571
for (const auto &E : Spills) {
556
572
E.first ->dump ();
557
573
dbgs () << " user: " ;
@@ -813,7 +829,7 @@ void FrameTypeBuilder::addFieldForAllocas(const Function &F,
813
829
StackLifetime StackLifetimeAnalyzer (F, ExtractAllocas (),
814
830
StackLifetime::LivenessType::May);
815
831
StackLifetimeAnalyzer.run ();
816
- auto IsAllocaInferenre = [&](const AllocaInst *AI1, const AllocaInst *AI2) {
832
+ auto DoAllocasInterfere = [&](const AllocaInst *AI1, const AllocaInst *AI2) {
817
833
return StackLifetimeAnalyzer.getLiveRange (AI1).overlaps (
818
834
StackLifetimeAnalyzer.getLiveRange (AI2));
819
835
};
@@ -833,13 +849,13 @@ void FrameTypeBuilder::addFieldForAllocas(const Function &F,
833
849
for (const auto &A : FrameData.Allocas ) {
834
850
AllocaInst *Alloca = A.Alloca ;
835
851
bool Merged = false ;
836
- // Try to find if the Alloca is not inferenced with any existing
852
+ // Try to find if the Alloca does not interfere with any existing
837
853
// NonOverlappedAllocaSet. If it is true, insert the alloca to that
838
854
// NonOverlappedAllocaSet.
839
855
for (auto &AllocaSet : NonOverlapedAllocas) {
840
856
assert (!AllocaSet.empty () && " Processing Alloca Set is not empty.\n " );
841
- bool NoInference = none_of (AllocaSet, [&](auto Iter) {
842
- return IsAllocaInferenre (Alloca, Iter);
857
+ bool NoInterference = none_of (AllocaSet, [&](auto Iter) {
858
+ return DoAllocasInterfere (Alloca, Iter);
843
859
});
844
860
// If the alignment of A is multiple of the alignment of B, the address
845
861
// of A should satisfy the requirement for aligning for B.
@@ -852,7 +868,7 @@ void FrameTypeBuilder::addFieldForAllocas(const Function &F,
852
868
return LargestAlloca->getAlign ().value () % Alloca->getAlign ().value () ==
853
869
0 ;
854
870
}();
855
- bool CouldMerge = NoInference && Alignable;
871
+ bool CouldMerge = NoInterference && Alignable;
856
872
if (!CouldMerge)
857
873
continue ;
858
874
AllocaSet.push_back (Alloca);
@@ -1730,6 +1746,51 @@ static Instruction *splitBeforeCatchSwitch(CatchSwitchInst *CatchSwitch) {
1730
1746
return CleanupRet;
1731
1747
}
1732
1748
1749
+ static BasicBlock::iterator getSpillInsertionPt (const coro::Shape &Shape,
1750
+ Value *Def,
1751
+ const DominatorTree &DT) {
1752
+ BasicBlock::iterator InsertPt;
1753
+ if (auto *Arg = dyn_cast<Argument>(Def)) {
1754
+ // For arguments, we will place the store instruction right after
1755
+ // the coroutine frame pointer instruction, i.e. coro.begin.
1756
+ InsertPt = Shape.getInsertPtAfterFramePtr ();
1757
+
1758
+ // If we're spilling an Argument, make sure we clear 'nocapture'
1759
+ // from the coroutine function.
1760
+ Arg->getParent ()->removeParamAttr (Arg->getArgNo (), Attribute::NoCapture);
1761
+ } else if (auto *CSI = dyn_cast<AnyCoroSuspendInst>(Def)) {
1762
+ // Don't spill immediately after a suspend; splitting assumes
1763
+ // that the suspend will be followed by a branch.
1764
+ InsertPt = CSI->getParent ()->getSingleSuccessor ()->getFirstNonPHIIt ();
1765
+ } else {
1766
+ auto *I = cast<Instruction>(Def);
1767
+ if (!DT.dominates (Shape.CoroBegin , I)) {
1768
+ // If it is not dominated by CoroBegin, then spill should be
1769
+ // inserted immediately after CoroFrame is computed.
1770
+ InsertPt = Shape.getInsertPtAfterFramePtr ();
1771
+ } else if (auto *II = dyn_cast<InvokeInst>(I)) {
1772
+ // If we are spilling the result of the invoke instruction, split
1773
+ // the normal edge and insert the spill in the new block.
1774
+ auto *NewBB = SplitEdge (II->getParent (), II->getNormalDest ());
1775
+ InsertPt = NewBB->getTerminator ()->getIterator ();
1776
+ } else if (isa<PHINode>(I)) {
1777
+ // Skip the PHINodes and EH pads instructions.
1778
+ BasicBlock *DefBlock = I->getParent ();
1779
+ if (auto *CSI = dyn_cast<CatchSwitchInst>(DefBlock->getTerminator ()))
1780
+ InsertPt = splitBeforeCatchSwitch (CSI)->getIterator ();
1781
+ else
1782
+ InsertPt = DefBlock->getFirstInsertionPt ();
1783
+ } else {
1784
+ assert (!I->isTerminator () && " unexpected terminator" );
1785
+ // For all other values, the spill is placed immediately after
1786
+ // the definition.
1787
+ InsertPt = I->getNextNode ()->getIterator ();
1788
+ }
1789
+ }
1790
+
1791
+ return InsertPt;
1792
+ }
1793
+
1733
1794
// Replace all alloca and SSA values that are accessed across suspend points
1734
1795
// with GetElementPointer from coroutine frame + loads and stores. Create an
1735
1796
// AllocaSpillBB that will become the new entry block for the resume parts of
@@ -1752,9 +1813,8 @@ static Instruction *splitBeforeCatchSwitch(CatchSwitchInst *CatchSwitch) {
1752
1813
//
1753
1814
//
1754
1815
static void insertSpills (const FrameDataInfo &FrameData, coro::Shape &Shape) {
1755
- auto *CB = Shape.CoroBegin ;
1756
- LLVMContext &C = CB->getContext ();
1757
- Function *F = CB->getFunction ();
1816
+ LLVMContext &C = Shape.CoroBegin ->getContext ();
1817
+ Function *F = Shape.CoroBegin ->getFunction ();
1758
1818
IRBuilder<> Builder (C);
1759
1819
StructType *FrameTy = Shape.FrameTy ;
1760
1820
Value *FramePtr = Shape.FramePtr ;
@@ -1815,47 +1875,16 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1815
1875
auto SpillAlignment = Align (FrameData.getAlign (Def));
1816
1876
// Create a store instruction storing the value into the
1817
1877
// coroutine frame.
1818
- BasicBlock::iterator InsertPt;
1878
+ BasicBlock::iterator InsertPt = getSpillInsertionPt (Shape, Def, DT);
1879
+
1819
1880
Type *ByValTy = nullptr ;
1820
1881
if (auto *Arg = dyn_cast<Argument>(Def)) {
1821
- // For arguments, we will place the store instruction right after
1822
- // the coroutine frame pointer instruction, i.e. coro.begin.
1823
- InsertPt = Shape.getInsertPtAfterFramePtr ();
1824
-
1825
1882
// If we're spilling an Argument, make sure we clear 'nocapture'
1826
1883
// from the coroutine function.
1827
1884
Arg->getParent ()->removeParamAttr (Arg->getArgNo (), Attribute::NoCapture);
1828
1885
1829
1886
if (Arg->hasByValAttr ())
1830
1887
ByValTy = Arg->getParamByValType ();
1831
- } else if (auto *CSI = dyn_cast<AnyCoroSuspendInst>(Def)) {
1832
- // Don't spill immediately after a suspend; splitting assumes
1833
- // that the suspend will be followed by a branch.
1834
- InsertPt = CSI->getParent ()->getSingleSuccessor ()->getFirstNonPHIIt ();
1835
- } else {
1836
- auto *I = cast<Instruction>(Def);
1837
- if (!DT.dominates (CB, I)) {
1838
- // If it is not dominated by CoroBegin, then spill should be
1839
- // inserted immediately after CoroFrame is computed.
1840
- InsertPt = Shape.getInsertPtAfterFramePtr ();
1841
- } else if (auto *II = dyn_cast<InvokeInst>(I)) {
1842
- // If we are spilling the result of the invoke instruction, split
1843
- // the normal edge and insert the spill in the new block.
1844
- auto *NewBB = SplitEdge (II->getParent (), II->getNormalDest ());
1845
- InsertPt = NewBB->getTerminator ()->getIterator ();
1846
- } else if (isa<PHINode>(I)) {
1847
- // Skip the PHINodes and EH pads instructions.
1848
- BasicBlock *DefBlock = I->getParent ();
1849
- if (auto *CSI = dyn_cast<CatchSwitchInst>(DefBlock->getTerminator ()))
1850
- InsertPt = splitBeforeCatchSwitch (CSI)->getIterator ();
1851
- else
1852
- InsertPt = DefBlock->getFirstInsertionPt ();
1853
- } else {
1854
- assert (!I->isTerminator () && " unexpected terminator" );
1855
- // For all other values, the spill is placed immediately after
1856
- // the definition.
1857
- InsertPt = I->getNextNode ()->getIterator ();
1858
- }
1859
1888
}
1860
1889
1861
1890
auto Index = FrameData.getFieldIndex (Def);
@@ -1998,7 +2027,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
1998
2027
UsersToUpdate.clear ();
1999
2028
for (User *U : Alloca->users ()) {
2000
2029
auto *I = cast<Instruction>(U);
2001
- if (DT.dominates (CB , I))
2030
+ if (DT.dominates (Shape. CoroBegin , I))
2002
2031
UsersToUpdate.push_back (I);
2003
2032
}
2004
2033
if (UsersToUpdate.empty ())
@@ -2040,7 +2069,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
2040
2069
Builder.CreateStore (Value, G);
2041
2070
}
2042
2071
// For each alias to Alloca created before CoroBegin but used after
2043
- // CoroBegin, we recreate them after CoroBegin by appplying the offset
2072
+ // CoroBegin, we recreate them after CoroBegin by applying the offset
2044
2073
// to the pointer in the frame.
2045
2074
for (const auto &Alias : A.Aliases ) {
2046
2075
auto *FramePtr = GetFramePointer (Alloca);
@@ -2049,7 +2078,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
2049
2078
auto *AliasPtr =
2050
2079
Builder.CreatePtrAdd (FramePtr, ConstantInt::get (ITy, Value));
2051
2080
Alias.first ->replaceUsesWithIf (
2052
- AliasPtr, [&](Use &U) { return DT.dominates (CB , U); });
2081
+ AliasPtr, [&](Use &U) { return DT.dominates (Shape. CoroBegin , U); });
2053
2082
}
2054
2083
}
2055
2084
@@ -2062,7 +2091,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
2062
2091
// If there is memory accessing to promise alloca before CoroBegin;
2063
2092
bool HasAccessingPromiseBeforeCB = llvm::any_of (PA->uses (), [&](Use &U) {
2064
2093
auto *Inst = dyn_cast<Instruction>(U.getUser ());
2065
- if (!Inst || DT.dominates (CB , Inst))
2094
+ if (!Inst || DT.dominates (Shape. CoroBegin , Inst))
2066
2095
return false ;
2067
2096
2068
2097
if (auto *CI = dyn_cast<CallInst>(Inst)) {
@@ -2105,8 +2134,9 @@ static void movePHIValuesToInsertedBlock(BasicBlock *SuccBB,
2105
2134
do {
2106
2135
int Index = PN->getBasicBlockIndex (InsertedBB);
2107
2136
Value *V = PN->getIncomingValue (Index);
2108
- PHINode *InputV = PHINode::Create (
2109
- V->getType (), 1 , V->getName () + Twine (" ." ) + SuccBB->getName ());
2137
+ PHINode *InputV =
2138
+ PHINode::Create (V->getType (), 1 ,
2139
+ V->getName () + Twine (" ." ) + getBasicBlockLabel (SuccBB));
2110
2140
InputV->insertBefore (InsertedBB->begin ());
2111
2141
InputV->addIncoming (V, PredBB);
2112
2142
PN->setIncomingValue (Index, InputV);
@@ -2151,10 +2181,10 @@ static void rewritePHIsForCleanupPad(BasicBlock *CleanupPadBB,
2151
2181
Builder.CreateUnreachable ();
2152
2182
2153
2183
// Create a new cleanuppad which will be the dispatcher.
2154
- auto *NewCleanupPadBB =
2155
- BasicBlock::Create ( CleanupPadBB->getContext (),
2156
- CleanupPadBB-> getName ( ) + Twine (" .corodispatch" ),
2157
- CleanupPadBB->getParent (), CleanupPadBB);
2184
+ auto *NewCleanupPadBB = BasicBlock::Create (
2185
+ CleanupPadBB->getContext (),
2186
+ getBasicBlockLabel (CleanupPadBB ) + Twine (" .corodispatch" ),
2187
+ CleanupPadBB->getParent (), CleanupPadBB);
2158
2188
Builder.SetInsertPoint (NewCleanupPadBB);
2159
2189
auto *SwitchType = Builder.getInt8Ty ();
2160
2190
auto *SetDispatchValuePN =
@@ -2168,13 +2198,14 @@ static void rewritePHIsForCleanupPad(BasicBlock *CleanupPadBB,
2168
2198
SmallVector<BasicBlock *, 8 > Preds (predecessors (CleanupPadBB));
2169
2199
for (BasicBlock *Pred : Preds) {
2170
2200
// Create a new cleanuppad and move the PHI values to there.
2171
- auto *CaseBB = BasicBlock::Create (CleanupPadBB->getContext (),
2172
- CleanupPadBB->getName () +
2173
- Twine (" .from." ) + Pred->getName (),
2174
- CleanupPadBB->getParent (), CleanupPadBB);
2201
+ auto *CaseBB =
2202
+ BasicBlock::Create (CleanupPadBB->getContext (),
2203
+ getBasicBlockLabel (CleanupPadBB) + Twine (" .from." ) +
2204
+ getBasicBlockLabel (Pred),
2205
+ CleanupPadBB->getParent (), CleanupPadBB);
2175
2206
updatePhiNodes (CleanupPadBB, Pred, CaseBB);
2176
- CaseBB->setName (CleanupPadBB-> getName ( ) + Twine (" .from." ) +
2177
- Pred-> getName ( ));
2207
+ CaseBB->setName (getBasicBlockLabel (CleanupPadBB ) + Twine (" .from." ) +
2208
+ getBasicBlockLabel (Pred ));
2178
2209
Builder.SetInsertPoint (CaseBB);
2179
2210
Builder.CreateBr (CleanupPadBB);
2180
2211
movePHIValuesToInsertedBlock (CleanupPadBB, CaseBB, NewCleanupPadBB);
@@ -2264,7 +2295,8 @@ static void rewritePHIs(BasicBlock &BB) {
2264
2295
SmallVector<BasicBlock *, 8 > Preds (predecessors (&BB));
2265
2296
for (BasicBlock *Pred : Preds) {
2266
2297
auto *IncomingBB = ehAwareSplitEdge (Pred, &BB, LandingPad, ReplPHI);
2267
- IncomingBB->setName (BB.getName () + Twine (" .from." ) + Pred->getName ());
2298
+ IncomingBB->setName (getBasicBlockLabel (&BB) + Twine (" .from." ) +
2299
+ getBasicBlockLabel (Pred));
2268
2300
2269
2301
// Stop the moving of values at ReplPHI, as this is either null or the PHI
2270
2302
// that replaced the landing pad.
@@ -2708,7 +2740,7 @@ static void eliminateSwiftError(Function &F, coro::Shape &Shape) {
2708
2740
}
2709
2741
}
2710
2742
2711
- // / retcon and retcon.once conventions assume that all spill uses can be sunk
2743
+ // / Async and Retcon{Once} conventions assume that all spill uses can be sunk
2712
2744
// / after the coro.begin intrinsic.
2713
2745
static void sinkSpillUsesAfterCoroBegin (Function &F,
2714
2746
const FrameDataInfo &FrameData,
@@ -3142,7 +3174,7 @@ void coro::buildCoroutineFrame(
3142
3174
cleanupSinglePredPHIs (F);
3143
3175
3144
3176
// Transforms multi-edge PHI Nodes, so that any value feeding into a PHI will
3145
- // never has its definition separated from the PHI by the suspend point.
3177
+ // never have its definition separated from the PHI by the suspend point.
3146
3178
rewritePHIs (F);
3147
3179
3148
3180
// Build suspend crossing info.
@@ -3239,6 +3271,7 @@ void coro::buildCoroutineFrame(
3239
3271
Shape.FramePtr = Shape.CoroBegin ;
3240
3272
// For now, this works for C++ programs only.
3241
3273
buildFrameDebugInfo (F, Shape, FrameData);
3274
+ // Insert spills and reloads
3242
3275
insertSpills (FrameData, Shape);
3243
3276
lowerLocalAllocas (LocalAllocas, DeadInstructions);
3244
3277
0 commit comments