xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Coroutines/CoroFrame.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- CoroFrame.cpp - Builds and manipulates coroutine frame -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // This file contains classes used to discover if for a particular value
9 // its definition precedes and its uses follow a suspend block. This is
10 // referred to as a suspend crossing value.
11 //
12 // Using the information discovered we form a Coroutine Frame structure to
13 // contain those values. All uses of those values are replaced with appropriate
14 // GEP + load from the coroutine frame. At the point of the definition we spill
15 // the value into the coroutine frame.
16 //===----------------------------------------------------------------------===//
17 
18 #include "CoroInternal.h"
19 #include "llvm/ADT/ScopeExit.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Analysis/StackLifetime.h"
22 #include "llvm/IR/DIBuilder.h"
23 #include "llvm/IR/DebugInfo.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/InstIterator.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/OptimizedStructLayout.h"
32 #include "llvm/Transforms/Coroutines/ABI.h"
33 #include "llvm/Transforms/Coroutines/CoroInstr.h"
34 #include "llvm/Transforms/Coroutines/MaterializationUtils.h"
35 #include "llvm/Transforms/Coroutines/SpillUtils.h"
36 #include "llvm/Transforms/Coroutines/SuspendCrossingInfo.h"
37 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
38 #include "llvm/Transforms/Utils/Local.h"
39 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
40 #include <algorithm>
41 #include <optional>
42 
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "coro-frame"
46 
47 namespace {
48 class FrameTypeBuilder;
49 // Mapping from the to-be-spilled value to all the users that need reload.
50 struct FrameDataInfo {
51   // All the values (that are not allocas) that needs to be spilled to the
52   // frame.
53   coro::SpillInfo &Spills;
54   // Allocas contains all values defined as allocas that need to live in the
55   // frame.
56   SmallVectorImpl<coro::AllocaInfo> &Allocas;
57 
FrameDataInfo__anon408e6fe60111::FrameDataInfo58   FrameDataInfo(coro::SpillInfo &Spills,
59                 SmallVectorImpl<coro::AllocaInfo> &Allocas)
60       : Spills(Spills), Allocas(Allocas) {}
61 
getAllDefs__anon408e6fe60111::FrameDataInfo62   SmallVector<Value *, 8> getAllDefs() const {
63     SmallVector<Value *, 8> Defs;
64     for (const auto &P : Spills)
65       Defs.push_back(P.first);
66     for (const auto &A : Allocas)
67       Defs.push_back(A.Alloca);
68     return Defs;
69   }
70 
getFieldIndex__anon408e6fe60111::FrameDataInfo71   uint32_t getFieldIndex(Value *V) const {
72     auto Itr = FieldIndexMap.find(V);
73     assert(Itr != FieldIndexMap.end() &&
74            "Value does not have a frame field index");
75     return Itr->second;
76   }
77 
setFieldIndex__anon408e6fe60111::FrameDataInfo78   void setFieldIndex(Value *V, uint32_t Index) {
79     assert((LayoutIndexUpdateStarted || FieldIndexMap.count(V) == 0) &&
80            "Cannot set the index for the same field twice.");
81     FieldIndexMap[V] = Index;
82   }
83 
getAlign__anon408e6fe60111::FrameDataInfo84   Align getAlign(Value *V) const {
85     auto Iter = FieldAlignMap.find(V);
86     assert(Iter != FieldAlignMap.end());
87     return Iter->second;
88   }
89 
setAlign__anon408e6fe60111::FrameDataInfo90   void setAlign(Value *V, Align AL) {
91     assert(FieldAlignMap.count(V) == 0);
92     FieldAlignMap.insert({V, AL});
93   }
94 
getDynamicAlign__anon408e6fe60111::FrameDataInfo95   uint64_t getDynamicAlign(Value *V) const {
96     auto Iter = FieldDynamicAlignMap.find(V);
97     assert(Iter != FieldDynamicAlignMap.end());
98     return Iter->second;
99   }
100 
setDynamicAlign__anon408e6fe60111::FrameDataInfo101   void setDynamicAlign(Value *V, uint64_t Align) {
102     assert(FieldDynamicAlignMap.count(V) == 0);
103     FieldDynamicAlignMap.insert({V, Align});
104   }
105 
getOffset__anon408e6fe60111::FrameDataInfo106   uint64_t getOffset(Value *V) const {
107     auto Iter = FieldOffsetMap.find(V);
108     assert(Iter != FieldOffsetMap.end());
109     return Iter->second;
110   }
111 
setOffset__anon408e6fe60111::FrameDataInfo112   void setOffset(Value *V, uint64_t Offset) {
113     assert(FieldOffsetMap.count(V) == 0);
114     FieldOffsetMap.insert({V, Offset});
115   }
116 
117   // Remap the index of every field in the frame, using the final layout index.
118   void updateLayoutIndex(FrameTypeBuilder &B);
119 
120 private:
121   // LayoutIndexUpdateStarted is used to avoid updating the index of any field
122   // twice by mistake.
123   bool LayoutIndexUpdateStarted = false;
124   // Map from values to their slot indexes on the frame. They will be first set
125   // with their original insertion field index. After the frame is built, their
126   // indexes will be updated into the final layout index.
127   DenseMap<Value *, uint32_t> FieldIndexMap;
128   // Map from values to their alignment on the frame. They would be set after
129   // the frame is built.
130   DenseMap<Value *, Align> FieldAlignMap;
131   DenseMap<Value *, uint64_t> FieldDynamicAlignMap;
132   // Map from values to their offset on the frame. They would be set after
133   // the frame is built.
134   DenseMap<Value *, uint64_t> FieldOffsetMap;
135 };
136 } // namespace
137 
138 #ifndef NDEBUG
dumpSpills(StringRef Title,const coro::SpillInfo & Spills)139 static void dumpSpills(StringRef Title, const coro::SpillInfo &Spills) {
140   dbgs() << "------------- " << Title << " --------------\n";
141   for (const auto &E : Spills) {
142     E.first->dump();
143     dbgs() << "   user: ";
144     for (auto *I : E.second)
145       I->dump();
146   }
147 }
148 
dumpAllocas(const SmallVectorImpl<coro::AllocaInfo> & Allocas)149 static void dumpAllocas(const SmallVectorImpl<coro::AllocaInfo> &Allocas) {
150   dbgs() << "------------- Allocas --------------\n";
151   for (const auto &A : Allocas) {
152     A.Alloca->dump();
153   }
154 }
155 #endif
156 
157 namespace {
158 using FieldIDType = size_t;
159 // We cannot rely solely on natural alignment of a type when building a
160 // coroutine frame and if the alignment specified on the Alloca instruction
161 // differs from the natural alignment of the alloca type we will need to insert
162 // padding.
163 class FrameTypeBuilder {
164 private:
165   struct Field {
166     uint64_t Size;
167     uint64_t Offset;
168     Type *Ty;
169     FieldIDType LayoutFieldIndex;
170     Align Alignment;
171     Align TyAlignment;
172     uint64_t DynamicAlignBuffer;
173   };
174 
175   const DataLayout &DL;
176   LLVMContext &Context;
177   uint64_t StructSize = 0;
178   Align StructAlign;
179   bool IsFinished = false;
180 
181   std::optional<Align> MaxFrameAlignment;
182 
183   SmallVector<Field, 8> Fields;
184   DenseMap<Value*, unsigned> FieldIndexByKey;
185 
186 public:
FrameTypeBuilder(LLVMContext & Context,const DataLayout & DL,std::optional<Align> MaxFrameAlignment)187   FrameTypeBuilder(LLVMContext &Context, const DataLayout &DL,
188                    std::optional<Align> MaxFrameAlignment)
189       : DL(DL), Context(Context), MaxFrameAlignment(MaxFrameAlignment) {}
190 
191   /// Add a field to this structure for the storage of an `alloca`
192   /// instruction.
addFieldForAlloca(AllocaInst * AI,bool IsHeader=false)193   [[nodiscard]] FieldIDType addFieldForAlloca(AllocaInst *AI,
194                                               bool IsHeader = false) {
195     Type *Ty = AI->getAllocatedType();
196 
197     // Make an array type if this is a static array allocation.
198     if (AI->isArrayAllocation()) {
199       if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
200         Ty = ArrayType::get(Ty, CI->getValue().getZExtValue());
201       else
202         report_fatal_error("Coroutines cannot handle non static allocas yet");
203     }
204 
205     return addField(Ty, AI->getAlign(), IsHeader);
206   }
207 
208   /// We want to put the allocas whose lifetime-ranges are not overlapped
209   /// into one slot of coroutine frame.
210   /// Consider the example at:https://bugs.llvm.org/show_bug.cgi?id=45566
211   ///
212   ///     cppcoro::task<void> alternative_paths(bool cond) {
213   ///         if (cond) {
214   ///             big_structure a;
215   ///             process(a);
216   ///             co_await something();
217   ///         } else {
218   ///             big_structure b;
219   ///             process2(b);
220   ///             co_await something();
221   ///         }
222   ///     }
223   ///
224   /// We want to put variable a and variable b in the same slot to
225   /// reduce the size of coroutine frame.
226   ///
227   /// This function use StackLifetime algorithm to partition the AllocaInsts in
228   /// Spills to non-overlapped sets in order to put Alloca in the same
229   /// non-overlapped set into the same slot in the Coroutine Frame. Then add
230   /// field for the allocas in the same non-overlapped set by using the largest
231   /// type as the field type.
232   ///
233   /// Side Effects: Because We sort the allocas, the order of allocas in the
234   /// frame may be different with the order in the source code.
235   void addFieldForAllocas(const Function &F, FrameDataInfo &FrameData,
236                           coro::Shape &Shape, bool OptimizeFrame);
237 
238   /// Add a field to this structure.
addField(Type * Ty,MaybeAlign MaybeFieldAlignment,bool IsHeader=false,bool IsSpillOfValue=false)239   [[nodiscard]] FieldIDType addField(Type *Ty, MaybeAlign MaybeFieldAlignment,
240                                      bool IsHeader = false,
241                                      bool IsSpillOfValue = false) {
242     assert(!IsFinished && "adding fields to a finished builder");
243     assert(Ty && "must provide a type for a field");
244 
245     // The field size is always the alloc size of the type.
246     uint64_t FieldSize = DL.getTypeAllocSize(Ty);
247 
248     // For an alloca with size=0, we don't need to add a field and they
249     // can just point to any index in the frame. Use index 0.
250     if (FieldSize == 0) {
251       return 0;
252     }
253 
254     // The field alignment might not be the type alignment, but we need
255     // to remember the type alignment anyway to build the type.
256     // If we are spilling values we don't need to worry about ABI alignment
257     // concerns.
258     Align ABIAlign = DL.getABITypeAlign(Ty);
259     Align TyAlignment = ABIAlign;
260     if (IsSpillOfValue && MaxFrameAlignment && *MaxFrameAlignment < ABIAlign)
261       TyAlignment = *MaxFrameAlignment;
262     Align FieldAlignment = MaybeFieldAlignment.value_or(TyAlignment);
263 
264     // The field alignment could be bigger than the max frame case, in that case
265     // we request additional storage to be able to dynamically align the
266     // pointer.
267     uint64_t DynamicAlignBuffer = 0;
268     if (MaxFrameAlignment && (FieldAlignment > *MaxFrameAlignment)) {
269       DynamicAlignBuffer =
270           offsetToAlignment(MaxFrameAlignment->value(), FieldAlignment);
271       FieldAlignment = *MaxFrameAlignment;
272       FieldSize = FieldSize + DynamicAlignBuffer;
273     }
274 
275     // Lay out header fields immediately.
276     uint64_t Offset;
277     if (IsHeader) {
278       Offset = alignTo(StructSize, FieldAlignment);
279       StructSize = Offset + FieldSize;
280 
281       // Everything else has a flexible offset.
282     } else {
283       Offset = OptimizedStructLayoutField::FlexibleOffset;
284     }
285 
286     Fields.push_back({FieldSize, Offset, Ty, 0, FieldAlignment, TyAlignment,
287                       DynamicAlignBuffer});
288     return Fields.size() - 1;
289   }
290 
291   /// Finish the layout and create the struct type with the given name.
292   StructType *finish(StringRef Name);
293 
getStructSize() const294   uint64_t getStructSize() const {
295     assert(IsFinished && "not yet finished!");
296     return StructSize;
297   }
298 
getStructAlign() const299   Align getStructAlign() const {
300     assert(IsFinished && "not yet finished!");
301     return StructAlign;
302   }
303 
getLayoutFieldIndex(FieldIDType Id) const304   FieldIDType getLayoutFieldIndex(FieldIDType Id) const {
305     assert(IsFinished && "not yet finished!");
306     return Fields[Id].LayoutFieldIndex;
307   }
308 
getLayoutField(FieldIDType Id) const309   Field getLayoutField(FieldIDType Id) const {
310     assert(IsFinished && "not yet finished!");
311     return Fields[Id];
312   }
313 };
314 } // namespace
315 
updateLayoutIndex(FrameTypeBuilder & B)316 void FrameDataInfo::updateLayoutIndex(FrameTypeBuilder &B) {
317   auto Updater = [&](Value *I) {
318     auto Field = B.getLayoutField(getFieldIndex(I));
319     setFieldIndex(I, Field.LayoutFieldIndex);
320     setAlign(I, Field.Alignment);
321     uint64_t dynamicAlign =
322         Field.DynamicAlignBuffer
323             ? Field.DynamicAlignBuffer + Field.Alignment.value()
324             : 0;
325     setDynamicAlign(I, dynamicAlign);
326     setOffset(I, Field.Offset);
327   };
328   LayoutIndexUpdateStarted = true;
329   for (auto &S : Spills)
330     Updater(S.first);
331   for (const auto &A : Allocas)
332     Updater(A.Alloca);
333   LayoutIndexUpdateStarted = false;
334 }
335 
addFieldForAllocas(const Function & F,FrameDataInfo & FrameData,coro::Shape & Shape,bool OptimizeFrame)336 void FrameTypeBuilder::addFieldForAllocas(const Function &F,
337                                           FrameDataInfo &FrameData,
338                                           coro::Shape &Shape,
339                                           bool OptimizeFrame) {
340   using AllocaSetType = SmallVector<AllocaInst *, 4>;
341   SmallVector<AllocaSetType, 4> NonOverlapedAllocas;
342 
343   // We need to add field for allocas at the end of this function.
344   auto AddFieldForAllocasAtExit = make_scope_exit([&]() {
345     for (auto AllocaList : NonOverlapedAllocas) {
346       auto *LargestAI = *AllocaList.begin();
347       FieldIDType Id = addFieldForAlloca(LargestAI);
348       for (auto *Alloca : AllocaList)
349         FrameData.setFieldIndex(Alloca, Id);
350     }
351   });
352 
353   if (!OptimizeFrame) {
354     for (const auto &A : FrameData.Allocas) {
355       AllocaInst *Alloca = A.Alloca;
356       NonOverlapedAllocas.emplace_back(AllocaSetType(1, Alloca));
357     }
358     return;
359   }
360 
361   // Because there are paths from the lifetime.start to coro.end
362   // for each alloca, the liferanges for every alloca is overlaped
363   // in the blocks who contain coro.end and the successor blocks.
364   // So we choose to skip there blocks when we calculate the liferange
365   // for each alloca. It should be reasonable since there shouldn't be uses
366   // in these blocks and the coroutine frame shouldn't be used outside the
367   // coroutine body.
368   //
369   // Note that the user of coro.suspend may not be SwitchInst. However, this
370   // case seems too complex to handle. And it is harmless to skip these
371   // patterns since it just prevend putting the allocas to live in the same
372   // slot.
373   DenseMap<SwitchInst *, BasicBlock *> DefaultSuspendDest;
374   for (auto *CoroSuspendInst : Shape.CoroSuspends) {
375     for (auto *U : CoroSuspendInst->users()) {
376       if (auto *ConstSWI = dyn_cast<SwitchInst>(U)) {
377         auto *SWI = const_cast<SwitchInst *>(ConstSWI);
378         DefaultSuspendDest[SWI] = SWI->getDefaultDest();
379         SWI->setDefaultDest(SWI->getSuccessor(1));
380       }
381     }
382   }
383 
384   auto ExtractAllocas = [&]() {
385     AllocaSetType Allocas;
386     Allocas.reserve(FrameData.Allocas.size());
387     for (const auto &A : FrameData.Allocas)
388       Allocas.push_back(A.Alloca);
389     return Allocas;
390   };
391   StackLifetime StackLifetimeAnalyzer(F, ExtractAllocas(),
392                                       StackLifetime::LivenessType::May);
393   StackLifetimeAnalyzer.run();
394   auto DoAllocasInterfere = [&](const AllocaInst *AI1, const AllocaInst *AI2) {
395     return StackLifetimeAnalyzer.getLiveRange(AI1).overlaps(
396         StackLifetimeAnalyzer.getLiveRange(AI2));
397   };
398   auto GetAllocaSize = [&](const coro::AllocaInfo &A) {
399     std::optional<TypeSize> RetSize = A.Alloca->getAllocationSize(DL);
400     assert(RetSize && "Variable Length Arrays (VLA) are not supported.\n");
401     assert(!RetSize->isScalable() && "Scalable vectors are not yet supported");
402     return RetSize->getFixedValue();
403   };
404   // Put larger allocas in the front. So the larger allocas have higher
405   // priority to merge, which can save more space potentially. Also each
406   // AllocaSet would be ordered. So we can get the largest Alloca in one
407   // AllocaSet easily.
408   sort(FrameData.Allocas, [&](const auto &Iter1, const auto &Iter2) {
409     return GetAllocaSize(Iter1) > GetAllocaSize(Iter2);
410   });
411   for (const auto &A : FrameData.Allocas) {
412     AllocaInst *Alloca = A.Alloca;
413     bool Merged = false;
414     // Try to find if the Alloca does not interfere with any existing
415     // NonOverlappedAllocaSet. If it is true, insert the alloca to that
416     // NonOverlappedAllocaSet.
417     for (auto &AllocaSet : NonOverlapedAllocas) {
418       assert(!AllocaSet.empty() && "Processing Alloca Set is not empty.\n");
419       bool NoInterference = none_of(AllocaSet, [&](auto Iter) {
420         return DoAllocasInterfere(Alloca, Iter);
421       });
422       // If the alignment of A is multiple of the alignment of B, the address
423       // of A should satisfy the requirement for aligning for B.
424       //
425       // There may be other more fine-grained strategies to handle the alignment
426       // infomation during the merging process. But it seems hard to handle
427       // these strategies and benefit little.
428       bool Alignable = [&]() -> bool {
429         auto *LargestAlloca = *AllocaSet.begin();
430         return LargestAlloca->getAlign().value() % Alloca->getAlign().value() ==
431                0;
432       }();
433       bool CouldMerge = NoInterference && Alignable;
434       if (!CouldMerge)
435         continue;
436       AllocaSet.push_back(Alloca);
437       Merged = true;
438       break;
439     }
440     if (!Merged) {
441       NonOverlapedAllocas.emplace_back(AllocaSetType(1, Alloca));
442     }
443   }
444   // Recover the default target destination for each Switch statement
445   // reserved.
446   for (auto SwitchAndDefaultDest : DefaultSuspendDest) {
447     SwitchInst *SWI = SwitchAndDefaultDest.first;
448     BasicBlock *DestBB = SwitchAndDefaultDest.second;
449     SWI->setDefaultDest(DestBB);
450   }
451   // This Debug Info could tell us which allocas are merged into one slot.
452   LLVM_DEBUG(for (auto &AllocaSet
453                   : NonOverlapedAllocas) {
454     if (AllocaSet.size() > 1) {
455       dbgs() << "In Function:" << F.getName() << "\n";
456       dbgs() << "Find Union Set "
457              << "\n";
458       dbgs() << "\tAllocas are \n";
459       for (auto Alloca : AllocaSet)
460         dbgs() << "\t\t" << *Alloca << "\n";
461     }
462   });
463 }
464 
finish(StringRef Name)465 StructType *FrameTypeBuilder::finish(StringRef Name) {
466   assert(!IsFinished && "already finished!");
467 
468   // Prepare the optimal-layout field array.
469   // The Id in the layout field is a pointer to our Field for it.
470   SmallVector<OptimizedStructLayoutField, 8> LayoutFields;
471   LayoutFields.reserve(Fields.size());
472   for (auto &Field : Fields) {
473     LayoutFields.emplace_back(&Field, Field.Size, Field.Alignment,
474                               Field.Offset);
475   }
476 
477   // Perform layout.
478   auto SizeAndAlign = performOptimizedStructLayout(LayoutFields);
479   StructSize = SizeAndAlign.first;
480   StructAlign = SizeAndAlign.second;
481 
482   auto getField = [](const OptimizedStructLayoutField &LayoutField) -> Field & {
483     return *static_cast<Field *>(const_cast<void*>(LayoutField.Id));
484   };
485 
486   // We need to produce a packed struct type if there's a field whose
487   // assigned offset isn't a multiple of its natural type alignment.
488   bool Packed = [&] {
489     for (auto &LayoutField : LayoutFields) {
490       auto &F = getField(LayoutField);
491       if (!isAligned(F.TyAlignment, LayoutField.Offset))
492         return true;
493     }
494     return false;
495   }();
496 
497   // Build the struct body.
498   SmallVector<Type*, 16> FieldTypes;
499   FieldTypes.reserve(LayoutFields.size() * 3 / 2);
500   uint64_t LastOffset = 0;
501   for (auto &LayoutField : LayoutFields) {
502     auto &F = getField(LayoutField);
503 
504     auto Offset = LayoutField.Offset;
505 
506     // Add a padding field if there's a padding gap and we're either
507     // building a packed struct or the padding gap is more than we'd
508     // get from aligning to the field type's natural alignment.
509     assert(Offset >= LastOffset);
510     if (Offset != LastOffset) {
511       if (Packed || alignTo(LastOffset, F.TyAlignment) != Offset)
512         FieldTypes.push_back(ArrayType::get(Type::getInt8Ty(Context),
513                                             Offset - LastOffset));
514     }
515 
516     F.Offset = Offset;
517     F.LayoutFieldIndex = FieldTypes.size();
518 
519     FieldTypes.push_back(F.Ty);
520     if (F.DynamicAlignBuffer) {
521       FieldTypes.push_back(
522           ArrayType::get(Type::getInt8Ty(Context), F.DynamicAlignBuffer));
523     }
524     LastOffset = Offset + F.Size;
525   }
526 
527   StructType *Ty = StructType::create(Context, FieldTypes, Name, Packed);
528 
529 #ifndef NDEBUG
530   // Check that the IR layout matches the offsets we expect.
531   auto Layout = DL.getStructLayout(Ty);
532   for (auto &F : Fields) {
533     assert(Ty->getElementType(F.LayoutFieldIndex) == F.Ty);
534     assert(Layout->getElementOffset(F.LayoutFieldIndex) == F.Offset);
535   }
536 #endif
537 
538   IsFinished = true;
539 
540   return Ty;
541 }
542 
cacheDIVar(FrameDataInfo & FrameData,DenseMap<Value *,DILocalVariable * > & DIVarCache)543 static void cacheDIVar(FrameDataInfo &FrameData,
544                        DenseMap<Value *, DILocalVariable *> &DIVarCache) {
545   for (auto *V : FrameData.getAllDefs()) {
546     if (DIVarCache.contains(V))
547       continue;
548 
549     auto CacheIt = [&DIVarCache, V](const auto &Container) {
550       auto *I = llvm::find_if(Container, [](auto *DDI) {
551         return DDI->getExpression()->getNumElements() == 0;
552       });
553       if (I != Container.end())
554         DIVarCache.insert({V, (*I)->getVariable()});
555     };
556     CacheIt(findDbgDeclares(V));
557     CacheIt(findDVRDeclares(V));
558   }
559 }
560 
561 /// Create name for Type. It uses MDString to store new created string to
562 /// avoid memory leak.
solveTypeName(Type * Ty)563 static StringRef solveTypeName(Type *Ty) {
564   if (Ty->isIntegerTy()) {
565     // The longest name in common may be '__int_128', which has 9 bits.
566     SmallString<16> Buffer;
567     raw_svector_ostream OS(Buffer);
568     OS << "__int_" << cast<IntegerType>(Ty)->getBitWidth();
569     auto *MDName = MDString::get(Ty->getContext(), OS.str());
570     return MDName->getString();
571   }
572 
573   if (Ty->isFloatingPointTy()) {
574     if (Ty->isFloatTy())
575       return "__float_";
576     if (Ty->isDoubleTy())
577       return "__double_";
578     return "__floating_type_";
579   }
580 
581   if (Ty->isPointerTy())
582     return "PointerType";
583 
584   if (Ty->isStructTy()) {
585     if (!cast<StructType>(Ty)->hasName())
586       return "__LiteralStructType_";
587 
588     auto Name = Ty->getStructName();
589 
590     SmallString<16> Buffer(Name);
591     for (auto &Iter : Buffer)
592       if (Iter == '.' || Iter == ':')
593         Iter = '_';
594     auto *MDName = MDString::get(Ty->getContext(), Buffer.str());
595     return MDName->getString();
596   }
597 
598   return "UnknownType";
599 }
600 
solveDIType(DIBuilder & Builder,Type * Ty,const DataLayout & Layout,DIScope * Scope,unsigned LineNum,DenseMap<Type *,DIType * > & DITypeCache)601 static DIType *solveDIType(DIBuilder &Builder, Type *Ty,
602                            const DataLayout &Layout, DIScope *Scope,
603                            unsigned LineNum,
604                            DenseMap<Type *, DIType *> &DITypeCache) {
605   if (DIType *DT = DITypeCache.lookup(Ty))
606     return DT;
607 
608   StringRef Name = solveTypeName(Ty);
609 
610   DIType *RetType = nullptr;
611 
612   if (Ty->isIntegerTy()) {
613     auto BitWidth = cast<IntegerType>(Ty)->getBitWidth();
614     RetType = Builder.createBasicType(Name, BitWidth, dwarf::DW_ATE_signed,
615                                       llvm::DINode::FlagArtificial);
616   } else if (Ty->isFloatingPointTy()) {
617     RetType = Builder.createBasicType(Name, Layout.getTypeSizeInBits(Ty),
618                                       dwarf::DW_ATE_float,
619                                       llvm::DINode::FlagArtificial);
620   } else if (Ty->isPointerTy()) {
621     // Construct PointerType points to null (aka void *) instead of exploring
622     // pointee type to avoid infinite search problem. For example, we would be
623     // in trouble if we traverse recursively:
624     //
625     //  struct Node {
626     //      Node* ptr;
627     //  };
628     RetType =
629         Builder.createPointerType(nullptr, Layout.getTypeSizeInBits(Ty),
630                                   Layout.getABITypeAlign(Ty).value() * CHAR_BIT,
631                                   /*DWARFAddressSpace=*/std::nullopt, Name);
632   } else if (Ty->isStructTy()) {
633     auto *DIStruct = Builder.createStructType(
634         Scope, Name, Scope->getFile(), LineNum, Layout.getTypeSizeInBits(Ty),
635         Layout.getPrefTypeAlign(Ty).value() * CHAR_BIT,
636         llvm::DINode::FlagArtificial, nullptr, llvm::DINodeArray());
637 
638     auto *StructTy = cast<StructType>(Ty);
639     SmallVector<Metadata *, 16> Elements;
640     for (unsigned I = 0; I < StructTy->getNumElements(); I++) {
641       DIType *DITy = solveDIType(Builder, StructTy->getElementType(I), Layout,
642                                  DIStruct, LineNum, DITypeCache);
643       assert(DITy);
644       Elements.push_back(Builder.createMemberType(
645           DIStruct, DITy->getName(), DIStruct->getFile(), LineNum,
646           DITy->getSizeInBits(), DITy->getAlignInBits(),
647           Layout.getStructLayout(StructTy)->getElementOffsetInBits(I),
648           llvm::DINode::FlagArtificial, DITy));
649     }
650 
651     Builder.replaceArrays(DIStruct, Builder.getOrCreateArray(Elements));
652 
653     RetType = DIStruct;
654   } else {
655     LLVM_DEBUG(dbgs() << "Unresolved Type: " << *Ty << "\n");
656     TypeSize Size = Layout.getTypeSizeInBits(Ty);
657     auto *CharSizeType = Builder.createBasicType(
658         Name, 8, dwarf::DW_ATE_unsigned_char, llvm::DINode::FlagArtificial);
659 
660     if (Size <= 8)
661       RetType = CharSizeType;
662     else {
663       if (Size % 8 != 0)
664         Size = TypeSize::getFixed(Size + 8 - (Size % 8));
665 
666       RetType = Builder.createArrayType(
667           Size, Layout.getPrefTypeAlign(Ty).value(), CharSizeType,
668           Builder.getOrCreateArray(Builder.getOrCreateSubrange(0, Size / 8)));
669     }
670   }
671 
672   DITypeCache.insert({Ty, RetType});
673   return RetType;
674 }
675 
676 /// Build artificial debug info for C++ coroutine frames to allow users to
677 /// inspect the contents of the frame directly
678 ///
679 /// Create Debug information for coroutine frame with debug name "__coro_frame".
680 /// The debug information for the fields of coroutine frame is constructed from
681 /// the following way:
682 /// 1. For all the value in the Frame, we search the use of dbg.declare to find
683 ///    the corresponding debug variables for the value. If we can find the
684 ///    debug variable, we can get full and accurate debug information.
685 /// 2. If we can't get debug information in step 1 and 2, we could only try to
686 ///    build the DIType by Type. We did this in solveDIType. We only handle
687 ///    integer, float, double, integer type and struct type for now.
buildFrameDebugInfo(Function & F,coro::Shape & Shape,FrameDataInfo & FrameData)688 static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
689                                 FrameDataInfo &FrameData) {
690   DISubprogram *DIS = F.getSubprogram();
691   // If there is no DISubprogram for F, it implies the function is compiled
692   // without debug info. So we also don't generate debug info for the frame.
693   if (!DIS || !DIS->getUnit() ||
694       !dwarf::isCPlusPlus(
695           (dwarf::SourceLanguage)DIS->getUnit()->getSourceLanguage()) ||
696       DIS->getUnit()->getEmissionKind() != DICompileUnit::DebugEmissionKind::FullDebug)
697     return;
698 
699   assert(Shape.ABI == coro::ABI::Switch &&
700          "We could only build debug infomation for C++ coroutine now.\n");
701 
702   DIBuilder DBuilder(*F.getParent(), /*AllowUnresolved*/ false);
703 
704   assert(Shape.getPromiseAlloca() &&
705          "Coroutine with switch ABI should own Promise alloca");
706 
707   DIFile *DFile = DIS->getFile();
708   unsigned LineNum = DIS->getLine();
709 
710   DICompositeType *FrameDITy = DBuilder.createStructType(
711       DIS->getUnit(), Twine(F.getName() + ".coro_frame_ty").str(),
712       DFile, LineNum, Shape.FrameSize * 8,
713       Shape.FrameAlign.value() * 8, llvm::DINode::FlagArtificial, nullptr,
714       llvm::DINodeArray());
715   StructType *FrameTy = Shape.FrameTy;
716   SmallVector<Metadata *, 16> Elements;
717   DataLayout Layout = F.getDataLayout();
718 
719   DenseMap<Value *, DILocalVariable *> DIVarCache;
720   cacheDIVar(FrameData, DIVarCache);
721 
722   unsigned ResumeIndex = coro::Shape::SwitchFieldIndex::Resume;
723   unsigned DestroyIndex = coro::Shape::SwitchFieldIndex::Destroy;
724   unsigned IndexIndex = Shape.SwitchLowering.IndexField;
725 
726   DenseMap<unsigned, StringRef> NameCache;
727   NameCache.insert({ResumeIndex, "__resume_fn"});
728   NameCache.insert({DestroyIndex, "__destroy_fn"});
729   NameCache.insert({IndexIndex, "__coro_index"});
730 
731   Type *ResumeFnTy = FrameTy->getElementType(ResumeIndex),
732        *DestroyFnTy = FrameTy->getElementType(DestroyIndex),
733        *IndexTy = FrameTy->getElementType(IndexIndex);
734 
735   DenseMap<unsigned, DIType *> TyCache;
736   TyCache.insert(
737       {ResumeIndex, DBuilder.createPointerType(
738                         nullptr, Layout.getTypeSizeInBits(ResumeFnTy))});
739   TyCache.insert(
740       {DestroyIndex, DBuilder.createPointerType(
741                          nullptr, Layout.getTypeSizeInBits(DestroyFnTy))});
742 
743   /// FIXME: If we fill the field `SizeInBits` with the actual size of
744   /// __coro_index in bits, then __coro_index wouldn't show in the debugger.
745   TyCache.insert({IndexIndex, DBuilder.createBasicType(
746                                   "__coro_index",
747                                   (Layout.getTypeSizeInBits(IndexTy) < 8)
748                                       ? 8
749                                       : Layout.getTypeSizeInBits(IndexTy),
750                                   dwarf::DW_ATE_unsigned_char)});
751 
752   for (auto *V : FrameData.getAllDefs()) {
753     auto It = DIVarCache.find(V);
754     if (It == DIVarCache.end())
755       continue;
756 
757     auto Index = FrameData.getFieldIndex(V);
758 
759     NameCache.insert({Index, It->second->getName()});
760     TyCache.insert({Index, It->second->getType()});
761   }
762 
763   // Cache from index to (Align, Offset Pair)
764   DenseMap<unsigned, std::pair<unsigned, unsigned>> OffsetCache;
765   // The Align and Offset of Resume function and Destroy function are fixed.
766   OffsetCache.insert({ResumeIndex, {8, 0}});
767   OffsetCache.insert({DestroyIndex, {8, 8}});
768   OffsetCache.insert(
769       {IndexIndex,
770        {Shape.SwitchLowering.IndexAlign, Shape.SwitchLowering.IndexOffset}});
771 
772   for (auto *V : FrameData.getAllDefs()) {
773     auto Index = FrameData.getFieldIndex(V);
774 
775     OffsetCache.insert(
776         {Index, {FrameData.getAlign(V).value(), FrameData.getOffset(V)}});
777   }
778 
779   DenseMap<Type *, DIType *> DITypeCache;
780   // This counter is used to avoid same type names. e.g., there would be
781   // many i32 and i64 types in one coroutine. And we would use i32_0 and
782   // i32_1 to avoid the same type. Since it makes no sense the name of the
783   // fields confilicts with each other.
784   unsigned UnknownTypeNum = 0;
785   for (unsigned Index = 0; Index < FrameTy->getNumElements(); Index++) {
786     auto OCIt = OffsetCache.find(Index);
787     if (OCIt == OffsetCache.end())
788       continue;
789 
790     std::string Name;
791     uint64_t SizeInBits;
792     uint32_t AlignInBits;
793     uint64_t OffsetInBits;
794     DIType *DITy = nullptr;
795 
796     Type *Ty = FrameTy->getElementType(Index);
797     assert(Ty->isSized() && "We can't handle type which is not sized.\n");
798     SizeInBits = Layout.getTypeSizeInBits(Ty).getFixedValue();
799     AlignInBits = OCIt->second.first * 8;
800     OffsetInBits = OCIt->second.second * 8;
801 
802     if (auto It = NameCache.find(Index); It != NameCache.end()) {
803       Name = It->second.str();
804       DITy = TyCache[Index];
805     } else {
806       DITy = solveDIType(DBuilder, Ty, Layout, FrameDITy, LineNum, DITypeCache);
807       assert(DITy && "SolveDIType shouldn't return nullptr.\n");
808       Name = DITy->getName().str();
809       Name += "_" + std::to_string(UnknownTypeNum);
810       UnknownTypeNum++;
811     }
812 
813     Elements.push_back(DBuilder.createMemberType(
814         FrameDITy, Name, DFile, LineNum, SizeInBits, AlignInBits, OffsetInBits,
815         llvm::DINode::FlagArtificial, DITy));
816   }
817 
818   DBuilder.replaceArrays(FrameDITy, DBuilder.getOrCreateArray(Elements));
819 
820   auto *FrameDIVar =
821       DBuilder.createAutoVariable(DIS, "__coro_frame", DFile, LineNum,
822                                   FrameDITy, true, DINode::FlagArtificial);
823 
824   // Subprogram would have ContainedNodes field which records the debug
825   // variables it contained. So we need to add __coro_frame to the
826   // ContainedNodes of it.
827   //
828   // If we don't add __coro_frame to the RetainedNodes, user may get
829   // `no symbol __coro_frame in context` rather than `__coro_frame`
830   // is optimized out, which is more precise.
831   auto RetainedNodes = DIS->getRetainedNodes();
832   SmallVector<Metadata *, 32> RetainedNodesVec(RetainedNodes.begin(),
833                                                RetainedNodes.end());
834   RetainedNodesVec.push_back(FrameDIVar);
835   DIS->replaceOperandWith(7, (MDTuple::get(F.getContext(), RetainedNodesVec)));
836 
837   // Construct the location for the frame debug variable. The column number
838   // is fake but it should be fine.
839   DILocation *DILoc =
840       DILocation::get(DIS->getContext(), LineNum, /*Column=*/1, DIS);
841   assert(FrameDIVar->isValidLocationForIntrinsic(DILoc));
842 
843   DbgVariableRecord *NewDVR =
844       new DbgVariableRecord(ValueAsMetadata::get(Shape.FramePtr), FrameDIVar,
845                             DBuilder.createExpression(), DILoc,
846                             DbgVariableRecord::LocationType::Declare);
847   BasicBlock::iterator It = Shape.getInsertPtAfterFramePtr();
848   It->getParent()->insertDbgRecordBefore(NewDVR, It);
849 }
850 
851 // Build a struct that will keep state for an active coroutine.
852 //   struct f.frame {
853 //     ResumeFnTy ResumeFnAddr;
854 //     ResumeFnTy DestroyFnAddr;
855 //     ... promise (if present) ...
856 //     int ResumeIndex;
857 //     ... spills ...
858 //   };
buildFrameType(Function & F,coro::Shape & Shape,FrameDataInfo & FrameData,bool OptimizeFrame)859 static StructType *buildFrameType(Function &F, coro::Shape &Shape,
860                                   FrameDataInfo &FrameData,
861                                   bool OptimizeFrame) {
862   LLVMContext &C = F.getContext();
863   const DataLayout &DL = F.getDataLayout();
864 
865   // We will use this value to cap the alignment of spilled values.
866   std::optional<Align> MaxFrameAlignment;
867   if (Shape.ABI == coro::ABI::Async)
868     MaxFrameAlignment = Shape.AsyncLowering.getContextAlignment();
869   FrameTypeBuilder B(C, DL, MaxFrameAlignment);
870 
871   AllocaInst *PromiseAlloca = Shape.getPromiseAlloca();
872   std::optional<FieldIDType> SwitchIndexFieldId;
873 
874   if (Shape.ABI == coro::ABI::Switch) {
875     auto *FnPtrTy = PointerType::getUnqual(C);
876 
877     // Add header fields for the resume and destroy functions.
878     // We can rely on these being perfectly packed.
879     (void)B.addField(FnPtrTy, std::nullopt, /*header*/ true);
880     (void)B.addField(FnPtrTy, std::nullopt, /*header*/ true);
881 
882     // PromiseAlloca field needs to be explicitly added here because it's
883     // a header field with a fixed offset based on its alignment. Hence it
884     // needs special handling and cannot be added to FrameData.Allocas.
885     if (PromiseAlloca)
886       FrameData.setFieldIndex(
887           PromiseAlloca, B.addFieldForAlloca(PromiseAlloca, /*header*/ true));
888 
889     // Add a field to store the suspend index.  This doesn't need to
890     // be in the header.
891     unsigned IndexBits = std::max(1U, Log2_64_Ceil(Shape.CoroSuspends.size()));
892     Type *IndexType = Type::getIntNTy(C, IndexBits);
893 
894     SwitchIndexFieldId = B.addField(IndexType, std::nullopt);
895   } else {
896     assert(PromiseAlloca == nullptr && "lowering doesn't support promises");
897   }
898 
899   // Because multiple allocas may own the same field slot,
900   // we add allocas to field here.
901   B.addFieldForAllocas(F, FrameData, Shape, OptimizeFrame);
902   // Add PromiseAlloca to Allocas list so that
903   // 1. updateLayoutIndex could update its index after
904   // `performOptimizedStructLayout`
905   // 2. it is processed in insertSpills.
906   if (Shape.ABI == coro::ABI::Switch && PromiseAlloca)
907     // We assume that the promise alloca won't be modified before
908     // CoroBegin and no alias will be create before CoroBegin.
909     FrameData.Allocas.emplace_back(
910         PromiseAlloca, DenseMap<Instruction *, std::optional<APInt>>{}, false);
911   // Create an entry for every spilled value.
912   for (auto &S : FrameData.Spills) {
913     Type *FieldType = S.first->getType();
914     // For byval arguments, we need to store the pointed value in the frame,
915     // instead of the pointer itself.
916     if (const Argument *A = dyn_cast<Argument>(S.first))
917       if (A->hasByValAttr())
918         FieldType = A->getParamByValType();
919     FieldIDType Id = B.addField(FieldType, std::nullopt, false /*header*/,
920                                 true /*IsSpillOfValue*/);
921     FrameData.setFieldIndex(S.first, Id);
922   }
923 
924   StructType *FrameTy = [&] {
925     SmallString<32> Name(F.getName());
926     Name.append(".Frame");
927     return B.finish(Name);
928   }();
929 
930   FrameData.updateLayoutIndex(B);
931   Shape.FrameAlign = B.getStructAlign();
932   Shape.FrameSize = B.getStructSize();
933 
934   switch (Shape.ABI) {
935   case coro::ABI::Switch: {
936     // In the switch ABI, remember the switch-index field.
937     auto IndexField = B.getLayoutField(*SwitchIndexFieldId);
938     Shape.SwitchLowering.IndexField = IndexField.LayoutFieldIndex;
939     Shape.SwitchLowering.IndexAlign = IndexField.Alignment.value();
940     Shape.SwitchLowering.IndexOffset = IndexField.Offset;
941 
942     // Also round the frame size up to a multiple of its alignment, as is
943     // generally expected in C/C++.
944     Shape.FrameSize = alignTo(Shape.FrameSize, Shape.FrameAlign);
945     break;
946   }
947 
948   // In the retcon ABI, remember whether the frame is inline in the storage.
949   case coro::ABI::Retcon:
950   case coro::ABI::RetconOnce: {
951     auto Id = Shape.getRetconCoroId();
952     Shape.RetconLowering.IsFrameInlineInStorage
953       = (B.getStructSize() <= Id->getStorageSize() &&
954          B.getStructAlign() <= Id->getStorageAlignment());
955     break;
956   }
957   case coro::ABI::Async: {
958     Shape.AsyncLowering.FrameOffset =
959         alignTo(Shape.AsyncLowering.ContextHeaderSize, Shape.FrameAlign);
960     // Also make the final context size a multiple of the context alignment to
961     // make allocation easier for allocators.
962     Shape.AsyncLowering.ContextSize =
963         alignTo(Shape.AsyncLowering.FrameOffset + Shape.FrameSize,
964                 Shape.AsyncLowering.getContextAlignment());
965     if (Shape.AsyncLowering.getContextAlignment() < Shape.FrameAlign) {
966       report_fatal_error(
967           "The alignment requirment of frame variables cannot be higher than "
968           "the alignment of the async function context");
969     }
970     break;
971   }
972   }
973 
974   return FrameTy;
975 }
976 
977 // Replace all alloca and SSA values that are accessed across suspend points
978 // with GetElementPointer from coroutine frame + loads and stores. Create an
979 // AllocaSpillBB that will become the new entry block for the resume parts of
980 // the coroutine:
981 //
982 //    %hdl = coro.begin(...)
983 //    whatever
984 //
985 // becomes:
986 //
987 //    %hdl = coro.begin(...)
988 //    br label %AllocaSpillBB
989 //
990 //  AllocaSpillBB:
991 //    ; geps corresponding to allocas that were moved to coroutine frame
992 //    br label PostSpill
993 //
994 //  PostSpill:
995 //    whatever
996 //
997 //
insertSpills(const FrameDataInfo & FrameData,coro::Shape & Shape)998 static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
999   LLVMContext &C = Shape.CoroBegin->getContext();
1000   Function *F = Shape.CoroBegin->getFunction();
1001   IRBuilder<> Builder(C);
1002   StructType *FrameTy = Shape.FrameTy;
1003   Value *FramePtr = Shape.FramePtr;
1004   DominatorTree DT(*F);
1005   SmallDenseMap<Argument *, AllocaInst *, 4> ArgToAllocaMap;
1006 
1007   // Create a GEP with the given index into the coroutine frame for the original
1008   // value Orig. Appends an extra 0 index for array-allocas, preserving the
1009   // original type.
1010   auto GetFramePointer = [&](Value *Orig) -> Value * {
1011     FieldIDType Index = FrameData.getFieldIndex(Orig);
1012     SmallVector<Value *, 3> Indices = {
1013         ConstantInt::get(Type::getInt32Ty(C), 0),
1014         ConstantInt::get(Type::getInt32Ty(C), Index),
1015     };
1016 
1017     if (auto *AI = dyn_cast<AllocaInst>(Orig)) {
1018       if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
1019         auto Count = CI->getValue().getZExtValue();
1020         if (Count > 1) {
1021           Indices.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
1022         }
1023       } else {
1024         report_fatal_error("Coroutines cannot handle non static allocas yet");
1025       }
1026     }
1027 
1028     auto GEP = cast<GetElementPtrInst>(
1029         Builder.CreateInBoundsGEP(FrameTy, FramePtr, Indices));
1030     if (auto *AI = dyn_cast<AllocaInst>(Orig)) {
1031       if (FrameData.getDynamicAlign(Orig) != 0) {
1032         assert(FrameData.getDynamicAlign(Orig) == AI->getAlign().value());
1033         auto *M = AI->getModule();
1034         auto *IntPtrTy = M->getDataLayout().getIntPtrType(AI->getType());
1035         auto *PtrValue = Builder.CreatePtrToInt(GEP, IntPtrTy);
1036         auto *AlignMask =
1037             ConstantInt::get(IntPtrTy, AI->getAlign().value() - 1);
1038         PtrValue = Builder.CreateAdd(PtrValue, AlignMask);
1039         PtrValue = Builder.CreateAnd(PtrValue, Builder.CreateNot(AlignMask));
1040         return Builder.CreateIntToPtr(PtrValue, AI->getType());
1041       }
1042       // If the type of GEP is not equal to the type of AllocaInst, it implies
1043       // that the AllocaInst may be reused in the Frame slot of other
1044       // AllocaInst. So We cast GEP to the AllocaInst here to re-use
1045       // the Frame storage.
1046       //
1047       // Note: If we change the strategy dealing with alignment, we need to refine
1048       // this casting.
1049       if (GEP->getType() != Orig->getType())
1050         return Builder.CreateAddrSpaceCast(GEP, Orig->getType(),
1051                                            Orig->getName() + Twine(".cast"));
1052     }
1053     return GEP;
1054   };
1055 
1056   for (auto const &E : FrameData.Spills) {
1057     Value *Def = E.first;
1058     auto SpillAlignment = Align(FrameData.getAlign(Def));
1059     // Create a store instruction storing the value into the
1060     // coroutine frame.
1061     BasicBlock::iterator InsertPt = coro::getSpillInsertionPt(Shape, Def, DT);
1062 
1063     Type *ByValTy = nullptr;
1064     if (auto *Arg = dyn_cast<Argument>(Def)) {
1065       // If we're spilling an Argument, make sure we clear 'captures'
1066       // from the coroutine function.
1067       Arg->getParent()->removeParamAttr(Arg->getArgNo(), Attribute::Captures);
1068 
1069       if (Arg->hasByValAttr())
1070         ByValTy = Arg->getParamByValType();
1071     }
1072 
1073     auto Index = FrameData.getFieldIndex(Def);
1074     Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
1075     auto *G = Builder.CreateConstInBoundsGEP2_32(
1076         FrameTy, FramePtr, 0, Index, Def->getName() + Twine(".spill.addr"));
1077     if (ByValTy) {
1078       // For byval arguments, we need to store the pointed value in the frame,
1079       // instead of the pointer itself.
1080       auto *Value = Builder.CreateLoad(ByValTy, Def);
1081       Builder.CreateAlignedStore(Value, G, SpillAlignment);
1082     } else {
1083       Builder.CreateAlignedStore(Def, G, SpillAlignment);
1084     }
1085 
1086     BasicBlock *CurrentBlock = nullptr;
1087     Value *CurrentReload = nullptr;
1088     for (auto *U : E.second) {
1089       // If we have not seen the use block, create a load instruction to reload
1090       // the spilled value from the coroutine frame. Populates the Value pointer
1091       // reference provided with the frame GEP.
1092       if (CurrentBlock != U->getParent()) {
1093         CurrentBlock = U->getParent();
1094         Builder.SetInsertPoint(CurrentBlock,
1095                                CurrentBlock->getFirstInsertionPt());
1096 
1097         auto *GEP = GetFramePointer(E.first);
1098         GEP->setName(E.first->getName() + Twine(".reload.addr"));
1099         if (ByValTy)
1100           CurrentReload = GEP;
1101         else
1102           CurrentReload = Builder.CreateAlignedLoad(
1103               FrameTy->getElementType(FrameData.getFieldIndex(E.first)), GEP,
1104               SpillAlignment, E.first->getName() + Twine(".reload"));
1105 
1106         TinyPtrVector<DbgDeclareInst *> DIs = findDbgDeclares(Def);
1107         TinyPtrVector<DbgVariableRecord *> DVRs = findDVRDeclares(Def);
1108         // Try best to find dbg.declare. If the spill is a temp, there may not
1109         // be a direct dbg.declare. Walk up the load chain to find one from an
1110         // alias.
1111         if (F->getSubprogram()) {
1112           auto *CurDef = Def;
1113           while (DIs.empty() && DVRs.empty() && isa<LoadInst>(CurDef)) {
1114             auto *LdInst = cast<LoadInst>(CurDef);
1115             // Only consider ptr to ptr same type load.
1116             if (LdInst->getPointerOperandType() != LdInst->getType())
1117               break;
1118             CurDef = LdInst->getPointerOperand();
1119             if (!isa<AllocaInst, LoadInst>(CurDef))
1120               break;
1121             DIs = findDbgDeclares(CurDef);
1122             DVRs = findDVRDeclares(CurDef);
1123           }
1124         }
1125 
1126         auto SalvageOne = [&](auto *DDI) {
1127           // This dbg.declare is preserved for all coro-split function
1128           // fragments. It will be unreachable in the main function, and
1129           // processed by coro::salvageDebugInfo() by the Cloner.
1130           DbgVariableRecord *NewDVR = new DbgVariableRecord(
1131               ValueAsMetadata::get(CurrentReload), DDI->getVariable(),
1132               DDI->getExpression(), DDI->getDebugLoc(),
1133               DbgVariableRecord::LocationType::Declare);
1134           Builder.GetInsertPoint()->getParent()->insertDbgRecordBefore(
1135               NewDVR, Builder.GetInsertPoint());
1136           // This dbg.declare is for the main function entry point.  It
1137           // will be deleted in all coro-split functions.
1138           coro::salvageDebugInfo(ArgToAllocaMap, *DDI, false /*UseEntryValue*/);
1139         };
1140         for_each(DIs, SalvageOne);
1141         for_each(DVRs, SalvageOne);
1142       }
1143 
1144       // If we have a single edge PHINode, remove it and replace it with a
1145       // reload from the coroutine frame. (We already took care of multi edge
1146       // PHINodes by normalizing them in the rewritePHIs function).
1147       if (auto *PN = dyn_cast<PHINode>(U)) {
1148         assert(PN->getNumIncomingValues() == 1 &&
1149                "unexpected number of incoming "
1150                "values in the PHINode");
1151         PN->replaceAllUsesWith(CurrentReload);
1152         PN->eraseFromParent();
1153         continue;
1154       }
1155 
1156       // Replace all uses of CurrentValue in the current instruction with
1157       // reload.
1158       U->replaceUsesOfWith(Def, CurrentReload);
1159       // Instructions are added to Def's user list if the attached
1160       // debug records use Def. Update those now.
1161       for (DbgVariableRecord &DVR : filterDbgVars(U->getDbgRecordRange()))
1162         DVR.replaceVariableLocationOp(Def, CurrentReload, true);
1163     }
1164   }
1165 
1166   BasicBlock *FramePtrBB = Shape.getInsertPtAfterFramePtr()->getParent();
1167 
1168   auto SpillBlock = FramePtrBB->splitBasicBlock(
1169       Shape.getInsertPtAfterFramePtr(), "AllocaSpillBB");
1170   SpillBlock->splitBasicBlock(&SpillBlock->front(), "PostSpill");
1171   Shape.AllocaSpillBlock = SpillBlock;
1172 
1173   // retcon and retcon.once lowering assumes all uses have been sunk.
1174   if (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce ||
1175       Shape.ABI == coro::ABI::Async) {
1176     // If we found any allocas, replace all of their remaining uses with Geps.
1177     Builder.SetInsertPoint(SpillBlock, SpillBlock->begin());
1178     for (const auto &P : FrameData.Allocas) {
1179       AllocaInst *Alloca = P.Alloca;
1180       auto *G = GetFramePointer(Alloca);
1181 
1182       // We are not using ReplaceInstWithInst(P.first, cast<Instruction>(G))
1183       // here, as we are changing location of the instruction.
1184       G->takeName(Alloca);
1185       Alloca->replaceAllUsesWith(G);
1186       Alloca->eraseFromParent();
1187     }
1188     return;
1189   }
1190 
1191   // If we found any alloca, replace all of their remaining uses with GEP
1192   // instructions. To remain debugbility, we replace the uses of allocas for
1193   // dbg.declares and dbg.values with the reload from the frame.
1194   // Note: We cannot replace the alloca with GEP instructions indiscriminately,
1195   // as some of the uses may not be dominated by CoroBegin.
1196   Builder.SetInsertPoint(Shape.AllocaSpillBlock,
1197                          Shape.AllocaSpillBlock->begin());
1198   SmallVector<Instruction *, 4> UsersToUpdate;
1199   for (const auto &A : FrameData.Allocas) {
1200     AllocaInst *Alloca = A.Alloca;
1201     UsersToUpdate.clear();
1202     for (User *U : make_early_inc_range(Alloca->users())) {
1203       auto *I = cast<Instruction>(U);
1204       // It is meaningless to retain the lifetime intrinsics refer for the
1205       // member of coroutine frames and the meaningless lifetime intrinsics
1206       // are possible to block further optimizations.
1207       if (I->isLifetimeStartOrEnd())
1208         I->eraseFromParent();
1209       else if (DT.dominates(Shape.CoroBegin, I))
1210         UsersToUpdate.push_back(I);
1211     }
1212 
1213     if (UsersToUpdate.empty())
1214       continue;
1215     auto *G = GetFramePointer(Alloca);
1216     G->setName(Alloca->getName() + Twine(".reload.addr"));
1217 
1218     SmallVector<DbgVariableIntrinsic *, 4> DIs;
1219     SmallVector<DbgVariableRecord *> DbgVariableRecords;
1220     findDbgUsers(DIs, Alloca, &DbgVariableRecords);
1221     for (auto *DVI : DIs)
1222       DVI->replaceUsesOfWith(Alloca, G);
1223     for (auto *DVR : DbgVariableRecords)
1224       DVR->replaceVariableLocationOp(Alloca, G);
1225 
1226     for (Instruction *I : UsersToUpdate)
1227       I->replaceUsesOfWith(Alloca, G);
1228   }
1229   Builder.SetInsertPoint(&*Shape.getInsertPtAfterFramePtr());
1230   for (const auto &A : FrameData.Allocas) {
1231     AllocaInst *Alloca = A.Alloca;
1232     if (A.MayWriteBeforeCoroBegin) {
1233       // isEscaped really means potentially modified before CoroBegin.
1234       if (Alloca->isArrayAllocation())
1235         report_fatal_error(
1236             "Coroutines cannot handle copying of array allocas yet");
1237 
1238       auto *G = GetFramePointer(Alloca);
1239       auto *Value = Builder.CreateLoad(Alloca->getAllocatedType(), Alloca);
1240       Builder.CreateStore(Value, G);
1241     }
1242     // For each alias to Alloca created before CoroBegin but used after
1243     // CoroBegin, we recreate them after CoroBegin by applying the offset
1244     // to the pointer in the frame.
1245     for (const auto &Alias : A.Aliases) {
1246       auto *FramePtr = GetFramePointer(Alloca);
1247       auto &Value = *Alias.second;
1248       auto ITy = IntegerType::get(C, Value.getBitWidth());
1249       auto *AliasPtr =
1250           Builder.CreatePtrAdd(FramePtr, ConstantInt::get(ITy, Value));
1251       Alias.first->replaceUsesWithIf(
1252           AliasPtr, [&](Use &U) { return DT.dominates(Shape.CoroBegin, U); });
1253     }
1254   }
1255 
1256   // PromiseAlloca is not collected in FrameData.Allocas. So we don't handle
1257   // the case that the PromiseAlloca may have writes before CoroBegin in the
1258   // above codes. And it may be problematic in edge cases. See
1259   // https://github.com/llvm/llvm-project/issues/57861 for an example.
1260   if (Shape.ABI == coro::ABI::Switch && Shape.SwitchLowering.PromiseAlloca) {
1261     AllocaInst *PA = Shape.SwitchLowering.PromiseAlloca;
1262     // If there is memory accessing to promise alloca before CoroBegin;
1263     bool HasAccessingPromiseBeforeCB = llvm::any_of(PA->uses(), [&](Use &U) {
1264       auto *Inst = dyn_cast<Instruction>(U.getUser());
1265       if (!Inst || DT.dominates(Shape.CoroBegin, Inst))
1266         return false;
1267 
1268       if (auto *CI = dyn_cast<CallInst>(Inst)) {
1269         // It is fine if the call wouldn't write to the Promise.
1270         // This is possible for @llvm.coro.id intrinsics, which
1271         // would take the promise as the second argument as a
1272         // marker.
1273         if (CI->onlyReadsMemory() ||
1274             CI->onlyReadsMemory(CI->getArgOperandNo(&U)))
1275           return false;
1276         return true;
1277       }
1278 
1279       return isa<StoreInst>(Inst) ||
1280              // It may take too much time to track the uses.
1281              // Be conservative about the case the use may escape.
1282              isa<GetElementPtrInst>(Inst) ||
1283              // There would always be a bitcast for the promise alloca
1284              // before we enabled Opaque pointers. And now given
1285              // opaque pointers are enabled by default. This should be
1286              // fine.
1287              isa<BitCastInst>(Inst);
1288     });
1289     if (HasAccessingPromiseBeforeCB) {
1290       Builder.SetInsertPoint(&*Shape.getInsertPtAfterFramePtr());
1291       auto *G = GetFramePointer(PA);
1292       auto *Value = Builder.CreateLoad(PA->getAllocatedType(), PA);
1293       Builder.CreateStore(Value, G);
1294     }
1295   }
1296 }
1297 
1298 // Moves the values in the PHIs in SuccBB that correspong to PredBB into a new
1299 // PHI in InsertedBB.
movePHIValuesToInsertedBlock(BasicBlock * SuccBB,BasicBlock * InsertedBB,BasicBlock * PredBB,PHINode * UntilPHI=nullptr)1300 static void movePHIValuesToInsertedBlock(BasicBlock *SuccBB,
1301                                          BasicBlock *InsertedBB,
1302                                          BasicBlock *PredBB,
1303                                          PHINode *UntilPHI = nullptr) {
1304   auto *PN = cast<PHINode>(&SuccBB->front());
1305   do {
1306     int Index = PN->getBasicBlockIndex(InsertedBB);
1307     Value *V = PN->getIncomingValue(Index);
1308     PHINode *InputV = PHINode::Create(
1309         V->getType(), 1, V->getName() + Twine(".") + SuccBB->getName());
1310     InputV->insertBefore(InsertedBB->begin());
1311     InputV->addIncoming(V, PredBB);
1312     PN->setIncomingValue(Index, InputV);
1313     PN = dyn_cast<PHINode>(PN->getNextNode());
1314   } while (PN != UntilPHI);
1315 }
1316 
1317 // Rewrites the PHI Nodes in a cleanuppad.
rewritePHIsForCleanupPad(BasicBlock * CleanupPadBB,CleanupPadInst * CleanupPad)1318 static void rewritePHIsForCleanupPad(BasicBlock *CleanupPadBB,
1319                                      CleanupPadInst *CleanupPad) {
1320   // For every incoming edge to a CleanupPad we will create a new block holding
1321   // all incoming values in single-value PHI nodes. We will then create another
1322   // block to act as a dispather (as all unwind edges for related EH blocks
1323   // must be the same).
1324   //
1325   // cleanuppad:
1326   //    %2 = phi i32[%0, %catchswitch], [%1, %catch.1]
1327   //    %3 = cleanuppad within none []
1328   //
1329   // It will create:
1330   //
1331   // cleanuppad.corodispatch
1332   //    %2 = phi i8[0, %catchswitch], [1, %catch.1]
1333   //    %3 = cleanuppad within none []
1334   //    switch i8 % 2, label %unreachable
1335   //            [i8 0, label %cleanuppad.from.catchswitch
1336   //             i8 1, label %cleanuppad.from.catch.1]
1337   // cleanuppad.from.catchswitch:
1338   //    %4 = phi i32 [%0, %catchswitch]
1339   //    br %label cleanuppad
1340   // cleanuppad.from.catch.1:
1341   //    %6 = phi i32 [%1, %catch.1]
1342   //    br %label cleanuppad
1343   // cleanuppad:
1344   //    %8 = phi i32 [%4, %cleanuppad.from.catchswitch],
1345   //                 [%6, %cleanuppad.from.catch.1]
1346 
1347   // Unreachable BB, in case switching on an invalid value in the dispatcher.
1348   auto *UnreachBB = BasicBlock::Create(
1349       CleanupPadBB->getContext(), "unreachable", CleanupPadBB->getParent());
1350   IRBuilder<> Builder(UnreachBB);
1351   Builder.CreateUnreachable();
1352 
1353   // Create a new cleanuppad which will be the dispatcher.
1354   auto *NewCleanupPadBB =
1355       BasicBlock::Create(CleanupPadBB->getContext(),
1356                          CleanupPadBB->getName() + Twine(".corodispatch"),
1357                          CleanupPadBB->getParent(), CleanupPadBB);
1358   Builder.SetInsertPoint(NewCleanupPadBB);
1359   auto *SwitchType = Builder.getInt8Ty();
1360   auto *SetDispatchValuePN =
1361       Builder.CreatePHI(SwitchType, pred_size(CleanupPadBB));
1362   CleanupPad->removeFromParent();
1363   CleanupPad->insertAfter(SetDispatchValuePN->getIterator());
1364   auto *SwitchOnDispatch = Builder.CreateSwitch(SetDispatchValuePN, UnreachBB,
1365                                                 pred_size(CleanupPadBB));
1366 
1367   int SwitchIndex = 0;
1368   SmallVector<BasicBlock *, 8> Preds(predecessors(CleanupPadBB));
1369   for (BasicBlock *Pred : Preds) {
1370     // Create a new cleanuppad and move the PHI values to there.
1371     auto *CaseBB = BasicBlock::Create(CleanupPadBB->getContext(),
1372                                       CleanupPadBB->getName() +
1373                                           Twine(".from.") + Pred->getName(),
1374                                       CleanupPadBB->getParent(), CleanupPadBB);
1375     updatePhiNodes(CleanupPadBB, Pred, CaseBB);
1376     CaseBB->setName(CleanupPadBB->getName() + Twine(".from.") +
1377                     Pred->getName());
1378     Builder.SetInsertPoint(CaseBB);
1379     Builder.CreateBr(CleanupPadBB);
1380     movePHIValuesToInsertedBlock(CleanupPadBB, CaseBB, NewCleanupPadBB);
1381 
1382     // Update this Pred to the new unwind point.
1383     setUnwindEdgeTo(Pred->getTerminator(), NewCleanupPadBB);
1384 
1385     // Setup the switch in the dispatcher.
1386     auto *SwitchConstant = ConstantInt::get(SwitchType, SwitchIndex);
1387     SetDispatchValuePN->addIncoming(SwitchConstant, Pred);
1388     SwitchOnDispatch->addCase(SwitchConstant, CaseBB);
1389     SwitchIndex++;
1390   }
1391 }
1392 
cleanupSinglePredPHIs(Function & F)1393 static void cleanupSinglePredPHIs(Function &F) {
1394   SmallVector<PHINode *, 32> Worklist;
1395   for (auto &BB : F) {
1396     for (auto &Phi : BB.phis()) {
1397       if (Phi.getNumIncomingValues() == 1) {
1398         Worklist.push_back(&Phi);
1399       } else
1400         break;
1401     }
1402   }
1403   while (!Worklist.empty()) {
1404     auto *Phi = Worklist.pop_back_val();
1405     auto *OriginalValue = Phi->getIncomingValue(0);
1406     Phi->replaceAllUsesWith(OriginalValue);
1407   }
1408 }
1409 
rewritePHIs(BasicBlock & BB)1410 static void rewritePHIs(BasicBlock &BB) {
1411   // For every incoming edge we will create a block holding all
1412   // incoming values in a single PHI nodes.
1413   //
1414   // loop:
1415   //    %n.val = phi i32[%n, %entry], [%inc, %loop]
1416   //
1417   // It will create:
1418   //
1419   // loop.from.entry:
1420   //    %n.loop.pre = phi i32 [%n, %entry]
1421   //    br %label loop
1422   // loop.from.loop:
1423   //    %inc.loop.pre = phi i32 [%inc, %loop]
1424   //    br %label loop
1425   //
1426   // After this rewrite, further analysis will ignore any phi nodes with more
1427   // than one incoming edge.
1428 
1429   // TODO: Simplify PHINodes in the basic block to remove duplicate
1430   // predecessors.
1431 
1432   // Special case for CleanupPad: all EH blocks must have the same unwind edge
1433   // so we need to create an additional "dispatcher" block.
1434   if (!BB.empty()) {
1435     if (auto *CleanupPad =
1436             dyn_cast_or_null<CleanupPadInst>(BB.getFirstNonPHIIt())) {
1437       SmallVector<BasicBlock *, 8> Preds(predecessors(&BB));
1438       for (BasicBlock *Pred : Preds) {
1439         if (CatchSwitchInst *CS =
1440                 dyn_cast<CatchSwitchInst>(Pred->getTerminator())) {
1441           // CleanupPad with a CatchSwitch predecessor: therefore this is an
1442           // unwind destination that needs to be handle specially.
1443           assert(CS->getUnwindDest() == &BB);
1444           (void)CS;
1445           rewritePHIsForCleanupPad(&BB, CleanupPad);
1446           return;
1447         }
1448       }
1449     }
1450   }
1451 
1452   LandingPadInst *LandingPad = nullptr;
1453   PHINode *ReplPHI = nullptr;
1454   if (!BB.empty()) {
1455     if ((LandingPad =
1456              dyn_cast_or_null<LandingPadInst>(BB.getFirstNonPHIIt()))) {
1457       // ehAwareSplitEdge will clone the LandingPad in all the edge blocks.
1458       // We replace the original landing pad with a PHINode that will collect the
1459       // results from all of them.
1460       ReplPHI = PHINode::Create(LandingPad->getType(), 1, "");
1461       ReplPHI->insertBefore(LandingPad->getIterator());
1462       ReplPHI->takeName(LandingPad);
1463       LandingPad->replaceAllUsesWith(ReplPHI);
1464       // We will erase the original landing pad at the end of this function after
1465       // ehAwareSplitEdge cloned it in the transition blocks.
1466     }
1467   }
1468 
1469   SmallVector<BasicBlock *, 8> Preds(predecessors(&BB));
1470   for (BasicBlock *Pred : Preds) {
1471     auto *IncomingBB = ehAwareSplitEdge(Pred, &BB, LandingPad, ReplPHI);
1472     IncomingBB->setName(BB.getName() + Twine(".from.") + Pred->getName());
1473 
1474     // Stop the moving of values at ReplPHI, as this is either null or the PHI
1475     // that replaced the landing pad.
1476     movePHIValuesToInsertedBlock(&BB, IncomingBB, Pred, ReplPHI);
1477   }
1478 
1479   if (LandingPad) {
1480     // Calls to ehAwareSplitEdge function cloned the original lading pad.
1481     // No longer need it.
1482     LandingPad->eraseFromParent();
1483   }
1484 }
1485 
rewritePHIs(Function & F)1486 static void rewritePHIs(Function &F) {
1487   SmallVector<BasicBlock *, 8> WorkList;
1488 
1489   for (BasicBlock &BB : F)
1490     if (auto *PN = dyn_cast<PHINode>(&BB.front()))
1491       if (PN->getNumIncomingValues() > 1)
1492         WorkList.push_back(&BB);
1493 
1494   for (BasicBlock *BB : WorkList)
1495     rewritePHIs(*BB);
1496 }
1497 
1498 // Splits the block at a particular instruction unless it is the first
1499 // instruction in the block with a single predecessor.
splitBlockIfNotFirst(Instruction * I,const Twine & Name)1500 static BasicBlock *splitBlockIfNotFirst(Instruction *I, const Twine &Name) {
1501   auto *BB = I->getParent();
1502   if (&BB->front() == I) {
1503     if (BB->getSinglePredecessor()) {
1504       BB->setName(Name);
1505       return BB;
1506     }
1507   }
1508   return BB->splitBasicBlock(I, Name);
1509 }
1510 
1511 // Split above and below a particular instruction so that it
1512 // will be all alone by itself in a block.
splitAround(Instruction * I,const Twine & Name)1513 static void splitAround(Instruction *I, const Twine &Name) {
1514   splitBlockIfNotFirst(I, Name);
1515   splitBlockIfNotFirst(I->getNextNode(), "After" + Name);
1516 }
1517 
1518 /// After we split the coroutine, will the given basic block be along
1519 /// an obvious exit path for the resumption function?
willLeaveFunctionImmediatelyAfter(BasicBlock * BB,unsigned depth=3)1520 static bool willLeaveFunctionImmediatelyAfter(BasicBlock *BB,
1521                                               unsigned depth = 3) {
1522   // If we've bottomed out our depth count, stop searching and assume
1523   // that the path might loop back.
1524   if (depth == 0) return false;
1525 
1526   // If this is a suspend block, we're about to exit the resumption function.
1527   if (coro::isSuspendBlock(BB))
1528     return true;
1529 
1530   // Recurse into the successors.
1531   for (auto *Succ : successors(BB)) {
1532     if (!willLeaveFunctionImmediatelyAfter(Succ, depth - 1))
1533       return false;
1534   }
1535 
1536   // If none of the successors leads back in a loop, we're on an exit/abort.
1537   return true;
1538 }
1539 
localAllocaNeedsStackSave(CoroAllocaAllocInst * AI)1540 static bool localAllocaNeedsStackSave(CoroAllocaAllocInst *AI) {
1541   // Look for a free that isn't sufficiently obviously followed by
1542   // either a suspend or a termination, i.e. something that will leave
1543   // the coro resumption frame.
1544   for (auto *U : AI->users()) {
1545     auto FI = dyn_cast<CoroAllocaFreeInst>(U);
1546     if (!FI) continue;
1547 
1548     if (!willLeaveFunctionImmediatelyAfter(FI->getParent()))
1549       return true;
1550   }
1551 
1552   // If we never found one, we don't need a stack save.
1553   return false;
1554 }
1555 
1556 /// Turn each of the given local allocas into a normal (dynamic) alloca
1557 /// instruction.
lowerLocalAllocas(ArrayRef<CoroAllocaAllocInst * > LocalAllocas,SmallVectorImpl<Instruction * > & DeadInsts)1558 static void lowerLocalAllocas(ArrayRef<CoroAllocaAllocInst*> LocalAllocas,
1559                               SmallVectorImpl<Instruction*> &DeadInsts) {
1560   for (auto *AI : LocalAllocas) {
1561     IRBuilder<> Builder(AI);
1562 
1563     // Save the stack depth.  Try to avoid doing this if the stackrestore
1564     // is going to immediately precede a return or something.
1565     Value *StackSave = nullptr;
1566     if (localAllocaNeedsStackSave(AI))
1567       StackSave = Builder.CreateStackSave();
1568 
1569     // Allocate memory.
1570     auto Alloca = Builder.CreateAlloca(Builder.getInt8Ty(), AI->getSize());
1571     Alloca->setAlignment(AI->getAlignment());
1572 
1573     for (auto *U : AI->users()) {
1574       // Replace gets with the allocation.
1575       if (isa<CoroAllocaGetInst>(U)) {
1576         U->replaceAllUsesWith(Alloca);
1577 
1578       // Replace frees with stackrestores.  This is safe because
1579       // alloca.alloc is required to obey a stack discipline, although we
1580       // don't enforce that structurally.
1581       } else {
1582         auto FI = cast<CoroAllocaFreeInst>(U);
1583         if (StackSave) {
1584           Builder.SetInsertPoint(FI);
1585           Builder.CreateStackRestore(StackSave);
1586         }
1587       }
1588       DeadInsts.push_back(cast<Instruction>(U));
1589     }
1590 
1591     DeadInsts.push_back(AI);
1592   }
1593 }
1594 
1595 /// Get the current swifterror value.
emitGetSwiftErrorValue(IRBuilder<> & Builder,Type * ValueTy,coro::Shape & Shape)1596 static Value *emitGetSwiftErrorValue(IRBuilder<> &Builder, Type *ValueTy,
1597                                      coro::Shape &Shape) {
1598   // Make a fake function pointer as a sort of intrinsic.
1599   auto FnTy = FunctionType::get(ValueTy, {}, false);
1600   auto Fn = ConstantPointerNull::get(Builder.getPtrTy());
1601 
1602   auto Call = Builder.CreateCall(FnTy, Fn, {});
1603   Shape.SwiftErrorOps.push_back(Call);
1604 
1605   return Call;
1606 }
1607 
1608 /// Set the given value as the current swifterror value.
1609 ///
1610 /// Returns a slot that can be used as a swifterror slot.
emitSetSwiftErrorValue(IRBuilder<> & Builder,Value * V,coro::Shape & Shape)1611 static Value *emitSetSwiftErrorValue(IRBuilder<> &Builder, Value *V,
1612                                      coro::Shape &Shape) {
1613   // Make a fake function pointer as a sort of intrinsic.
1614   auto FnTy = FunctionType::get(Builder.getPtrTy(),
1615                                 {V->getType()}, false);
1616   auto Fn = ConstantPointerNull::get(Builder.getPtrTy());
1617 
1618   auto Call = Builder.CreateCall(FnTy, Fn, { V });
1619   Shape.SwiftErrorOps.push_back(Call);
1620 
1621   return Call;
1622 }
1623 
1624 /// Set the swifterror value from the given alloca before a call,
1625 /// then put in back in the alloca afterwards.
1626 ///
1627 /// Returns an address that will stand in for the swifterror slot
1628 /// until splitting.
emitSetAndGetSwiftErrorValueAround(Instruction * Call,AllocaInst * Alloca,coro::Shape & Shape)1629 static Value *emitSetAndGetSwiftErrorValueAround(Instruction *Call,
1630                                                  AllocaInst *Alloca,
1631                                                  coro::Shape &Shape) {
1632   auto ValueTy = Alloca->getAllocatedType();
1633   IRBuilder<> Builder(Call);
1634 
1635   // Load the current value from the alloca and set it as the
1636   // swifterror value.
1637   auto ValueBeforeCall = Builder.CreateLoad(ValueTy, Alloca);
1638   auto Addr = emitSetSwiftErrorValue(Builder, ValueBeforeCall, Shape);
1639 
1640   // Move to after the call.  Since swifterror only has a guaranteed
1641   // value on normal exits, we can ignore implicit and explicit unwind
1642   // edges.
1643   if (isa<CallInst>(Call)) {
1644     Builder.SetInsertPoint(Call->getNextNode());
1645   } else {
1646     auto Invoke = cast<InvokeInst>(Call);
1647     Builder.SetInsertPoint(Invoke->getNormalDest()->getFirstNonPHIOrDbg());
1648   }
1649 
1650   // Get the current swifterror value and store it to the alloca.
1651   auto ValueAfterCall = emitGetSwiftErrorValue(Builder, ValueTy, Shape);
1652   Builder.CreateStore(ValueAfterCall, Alloca);
1653 
1654   return Addr;
1655 }
1656 
1657 /// Eliminate a formerly-swifterror alloca by inserting the get/set
1658 /// intrinsics and attempting to MemToReg the alloca away.
eliminateSwiftErrorAlloca(Function & F,AllocaInst * Alloca,coro::Shape & Shape)1659 static void eliminateSwiftErrorAlloca(Function &F, AllocaInst *Alloca,
1660                                       coro::Shape &Shape) {
1661   for (Use &Use : llvm::make_early_inc_range(Alloca->uses())) {
1662     // swifterror values can only be used in very specific ways.
1663     // We take advantage of that here.
1664     auto User = Use.getUser();
1665     if (isa<LoadInst>(User) || isa<StoreInst>(User))
1666       continue;
1667 
1668     assert(isa<CallInst>(User) || isa<InvokeInst>(User));
1669     auto Call = cast<Instruction>(User);
1670 
1671     auto Addr = emitSetAndGetSwiftErrorValueAround(Call, Alloca, Shape);
1672 
1673     // Use the returned slot address as the call argument.
1674     Use.set(Addr);
1675   }
1676 
1677   // All the uses should be loads and stores now.
1678   assert(isAllocaPromotable(Alloca));
1679 }
1680 
1681 /// "Eliminate" a swifterror argument by reducing it to the alloca case
1682 /// and then loading and storing in the prologue and epilog.
1683 ///
1684 /// The argument keeps the swifterror flag.
eliminateSwiftErrorArgument(Function & F,Argument & Arg,coro::Shape & Shape,SmallVectorImpl<AllocaInst * > & AllocasToPromote)1685 static void eliminateSwiftErrorArgument(Function &F, Argument &Arg,
1686                                         coro::Shape &Shape,
1687                              SmallVectorImpl<AllocaInst*> &AllocasToPromote) {
1688   IRBuilder<> Builder(&F.getEntryBlock(),
1689                       F.getEntryBlock().getFirstNonPHIOrDbg());
1690 
1691   auto ArgTy = cast<PointerType>(Arg.getType());
1692   auto ValueTy = PointerType::getUnqual(F.getContext());
1693 
1694   // Reduce to the alloca case:
1695 
1696   // Create an alloca and replace all uses of the arg with it.
1697   auto Alloca = Builder.CreateAlloca(ValueTy, ArgTy->getAddressSpace());
1698   Arg.replaceAllUsesWith(Alloca);
1699 
1700   // Set an initial value in the alloca.  swifterror is always null on entry.
1701   auto InitialValue = Constant::getNullValue(ValueTy);
1702   Builder.CreateStore(InitialValue, Alloca);
1703 
1704   // Find all the suspends in the function and save and restore around them.
1705   for (auto *Suspend : Shape.CoroSuspends) {
1706     (void) emitSetAndGetSwiftErrorValueAround(Suspend, Alloca, Shape);
1707   }
1708 
1709   // Find all the coro.ends in the function and restore the error value.
1710   for (auto *End : Shape.CoroEnds) {
1711     Builder.SetInsertPoint(End);
1712     auto FinalValue = Builder.CreateLoad(ValueTy, Alloca);
1713     (void) emitSetSwiftErrorValue(Builder, FinalValue, Shape);
1714   }
1715 
1716   // Now we can use the alloca logic.
1717   AllocasToPromote.push_back(Alloca);
1718   eliminateSwiftErrorAlloca(F, Alloca, Shape);
1719 }
1720 
1721 /// Eliminate all problematic uses of swifterror arguments and allocas
1722 /// from the function.  We'll fix them up later when splitting the function.
eliminateSwiftError(Function & F,coro::Shape & Shape)1723 static void eliminateSwiftError(Function &F, coro::Shape &Shape) {
1724   SmallVector<AllocaInst*, 4> AllocasToPromote;
1725 
1726   // Look for a swifterror argument.
1727   for (auto &Arg : F.args()) {
1728     if (!Arg.hasSwiftErrorAttr()) continue;
1729 
1730     eliminateSwiftErrorArgument(F, Arg, Shape, AllocasToPromote);
1731     break;
1732   }
1733 
1734   // Look for swifterror allocas.
1735   for (auto &Inst : F.getEntryBlock()) {
1736     auto Alloca = dyn_cast<AllocaInst>(&Inst);
1737     if (!Alloca || !Alloca->isSwiftError()) continue;
1738 
1739     // Clear the swifterror flag.
1740     Alloca->setSwiftError(false);
1741 
1742     AllocasToPromote.push_back(Alloca);
1743     eliminateSwiftErrorAlloca(F, Alloca, Shape);
1744   }
1745 
1746   // If we have any allocas to promote, compute a dominator tree and
1747   // promote them en masse.
1748   if (!AllocasToPromote.empty()) {
1749     DominatorTree DT(F);
1750     PromoteMemToReg(AllocasToPromote, DT);
1751   }
1752 }
1753 
1754 /// For each local variable that all of its user are only used inside one of
1755 /// suspended region, we sink their lifetime.start markers to the place where
1756 /// after the suspend block. Doing so minimizes the lifetime of each variable,
1757 /// hence minimizing the amount of data we end up putting on the frame.
sinkLifetimeStartMarkers(Function & F,coro::Shape & Shape,SuspendCrossingInfo & Checker,const DominatorTree & DT)1758 static void sinkLifetimeStartMarkers(Function &F, coro::Shape &Shape,
1759                                      SuspendCrossingInfo &Checker,
1760                                      const DominatorTree &DT) {
1761   if (F.hasOptNone())
1762     return;
1763 
1764   // Collect all possible basic blocks which may dominate all uses of allocas.
1765   SmallPtrSet<BasicBlock *, 4> DomSet;
1766   DomSet.insert(&F.getEntryBlock());
1767   for (auto *CSI : Shape.CoroSuspends) {
1768     BasicBlock *SuspendBlock = CSI->getParent();
1769     assert(coro::isSuspendBlock(SuspendBlock) &&
1770            SuspendBlock->getSingleSuccessor() &&
1771            "should have split coro.suspend into its own block");
1772     DomSet.insert(SuspendBlock->getSingleSuccessor());
1773   }
1774 
1775   for (Instruction &I : instructions(F)) {
1776     AllocaInst* AI = dyn_cast<AllocaInst>(&I);
1777     if (!AI)
1778       continue;
1779 
1780     for (BasicBlock *DomBB : DomSet) {
1781       bool Valid = true;
1782       SmallVector<Instruction *, 1> Lifetimes;
1783 
1784       auto isLifetimeStart = [](Instruction* I) {
1785         if (auto* II = dyn_cast<IntrinsicInst>(I))
1786           return II->getIntrinsicID() == Intrinsic::lifetime_start;
1787         return false;
1788       };
1789 
1790       auto collectLifetimeStart = [&](Instruction *U, AllocaInst *AI) {
1791         if (isLifetimeStart(U)) {
1792           Lifetimes.push_back(U);
1793           return true;
1794         }
1795         if (!U->hasOneUse() || U->stripPointerCasts() != AI)
1796           return false;
1797         if (isLifetimeStart(U->user_back())) {
1798           Lifetimes.push_back(U->user_back());
1799           return true;
1800         }
1801         return false;
1802       };
1803 
1804       for (User *U : AI->users()) {
1805         Instruction *UI = cast<Instruction>(U);
1806         // For all users except lifetime.start markers, if they are all
1807         // dominated by one of the basic blocks and do not cross
1808         // suspend points as well, then there is no need to spill the
1809         // instruction.
1810         if (!DT.dominates(DomBB, UI->getParent()) ||
1811             Checker.isDefinitionAcrossSuspend(DomBB, UI)) {
1812           // Skip lifetime.start, GEP and bitcast used by lifetime.start
1813           // markers.
1814           if (collectLifetimeStart(UI, AI))
1815             continue;
1816           Valid = false;
1817           break;
1818         }
1819       }
1820       // Sink lifetime.start markers to dominate block when they are
1821       // only used outside the region.
1822       if (Valid && Lifetimes.size() != 0) {
1823         auto *NewLifetime = Lifetimes[0]->clone();
1824         NewLifetime->replaceUsesOfWith(NewLifetime->getOperand(1), AI);
1825         NewLifetime->insertBefore(DomBB->getTerminator()->getIterator());
1826 
1827         // All the outsided lifetime.start markers are no longer necessary.
1828         for (Instruction *S : Lifetimes)
1829           S->eraseFromParent();
1830 
1831         break;
1832       }
1833     }
1834   }
1835 }
1836 
1837 static std::optional<std::pair<Value &, DIExpression &>>
salvageDebugInfoImpl(SmallDenseMap<Argument *,AllocaInst *,4> & ArgToAllocaMap,bool UseEntryValue,Function * F,Value * Storage,DIExpression * Expr,bool SkipOutermostLoad)1838 salvageDebugInfoImpl(SmallDenseMap<Argument *, AllocaInst *, 4> &ArgToAllocaMap,
1839                      bool UseEntryValue, Function *F, Value *Storage,
1840                      DIExpression *Expr, bool SkipOutermostLoad) {
1841   IRBuilder<> Builder(F->getContext());
1842   auto InsertPt = F->getEntryBlock().getFirstInsertionPt();
1843   while (isa<IntrinsicInst>(InsertPt))
1844     ++InsertPt;
1845   Builder.SetInsertPoint(&F->getEntryBlock(), InsertPt);
1846 
1847   while (auto *Inst = dyn_cast_or_null<Instruction>(Storage)) {
1848     if (auto *LdInst = dyn_cast<LoadInst>(Inst)) {
1849       Storage = LdInst->getPointerOperand();
1850       // FIXME: This is a heuristic that works around the fact that
1851       // LLVM IR debug intrinsics cannot yet distinguish between
1852       // memory and value locations: Because a dbg.declare(alloca) is
1853       // implicitly a memory location no DW_OP_deref operation for the
1854       // last direct load from an alloca is necessary.  This condition
1855       // effectively drops the *last* DW_OP_deref in the expression.
1856       if (!SkipOutermostLoad)
1857         Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
1858     } else if (auto *StInst = dyn_cast<StoreInst>(Inst)) {
1859       Storage = StInst->getValueOperand();
1860     } else {
1861       SmallVector<uint64_t, 16> Ops;
1862       SmallVector<Value *, 0> AdditionalValues;
1863       Value *Op = llvm::salvageDebugInfoImpl(
1864           *Inst, Expr ? Expr->getNumLocationOperands() : 0, Ops,
1865           AdditionalValues);
1866       if (!Op || !AdditionalValues.empty()) {
1867         // If salvaging failed or salvaging produced more than one location
1868         // operand, give up.
1869         break;
1870       }
1871       Storage = Op;
1872       Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, /*StackValue*/ false);
1873     }
1874     SkipOutermostLoad = false;
1875   }
1876   if (!Storage)
1877     return std::nullopt;
1878 
1879   auto *StorageAsArg = dyn_cast<Argument>(Storage);
1880   const bool IsSwiftAsyncArg =
1881       StorageAsArg && StorageAsArg->hasAttribute(Attribute::SwiftAsync);
1882 
1883   // Swift async arguments are described by an entry value of the ABI-defined
1884   // register containing the coroutine context.
1885   // Entry values in variadic expressions are not supported.
1886   if (IsSwiftAsyncArg && UseEntryValue && !Expr->isEntryValue() &&
1887       Expr->isSingleLocationExpression())
1888     Expr = DIExpression::prepend(Expr, DIExpression::EntryValue);
1889 
1890   // If the coroutine frame is an Argument, store it in an alloca to improve
1891   // its availability (e.g. registers may be clobbered).
1892   // Avoid this if the value is guaranteed to be available through other means
1893   // (e.g. swift ABI guarantees).
1894   if (StorageAsArg && !IsSwiftAsyncArg) {
1895     auto &Cached = ArgToAllocaMap[StorageAsArg];
1896     if (!Cached) {
1897       Cached = Builder.CreateAlloca(Storage->getType(), 0, nullptr,
1898                                     Storage->getName() + ".debug");
1899       Builder.CreateStore(Storage, Cached);
1900     }
1901     Storage = Cached;
1902     // FIXME: LLVM lacks nuanced semantics to differentiate between
1903     // memory and direct locations at the IR level. The backend will
1904     // turn a dbg.declare(alloca, ..., DIExpression()) into a memory
1905     // location. Thus, if there are deref and offset operations in the
1906     // expression, we need to add a DW_OP_deref at the *start* of the
1907     // expression to first load the contents of the alloca before
1908     // adjusting it with the expression.
1909     Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
1910   }
1911 
1912   Expr = Expr->foldConstantMath();
1913   return {{*Storage, *Expr}};
1914 }
1915 
salvageDebugInfo(SmallDenseMap<Argument *,AllocaInst *,4> & ArgToAllocaMap,DbgVariableIntrinsic & DVI,bool UseEntryValue)1916 void coro::salvageDebugInfo(
1917     SmallDenseMap<Argument *, AllocaInst *, 4> &ArgToAllocaMap,
1918     DbgVariableIntrinsic &DVI, bool UseEntryValue) {
1919 
1920   Function *F = DVI.getFunction();
1921   // Follow the pointer arithmetic all the way to the incoming
1922   // function argument and convert into a DIExpression.
1923   bool SkipOutermostLoad = !isa<DbgValueInst>(DVI);
1924   Value *OriginalStorage = DVI.getVariableLocationOp(0);
1925 
1926   auto SalvagedInfo =
1927       ::salvageDebugInfoImpl(ArgToAllocaMap, UseEntryValue, F, OriginalStorage,
1928                              DVI.getExpression(), SkipOutermostLoad);
1929   if (!SalvagedInfo)
1930     return;
1931 
1932   Value *Storage = &SalvagedInfo->first;
1933   DIExpression *Expr = &SalvagedInfo->second;
1934 
1935   DVI.replaceVariableLocationOp(OriginalStorage, Storage);
1936   DVI.setExpression(Expr);
1937   // We only hoist dbg.declare today since it doesn't make sense to hoist
1938   // dbg.value since it does not have the same function wide guarantees that
1939   // dbg.declare does.
1940   if (isa<DbgDeclareInst>(DVI)) {
1941     std::optional<BasicBlock::iterator> InsertPt;
1942     if (auto *I = dyn_cast<Instruction>(Storage)) {
1943       InsertPt = I->getInsertionPointAfterDef();
1944       // Update DILocation only if variable was not inlined.
1945       DebugLoc ILoc = I->getDebugLoc();
1946       DebugLoc DVILoc = DVI.getDebugLoc();
1947       if (ILoc && DVILoc &&
1948           DVILoc->getScope()->getSubprogram() ==
1949               ILoc->getScope()->getSubprogram())
1950         DVI.setDebugLoc(I->getDebugLoc());
1951     } else if (isa<Argument>(Storage))
1952       InsertPt = F->getEntryBlock().begin();
1953     if (InsertPt)
1954       DVI.moveBefore(*(*InsertPt)->getParent(), *InsertPt);
1955   }
1956 }
1957 
salvageDebugInfo(SmallDenseMap<Argument *,AllocaInst *,4> & ArgToAllocaMap,DbgVariableRecord & DVR,bool UseEntryValue)1958 void coro::salvageDebugInfo(
1959     SmallDenseMap<Argument *, AllocaInst *, 4> &ArgToAllocaMap,
1960     DbgVariableRecord &DVR, bool UseEntryValue) {
1961 
1962   Function *F = DVR.getFunction();
1963   // Follow the pointer arithmetic all the way to the incoming
1964   // function argument and convert into a DIExpression.
1965   bool SkipOutermostLoad = DVR.isDbgDeclare();
1966   Value *OriginalStorage = DVR.getVariableLocationOp(0);
1967 
1968   auto SalvagedInfo =
1969       ::salvageDebugInfoImpl(ArgToAllocaMap, UseEntryValue, F, OriginalStorage,
1970                              DVR.getExpression(), SkipOutermostLoad);
1971   if (!SalvagedInfo)
1972     return;
1973 
1974   Value *Storage = &SalvagedInfo->first;
1975   DIExpression *Expr = &SalvagedInfo->second;
1976 
1977   DVR.replaceVariableLocationOp(OriginalStorage, Storage);
1978   DVR.setExpression(Expr);
1979   // We only hoist dbg.declare today since it doesn't make sense to hoist
1980   // dbg.value since it does not have the same function wide guarantees that
1981   // dbg.declare does.
1982   if (DVR.getType() == DbgVariableRecord::LocationType::Declare) {
1983     std::optional<BasicBlock::iterator> InsertPt;
1984     if (auto *I = dyn_cast<Instruction>(Storage)) {
1985       InsertPt = I->getInsertionPointAfterDef();
1986       // Update DILocation only if variable was not inlined.
1987       DebugLoc ILoc = I->getDebugLoc();
1988       DebugLoc DVRLoc = DVR.getDebugLoc();
1989       if (ILoc && DVRLoc &&
1990           DVRLoc->getScope()->getSubprogram() ==
1991               ILoc->getScope()->getSubprogram())
1992         DVR.setDebugLoc(ILoc);
1993     } else if (isa<Argument>(Storage))
1994       InsertPt = F->getEntryBlock().begin();
1995     if (InsertPt) {
1996       DVR.removeFromParent();
1997       (*InsertPt)->getParent()->insertDbgRecordBefore(&DVR, *InsertPt);
1998     }
1999   }
2000 }
2001 
normalizeCoroutine(Function & F,coro::Shape & Shape,TargetTransformInfo & TTI)2002 void coro::normalizeCoroutine(Function &F, coro::Shape &Shape,
2003                               TargetTransformInfo &TTI) {
2004   // Don't eliminate swifterror in async functions that won't be split.
2005   if (Shape.ABI != coro::ABI::Async || !Shape.CoroSuspends.empty())
2006     eliminateSwiftError(F, Shape);
2007 
2008   if (Shape.ABI == coro::ABI::Switch &&
2009       Shape.SwitchLowering.PromiseAlloca) {
2010     Shape.getSwitchCoroId()->clearPromise();
2011   }
2012 
2013   // Make sure that all coro.save, coro.suspend and the fallthrough coro.end
2014   // intrinsics are in their own blocks to simplify the logic of building up
2015   // SuspendCrossing data.
2016   for (auto *CSI : Shape.CoroSuspends) {
2017     if (auto *Save = CSI->getCoroSave())
2018       splitAround(Save, "CoroSave");
2019     splitAround(CSI, "CoroSuspend");
2020   }
2021 
2022   // Put CoroEnds into their own blocks.
2023   for (AnyCoroEndInst *CE : Shape.CoroEnds) {
2024     splitAround(CE, "CoroEnd");
2025 
2026     // Emit the musttail call function in a new block before the CoroEnd.
2027     // We do this here so that the right suspend crossing info is computed for
2028     // the uses of the musttail call function call. (Arguments to the coro.end
2029     // instructions would be ignored)
2030     if (auto *AsyncEnd = dyn_cast<CoroAsyncEndInst>(CE)) {
2031       auto *MustTailCallFn = AsyncEnd->getMustTailCallFunction();
2032       if (!MustTailCallFn)
2033         continue;
2034       IRBuilder<> Builder(AsyncEnd);
2035       SmallVector<Value *, 8> Args(AsyncEnd->args());
2036       auto Arguments = ArrayRef<Value *>(Args).drop_front(3);
2037       auto *Call = coro::createMustTailCall(
2038           AsyncEnd->getDebugLoc(), MustTailCallFn, TTI, Arguments, Builder);
2039       splitAround(Call, "MustTailCall.Before.CoroEnd");
2040     }
2041   }
2042 
2043   // Later code makes structural assumptions about single predecessors phis e.g
2044   // that they are not live across a suspend point.
2045   cleanupSinglePredPHIs(F);
2046 
2047   // Transforms multi-edge PHI Nodes, so that any value feeding into a PHI will
2048   // never have its definition separated from the PHI by the suspend point.
2049   rewritePHIs(F);
2050 }
2051 
buildCoroutineFrame(bool OptimizeFrame)2052 void coro::BaseABI::buildCoroutineFrame(bool OptimizeFrame) {
2053   SuspendCrossingInfo Checker(F, Shape.CoroSuspends, Shape.CoroEnds);
2054   doRematerializations(F, Checker, IsMaterializable);
2055 
2056   const DominatorTree DT(F);
2057   if (Shape.ABI != coro::ABI::Async && Shape.ABI != coro::ABI::Retcon &&
2058       Shape.ABI != coro::ABI::RetconOnce)
2059     sinkLifetimeStartMarkers(F, Shape, Checker, DT);
2060 
2061   // All values (that are not allocas) that needs to be spilled to the frame.
2062   coro::SpillInfo Spills;
2063   // All values defined as allocas that need to live in the frame.
2064   SmallVector<coro::AllocaInfo, 8> Allocas;
2065 
2066   // Collect the spills for arguments and other not-materializable values.
2067   coro::collectSpillsFromArgs(Spills, F, Checker);
2068   SmallVector<Instruction *, 4> DeadInstructions;
2069   SmallVector<CoroAllocaAllocInst *, 4> LocalAllocas;
2070   coro::collectSpillsAndAllocasFromInsts(Spills, Allocas, DeadInstructions,
2071                                          LocalAllocas, F, Checker, DT, Shape);
2072   coro::collectSpillsFromDbgInfo(Spills, F, Checker);
2073 
2074   LLVM_DEBUG(dumpAllocas(Allocas));
2075   LLVM_DEBUG(dumpSpills("Spills", Spills));
2076 
2077   if (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce ||
2078       Shape.ABI == coro::ABI::Async)
2079     sinkSpillUsesAfterCoroBegin(DT, Shape.CoroBegin, Spills, Allocas);
2080 
2081   // Build frame
2082   FrameDataInfo FrameData(Spills, Allocas);
2083   Shape.FrameTy = buildFrameType(F, Shape, FrameData, OptimizeFrame);
2084   Shape.FramePtr = Shape.CoroBegin;
2085   // For now, this works for C++ programs only.
2086   buildFrameDebugInfo(F, Shape, FrameData);
2087   // Insert spills and reloads
2088   insertSpills(FrameData, Shape);
2089   lowerLocalAllocas(LocalAllocas, DeadInstructions);
2090 
2091   for (auto *I : DeadInstructions)
2092     I->eraseFromParent();
2093 }
2094