| 1 | //===- CoroSplit.cpp - Converts a coroutine into a state machine ----------===// |
| 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 pass builds the coroutine frame and outlines resume and destroy parts |
| 9 | // of the coroutine into separate functions. |
| 10 | // |
| 11 | // We present a coroutine to an LLVM as an ordinary function with suspension |
| 12 | // points marked up with intrinsics. We let the optimizer party on the coroutine |
| 13 | // as a single function for as long as possible. Shortly before the coroutine is |
| 14 | // eligible to be inlined into its callers, we split up the coroutine into parts |
| 15 | // corresponding to an initial, resume and destroy invocations of the coroutine, |
| 16 | // add them to the current SCC and restart the IPO pipeline to optimize the |
| 17 | // coroutine subfunctions we extracted before proceeding to the caller of the |
| 18 | // coroutine. |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "llvm/Transforms/Coroutines/CoroSplit.h" |
| 22 | #include "CoroCloner.h" |
| 23 | #include "CoroInternal.h" |
| 24 | #include "llvm/ADT/DenseMap.h" |
| 25 | #include "llvm/ADT/PriorityWorklist.h" |
| 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include "llvm/ADT/SmallPtrSet.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
| 29 | #include "llvm/ADT/StringExtras.h" |
| 30 | #include "llvm/ADT/StringRef.h" |
| 31 | #include "llvm/ADT/Twine.h" |
| 32 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 33 | #include "llvm/Analysis/CFG.h" |
| 34 | #include "llvm/Analysis/CallGraph.h" |
| 35 | #include "llvm/Analysis/ConstantFolding.h" |
| 36 | #include "llvm/Analysis/LazyCallGraph.h" |
| 37 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
| 38 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 39 | #include "llvm/BinaryFormat/Dwarf.h" |
| 40 | #include "llvm/IR/Argument.h" |
| 41 | #include "llvm/IR/Attributes.h" |
| 42 | #include "llvm/IR/BasicBlock.h" |
| 43 | #include "llvm/IR/CFG.h" |
| 44 | #include "llvm/IR/Constants.h" |
| 45 | #include "llvm/IR/DIBuilder.h" |
| 46 | #include "llvm/IR/DataLayout.h" |
| 47 | #include "llvm/IR/DebugInfo.h" |
| 48 | #include "llvm/IR/DerivedTypes.h" |
| 49 | #include "llvm/IR/Dominators.h" |
| 50 | #include "llvm/IR/GlobalValue.h" |
| 51 | #include "llvm/IR/GlobalVariable.h" |
| 52 | #include "llvm/IR/InstIterator.h" |
| 53 | #include "llvm/IR/InstrTypes.h" |
| 54 | #include "llvm/IR/Instruction.h" |
| 55 | #include "llvm/IR/Instructions.h" |
| 56 | #include "llvm/IR/IntrinsicInst.h" |
| 57 | #include "llvm/IR/LLVMContext.h" |
| 58 | #include "llvm/IR/MDBuilder.h" |
| 59 | #include "llvm/IR/Module.h" |
| 60 | #include "llvm/IR/ProfDataUtils.h" |
| 61 | #include "llvm/IR/Type.h" |
| 62 | #include "llvm/IR/Value.h" |
| 63 | #include "llvm/IR/Verifier.h" |
| 64 | #include "llvm/Support/Casting.h" |
| 65 | #include "llvm/Support/Debug.h" |
| 66 | #include "llvm/Support/PrettyStackTrace.h" |
| 67 | #include "llvm/Support/raw_ostream.h" |
| 68 | #include "llvm/Transforms/Coroutines/MaterializationUtils.h" |
| 69 | #include "llvm/Transforms/Scalar.h" |
| 70 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 71 | #include "llvm/Transforms/Utils/CallGraphUpdater.h" |
| 72 | #include "llvm/Transforms/Utils/Cloning.h" |
| 73 | #include "llvm/Transforms/Utils/Local.h" |
| 74 | #include <cassert> |
| 75 | #include <cstddef> |
| 76 | #include <cstdint> |
| 77 | #include <initializer_list> |
| 78 | #include <iterator> |
| 79 | |
| 80 | using namespace llvm; |
| 81 | |
| 82 | #define DEBUG_TYPE "coro-split" |
| 83 | |
| 84 | // FIXME: |
| 85 | // Lower the intrinisc in CoroEarly phase if coroutine frame doesn't escape |
| 86 | // and it is known that other transformations, for example, sanitizers |
| 87 | // won't lead to incorrect code. |
| 88 | static void lowerAwaitSuspend(IRBuilder<> &Builder, CoroAwaitSuspendInst *CB, |
| 89 | coro::Shape &Shape) { |
| 90 | auto Wrapper = CB->getWrapperFunction(); |
| 91 | auto Awaiter = CB->getAwaiter(); |
| 92 | auto FramePtr = CB->getFrame(); |
| 93 | |
| 94 | Builder.SetInsertPoint(CB); |
| 95 | |
| 96 | CallBase *NewCall = nullptr; |
| 97 | // await_suspend has only 2 parameters, awaiter and handle. |
| 98 | // Copy parameter attributes from the intrinsic call, but remove the last, |
| 99 | // because the last parameter now becomes the function that is being called. |
| 100 | AttributeList NewAttributes = |
| 101 | CB->getAttributes().removeParamAttributes(C&: CB->getContext(), ArgNo: 2); |
| 102 | |
| 103 | if (auto Invoke = dyn_cast<InvokeInst>(Val: CB)) { |
| 104 | auto WrapperInvoke = |
| 105 | Builder.CreateInvoke(Callee: Wrapper, NormalDest: Invoke->getNormalDest(), |
| 106 | UnwindDest: Invoke->getUnwindDest(), Args: {Awaiter, FramePtr}); |
| 107 | |
| 108 | WrapperInvoke->setCallingConv(Invoke->getCallingConv()); |
| 109 | std::copy(first: Invoke->bundle_op_info_begin(), last: Invoke->bundle_op_info_end(), |
| 110 | result: WrapperInvoke->bundle_op_info_begin()); |
| 111 | WrapperInvoke->setAttributes(NewAttributes); |
| 112 | WrapperInvoke->setDebugLoc(Invoke->getDebugLoc()); |
| 113 | NewCall = WrapperInvoke; |
| 114 | } else if (auto Call = dyn_cast<CallInst>(Val: CB)) { |
| 115 | auto WrapperCall = Builder.CreateCall(Callee: Wrapper, Args: {Awaiter, FramePtr}); |
| 116 | |
| 117 | WrapperCall->setAttributes(NewAttributes); |
| 118 | WrapperCall->setDebugLoc(Call->getDebugLoc()); |
| 119 | NewCall = WrapperCall; |
| 120 | } else { |
| 121 | llvm_unreachable("Unexpected coro_await_suspend invocation method" ); |
| 122 | } |
| 123 | |
| 124 | if (CB->getCalledFunction()->getIntrinsicID() == |
| 125 | Intrinsic::coro_await_suspend_handle) { |
| 126 | // Follow the lowered await_suspend call above with a lowered resume call |
| 127 | // to the returned coroutine. |
| 128 | if (auto *Invoke = dyn_cast<InvokeInst>(Val: CB)) { |
| 129 | // If the await_suspend call is an invoke, we continue in the next block. |
| 130 | Builder.SetInsertPoint(Invoke->getNormalDest()->getFirstInsertionPt()); |
| 131 | } |
| 132 | |
| 133 | coro::LowererBase LB(*Wrapper->getParent()); |
| 134 | auto *ResumeAddr = LB.makeSubFnCall(Arg: NewCall, Index: CoroSubFnInst::ResumeIndex, |
| 135 | InsertPt: &*Builder.GetInsertPoint()); |
| 136 | |
| 137 | LLVMContext &Ctx = Builder.getContext(); |
| 138 | FunctionType *ResumeTy = FunctionType::get( |
| 139 | Result: Type::getVoidTy(C&: Ctx), Params: PointerType::getUnqual(C&: Ctx), isVarArg: false); |
| 140 | auto *ResumeCall = Builder.CreateCall(FTy: ResumeTy, Callee: ResumeAddr, Args: {NewCall}); |
| 141 | |
| 142 | // We can't insert the 'ret' instruction and adjust the cc until the |
| 143 | // function has been split, so remember this for later. |
| 144 | Shape.SymmetricTransfers.push_back(Elt: ResumeCall); |
| 145 | |
| 146 | NewCall = ResumeCall; |
| 147 | } |
| 148 | |
| 149 | CB->replaceAllUsesWith(V: NewCall); |
| 150 | CB->eraseFromParent(); |
| 151 | } |
| 152 | |
| 153 | static void lowerAwaitSuspends(Function &F, coro::Shape &Shape) { |
| 154 | IRBuilder<> Builder(F.getContext()); |
| 155 | for (auto *AWS : Shape.CoroAwaitSuspends) |
| 156 | lowerAwaitSuspend(Builder, CB: AWS, Shape); |
| 157 | } |
| 158 | |
| 159 | static void maybeFreeRetconStorage(IRBuilder<> &Builder, |
| 160 | const coro::Shape &Shape, Value *FramePtr, |
| 161 | CallGraph *CG) { |
| 162 | assert(Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce); |
| 163 | if (Shape.RetconLowering.IsFrameInlineInStorage) |
| 164 | return; |
| 165 | |
| 166 | Shape.emitDealloc(Builder, Ptr: FramePtr, CG); |
| 167 | } |
| 168 | |
| 169 | /// Create a pointer to the switch destroy function field in the coroutine |
| 170 | /// frame. |
| 171 | static Value *createSwitchDestroyPtr(const coro::Shape &Shape, |
| 172 | IRBuilder<> &Builder, Value *FramePtr) { |
| 173 | auto *Offset = ConstantInt::get(Ty: Type::getInt64Ty(C&: FramePtr->getContext()), |
| 174 | V: Shape.SwitchLowering.DestroyOffset); |
| 175 | return Builder.CreateInBoundsPtrAdd(Ptr: FramePtr, Offset, Name: "destroy.addr" ); |
| 176 | } |
| 177 | |
| 178 | /// Make resume-clone coro.free conditional on whether the frame is elided. |
| 179 | /// |
| 180 | /// The destroy slot holds the cleanup clone for an elided frame and the destroy |
| 181 | /// clone for a heap frame. Load it before user code can reentrantly destroy the |
| 182 | /// enclosing caller frame, then use the cached comparison to suppress only the |
| 183 | /// deallocation. The resume clone has already performed the shared coroutine |
| 184 | /// cleanup, so calling either clone here would run that cleanup twice. |
| 185 | static void replaceSwitchResumeCoroFree(const coro::Shape &Shape, |
| 186 | Function &Resume, Function &Cleanup) { |
| 187 | Value *FramePtr = Resume.getArg(i: 0); |
| 188 | IRBuilder<> EntryBuilder(Resume.getEntryBlock().getTerminator()); |
| 189 | Value *DestroyAddr = createSwitchDestroyPtr(Shape, Builder&: EntryBuilder, FramePtr); |
| 190 | Value *DestroyFn = EntryBuilder.CreateLoad(Ty: Shape.getSwitchResumePointerType(), |
| 191 | Ptr: DestroyAddr, Name: "destroy" ); |
| 192 | Value *CleanupFn = |
| 193 | EntryBuilder.CreatePointerCast(V: &Cleanup, DestTy: DestroyFn->getType()); |
| 194 | Value *IsElided = |
| 195 | EntryBuilder.CreateICmpEQ(LHS: DestroyFn, RHS: CleanupFn, Name: "is.elided" ); |
| 196 | |
| 197 | SmallVector<CoroFreeInst *, 4> CoroFrees; |
| 198 | for (User *U : FramePtr->users()) { |
| 199 | if (auto *CF = dyn_cast<CoroFreeInst>(Val: U)) |
| 200 | CoroFrees.push_back(Elt: CF); |
| 201 | } |
| 202 | |
| 203 | for (CoroFreeInst *CF : CoroFrees) { |
| 204 | IRBuilder<> Builder(CF); |
| 205 | auto *Null = ConstantPointerNull::get(T: cast<PointerType>(Val: CF->getType())); |
| 206 | Value *Replacement = |
| 207 | Builder.CreateSelect(C: IsElided, True: Null, False: FramePtr, Name: "coro.free" ); |
| 208 | // Add unknown branch weights to the select since whether the frame is |
| 209 | // heap-allocated or elided cannot be determined. |
| 210 | applyProfMetadataIfEnabled(V: Replacement, setMetadataCallback: [&](Instruction *Inst) { |
| 211 | setExplicitlyUnknownBranchWeightsIfProfiled(I&: *Inst, DEBUG_TYPE, |
| 212 | F: Inst->getFunction()); |
| 213 | }); |
| 214 | CF->replaceAllUsesWith(V: Replacement); |
| 215 | CF->eraseFromParent(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// Replace an llvm.coro.end.async. |
| 220 | /// Will inline the must tail call function call if there is one. |
| 221 | /// \returns true if cleanup of the coro.end block is needed, false otherwise. |
| 222 | static bool replaceCoroEndAsync(AnyCoroEndInst *End) { |
| 223 | IRBuilder<> Builder(End); |
| 224 | |
| 225 | auto *EndAsync = dyn_cast<CoroAsyncEndInst>(Val: End); |
| 226 | if (!EndAsync) { |
| 227 | Builder.CreateRetVoid(); |
| 228 | return true /*needs cleanup of coro.end block*/; |
| 229 | } |
| 230 | |
| 231 | auto *MustTailCallFunc = EndAsync->getMustTailCallFunction(); |
| 232 | if (!MustTailCallFunc) { |
| 233 | Builder.CreateRetVoid(); |
| 234 | return true /*needs cleanup of coro.end block*/; |
| 235 | } |
| 236 | |
| 237 | // Move the must tail call from the predecessor block into the end block. |
| 238 | auto *CoroEndBlock = End->getParent(); |
| 239 | auto *MustTailCallFuncBlock = CoroEndBlock->getSinglePredecessor(); |
| 240 | assert(MustTailCallFuncBlock && "Must have a single predecessor block" ); |
| 241 | auto It = MustTailCallFuncBlock->getTerminator()->getIterator(); |
| 242 | auto *MustTailCall = cast<CallInst>(Val: &*std::prev(x: It)); |
| 243 | CoroEndBlock->splice(ToIt: End->getIterator(), FromBB: MustTailCallFuncBlock, |
| 244 | FromIt: MustTailCall->getIterator()); |
| 245 | |
| 246 | // Insert the return instruction. |
| 247 | Builder.SetInsertPoint(End); |
| 248 | Builder.CreateRetVoid(); |
| 249 | InlineFunctionInfo FnInfo; |
| 250 | |
| 251 | // Remove the rest of the block, by splitting it into an unreachable block. |
| 252 | auto *BB = End->getParent(); |
| 253 | BB->splitBasicBlock(I: End); |
| 254 | BB->getTerminator()->eraseFromParent(); |
| 255 | |
| 256 | auto InlineRes = InlineFunction(CB&: *MustTailCall, IFI&: FnInfo); |
| 257 | assert(InlineRes.isSuccess() && "Expected inlining to succeed" ); |
| 258 | (void)InlineRes; |
| 259 | |
| 260 | // We have cleaned up the coro.end block above. |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | /// Replace a non-unwind call to llvm.coro.end. |
| 265 | static void replaceFallthroughCoroEnd(AnyCoroEndInst *End, |
| 266 | const coro::Shape &Shape, Value *FramePtr, |
| 267 | bool InRamp, CallGraph *CG) { |
| 268 | // Start inserting right before the coro.end. |
| 269 | IRBuilder<> Builder(End); |
| 270 | |
| 271 | // Create the return instruction. |
| 272 | switch (Shape.ABI) { |
| 273 | // The cloned functions in switch-lowering always return void. |
| 274 | case coro::ABI::Switch: |
| 275 | assert(!cast<CoroEndInst>(End)->hasResults() && |
| 276 | "switch coroutine should not return any values" ); |
| 277 | // coro.end doesn't immediately end the coroutine in the main function |
| 278 | // in this lowering, because we need to deallocate the coroutine. |
| 279 | if (InRamp) |
| 280 | return; |
| 281 | Builder.CreateRetVoid(); |
| 282 | break; |
| 283 | |
| 284 | // In async lowering this returns. |
| 285 | case coro::ABI::Async: { |
| 286 | bool CoroEndBlockNeedsCleanup = replaceCoroEndAsync(End); |
| 287 | if (!CoroEndBlockNeedsCleanup) |
| 288 | return; |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | // In unique continuation lowering, the continuations always return void. |
| 293 | // But we may have implicitly allocated storage. |
| 294 | case coro::ABI::RetconOnce: { |
| 295 | maybeFreeRetconStorage(Builder, Shape, FramePtr, CG); |
| 296 | auto *CoroEnd = cast<CoroEndInst>(Val: End); |
| 297 | auto *RetTy = Shape.getResumeFunctionType()->getReturnType(); |
| 298 | |
| 299 | if (!CoroEnd->hasResults()) { |
| 300 | assert(RetTy->isVoidTy()); |
| 301 | Builder.CreateRetVoid(); |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | auto *CoroResults = CoroEnd->getResults(); |
| 306 | unsigned NumReturns = CoroResults->numReturns(); |
| 307 | |
| 308 | if (auto *RetStructTy = dyn_cast<StructType>(Val: RetTy)) { |
| 309 | assert(RetStructTy->getNumElements() == NumReturns && |
| 310 | "numbers of returns should match resume function singature" ); |
| 311 | Value *ReturnValue = PoisonValue::get(T: RetStructTy); |
| 312 | unsigned Idx = 0; |
| 313 | for (Value *RetValEl : CoroResults->return_values()) |
| 314 | ReturnValue = Builder.CreateInsertValue(Agg: ReturnValue, Val: RetValEl, Idxs: Idx++); |
| 315 | Builder.CreateRet(V: ReturnValue); |
| 316 | } else if (NumReturns == 0) { |
| 317 | assert(RetTy->isVoidTy()); |
| 318 | Builder.CreateRetVoid(); |
| 319 | } else { |
| 320 | assert(NumReturns == 1); |
| 321 | Builder.CreateRet(V: *CoroResults->retval_begin()); |
| 322 | } |
| 323 | CoroResults->replaceAllUsesWith( |
| 324 | V: ConstantTokenNone::get(Context&: CoroResults->getContext())); |
| 325 | CoroResults->eraseFromParent(); |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | // In non-unique continuation lowering, we signal completion by returning |
| 330 | // a null continuation. |
| 331 | case coro::ABI::Retcon: { |
| 332 | assert(!cast<CoroEndInst>(End)->hasResults() && |
| 333 | "retcon coroutine should not return any values" ); |
| 334 | maybeFreeRetconStorage(Builder, Shape, FramePtr, CG); |
| 335 | auto RetTy = Shape.getResumeFunctionType()->getReturnType(); |
| 336 | auto RetStructTy = dyn_cast<StructType>(Val: RetTy); |
| 337 | PointerType *ContinuationTy = |
| 338 | cast<PointerType>(Val: RetStructTy ? RetStructTy->getElementType(N: 0) : RetTy); |
| 339 | |
| 340 | Value *ReturnValue = ConstantPointerNull::get(T: ContinuationTy); |
| 341 | if (RetStructTy) { |
| 342 | ReturnValue = Builder.CreateInsertValue(Agg: PoisonValue::get(T: RetStructTy), |
| 343 | Val: ReturnValue, Idxs: 0); |
| 344 | } |
| 345 | Builder.CreateRet(V: ReturnValue); |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // Remove the rest of the block, by splitting it into an unreachable block. |
| 351 | auto *BB = End->getParent(); |
| 352 | BB->splitBasicBlock(I: End); |
| 353 | BB->getTerminator()->eraseFromParent(); |
| 354 | } |
| 355 | |
| 356 | /// Create a pointer to the switch index field in the coroutine frame. |
| 357 | static Value *createSwitchIndexPtr(const coro::Shape &Shape, |
| 358 | IRBuilder<> &Builder, Value *FramePtr) { |
| 359 | auto *Offset = ConstantInt::get(Ty: Type::getInt64Ty(C&: FramePtr->getContext()), |
| 360 | V: Shape.SwitchLowering.IndexOffset); |
| 361 | return Builder.CreateInBoundsPtrAdd(Ptr: FramePtr, Offset, Name: "index.addr" ); |
| 362 | } |
| 363 | |
| 364 | // Mark a coroutine as done, which implies that the coroutine is finished and |
| 365 | // never gets resumed. |
| 366 | // |
| 367 | // In resume-switched ABI, the done state is represented by storing zero in |
| 368 | // ResumeFnAddr. |
| 369 | // |
| 370 | // NOTE: We couldn't omit the argument `FramePtr`. It is necessary because the |
| 371 | // pointer to the frame in splitted function is not stored in `Shape`. |
| 372 | static void markCoroutineAsDone(IRBuilder<> &Builder, const coro::Shape &Shape, |
| 373 | Value *FramePtr) { |
| 374 | assert( |
| 375 | Shape.ABI == coro::ABI::Switch && |
| 376 | "markCoroutineAsDone is only supported for Switch-Resumed ABI for now." ); |
| 377 | // Resume function pointer is always first |
| 378 | auto *NullPtr = ConstantPointerNull::get(T: Shape.getSwitchResumePointerType()); |
| 379 | Builder.CreateStore(Val: NullPtr, Ptr: FramePtr); |
| 380 | |
| 381 | // If the coroutine don't have unwind coro end, we could omit the store to |
| 382 | // the final suspend point since we could infer the coroutine is suspended |
| 383 | // at the final suspend point by the nullness of ResumeFnAddr. |
| 384 | // However, we can't skip it if the coroutine have unwind coro end. Since |
| 385 | // the coroutine reaches unwind coro end is considered suspended at the |
| 386 | // final suspend point (the ResumeFnAddr is null) but in fact the coroutine |
| 387 | // didn't complete yet. We need the IndexVal for the final suspend point |
| 388 | // to make the states clear. |
| 389 | if (Shape.SwitchLowering.HasUnwindCoroEnd && |
| 390 | Shape.SwitchLowering.HasFinalSuspend) { |
| 391 | assert(cast<CoroSuspendInst>(Shape.CoroSuspends.back())->isFinal() && |
| 392 | "The final suspend should only live in the last position of " |
| 393 | "CoroSuspends." ); |
| 394 | ConstantInt *IndexVal = Shape.getIndex(Value: Shape.CoroSuspends.size() - 1); |
| 395 | Value *FinalIndex = createSwitchIndexPtr(Shape, Builder, FramePtr); |
| 396 | Builder.CreateStore(Val: IndexVal, Ptr: FinalIndex); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /// Replace an unwind call to llvm.coro.end. |
| 401 | static void replaceUnwindCoroEnd(AnyCoroEndInst *End, const coro::Shape &Shape, |
| 402 | Value *FramePtr, bool InRamp, CallGraph *CG) { |
| 403 | IRBuilder<> Builder(End); |
| 404 | |
| 405 | switch (Shape.ABI) { |
| 406 | // In switch-lowering, this does nothing in the main function. |
| 407 | case coro::ABI::Switch: { |
| 408 | // In C++'s specification, the coroutine should be marked as done |
| 409 | // if promise.unhandled_exception() throws. The frontend will |
| 410 | // call coro.end(true) along this path. |
| 411 | // |
| 412 | // FIXME: We should refactor this once there is other language |
| 413 | // which uses Switch-Resumed style other than C++. |
| 414 | markCoroutineAsDone(Builder, Shape, FramePtr); |
| 415 | if (InRamp) |
| 416 | return; |
| 417 | break; |
| 418 | } |
| 419 | // In async lowering this does nothing. |
| 420 | case coro::ABI::Async: |
| 421 | break; |
| 422 | // In continuation-lowering, this frees the continuation storage. |
| 423 | case coro::ABI::Retcon: |
| 424 | case coro::ABI::RetconOnce: |
| 425 | maybeFreeRetconStorage(Builder, Shape, FramePtr, CG); |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | // If coro.end has an associated bundle, add cleanupret instruction. |
| 430 | if (auto Bundle = End->getOperandBundle(ID: LLVMContext::OB_funclet)) { |
| 431 | auto *FromPad = cast<CleanupPadInst>(Val: Bundle->Inputs[0]); |
| 432 | auto *CleanupRet = Builder.CreateCleanupRet(CleanupPad: FromPad, UnwindBB: nullptr); |
| 433 | End->getParent()->splitBasicBlock(I: End); |
| 434 | CleanupRet->getParent()->getTerminator()->eraseFromParent(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | static void replaceCoroEnd(AnyCoroEndInst *End, const coro::Shape &Shape, |
| 439 | Value *FramePtr, bool InRamp, CallGraph *CG) { |
| 440 | if (End->isUnwind()) |
| 441 | replaceUnwindCoroEnd(End, Shape, FramePtr, InRamp, CG); |
| 442 | else |
| 443 | replaceFallthroughCoroEnd(End, Shape, FramePtr, InRamp, CG); |
| 444 | End->eraseFromParent(); |
| 445 | } |
| 446 | |
| 447 | // In the resume function, we remove the last case (when coro::Shape is built, |
| 448 | // the final suspend point (if present) is always the last element of |
| 449 | // CoroSuspends array) since it is an undefined behavior to resume a coroutine |
| 450 | // suspended at the final suspend point. |
| 451 | // In the destroy function, if it isn't possible that the ResumeFnAddr is NULL |
| 452 | // and the coroutine doesn't suspend at the final suspend point actually (this |
| 453 | // is possible since the coroutine is considered suspended at the final suspend |
| 454 | // point if promise.unhandled_exception() exits via an exception), we can |
| 455 | // remove the last case. |
| 456 | void coro::BaseCloner::handleFinalSuspend() { |
| 457 | assert(Shape.ABI == coro::ABI::Switch && |
| 458 | Shape.SwitchLowering.HasFinalSuspend); |
| 459 | |
| 460 | if (isSwitchDestroyFunction() && Shape.SwitchLowering.HasUnwindCoroEnd) |
| 461 | return; |
| 462 | |
| 463 | auto *Switch = cast<SwitchInst>(Val&: VMap[Shape.SwitchLowering.ResumeSwitch]); |
| 464 | auto FinalCaseIt = std::prev(x: Switch->case_end()); |
| 465 | BasicBlock *ResumeBB = FinalCaseIt->getCaseSuccessor(); |
| 466 | |
| 467 | // Use SwitchInstProfUpdateWrapper to remove the case, keeping the profile |
| 468 | // branch weights in sync with the switch successors. |
| 469 | SwitchInstProfUpdateWrapper SwitchWrapper(*Switch); |
| 470 | SwitchWrapper.removeCase(I: FinalCaseIt); |
| 471 | if (isSwitchDestroyFunction()) { |
| 472 | BasicBlock *OldSwitchBB = Switch->getParent(); |
| 473 | auto *NewSwitchBB = OldSwitchBB->splitBasicBlock(I: Switch, BBName: "Switch" ); |
| 474 | Builder.SetInsertPoint(OldSwitchBB->getTerminator()); |
| 475 | |
| 476 | if (NewF->isCoroOnlyDestroyWhenComplete()) { |
| 477 | // When the coroutine can only be destroyed when complete, we don't need |
| 478 | // to generate code for other cases. |
| 479 | Builder.CreateBr(Dest: ResumeBB); |
| 480 | } else { |
| 481 | // Resume function pointer is always first |
| 482 | auto *Load = |
| 483 | Builder.CreateLoad(Ty: Shape.getSwitchResumePointerType(), Ptr: NewFramePtr); |
| 484 | auto *Cond = Builder.CreateIsNull(Arg: Load); |
| 485 | auto *Br = Builder.CreateCondBr(Cond, True: ResumeBB, False: NewSwitchBB); |
| 486 | applyProfMetadataIfEnabled(V: Br, setMetadataCallback: [&](Instruction *Inst) { |
| 487 | setExplicitlyUnknownBranchWeightsIfProfiled(I&: *Inst, DEBUG_TYPE, |
| 488 | F: Inst->getFunction()); |
| 489 | }); |
| 490 | } |
| 491 | OldSwitchBB->getTerminator()->eraseFromParent(); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static FunctionType * |
| 496 | getFunctionTypeFromAsyncSuspend(AnyCoroSuspendInst *Suspend) { |
| 497 | auto *AsyncSuspend = cast<CoroSuspendAsyncInst>(Val: Suspend); |
| 498 | auto *StructTy = cast<StructType>(Val: AsyncSuspend->getType()); |
| 499 | auto &Context = Suspend->getParent()->getParent()->getContext(); |
| 500 | auto *VoidTy = Type::getVoidTy(C&: Context); |
| 501 | return FunctionType::get(Result: VoidTy, Params: StructTy->elements(), isVarArg: false); |
| 502 | } |
| 503 | |
| 504 | static Function *createCloneDeclaration(Function &OrigF, coro::Shape &Shape, |
| 505 | const Twine &Suffix, |
| 506 | Module::iterator InsertBefore, |
| 507 | AnyCoroSuspendInst *ActiveSuspend) { |
| 508 | Module *M = OrigF.getParent(); |
| 509 | auto *FnTy = (Shape.ABI != coro::ABI::Async) |
| 510 | ? Shape.getResumeFunctionType() |
| 511 | : getFunctionTypeFromAsyncSuspend(Suspend: ActiveSuspend); |
| 512 | |
| 513 | Function *NewF = |
| 514 | Function::Create(Ty: FnTy, Linkage: GlobalValue::LinkageTypes::InternalLinkage, |
| 515 | AddrSpace: OrigF.getAddressSpace(), N: OrigF.getName() + Suffix); |
| 516 | |
| 517 | M->getFunctionList().insert(where: InsertBefore, New: NewF); |
| 518 | |
| 519 | return NewF; |
| 520 | } |
| 521 | |
| 522 | /// Replace uses of the active llvm.coro.suspend.retcon/async call with the |
| 523 | /// arguments to the continuation function. |
| 524 | /// |
| 525 | /// This assumes that the builder has a meaningful insertion point. |
| 526 | void coro::BaseCloner::replaceRetconOrAsyncSuspendUses() { |
| 527 | assert(Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce || |
| 528 | Shape.ABI == coro::ABI::Async); |
| 529 | |
| 530 | auto NewS = VMap[ActiveSuspend]; |
| 531 | if (NewS->use_empty()) |
| 532 | return; |
| 533 | |
| 534 | // Copy out all the continuation arguments after the buffer pointer into |
| 535 | // an easily-indexed data structure for convenience. |
| 536 | SmallVector<Value *, 8> Args; |
| 537 | // The async ABI includes all arguments -- including the first argument. |
| 538 | bool IsAsyncABI = Shape.ABI == coro::ABI::Async; |
| 539 | for (auto I = IsAsyncABI ? NewF->arg_begin() : std::next(x: NewF->arg_begin()), |
| 540 | E = NewF->arg_end(); |
| 541 | I != E; ++I) |
| 542 | Args.push_back(Elt: &*I); |
| 543 | |
| 544 | // If the suspend returns a single scalar value, we can just do a simple |
| 545 | // replacement. |
| 546 | if (!isa<StructType>(Val: NewS->getType())) { |
| 547 | assert(Args.size() == 1); |
| 548 | NewS->replaceAllUsesWith(V: Args.front()); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | // Try to peephole extracts of an aggregate return. |
| 553 | for (Use &U : llvm::make_early_inc_range(Range: NewS->uses())) { |
| 554 | auto *EVI = dyn_cast<ExtractValueInst>(Val: U.getUser()); |
| 555 | if (!EVI || EVI->getNumIndices() != 1) |
| 556 | continue; |
| 557 | |
| 558 | EVI->replaceAllUsesWith(V: Args[EVI->getIndices().front()]); |
| 559 | EVI->eraseFromParent(); |
| 560 | } |
| 561 | |
| 562 | // If we have no remaining uses, we're done. |
| 563 | if (NewS->use_empty()) |
| 564 | return; |
| 565 | |
| 566 | // Otherwise, we need to create an aggregate. |
| 567 | Value *Aggr = PoisonValue::get(T: NewS->getType()); |
| 568 | for (auto [Idx, Arg] : llvm::enumerate(First&: Args)) |
| 569 | Aggr = Builder.CreateInsertValue(Agg: Aggr, Val: Arg, Idxs: Idx); |
| 570 | |
| 571 | NewS->replaceAllUsesWith(V: Aggr); |
| 572 | } |
| 573 | |
| 574 | void coro::BaseCloner::replaceCoroSuspends() { |
| 575 | Value *SuspendResult; |
| 576 | |
| 577 | switch (Shape.ABI) { |
| 578 | // In switch lowering, replace coro.suspend with the appropriate value |
| 579 | // for the type of function we're extracting. |
| 580 | // Replacing coro.suspend with (0) will result in control flow proceeding to |
| 581 | // a resume label associated with a suspend point, replacing it with (1) will |
| 582 | // result in control flow proceeding to a cleanup label associated with this |
| 583 | // suspend point. |
| 584 | case coro::ABI::Switch: |
| 585 | SuspendResult = Builder.getInt8(C: isSwitchDestroyFunction() ? 1 : 0); |
| 586 | break; |
| 587 | |
| 588 | // In async lowering there are no uses of the result. |
| 589 | case coro::ABI::Async: |
| 590 | return; |
| 591 | |
| 592 | // In returned-continuation lowering, the arguments from earlier |
| 593 | // continuations are theoretically arbitrary, and they should have been |
| 594 | // spilled. |
| 595 | case coro::ABI::RetconOnce: |
| 596 | case coro::ABI::Retcon: |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | for (AnyCoroSuspendInst *CS : Shape.CoroSuspends) { |
| 601 | // The active suspend was handled earlier. |
| 602 | if (CS == ActiveSuspend) |
| 603 | continue; |
| 604 | |
| 605 | auto *MappedCS = cast<AnyCoroSuspendInst>(Val&: VMap[CS]); |
| 606 | MappedCS->replaceAllUsesWith(V: SuspendResult); |
| 607 | MappedCS->eraseFromParent(); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void coro::BaseCloner::replaceCoroEnds() { |
| 612 | for (AnyCoroEndInst *CE : Shape.CoroEnds) { |
| 613 | // We use a null call graph because there's no call graph node for |
| 614 | // the cloned function yet. We'll just be rebuilding that later. |
| 615 | auto *NewCE = cast<AnyCoroEndInst>(Val&: VMap[CE]); |
| 616 | replaceCoroEnd(End: NewCE, Shape, FramePtr: NewFramePtr, /*in ramp*/ InRamp: false, CG: nullptr); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void coro::BaseCloner::replaceCoroIsInRamp() { |
| 621 | auto &Ctx = OrigF.getContext(); |
| 622 | for (auto *II : Shape.CoroIsInRampInsts) { |
| 623 | auto *NewII = cast<CoroIsInRampInst>(Val&: VMap[II]); |
| 624 | NewII->replaceAllUsesWith(V: ConstantInt::getFalse(Context&: Ctx)); |
| 625 | NewII->eraseFromParent(); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static void replaceSwiftErrorOps(Function &F, coro::Shape &Shape, |
| 630 | ValueToValueMapTy *VMap) { |
| 631 | if (Shape.ABI == coro::ABI::Async && Shape.CoroSuspends.empty()) |
| 632 | return; |
| 633 | Value *CachedSlot = nullptr; |
| 634 | auto getSwiftErrorSlot = [&](Type *ValueTy) -> Value * { |
| 635 | if (CachedSlot) |
| 636 | return CachedSlot; |
| 637 | |
| 638 | // Check if the function has a swifterror argument. |
| 639 | for (auto &Arg : F.args()) { |
| 640 | if (Arg.isSwiftError()) { |
| 641 | CachedSlot = &Arg; |
| 642 | return &Arg; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // Create a swifterror alloca. |
| 647 | IRBuilder<> Builder(&F.getEntryBlock(), |
| 648 | F.getEntryBlock().getFirstNonPHIOrDbg()); |
| 649 | auto Alloca = Builder.CreateAlloca(Ty: ValueTy); |
| 650 | Alloca->setSwiftError(true); |
| 651 | |
| 652 | CachedSlot = Alloca; |
| 653 | return Alloca; |
| 654 | }; |
| 655 | |
| 656 | for (CallInst *Op : Shape.SwiftErrorOps) { |
| 657 | auto MappedOp = VMap ? cast<CallInst>(Val&: (*VMap)[Op]) : Op; |
| 658 | IRBuilder<> Builder(MappedOp); |
| 659 | |
| 660 | // If there are no arguments, this is a 'get' operation. |
| 661 | Value *MappedResult; |
| 662 | if (Op->arg_empty()) { |
| 663 | auto ValueTy = Op->getType(); |
| 664 | auto Slot = getSwiftErrorSlot(ValueTy); |
| 665 | MappedResult = Builder.CreateLoad(Ty: ValueTy, Ptr: Slot); |
| 666 | } else { |
| 667 | assert(Op->arg_size() == 1); |
| 668 | auto Value = MappedOp->getArgOperand(i: 0); |
| 669 | auto ValueTy = Value->getType(); |
| 670 | auto Slot = getSwiftErrorSlot(ValueTy); |
| 671 | Builder.CreateStore(Val: Value, Ptr: Slot); |
| 672 | MappedResult = Slot; |
| 673 | } |
| 674 | |
| 675 | MappedOp->replaceAllUsesWith(V: MappedResult); |
| 676 | MappedOp->eraseFromParent(); |
| 677 | } |
| 678 | |
| 679 | // If we're updating the original function, we've invalidated SwiftErrorOps. |
| 680 | if (VMap == nullptr) { |
| 681 | Shape.SwiftErrorOps.clear(); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | /// Returns all debug records in F. |
| 686 | static SmallVector<DbgVariableRecord *> |
| 687 | collectDbgVariableRecords(Function &F) { |
| 688 | SmallVector<DbgVariableRecord *> DbgVariableRecords; |
| 689 | for (auto &I : instructions(F)) { |
| 690 | for (DbgVariableRecord &DVR : filterDbgVars(R: I.getDbgRecordRange())) |
| 691 | DbgVariableRecords.push_back(Elt: &DVR); |
| 692 | } |
| 693 | return DbgVariableRecords; |
| 694 | } |
| 695 | |
| 696 | void coro::BaseCloner::replaceSwiftErrorOps() { |
| 697 | ::replaceSwiftErrorOps(F&: *NewF, Shape, VMap: &VMap); |
| 698 | } |
| 699 | |
| 700 | void coro::BaseCloner::salvageDebugInfo() { |
| 701 | auto DbgVariableRecords = collectDbgVariableRecords(F&: *NewF); |
| 702 | SmallDenseMap<Argument *, AllocaInst *, 4> ArgToAllocaMap; |
| 703 | |
| 704 | // Only 64-bit ABIs have a register we can refer to with the entry value. |
| 705 | bool UseEntryValue = OrigF.getParent()->getTargetTriple().isArch64Bit(); |
| 706 | for (DbgVariableRecord *DVR : DbgVariableRecords) |
| 707 | coro::salvageDebugInfo(ArgToAllocaMap, DVR&: *DVR, UseEntryValue); |
| 708 | |
| 709 | // Remove all salvaged dbg.declare intrinsics that became |
| 710 | // either unreachable or stale due to the CoroSplit transformation. |
| 711 | DominatorTree DomTree(*NewF); |
| 712 | auto IsUnreachableBlock = [&](BasicBlock *BB) { |
| 713 | return !isPotentiallyReachable(From: &NewF->getEntryBlock(), To: BB, ExclusionSet: nullptr, |
| 714 | DT: &DomTree); |
| 715 | }; |
| 716 | auto RemoveOne = [&](DbgVariableRecord *DVI) { |
| 717 | if (IsUnreachableBlock(DVI->getParent())) |
| 718 | DVI->eraseFromParent(); |
| 719 | else if (isa_and_nonnull<AllocaInst>(Val: DVI->getVariableLocationOp(OpIdx: 0))) { |
| 720 | // Count all non-debuginfo uses in reachable blocks. |
| 721 | unsigned Uses = 0; |
| 722 | for (auto *User : DVI->getVariableLocationOp(OpIdx: 0)->users()) |
| 723 | if (auto *I = dyn_cast<Instruction>(Val: User)) |
| 724 | if (!isa<AllocaInst>(Val: I) && !IsUnreachableBlock(I->getParent())) |
| 725 | ++Uses; |
| 726 | if (!Uses) |
| 727 | DVI->eraseFromParent(); |
| 728 | } |
| 729 | }; |
| 730 | for_each(Range&: DbgVariableRecords, F: RemoveOne); |
| 731 | } |
| 732 | |
| 733 | void coro::BaseCloner::replaceEntryBlock() { |
| 734 | // In the original function, the AllocaSpillBlock is a block immediately |
| 735 | // following the allocation of the frame object which defines GEPs for |
| 736 | // all the allocas that have been moved into the frame, and it ends by |
| 737 | // branching to the original beginning of the coroutine. Make this |
| 738 | // the entry block of the cloned function. |
| 739 | auto *Entry = cast<BasicBlock>(Val&: VMap[Shape.AllocaSpillBlock]); |
| 740 | auto *OldEntry = &NewF->getEntryBlock(); |
| 741 | Entry->setName("entry" + Suffix); |
| 742 | Entry->moveBefore(MovePos: OldEntry); |
| 743 | Entry->getTerminator()->eraseFromParent(); |
| 744 | |
| 745 | // Clear all predecessors of the new entry block. There should be |
| 746 | // exactly one predecessor, which we created when splitting out |
| 747 | // AllocaSpillBlock to begin with. |
| 748 | assert(Entry->hasOneUse()); |
| 749 | auto BranchToEntry = cast<UncondBrInst>(Val: Entry->user_back()); |
| 750 | Builder.SetInsertPoint(BranchToEntry); |
| 751 | Builder.CreateUnreachable(); |
| 752 | BranchToEntry->eraseFromParent(); |
| 753 | |
| 754 | // Branch from the entry to the appropriate place. |
| 755 | Builder.SetInsertPoint(Entry); |
| 756 | switch (Shape.ABI) { |
| 757 | case coro::ABI::Switch: { |
| 758 | // In switch-lowering, we built a resume-entry block in the original |
| 759 | // function. Make the entry block branch to this. |
| 760 | auto *SwitchBB = |
| 761 | cast<BasicBlock>(Val&: VMap[Shape.SwitchLowering.ResumeEntryBlock]); |
| 762 | Builder.CreateBr(Dest: SwitchBB); |
| 763 | SwitchBB->moveAfter(MovePos: Entry); |
| 764 | break; |
| 765 | } |
| 766 | case coro::ABI::Async: |
| 767 | case coro::ABI::Retcon: |
| 768 | case coro::ABI::RetconOnce: { |
| 769 | // In continuation ABIs, we want to branch to immediately after the |
| 770 | // active suspend point. Earlier phases will have put the suspend in its |
| 771 | // own basic block, so just thread our jump directly to its successor. |
| 772 | assert((Shape.ABI == coro::ABI::Async && |
| 773 | isa<CoroSuspendAsyncInst>(ActiveSuspend)) || |
| 774 | ((Shape.ABI == coro::ABI::Retcon || |
| 775 | Shape.ABI == coro::ABI::RetconOnce) && |
| 776 | isa<CoroSuspendRetconInst>(ActiveSuspend))); |
| 777 | auto *MappedCS = cast<AnyCoroSuspendInst>(Val&: VMap[ActiveSuspend]); |
| 778 | auto Branch = cast<UncondBrInst>(Val: MappedCS->getNextNode()); |
| 779 | Builder.CreateBr(Dest: Branch->getSuccessor(i: 0)); |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // Any static alloca that's still being used but not reachable from the new |
| 785 | // entry needs to be moved to the new entry. |
| 786 | Function *F = OldEntry->getParent(); |
| 787 | DominatorTree DT{*F}; |
| 788 | for (Instruction &I : llvm::make_early_inc_range(Range: instructions(F))) { |
| 789 | auto *Alloca = dyn_cast<AllocaInst>(Val: &I); |
| 790 | if (!Alloca || I.use_empty()) |
| 791 | continue; |
| 792 | if (DT.isReachableFromEntry(A: I.getParent()) || |
| 793 | !isa<ConstantInt>(Val: Alloca->getArraySize())) |
| 794 | continue; |
| 795 | I.moveBefore(BB&: *Entry, I: Entry->getFirstInsertionPt()); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /// Derive the value of the new frame pointer. |
| 800 | Value *coro::BaseCloner::deriveNewFramePointer() { |
| 801 | // Builder should be inserting to the front of the new entry block. |
| 802 | |
| 803 | switch (Shape.ABI) { |
| 804 | // In switch-lowering, the argument is the frame pointer. |
| 805 | case coro::ABI::Switch: |
| 806 | return &*NewF->arg_begin(); |
| 807 | // In async-lowering, one of the arguments is an async context as determined |
| 808 | // by the `llvm.coro.id.async` intrinsic. We can retrieve the async context of |
| 809 | // the resume function from the async context projection function associated |
| 810 | // with the active suspend. The frame is located as a tail to the async |
| 811 | // context header. |
| 812 | case coro::ABI::Async: { |
| 813 | auto *ActiveAsyncSuspend = cast<CoroSuspendAsyncInst>(Val: ActiveSuspend); |
| 814 | auto ContextIdx = ActiveAsyncSuspend->getStorageArgumentIndex() & 0xff; |
| 815 | auto *CalleeContext = NewF->getArg(i: ContextIdx); |
| 816 | auto *ProjectionFunc = |
| 817 | ActiveAsyncSuspend->getAsyncContextProjectionFunction(); |
| 818 | auto DbgLoc = |
| 819 | cast<CoroSuspendAsyncInst>(Val&: VMap[ActiveSuspend])->getDebugLoc(); |
| 820 | // Calling i8* (i8*) |
| 821 | auto *CallerContext = Builder.CreateCall(FTy: ProjectionFunc->getFunctionType(), |
| 822 | Callee: ProjectionFunc, Args: CalleeContext); |
| 823 | CallerContext->setCallingConv(ProjectionFunc->getCallingConv()); |
| 824 | CallerContext->setDebugLoc(DbgLoc); |
| 825 | // The frame is located after the async_context header. |
| 826 | auto &Context = Builder.getContext(); |
| 827 | auto *FramePtrAddr = Builder.CreateInBoundsPtrAdd( |
| 828 | Ptr: CallerContext, |
| 829 | Offset: ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), |
| 830 | V: Shape.AsyncLowering.FrameOffset), |
| 831 | Name: "async.ctx.frameptr" ); |
| 832 | // Inline the projection function. |
| 833 | InlineFunctionInfo InlineInfo; |
| 834 | auto InlineRes = InlineFunction(CB&: *CallerContext, IFI&: InlineInfo); |
| 835 | assert(InlineRes.isSuccess()); |
| 836 | (void)InlineRes; |
| 837 | return FramePtrAddr; |
| 838 | } |
| 839 | // In continuation-lowering, the argument is the opaque storage. |
| 840 | case coro::ABI::Retcon: |
| 841 | case coro::ABI::RetconOnce: { |
| 842 | Argument *NewStorage = &*NewF->arg_begin(); |
| 843 | auto FramePtrTy = PointerType::getUnqual(C&: Shape.FramePtr->getContext()); |
| 844 | |
| 845 | // If the storage is inline, just bitcast to the storage to the frame type. |
| 846 | if (Shape.RetconLowering.IsFrameInlineInStorage) |
| 847 | return NewStorage; |
| 848 | |
| 849 | // Otherwise, load the real frame from the opaque storage. |
| 850 | return Builder.CreateLoad(Ty: FramePtrTy, Ptr: NewStorage); |
| 851 | } |
| 852 | } |
| 853 | llvm_unreachable("bad ABI" ); |
| 854 | } |
| 855 | |
| 856 | /// Adjust the scope line of the funclet to the first line number after the |
| 857 | /// suspend point. This avoids a jump in the line table from the function |
| 858 | /// declaration (where prologue instructions are attributed to) to the suspend |
| 859 | /// point. |
| 860 | /// Only adjust the scope line when the files are the same. |
| 861 | /// If no candidate line number is found, fallback to the line of ActiveSuspend. |
| 862 | static void updateScopeLine(Instruction *ActiveSuspend, |
| 863 | DISubprogram &SPToUpdate) { |
| 864 | if (!ActiveSuspend) |
| 865 | return; |
| 866 | |
| 867 | // No subsequent instruction -> fallback to the location of ActiveSuspend. |
| 868 | if (!ActiveSuspend->getNextNode()) { |
| 869 | if (auto DL = ActiveSuspend->getDebugLoc()) |
| 870 | if (SPToUpdate.getFile() == DL->getFile()) |
| 871 | SPToUpdate.setScopeLine(DL->getLine()); |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | BasicBlock::iterator Successor = ActiveSuspend->getNextNode()->getIterator(); |
| 876 | // Corosplit splits the BB around ActiveSuspend, so the meaningful |
| 877 | // instructions are not in the same BB. |
| 878 | // FIXME: remove this hardcoded number of tries. |
| 879 | for (unsigned Repeat = 0; Repeat < 2; Repeat++) { |
| 880 | auto *Branch = dyn_cast_or_null<UncondBrInst>(Val&: Successor); |
| 881 | if (!Branch) |
| 882 | break; |
| 883 | Successor = Branch->getSuccessor()->getFirstNonPHIOrDbg(); |
| 884 | } |
| 885 | |
| 886 | // Find the first successor of ActiveSuspend with a non-zero line location. |
| 887 | // If that matches the file of ActiveSuspend, use it. |
| 888 | BasicBlock *PBB = Successor->getParent(); |
| 889 | for (; Successor != PBB->end(); Successor = std::next(x: Successor)) { |
| 890 | Successor = skipDebugIntrinsics(It: Successor); |
| 891 | auto DL = Successor->getDebugLoc(); |
| 892 | if (!DL || DL.getLine() == 0) |
| 893 | continue; |
| 894 | |
| 895 | if (SPToUpdate.getFile() == DL->getFile()) { |
| 896 | SPToUpdate.setScopeLine(DL.getLine()); |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | break; |
| 901 | } |
| 902 | |
| 903 | // If the search above failed, fallback to the location of ActiveSuspend. |
| 904 | if (auto DL = ActiveSuspend->getDebugLoc()) |
| 905 | if (SPToUpdate.getFile() == DL->getFile()) |
| 906 | SPToUpdate.setScopeLine(DL->getLine()); |
| 907 | } |
| 908 | |
| 909 | static void addFramePointerAttrs(AttributeList &Attrs, LLVMContext &Context, |
| 910 | unsigned ParamIndex, uint64_t Size, |
| 911 | Align Alignment, bool NoAlias) { |
| 912 | AttrBuilder ParamAttrs(Context); |
| 913 | ParamAttrs.addAttribute(Val: Attribute::NonNull); |
| 914 | ParamAttrs.addAttribute(Val: Attribute::NoUndef); |
| 915 | |
| 916 | if (NoAlias) |
| 917 | ParamAttrs.addAttribute(Val: Attribute::NoAlias); |
| 918 | |
| 919 | ParamAttrs.addAlignmentAttr(Align: Alignment); |
| 920 | ParamAttrs.addDereferenceableAttr(Bytes: Size); |
| 921 | Attrs = Attrs.addParamAttributes(C&: Context, ArgNo: ParamIndex, B: ParamAttrs); |
| 922 | } |
| 923 | |
| 924 | static void addAsyncContextAttrs(AttributeList &Attrs, LLVMContext &Context, |
| 925 | unsigned ParamIndex) { |
| 926 | AttrBuilder ParamAttrs(Context); |
| 927 | ParamAttrs.addAttribute(Val: Attribute::SwiftAsync); |
| 928 | Attrs = Attrs.addParamAttributes(C&: Context, ArgNo: ParamIndex, B: ParamAttrs); |
| 929 | } |
| 930 | |
| 931 | static void addSwiftSelfAttrs(AttributeList &Attrs, LLVMContext &Context, |
| 932 | unsigned ParamIndex) { |
| 933 | AttrBuilder ParamAttrs(Context); |
| 934 | ParamAttrs.addAttribute(Val: Attribute::SwiftSelf); |
| 935 | Attrs = Attrs.addParamAttributes(C&: Context, ArgNo: ParamIndex, B: ParamAttrs); |
| 936 | } |
| 937 | |
| 938 | /// Clone the body of the original function into a resume function of |
| 939 | /// some sort. |
| 940 | void coro::BaseCloner::create() { |
| 941 | assert(NewF); |
| 942 | |
| 943 | // Replace all args with dummy instructions. If an argument is the old frame |
| 944 | // pointer, the dummy will be replaced by the new frame pointer once it is |
| 945 | // computed below. Uses of all other arguments should have already been |
| 946 | // rewritten by buildCoroutineFrame() to use loads/stores on the coroutine |
| 947 | // frame. |
| 948 | SmallVector<Instruction *> DummyArgs; |
| 949 | for (Argument &A : OrigF.args()) { |
| 950 | DummyArgs.push_back(Elt: new FreezeInst(PoisonValue::get(T: A.getType()))); |
| 951 | VMap[&A] = DummyArgs.back(); |
| 952 | } |
| 953 | |
| 954 | SmallVector<ReturnInst *, 4> Returns; |
| 955 | |
| 956 | // Ignore attempts to change certain attributes of the function. |
| 957 | // TODO: maybe there should be a way to suppress this during cloning? |
| 958 | auto savedVisibility = NewF->getVisibility(); |
| 959 | auto savedUnnamedAddr = NewF->getUnnamedAddr(); |
| 960 | auto savedDLLStorageClass = NewF->getDLLStorageClass(); |
| 961 | |
| 962 | // NewF's linkage (which CloneFunctionInto does *not* change) might not |
| 963 | // be compatible with the visibility of OrigF (which it *does* change), |
| 964 | // so protect against that. |
| 965 | auto savedLinkage = NewF->getLinkage(); |
| 966 | NewF->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 967 | |
| 968 | CloneFunctionInto(NewFunc: NewF, OldFunc: &OrigF, VMap, |
| 969 | Changes: CloneFunctionChangeType::LocalChangesOnly, Returns); |
| 970 | |
| 971 | auto &Context = NewF->getContext(); |
| 972 | |
| 973 | if (DISubprogram *SP = NewF->getSubprogram()) { |
| 974 | assert(SP != OrigF.getSubprogram() && SP->isDistinct()); |
| 975 | updateScopeLine(ActiveSuspend, SPToUpdate&: *SP); |
| 976 | |
| 977 | // Update the linkage name and the function name to reflect the modified |
| 978 | // name. |
| 979 | MDString *NewLinkageName = MDString::get(Context, Str: NewF->getName()); |
| 980 | SP->replaceLinkageName(LN: NewLinkageName); |
| 981 | if (DISubprogram *Decl = SP->getDeclaration()) { |
| 982 | TempDISubprogram NewDecl = Decl->clone(); |
| 983 | NewDecl->replaceLinkageName(LN: NewLinkageName); |
| 984 | SP->replaceDeclaration(Decl: MDNode::replaceWithUniqued(N: std::move(NewDecl))); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | NewF->setLinkage(savedLinkage); |
| 989 | NewF->setVisibility(savedVisibility); |
| 990 | NewF->setUnnamedAddr(savedUnnamedAddr); |
| 991 | NewF->setDLLStorageClass(savedDLLStorageClass); |
| 992 | // The function sanitizer metadata needs to match the signature of the |
| 993 | // function it is being attached to. However this does not hold for split |
| 994 | // functions here. Thus remove the metadata for split functions. |
| 995 | if (Shape.ABI == coro::ABI::Switch && |
| 996 | NewF->hasMetadata(KindID: LLVMContext::MD_func_sanitize)) |
| 997 | NewF->eraseMetadata(KindID: LLVMContext::MD_func_sanitize); |
| 998 | |
| 999 | // Replace the attributes of the new function: |
| 1000 | auto OrigAttrs = NewF->getAttributes(); |
| 1001 | auto NewAttrs = AttributeList(); |
| 1002 | |
| 1003 | switch (Shape.ABI) { |
| 1004 | case coro::ABI::Switch: |
| 1005 | // Bootstrap attributes by copying function attributes from the |
| 1006 | // original function. This should include optimization settings and so on. |
| 1007 | NewAttrs = NewAttrs.addFnAttributes( |
| 1008 | C&: Context, B: AttrBuilder(Context, OrigAttrs.getFnAttrs())); |
| 1009 | |
| 1010 | addFramePointerAttrs(Attrs&: NewAttrs, Context, ParamIndex: 0, Size: Shape.FrameSize, |
| 1011 | Alignment: Shape.FrameAlign, /*NoAlias=*/false); |
| 1012 | break; |
| 1013 | case coro::ABI::Async: { |
| 1014 | auto *ActiveAsyncSuspend = cast<CoroSuspendAsyncInst>(Val: ActiveSuspend); |
| 1015 | if (OrigF.hasParamAttribute(ArgNo: Shape.AsyncLowering.ContextArgNo, |
| 1016 | Kind: Attribute::SwiftAsync)) { |
| 1017 | uint32_t ArgAttributeIndices = |
| 1018 | ActiveAsyncSuspend->getStorageArgumentIndex(); |
| 1019 | auto ContextArgIndex = ArgAttributeIndices & 0xff; |
| 1020 | addAsyncContextAttrs(Attrs&: NewAttrs, Context, ParamIndex: ContextArgIndex); |
| 1021 | |
| 1022 | // `swiftasync` must preceed `swiftself` so 0 is not a valid index for |
| 1023 | // `swiftself`. |
| 1024 | auto SwiftSelfIndex = ArgAttributeIndices >> 8; |
| 1025 | if (SwiftSelfIndex) |
| 1026 | addSwiftSelfAttrs(Attrs&: NewAttrs, Context, ParamIndex: SwiftSelfIndex); |
| 1027 | } |
| 1028 | |
| 1029 | // Transfer the original function's attributes. |
| 1030 | auto FnAttrs = OrigF.getAttributes().getFnAttrs(); |
| 1031 | NewAttrs = NewAttrs.addFnAttributes(C&: Context, B: AttrBuilder(Context, FnAttrs)); |
| 1032 | break; |
| 1033 | } |
| 1034 | case coro::ABI::Retcon: |
| 1035 | case coro::ABI::RetconOnce: |
| 1036 | // If we have a continuation prototype, just use its attributes, |
| 1037 | // full-stop. |
| 1038 | NewAttrs = Shape.RetconLowering.ResumePrototype->getAttributes(); |
| 1039 | |
| 1040 | /// FIXME: Is it really good to add the NoAlias attribute? |
| 1041 | addFramePointerAttrs(Attrs&: NewAttrs, Context, ParamIndex: 0, |
| 1042 | Size: Shape.getRetconCoroId()->getStorageSize(), |
| 1043 | Alignment: Shape.getRetconCoroId()->getStorageAlignment(), |
| 1044 | /*NoAlias=*/true); |
| 1045 | |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | switch (Shape.ABI) { |
| 1050 | // In these ABIs, the cloned functions always return 'void', and the |
| 1051 | // existing return sites are meaningless. Note that for unique |
| 1052 | // continuations, this includes the returns associated with suspends; |
| 1053 | // this is fine because we can't suspend twice. |
| 1054 | case coro::ABI::Switch: |
| 1055 | case coro::ABI::RetconOnce: |
| 1056 | // Remove old returns. |
| 1057 | for (ReturnInst *Return : Returns) |
| 1058 | changeToUnreachable(I: Return); |
| 1059 | break; |
| 1060 | |
| 1061 | // With multi-suspend continuations, we'll already have eliminated the |
| 1062 | // original returns and inserted returns before all the suspend points, |
| 1063 | // so we want to leave any returns in place. |
| 1064 | case coro::ABI::Retcon: |
| 1065 | break; |
| 1066 | // Async lowering will insert musttail call functions at all suspend points |
| 1067 | // followed by a return. |
| 1068 | // Don't change returns to unreachable because that will trip up the verifier. |
| 1069 | // These returns should be unreachable from the clone. |
| 1070 | case coro::ABI::Async: |
| 1071 | break; |
| 1072 | } |
| 1073 | |
| 1074 | NewF->setAttributes(NewAttrs); |
| 1075 | NewF->setCallingConv(Shape.getResumeFunctionCC()); |
| 1076 | |
| 1077 | // Set up the new entry block. |
| 1078 | replaceEntryBlock(); |
| 1079 | |
| 1080 | // Turn symmetric transfers into musttail calls. |
| 1081 | for (CallInst *ResumeCall : Shape.SymmetricTransfers) { |
| 1082 | ResumeCall = cast<CallInst>(Val&: VMap[ResumeCall]); |
| 1083 | if (TTI.supportsTailCallFor(CB: ResumeCall)) { |
| 1084 | // FIXME: Could we support symmetric transfer effectively without |
| 1085 | // musttail? |
| 1086 | ResumeCall->setTailCallKind(CallInst::TCK_MustTail); |
| 1087 | } |
| 1088 | |
| 1089 | // Put a 'ret void' after the call, and split any remaining instructions to |
| 1090 | // an unreachable block. |
| 1091 | BasicBlock *BB = ResumeCall->getParent(); |
| 1092 | BB->splitBasicBlock(I: ResumeCall->getNextNode()); |
| 1093 | Builder.SetInsertPoint(BB->getTerminator()); |
| 1094 | Builder.CreateRetVoid(); |
| 1095 | BB->getTerminator()->eraseFromParent(); |
| 1096 | } |
| 1097 | |
| 1098 | Builder.SetInsertPoint(&NewF->getEntryBlock().front()); |
| 1099 | NewFramePtr = deriveNewFramePointer(); |
| 1100 | |
| 1101 | // Remap frame pointer. |
| 1102 | Value *OldFramePtr = VMap[Shape.FramePtr]; |
| 1103 | NewFramePtr->takeName(V: OldFramePtr); |
| 1104 | OldFramePtr->replaceAllUsesWith(V: NewFramePtr); |
| 1105 | |
| 1106 | // Remap vFrame pointer. |
| 1107 | auto *NewVFrame = Builder.CreateBitCast( |
| 1108 | V: NewFramePtr, DestTy: PointerType::getUnqual(C&: Builder.getContext()), Name: "vFrame" ); |
| 1109 | Value *OldVFrame = cast<Value>(Val&: VMap[Shape.CoroBegin]); |
| 1110 | if (OldVFrame != NewVFrame) |
| 1111 | OldVFrame->replaceAllUsesWith(V: NewVFrame); |
| 1112 | |
| 1113 | // All uses of the arguments should have been resolved by this point, |
| 1114 | // so we can safely remove the dummy values. |
| 1115 | for (Instruction *DummyArg : DummyArgs) { |
| 1116 | DummyArg->replaceAllUsesWith(V: PoisonValue::get(T: DummyArg->getType())); |
| 1117 | DummyArg->deleteValue(); |
| 1118 | } |
| 1119 | |
| 1120 | switch (Shape.ABI) { |
| 1121 | case coro::ABI::Switch: |
| 1122 | // Rewrite final suspend handling as it is not done via switch (allows to |
| 1123 | // remove final case from the switch, since it is undefined behavior to |
| 1124 | // resume the coroutine suspended at the final suspend point. |
| 1125 | if (Shape.SwitchLowering.HasFinalSuspend) |
| 1126 | handleFinalSuspend(); |
| 1127 | break; |
| 1128 | case coro::ABI::Async: |
| 1129 | case coro::ABI::Retcon: |
| 1130 | case coro::ABI::RetconOnce: |
| 1131 | // Replace uses of the active suspend with the corresponding |
| 1132 | // continuation-function arguments. |
| 1133 | assert(ActiveSuspend != nullptr && |
| 1134 | "no active suspend when lowering a continuation-style coroutine" ); |
| 1135 | replaceRetconOrAsyncSuspendUses(); |
| 1136 | break; |
| 1137 | } |
| 1138 | |
| 1139 | // Handle suspends. |
| 1140 | replaceCoroSuspends(); |
| 1141 | |
| 1142 | // Handle swifterror. |
| 1143 | replaceSwiftErrorOps(); |
| 1144 | |
| 1145 | // Remove coro.end intrinsics. |
| 1146 | replaceCoroEnds(); |
| 1147 | |
| 1148 | replaceCoroIsInRamp(); |
| 1149 | |
| 1150 | // Salvage debug info that points into the coroutine frame. |
| 1151 | salvageDebugInfo(); |
| 1152 | } |
| 1153 | |
| 1154 | void coro::SwitchCloner::create() { |
| 1155 | // Create a new function matching the original type |
| 1156 | NewF = createCloneDeclaration(OrigF, Shape, Suffix, InsertBefore: OrigF.getParent()->end(), |
| 1157 | ActiveSuspend); |
| 1158 | |
| 1159 | // Clone the function |
| 1160 | coro::BaseCloner::create(); |
| 1161 | |
| 1162 | // Override EntryCount for the cloned resume function with the true sum of |
| 1163 | // all suspension points profile counts. |
| 1164 | if (FKind == coro::CloneKind::SwitchResume && OrigF.hasProfileData() && |
| 1165 | Shape.ResumeEntryCount.has_value()) { |
| 1166 | NewF->setEntryCount(Count: Shape.ResumeEntryCount.value()); |
| 1167 | } |
| 1168 | |
| 1169 | // Replacing coro.free with 'null' in cleanup to suppress deallocation code. |
| 1170 | if (FKind == coro::CloneKind::SwitchCleanup) |
| 1171 | elideCoroFree(FramePtr: NewFramePtr); |
| 1172 | } |
| 1173 | |
| 1174 | static void updateAsyncFuncPointerContextSize(coro::Shape &Shape) { |
| 1175 | assert(Shape.ABI == coro::ABI::Async); |
| 1176 | |
| 1177 | auto *FuncPtrStruct = cast<ConstantStruct>( |
| 1178 | Val: Shape.AsyncLowering.AsyncFuncPointer->getInitializer()); |
| 1179 | auto *OrigRelativeFunOffset = FuncPtrStruct->getOperand(i_nocapture: 0); |
| 1180 | auto *OrigContextSize = FuncPtrStruct->getOperand(i_nocapture: 1); |
| 1181 | auto *NewContextSize = ConstantInt::get(Ty: OrigContextSize->getType(), |
| 1182 | V: Shape.AsyncLowering.ContextSize); |
| 1183 | auto *NewFuncPtrStruct = ConstantStruct::get( |
| 1184 | T: FuncPtrStruct->getType(), Vs: OrigRelativeFunOffset, Vs: NewContextSize); |
| 1185 | |
| 1186 | Shape.AsyncLowering.AsyncFuncPointer->setInitializer(NewFuncPtrStruct); |
| 1187 | } |
| 1188 | |
| 1189 | static void replaceFrameSizeAndAlignment(coro::Shape &Shape) { |
| 1190 | if (Shape.ABI == coro::ABI::Async) |
| 1191 | updateAsyncFuncPointerContextSize(Shape); |
| 1192 | |
| 1193 | for (CoroAlignInst *CA : Shape.CoroAligns) { |
| 1194 | CA->replaceAllUsesWith( |
| 1195 | V: ConstantInt::get(Ty: CA->getType(), V: Shape.FrameAlign.value())); |
| 1196 | CA->eraseFromParent(); |
| 1197 | } |
| 1198 | |
| 1199 | if (Shape.CoroSizes.empty()) |
| 1200 | return; |
| 1201 | |
| 1202 | // In the same function all coro.sizes should have the same result type. |
| 1203 | auto *SizeIntrin = Shape.CoroSizes.back(); |
| 1204 | auto *SizeConstant = ConstantInt::get(Ty: SizeIntrin->getType(), |
| 1205 | V: TypeSize::getFixed(ExactSize: Shape.FrameSize)); |
| 1206 | |
| 1207 | for (CoroSizeInst *CS : Shape.CoroSizes) { |
| 1208 | CS->replaceAllUsesWith(V: SizeConstant); |
| 1209 | CS->eraseFromParent(); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | static void postSplitCleanup(Function &F) { |
| 1214 | removeUnreachableBlocks(F); |
| 1215 | |
| 1216 | #ifndef NDEBUG |
| 1217 | // For now, we do a mandatory verification step because we don't |
| 1218 | // entirely trust this pass. Note that we don't want to add a verifier |
| 1219 | // pass to FPM below because it will also verify all the global data. |
| 1220 | if (verifyFunction(F, &errs())) |
| 1221 | report_fatal_error("Broken function" ); |
| 1222 | #endif |
| 1223 | } |
| 1224 | |
| 1225 | // Coroutine has no suspend points. Remove heap allocation for the coroutine |
| 1226 | // frame if possible. |
| 1227 | static void handleNoSuspendCoroutine(coro::Shape &Shape) { |
| 1228 | auto *CoroBegin = Shape.CoroBegin; |
| 1229 | switch (Shape.ABI) { |
| 1230 | case coro::ABI::Switch: { |
| 1231 | if (auto *AllocInst = Shape.getSwitchCoroId()->getCoroAlloc()) { |
| 1232 | coro::elideCoroFree(FramePtr: CoroBegin); |
| 1233 | |
| 1234 | IRBuilder<> Builder(AllocInst); |
| 1235 | // Create an alloca for a byte array of the frame size |
| 1236 | auto *FrameTy = ArrayType::get(ElementType: Type::getInt8Ty(C&: Builder.getContext()), |
| 1237 | NumElements: Shape.FrameSize); |
| 1238 | auto *Frame = Builder.CreateAlloca( |
| 1239 | Ty: FrameTy, ArraySize: nullptr, Name: AllocInst->getFunction()->getName() + ".Frame" ); |
| 1240 | Frame->setAlignment(Shape.FrameAlign); |
| 1241 | AllocInst->replaceAllUsesWith(V: Builder.getFalse()); |
| 1242 | AllocInst->eraseFromParent(); |
| 1243 | CoroBegin->replaceAllUsesWith(V: Frame); |
| 1244 | } else { |
| 1245 | CoroBegin->replaceAllUsesWith(V: CoroBegin->getMem()); |
| 1246 | } |
| 1247 | |
| 1248 | break; |
| 1249 | } |
| 1250 | case coro::ABI::Async: |
| 1251 | case coro::ABI::Retcon: |
| 1252 | case coro::ABI::RetconOnce: |
| 1253 | CoroBegin->replaceAllUsesWith(V: PoisonValue::get(T: CoroBegin->getType())); |
| 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | CoroBegin->eraseFromParent(); |
| 1258 | Shape.CoroBegin = nullptr; |
| 1259 | } |
| 1260 | |
| 1261 | // SimplifySuspendPoint needs to check that there is no calls between |
| 1262 | // coro_save and coro_suspend, since any of the calls may potentially resume |
| 1263 | // the coroutine and if that is the case we cannot eliminate the suspend point. |
| 1264 | static bool hasCallsInBlockBetween(iterator_range<BasicBlock::iterator> R) { |
| 1265 | for (Instruction &I : R) { |
| 1266 | // Assume that no intrinsic can resume the coroutine. |
| 1267 | if (isa<IntrinsicInst>(Val: I)) |
| 1268 | continue; |
| 1269 | |
| 1270 | if (isa<CallBase>(Val: I)) |
| 1271 | return true; |
| 1272 | } |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | static bool hasCallsInBlocksBetween(BasicBlock *SaveBB, BasicBlock *ResDesBB) { |
| 1277 | SmallPtrSet<BasicBlock *, 8> Set; |
| 1278 | SmallVector<BasicBlock *, 8> Worklist; |
| 1279 | |
| 1280 | Set.insert(Ptr: SaveBB); |
| 1281 | Worklist.push_back(Elt: ResDesBB); |
| 1282 | |
| 1283 | // Accumulate all blocks between SaveBB and ResDesBB. Because CoroSaveIntr |
| 1284 | // returns a token consumed by suspend instruction, all blocks in between |
| 1285 | // will have to eventually hit SaveBB when going backwards from ResDesBB. |
| 1286 | while (!Worklist.empty()) { |
| 1287 | auto *BB = Worklist.pop_back_val(); |
| 1288 | Set.insert(Ptr: BB); |
| 1289 | for (auto *Pred : predecessors(BB)) |
| 1290 | if (!Set.contains(Ptr: Pred)) |
| 1291 | Worklist.push_back(Elt: Pred); |
| 1292 | } |
| 1293 | |
| 1294 | // SaveBB and ResDesBB are checked separately in hasCallsBetween. |
| 1295 | Set.erase(Ptr: SaveBB); |
| 1296 | Set.erase(Ptr: ResDesBB); |
| 1297 | |
| 1298 | for (auto *BB : Set) |
| 1299 | if (hasCallsInBlockBetween(R: {BB->getFirstNonPHIIt(), BB->end()})) |
| 1300 | return true; |
| 1301 | |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
| 1305 | static bool hasCallsBetween(Instruction *Save, Instruction *ResumeOrDestroy) { |
| 1306 | auto *SaveBB = Save->getParent(); |
| 1307 | auto *ResumeOrDestroyBB = ResumeOrDestroy->getParent(); |
| 1308 | BasicBlock::iterator SaveIt = Save->getIterator(); |
| 1309 | BasicBlock::iterator ResumeOrDestroyIt = ResumeOrDestroy->getIterator(); |
| 1310 | |
| 1311 | if (SaveBB == ResumeOrDestroyBB) |
| 1312 | return hasCallsInBlockBetween(R: {std::next(x: SaveIt), ResumeOrDestroyIt}); |
| 1313 | |
| 1314 | // Any calls from Save to the end of the block? |
| 1315 | if (hasCallsInBlockBetween(R: {std::next(x: SaveIt), SaveBB->end()})) |
| 1316 | return true; |
| 1317 | |
| 1318 | // Any calls from begging of the block up to ResumeOrDestroy? |
| 1319 | if (hasCallsInBlockBetween( |
| 1320 | R: {ResumeOrDestroyBB->getFirstNonPHIIt(), ResumeOrDestroyIt})) |
| 1321 | return true; |
| 1322 | |
| 1323 | // Any calls in all of the blocks between SaveBB and ResumeOrDestroyBB? |
| 1324 | if (hasCallsInBlocksBetween(SaveBB, ResDesBB: ResumeOrDestroyBB)) |
| 1325 | return true; |
| 1326 | |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
| 1330 | // If a SuspendIntrin is preceded by Resume or Destroy, we can eliminate the |
| 1331 | // suspend point and replace it with nornal control flow. |
| 1332 | static bool simplifySuspendPoint(CoroSuspendInst *Suspend, |
| 1333 | CoroBeginInst *CoroBegin) { |
| 1334 | Instruction *Prev = Suspend->getPrevNode(); |
| 1335 | if (!Prev) { |
| 1336 | auto *Pred = Suspend->getParent()->getSinglePredecessor(); |
| 1337 | if (!Pred) |
| 1338 | return false; |
| 1339 | Prev = Pred->getTerminator(); |
| 1340 | } |
| 1341 | |
| 1342 | CallBase *CB = dyn_cast<CallBase>(Val: Prev); |
| 1343 | if (!CB) |
| 1344 | return false; |
| 1345 | |
| 1346 | auto *Callee = CB->getCalledOperand()->stripPointerCasts(); |
| 1347 | |
| 1348 | // See if the callsite is for resumption or destruction of the coroutine. |
| 1349 | auto *SubFn = dyn_cast<CoroSubFnInst>(Val: Callee); |
| 1350 | if (!SubFn) |
| 1351 | return false; |
| 1352 | |
| 1353 | // Does not refer to the current coroutine, we cannot do anything with it. |
| 1354 | if (SubFn->getFrame() != CoroBegin) |
| 1355 | return false; |
| 1356 | |
| 1357 | // See if the transformation is safe. Specifically, see if there are any |
| 1358 | // calls in between Save and CallInstr. They can potenitally resume the |
| 1359 | // coroutine rendering this optimization unsafe. |
| 1360 | auto *Save = Suspend->getCoroSave(); |
| 1361 | if (hasCallsBetween(Save, ResumeOrDestroy: CB)) |
| 1362 | return false; |
| 1363 | |
| 1364 | // Replace llvm.coro.suspend with the value that results in resumption over |
| 1365 | // the resume or cleanup path. |
| 1366 | Suspend->replaceAllUsesWith(V: SubFn->getRawIndex()); |
| 1367 | Suspend->eraseFromParent(); |
| 1368 | Save->eraseFromParent(); |
| 1369 | |
| 1370 | // No longer need a call to coro.resume or coro.destroy. |
| 1371 | if (auto *Invoke = dyn_cast<InvokeInst>(Val: CB)) { |
| 1372 | UncondBrInst::Create(Target: Invoke->getNormalDest(), InsertBefore: Invoke->getIterator()); |
| 1373 | } |
| 1374 | |
| 1375 | // Grab the CalledValue from CB before erasing the CallInstr. |
| 1376 | auto *CalledValue = CB->getCalledOperand(); |
| 1377 | CB->eraseFromParent(); |
| 1378 | |
| 1379 | // If no more users remove it. Usually it is a bitcast of SubFn. |
| 1380 | if (CalledValue != SubFn && CalledValue->user_empty()) |
| 1381 | if (auto *I = dyn_cast<Instruction>(Val: CalledValue)) |
| 1382 | I->eraseFromParent(); |
| 1383 | |
| 1384 | // Now we are good to remove SubFn. |
| 1385 | if (SubFn->user_empty()) |
| 1386 | SubFn->eraseFromParent(); |
| 1387 | |
| 1388 | return true; |
| 1389 | } |
| 1390 | |
| 1391 | // Remove suspend points that are simplified. |
| 1392 | static void simplifySuspendPoints(coro::Shape &Shape) { |
| 1393 | // Currently, the only simplification we do is switch-lowering-specific. |
| 1394 | if (Shape.ABI != coro::ABI::Switch) |
| 1395 | return; |
| 1396 | |
| 1397 | auto &S = Shape.CoroSuspends; |
| 1398 | size_t I = 0, N = S.size(); |
| 1399 | if (N == 0) |
| 1400 | return; |
| 1401 | |
| 1402 | size_t ChangedFinalIndex = std::numeric_limits<size_t>::max(); |
| 1403 | while (true) { |
| 1404 | auto SI = cast<CoroSuspendInst>(Val: S[I]); |
| 1405 | // Leave final.suspend to handleFinalSuspend since it is undefined behavior |
| 1406 | // to resume a coroutine suspended at the final suspend point. |
| 1407 | if (!SI->isFinal() && simplifySuspendPoint(Suspend: SI, CoroBegin: Shape.CoroBegin)) { |
| 1408 | if (--N == I) |
| 1409 | break; |
| 1410 | |
| 1411 | std::swap(a&: S[I], b&: S[N]); |
| 1412 | |
| 1413 | if (cast<CoroSuspendInst>(Val: S[I])->isFinal()) { |
| 1414 | assert(Shape.SwitchLowering.HasFinalSuspend); |
| 1415 | ChangedFinalIndex = I; |
| 1416 | } |
| 1417 | |
| 1418 | continue; |
| 1419 | } |
| 1420 | if (++I == N) |
| 1421 | break; |
| 1422 | } |
| 1423 | S.resize(N); |
| 1424 | |
| 1425 | // Maintain final.suspend in case final suspend was swapped. |
| 1426 | // Due to we requrie the final suspend to be the last element of CoroSuspends. |
| 1427 | if (ChangedFinalIndex < N) { |
| 1428 | assert(cast<CoroSuspendInst>(S[ChangedFinalIndex])->isFinal()); |
| 1429 | std::swap(a&: S[ChangedFinalIndex], b&: S.back()); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | namespace { |
| 1434 | |
| 1435 | struct SwitchCoroutineSplitter { |
| 1436 | static void split(Function &F, coro::Shape &Shape, |
| 1437 | SmallVectorImpl<Function *> &Clones, |
| 1438 | TargetTransformInfo &TTI) { |
| 1439 | assert(Shape.ABI == coro::ABI::Switch); |
| 1440 | |
| 1441 | // Create a resume clone by cloning the body of the original function, |
| 1442 | // setting new entry block and replacing coro.suspend an appropriate value |
| 1443 | // to force resume or cleanup pass for every suspend point. |
| 1444 | createResumeEntryBlock(F, Shape); |
| 1445 | auto *ResumeClone = coro::SwitchCloner::createClone( |
| 1446 | OrigF&: F, Suffix: ".resume" , Shape, FKind: coro::CloneKind::SwitchResume, TTI); |
| 1447 | auto *DestroyClone = coro::SwitchCloner::createClone( |
| 1448 | OrigF&: F, Suffix: ".destroy" , Shape, FKind: coro::CloneKind::SwitchUnwind, TTI); |
| 1449 | auto *CleanupClone = coro::SwitchCloner::createClone( |
| 1450 | OrigF&: F, Suffix: ".cleanup" , Shape, FKind: coro::CloneKind::SwitchCleanup, TTI); |
| 1451 | |
| 1452 | if (Shape.SwitchLowering.HasCoroElideNoAllocVariant) |
| 1453 | replaceSwitchResumeCoroFree(Shape, Resume&: *ResumeClone, Cleanup&: *CleanupClone); |
| 1454 | |
| 1455 | postSplitCleanup(F&: *ResumeClone); |
| 1456 | postSplitCleanup(F&: *DestroyClone); |
| 1457 | postSplitCleanup(F&: *CleanupClone); |
| 1458 | |
| 1459 | // Store addresses resume/destroy/cleanup functions in the coroutine frame. |
| 1460 | updateCoroFrame(Shape, ResumeFn: ResumeClone, DestroyFn: DestroyClone, CleanupFn: CleanupClone); |
| 1461 | |
| 1462 | assert(Clones.empty()); |
| 1463 | Clones.push_back(Elt: ResumeClone); |
| 1464 | Clones.push_back(Elt: DestroyClone); |
| 1465 | Clones.push_back(Elt: CleanupClone); |
| 1466 | |
| 1467 | // Create a constant array referring to resume/destroy/clone functions |
| 1468 | // pointed by the last argument of @llvm.coro.info, so that CoroElide pass |
| 1469 | // can determined correct function to call. |
| 1470 | setCoroInfo(F, Shape, Fns: Clones); |
| 1471 | } |
| 1472 | |
| 1473 | // Create a variant of ramp function that does not perform heap allocation |
| 1474 | // for a switch ABI coroutine. |
| 1475 | // |
| 1476 | // The newly split `.noalloc` ramp function has the following differences: |
| 1477 | // - Has one additional frame pointer parameter in lieu of dynamic |
| 1478 | // allocation. |
| 1479 | // - Suppressed allocations by replacing coro.alloc and coro.free. |
| 1480 | static Function *createNoAllocVariant(Function &F, coro::Shape &Shape, |
| 1481 | SmallVectorImpl<Function *> &Clones) { |
| 1482 | assert(Shape.ABI == coro::ABI::Switch); |
| 1483 | auto *OrigFnTy = F.getFunctionType(); |
| 1484 | auto OldParams = OrigFnTy->params(); |
| 1485 | |
| 1486 | SmallVector<Type *> NewParams; |
| 1487 | NewParams.reserve(N: OldParams.size() + 1); |
| 1488 | NewParams.append(in_start: OldParams.begin(), in_end: OldParams.end()); |
| 1489 | NewParams.push_back(Elt: PointerType::getUnqual(C&: Shape.FramePtr->getContext())); |
| 1490 | |
| 1491 | auto *NewFnTy = FunctionType::get(Result: OrigFnTy->getReturnType(), Params: NewParams, |
| 1492 | isVarArg: OrigFnTy->isVarArg()); |
| 1493 | Function *NoAllocF = Function::Create( |
| 1494 | Ty: NewFnTy, Linkage: F.getLinkage(), AddrSpace: F.getAddressSpace(), N: F.getName() + ".noalloc" ); |
| 1495 | |
| 1496 | ValueToValueMapTy VMap; |
| 1497 | unsigned int Idx = 0; |
| 1498 | for (const auto &I : F.args()) { |
| 1499 | VMap[&I] = NoAllocF->getArg(i: Idx++); |
| 1500 | } |
| 1501 | // We just appended the frame pointer as the last argument of the new |
| 1502 | // function. |
| 1503 | auto FrameIdx = NoAllocF->arg_size() - 1; |
| 1504 | SmallVector<ReturnInst *, 4> Returns; |
| 1505 | CloneFunctionInto(NewFunc: NoAllocF, OldFunc: &F, VMap, |
| 1506 | Changes: CloneFunctionChangeType::LocalChangesOnly, Returns); |
| 1507 | |
| 1508 | if (Shape.CoroBegin) { |
| 1509 | auto *NewCoroBegin = |
| 1510 | cast_if_present<CoroBeginInst>(Val&: VMap[Shape.CoroBegin]); |
| 1511 | coro::elideCoroFree(FramePtr: NewCoroBegin); |
| 1512 | coro::suppressCoroAllocs(CoroId: cast<CoroIdInst>(Val: NewCoroBegin->getId())); |
| 1513 | NewCoroBegin->replaceAllUsesWith(V: NoAllocF->getArg(i: FrameIdx)); |
| 1514 | NewCoroBegin->eraseFromParent(); |
| 1515 | } |
| 1516 | |
| 1517 | Module *M = F.getParent(); |
| 1518 | M->getFunctionList().insert(where: M->end(), New: NoAllocF); |
| 1519 | |
| 1520 | removeUnreachableBlocks(F&: *NoAllocF); |
| 1521 | auto NewAttrs = NoAllocF->getAttributes(); |
| 1522 | // When we elide allocation, we read these attributes to determine the |
| 1523 | // frame size and alignment. |
| 1524 | addFramePointerAttrs(Attrs&: NewAttrs, Context&: NoAllocF->getContext(), ParamIndex: FrameIdx, |
| 1525 | Size: Shape.FrameSize, Alignment: Shape.FrameAlign, |
| 1526 | /*NoAlias=*/false); |
| 1527 | |
| 1528 | NoAllocF->setAttributes(NewAttrs); |
| 1529 | |
| 1530 | Clones.push_back(Elt: NoAllocF); |
| 1531 | // Reset the original function's coro info, make the new noalloc variant |
| 1532 | // connected to the original ramp function. |
| 1533 | setCoroInfo(F, Shape, Fns: Clones); |
| 1534 | // After copying, set the linkage to internal linkage. Original function |
| 1535 | // may have different linkage, but optimization dependent on this function |
| 1536 | // generally relies on LTO. |
| 1537 | NoAllocF->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1538 | return NoAllocF; |
| 1539 | } |
| 1540 | |
| 1541 | private: |
| 1542 | // Create an entry block for a resume function with a switch that will jump to |
| 1543 | // suspend points. |
| 1544 | static void createResumeEntryBlock(Function &F, coro::Shape &Shape) { |
| 1545 | LLVMContext &C = F.getContext(); |
| 1546 | |
| 1547 | DIBuilder DBuilder(*F.getParent(), /*AllowUnresolved*/ false); |
| 1548 | DISubprogram *DIS = F.getSubprogram(); |
| 1549 | // If there is no DISubprogram for F, it implies the function is compiled |
| 1550 | // without debug info. So we also don't generate debug info for the |
| 1551 | // suspension points. |
| 1552 | bool AddDebugLabels = DIS && DIS->getUnit() && |
| 1553 | (DIS->getUnit()->getEmissionKind() == |
| 1554 | DICompileUnit::DebugEmissionKind::FullDebug); |
| 1555 | |
| 1556 | // resume.entry: |
| 1557 | // %index.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 |
| 1558 | // 0, i32 2 % index = load i32, i32* %index.addr switch i32 %index, label |
| 1559 | // %unreachable [ |
| 1560 | // i32 0, label %resume.0 |
| 1561 | // i32 1, label %resume.1 |
| 1562 | // ... |
| 1563 | // ] |
| 1564 | |
| 1565 | auto *NewEntry = BasicBlock::Create(Context&: C, Name: "resume.entry" , Parent: &F); |
| 1566 | auto *UnreachBB = BasicBlock::Create(Context&: C, Name: "unreachable" , Parent: &F); |
| 1567 | |
| 1568 | IRBuilder<> Builder(NewEntry); |
| 1569 | auto *FramePtr = Shape.FramePtr; |
| 1570 | Value *GepIndex = createSwitchIndexPtr(Shape, Builder, FramePtr); |
| 1571 | auto *Index = Builder.CreateLoad(Ty: Shape.getIndexType(), Ptr: GepIndex, Name: "index" ); |
| 1572 | auto *Switch = |
| 1573 | Builder.CreateSwitch(V: Index, Dest: UnreachBB, NumCases: Shape.CoroSuspends.size()); |
| 1574 | Shape.SwitchLowering.ResumeSwitch = Switch; |
| 1575 | |
| 1576 | // Split all coro.suspend calls |
| 1577 | size_t SuspendIndex = 0; |
| 1578 | SmallVector<uint64_t, 8> SwitchWeights64; |
| 1579 | // Default destination (unreachable) has weight 0 |
| 1580 | SwitchWeights64.push_back(Elt: 0); |
| 1581 | |
| 1582 | for (auto *AnyS : Shape.CoroSuspends) { |
| 1583 | auto *S = cast<CoroSuspendInst>(Val: AnyS); |
| 1584 | ConstantInt *IndexVal = Shape.getIndex(Value: SuspendIndex); |
| 1585 | |
| 1586 | // Replace CoroSave with a store to Index: |
| 1587 | // %index.addr = getelementptr %f.frame... (index field number) |
| 1588 | // store i32 %IndexVal, i32* %index.addr1 |
| 1589 | auto *Save = S->getCoroSave(); |
| 1590 | Builder.SetInsertPoint(Save); |
| 1591 | if (S->isFinal()) { |
| 1592 | // The coroutine should be marked done if it reaches the final suspend |
| 1593 | // point. |
| 1594 | markCoroutineAsDone(Builder, Shape, FramePtr); |
| 1595 | } else { |
| 1596 | Value *GepIndex = createSwitchIndexPtr(Shape, Builder, FramePtr); |
| 1597 | Builder.CreateStore(Val: IndexVal, Ptr: GepIndex); |
| 1598 | } |
| 1599 | |
| 1600 | Save->replaceAllUsesWith(V: ConstantTokenNone::get(Context&: C)); |
| 1601 | Save->eraseFromParent(); |
| 1602 | |
| 1603 | // Split block before and after coro.suspend and add a jump from an entry |
| 1604 | // switch: |
| 1605 | // |
| 1606 | // whateverBB: |
| 1607 | // whatever |
| 1608 | // %0 = call i8 @llvm.coro.suspend(token none, i1 false) |
| 1609 | // switch i8 %0, label %suspend[i8 0, label %resume |
| 1610 | // i8 1, label %cleanup] |
| 1611 | // becomes: |
| 1612 | // |
| 1613 | // whateverBB: |
| 1614 | // whatever |
| 1615 | // br label %resume.0.landing |
| 1616 | // |
| 1617 | // resume.0: ; <--- jump from the switch in the resume.entry |
| 1618 | // #dbg_label(...) ; <--- artificial label for debuggers |
| 1619 | // %0 = tail call i8 @llvm.coro.suspend(token none, i1 false) |
| 1620 | // br label %resume.0.landing |
| 1621 | // |
| 1622 | // resume.0.landing: |
| 1623 | // %1 = phi i8[-1, %whateverBB], [%0, %resume.0] |
| 1624 | // switch i8 % 1, label %suspend [i8 0, label %resume |
| 1625 | // i8 1, label %cleanup] |
| 1626 | |
| 1627 | auto *SuspendBB = S->getParent(); |
| 1628 | auto *ResumeBB = |
| 1629 | SuspendBB->splitBasicBlock(I: S, BBName: "resume." + Twine(SuspendIndex)); |
| 1630 | auto *LandingBB = ResumeBB->splitBasicBlock( |
| 1631 | I: S->getNextNode(), BBName: ResumeBB->getName() + Twine(".landing" )); |
| 1632 | Switch->addCase(OnVal: IndexVal, Dest: ResumeBB); |
| 1633 | |
| 1634 | // Get pre-split frequency for this suspend point |
| 1635 | uint64_t Weight = 1; // Default fallback weight |
| 1636 | auto It = Shape.SuspendFreqs.find(Val: AnyS); |
| 1637 | if (It != Shape.SuspendFreqs.end()) { |
| 1638 | Weight = It->second; |
| 1639 | } |
| 1640 | SwitchWeights64.push_back(Elt: Weight); |
| 1641 | |
| 1642 | cast<UncondBrInst>(Val: SuspendBB->getTerminator())->setSuccessor(LandingBB); |
| 1643 | auto *PN = PHINode::Create(Ty: Builder.getInt8Ty(), NumReservedValues: 2, NameStr: "" ); |
| 1644 | PN->insertBefore(InsertPos: LandingBB->begin()); |
| 1645 | S->replaceAllUsesWith(V: PN); |
| 1646 | PN->addIncoming(V: Builder.getInt8(C: -1), BB: SuspendBB); |
| 1647 | PN->addIncoming(V: S, BB: ResumeBB); |
| 1648 | |
| 1649 | if (AddDebugLabels) { |
| 1650 | if (DebugLoc SuspendLoc = S->getDebugLoc()) { |
| 1651 | std::string LabelName = |
| 1652 | ("__coro_resume_" + Twine(SuspendIndex)).str(); |
| 1653 | // Take the "inlined at" location recursively, if present. This is |
| 1654 | // mandatory as the DILabel insertion checks that the scopes of label |
| 1655 | // and the attached location match. This is not the case when the |
| 1656 | // suspend location has been inlined due to pointing to the original |
| 1657 | // scope. |
| 1658 | DILocation *DILoc = SuspendLoc; |
| 1659 | while (DILocation *InlinedAt = DILoc->getInlinedAt()) |
| 1660 | DILoc = InlinedAt; |
| 1661 | |
| 1662 | DILabel *ResumeLabel = |
| 1663 | DBuilder.createLabel(Scope: DIS, Name: LabelName, File: DILoc->getFile(), |
| 1664 | LineNo: SuspendLoc.getLine(), Column: SuspendLoc.getCol(), |
| 1665 | /*IsArtificial=*/true, |
| 1666 | /*CoroSuspendIdx=*/SuspendIndex, |
| 1667 | /*AlwaysPreserve=*/false); |
| 1668 | DBuilder.insertLabel(LabelInfo: ResumeLabel, DL: DILoc, InsertPt: ResumeBB->begin()); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | ++SuspendIndex; |
| 1673 | } |
| 1674 | |
| 1675 | if (!Shape.SuspendFreqs.empty()) { |
| 1676 | auto SwitchWeights32 = llvm::fitWeights(Weights: SwitchWeights64); |
| 1677 | MDBuilder MDB(C); |
| 1678 | Switch->setMetadata(KindID: LLVMContext::MD_prof, |
| 1679 | Node: MDB.createBranchWeights(Weights: SwitchWeights32)); |
| 1680 | } |
| 1681 | |
| 1682 | Builder.SetInsertPoint(UnreachBB); |
| 1683 | Builder.CreateUnreachable(); |
| 1684 | DBuilder.finalize(); |
| 1685 | |
| 1686 | Shape.SwitchLowering.ResumeEntryBlock = NewEntry; |
| 1687 | } |
| 1688 | |
| 1689 | // Store addresses of Resume/Destroy/Cleanup functions in the coroutine frame. |
| 1690 | static void updateCoroFrame(coro::Shape &Shape, Function *ResumeFn, |
| 1691 | Function *DestroyFn, Function *CleanupFn) { |
| 1692 | IRBuilder<> Builder(&*Shape.getInsertPtAfterFramePtr()); |
| 1693 | LLVMContext &C = ResumeFn->getContext(); |
| 1694 | |
| 1695 | // Resume function pointer |
| 1696 | Value *ResumeAddr = Shape.FramePtr; |
| 1697 | Builder.CreateStore(Val: ResumeFn, Ptr: ResumeAddr); |
| 1698 | |
| 1699 | Value *DestroyOrCleanupFn = DestroyFn; |
| 1700 | |
| 1701 | CoroIdInst *CoroId = Shape.getSwitchCoroId(); |
| 1702 | if (CoroAllocInst *CA = CoroId->getCoroAlloc()) { |
| 1703 | // If there is a CoroAlloc and it returns false (meaning we elide the |
| 1704 | // allocation, use CleanupFn instead of DestroyFn). |
| 1705 | DestroyOrCleanupFn = Builder.CreateSelect(C: CA, True: DestroyFn, False: CleanupFn); |
| 1706 | applyProfMetadataIfEnabled(V: DestroyOrCleanupFn, setMetadataCallback: [&](Instruction *Inst) { |
| 1707 | setExplicitlyUnknownBranchWeightsIfProfiled(I&: *Inst, DEBUG_TYPE, |
| 1708 | F: CoroId->getFunction()); |
| 1709 | }); |
| 1710 | } |
| 1711 | |
| 1712 | // Destroy function pointer |
| 1713 | Value *DestroyAddr = Builder.CreateInBoundsPtrAdd( |
| 1714 | Ptr: Shape.FramePtr, |
| 1715 | Offset: ConstantInt::get(Ty: Type::getInt64Ty(C), |
| 1716 | V: Shape.SwitchLowering.DestroyOffset), |
| 1717 | Name: "destroy.addr" ); |
| 1718 | Builder.CreateStore(Val: DestroyOrCleanupFn, Ptr: DestroyAddr); |
| 1719 | } |
| 1720 | |
| 1721 | // Create a global constant array containing pointers to functions provided |
| 1722 | // and set Info parameter of CoroBegin to point at this constant. Example: |
| 1723 | // |
| 1724 | // @f.resumers = internal constant [2 x void(%f.frame*)*] |
| 1725 | // [void(%f.frame*)* @f.resume, void(%f.frame*)* |
| 1726 | // @f.destroy] |
| 1727 | // define void @f() { |
| 1728 | // ... |
| 1729 | // call i8* @llvm.coro.begin(i8* null, i32 0, i8* null, |
| 1730 | // i8* bitcast([2 x void(%f.frame*)*] * @f.resumers to |
| 1731 | // i8*)) |
| 1732 | // |
| 1733 | // Assumes that all the functions have the same signature. |
| 1734 | static void setCoroInfo(Function &F, coro::Shape &Shape, |
| 1735 | ArrayRef<Function *> Fns) { |
| 1736 | // This only works under the switch-lowering ABI because coro elision |
| 1737 | // only works on the switch-lowering ABI. |
| 1738 | SmallVector<Constant *, 4> Args(Fns); |
| 1739 | assert(!Args.empty()); |
| 1740 | Function *Part = *Fns.begin(); |
| 1741 | Module *M = Part->getParent(); |
| 1742 | auto *ArrTy = ArrayType::get(ElementType: Part->getType(), NumElements: Args.size()); |
| 1743 | |
| 1744 | auto *ConstVal = ConstantArray::get(T: ArrTy, V: Args); |
| 1745 | auto *GV = new GlobalVariable(*M, ConstVal->getType(), /*isConstant=*/true, |
| 1746 | GlobalVariable::PrivateLinkage, ConstVal, |
| 1747 | F.getName() + Twine(".resumers" )); |
| 1748 | |
| 1749 | // Update coro.begin instruction to refer to this constant. |
| 1750 | LLVMContext &C = F.getContext(); |
| 1751 | auto *BC = ConstantExpr::getPointerCast(C: GV, Ty: PointerType::getUnqual(C)); |
| 1752 | Shape.getSwitchCoroId()->setInfo(BC); |
| 1753 | } |
| 1754 | }; |
| 1755 | |
| 1756 | } // namespace |
| 1757 | |
| 1758 | static void replaceAsyncResumeFunction(CoroSuspendAsyncInst *Suspend, |
| 1759 | Value *Continuation) { |
| 1760 | auto *ResumeIntrinsic = Suspend->getResumeFunction(); |
| 1761 | auto &Context = Suspend->getParent()->getParent()->getContext(); |
| 1762 | auto *Int8PtrTy = PointerType::getUnqual(C&: Context); |
| 1763 | |
| 1764 | IRBuilder<> Builder(ResumeIntrinsic); |
| 1765 | auto *Val = Builder.CreateBitOrPointerCast(V: Continuation, DestTy: Int8PtrTy); |
| 1766 | ResumeIntrinsic->replaceAllUsesWith(V: Val); |
| 1767 | ResumeIntrinsic->eraseFromParent(); |
| 1768 | Suspend->setOperand(i_nocapture: CoroSuspendAsyncInst::ResumeFunctionArg, |
| 1769 | Val_nocapture: PoisonValue::get(T: Int8PtrTy)); |
| 1770 | } |
| 1771 | |
| 1772 | /// Coerce the arguments in \p FnArgs according to \p FnTy in \p CallArgs. |
| 1773 | static void coerceArguments(IRBuilder<> &Builder, FunctionType *FnTy, |
| 1774 | ArrayRef<Value *> FnArgs, |
| 1775 | SmallVectorImpl<Value *> &CallArgs) { |
| 1776 | size_t ArgIdx = 0; |
| 1777 | for (auto *paramTy : FnTy->params()) { |
| 1778 | assert(ArgIdx < FnArgs.size()); |
| 1779 | if (paramTy != FnArgs[ArgIdx]->getType()) |
| 1780 | CallArgs.push_back( |
| 1781 | Elt: Builder.CreateBitOrPointerCast(V: FnArgs[ArgIdx], DestTy: paramTy)); |
| 1782 | else |
| 1783 | CallArgs.push_back(Elt: FnArgs[ArgIdx]); |
| 1784 | ++ArgIdx; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | CallInst *coro::createMustTailCall(DebugLoc Loc, Function *MustTailCallFn, |
| 1789 | TargetTransformInfo &TTI, |
| 1790 | ArrayRef<Value *> Arguments, |
| 1791 | IRBuilder<> &Builder) { |
| 1792 | auto *FnTy = MustTailCallFn->getFunctionType(); |
| 1793 | // Coerce the arguments, llvm optimizations seem to ignore the types in |
| 1794 | // vaarg functions and throws away casts in optimized mode. |
| 1795 | SmallVector<Value *, 8> CallArgs; |
| 1796 | coerceArguments(Builder, FnTy, FnArgs: Arguments, CallArgs); |
| 1797 | |
| 1798 | auto *TailCall = Builder.CreateCall(FTy: FnTy, Callee: MustTailCallFn, Args: CallArgs); |
| 1799 | // Skip targets which don't support tail call. |
| 1800 | if (TTI.supportsTailCallFor(CB: TailCall)) { |
| 1801 | TailCall->setTailCallKind(CallInst::TCK_MustTail); |
| 1802 | } |
| 1803 | TailCall->setDebugLoc(Loc); |
| 1804 | TailCall->setCallingConv(MustTailCallFn->getCallingConv()); |
| 1805 | return TailCall; |
| 1806 | } |
| 1807 | |
| 1808 | void coro::AsyncABI::splitCoroutine(Function &F, coro::Shape &Shape, |
| 1809 | SmallVectorImpl<Function *> &Clones, |
| 1810 | TargetTransformInfo &TTI) { |
| 1811 | assert(Shape.ABI == coro::ABI::Async); |
| 1812 | assert(Clones.empty()); |
| 1813 | // Reset various things that the optimizer might have decided it |
| 1814 | // "knows" about the coroutine function due to not seeing a return. |
| 1815 | F.removeFnAttr(Kind: Attribute::NoReturn); |
| 1816 | F.removeRetAttr(Kind: Attribute::NoAlias); |
| 1817 | F.removeRetAttr(Kind: Attribute::NonNull); |
| 1818 | |
| 1819 | auto &Context = F.getContext(); |
| 1820 | auto *Int8PtrTy = PointerType::getUnqual(C&: Context); |
| 1821 | |
| 1822 | auto *Id = Shape.getAsyncCoroId(); |
| 1823 | IRBuilder<> Builder(Id); |
| 1824 | |
| 1825 | auto *FramePtr = Id->getStorage(); |
| 1826 | FramePtr = Builder.CreateBitOrPointerCast(V: FramePtr, DestTy: Int8PtrTy); |
| 1827 | FramePtr = Builder.CreateInBoundsPtrAdd( |
| 1828 | Ptr: FramePtr, |
| 1829 | Offset: ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), |
| 1830 | V: Shape.AsyncLowering.FrameOffset), |
| 1831 | Name: "async.ctx.frameptr" ); |
| 1832 | |
| 1833 | // Map all uses of llvm.coro.begin to the allocated frame pointer. |
| 1834 | { |
| 1835 | // Make sure we don't invalidate Shape.FramePtr. |
| 1836 | TrackingVH<Value> Handle(Shape.FramePtr); |
| 1837 | Shape.CoroBegin->replaceAllUsesWith(V: FramePtr); |
| 1838 | Shape.FramePtr = Handle.getValPtr(); |
| 1839 | } |
| 1840 | |
| 1841 | // Create all the functions in order after the main function. |
| 1842 | auto NextF = std::next(x: F.getIterator()); |
| 1843 | |
| 1844 | // Create a continuation function for each of the suspend points. |
| 1845 | Clones.reserve(N: Shape.CoroSuspends.size()); |
| 1846 | for (auto [Idx, CS] : llvm::enumerate(First&: Shape.CoroSuspends)) { |
| 1847 | auto *Suspend = cast<CoroSuspendAsyncInst>(Val: CS); |
| 1848 | |
| 1849 | // Create the clone declaration. |
| 1850 | auto ResumeNameSuffix = ".resume." ; |
| 1851 | auto ProjectionFunctionName = |
| 1852 | Suspend->getAsyncContextProjectionFunction()->getName(); |
| 1853 | bool UseSwiftMangling = false; |
| 1854 | if (ProjectionFunctionName == "__swift_async_resume_project_context" ) { |
| 1855 | ResumeNameSuffix = "TQ" ; |
| 1856 | UseSwiftMangling = true; |
| 1857 | } else if (ProjectionFunctionName == "__swift_async_resume_get_context" ) { |
| 1858 | ResumeNameSuffix = "TY" ; |
| 1859 | UseSwiftMangling = true; |
| 1860 | } |
| 1861 | auto *Continuation = createCloneDeclaration( |
| 1862 | OrigF&: F, Shape, |
| 1863 | Suffix: UseSwiftMangling ? ResumeNameSuffix + Twine(Idx) + "_" |
| 1864 | : ResumeNameSuffix + Twine(Idx), |
| 1865 | InsertBefore: NextF, ActiveSuspend: Suspend); |
| 1866 | Clones.push_back(Elt: Continuation); |
| 1867 | |
| 1868 | // Insert a branch to a new return block immediately before the suspend |
| 1869 | // point. |
| 1870 | auto *SuspendBB = Suspend->getParent(); |
| 1871 | auto *NewSuspendBB = SuspendBB->splitBasicBlock(I: Suspend); |
| 1872 | auto *Branch = cast<UncondBrInst>(Val: SuspendBB->getTerminator()); |
| 1873 | |
| 1874 | // Place it before the first suspend. |
| 1875 | auto *ReturnBB = |
| 1876 | BasicBlock::Create(Context&: F.getContext(), Name: "coro.return" , Parent: &F, InsertBefore: NewSuspendBB); |
| 1877 | Branch->setSuccessor(idx: 0, NewSucc: ReturnBB); |
| 1878 | |
| 1879 | IRBuilder<> Builder(ReturnBB); |
| 1880 | |
| 1881 | // Insert the call to the tail call function and inline it. |
| 1882 | auto *Fn = Suspend->getMustTailCallFunction(); |
| 1883 | SmallVector<Value *, 8> Args(Suspend->args()); |
| 1884 | auto FnArgs = ArrayRef<Value *>(Args).drop_front( |
| 1885 | N: CoroSuspendAsyncInst::MustTailCallFuncArg + 1); |
| 1886 | auto *TailCall = coro::createMustTailCall(Loc: Suspend->getDebugLoc(), MustTailCallFn: Fn, TTI, |
| 1887 | Arguments: FnArgs, Builder); |
| 1888 | Builder.CreateRetVoid(); |
| 1889 | InlineFunctionInfo FnInfo; |
| 1890 | (void)InlineFunction(CB&: *TailCall, IFI&: FnInfo); |
| 1891 | |
| 1892 | // Replace the lvm.coro.async.resume intrisic call. |
| 1893 | replaceAsyncResumeFunction(Suspend, Continuation); |
| 1894 | } |
| 1895 | |
| 1896 | assert(Clones.size() == Shape.CoroSuspends.size()); |
| 1897 | |
| 1898 | for (auto [Idx, CS] : llvm::enumerate(First&: Shape.CoroSuspends)) { |
| 1899 | auto *Suspend = CS; |
| 1900 | auto *Clone = Clones[Idx]; |
| 1901 | |
| 1902 | coro::BaseCloner::createClone(OrigF&: F, Suffix: "resume." + Twine(Idx), Shape, NewF: Clone, |
| 1903 | ActiveSuspend: Suspend, TTI); |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | void coro::AnyRetconABI::splitCoroutine(Function &F, coro::Shape &Shape, |
| 1908 | SmallVectorImpl<Function *> &Clones, |
| 1909 | TargetTransformInfo &TTI) { |
| 1910 | assert(Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce); |
| 1911 | assert(Clones.empty()); |
| 1912 | |
| 1913 | // Reset various things that the optimizer might have decided it |
| 1914 | // "knows" about the coroutine function due to not seeing a return. |
| 1915 | F.removeFnAttr(Kind: Attribute::NoReturn); |
| 1916 | F.removeRetAttr(Kind: Attribute::NoAlias); |
| 1917 | F.removeRetAttr(Kind: Attribute::NonNull); |
| 1918 | |
| 1919 | // Allocate the frame. |
| 1920 | auto *Id = Shape.getRetconCoroId(); |
| 1921 | Value *RawFramePtr; |
| 1922 | if (Shape.RetconLowering.IsFrameInlineInStorage) { |
| 1923 | RawFramePtr = Id->getStorage(); |
| 1924 | } else { |
| 1925 | IRBuilder<> Builder(Id); |
| 1926 | |
| 1927 | auto FrameSize = Builder.getInt64(C: Shape.FrameSize); |
| 1928 | |
| 1929 | // Allocate. We don't need to update the call graph node because we're |
| 1930 | // going to recompute it from scratch after splitting. |
| 1931 | // FIXME: pass the required alignment |
| 1932 | RawFramePtr = Shape.emitAlloc(Builder, Size: FrameSize, CG: nullptr); |
| 1933 | RawFramePtr = |
| 1934 | Builder.CreateBitCast(V: RawFramePtr, DestTy: Shape.CoroBegin->getType()); |
| 1935 | |
| 1936 | // Stash the allocated frame pointer in the continuation storage. |
| 1937 | Builder.CreateStore(Val: RawFramePtr, Ptr: Id->getStorage()); |
| 1938 | } |
| 1939 | |
| 1940 | // Map all uses of llvm.coro.begin to the allocated frame pointer. |
| 1941 | { |
| 1942 | // Make sure we don't invalidate Shape.FramePtr. |
| 1943 | TrackingVH<Value> Handle(Shape.FramePtr); |
| 1944 | Shape.CoroBegin->replaceAllUsesWith(V: RawFramePtr); |
| 1945 | Shape.FramePtr = Handle.getValPtr(); |
| 1946 | } |
| 1947 | |
| 1948 | // Create a unique return block. |
| 1949 | BasicBlock *ReturnBB = nullptr; |
| 1950 | PHINode *ContinuationPhi = nullptr; |
| 1951 | SmallVector<PHINode *, 4> ReturnPHIs; |
| 1952 | |
| 1953 | // Create all the functions in order after the main function. |
| 1954 | auto NextF = std::next(x: F.getIterator()); |
| 1955 | |
| 1956 | // Create a continuation function for each of the suspend points. |
| 1957 | Clones.reserve(N: Shape.CoroSuspends.size()); |
| 1958 | for (auto [Idx, CS] : llvm::enumerate(First&: Shape.CoroSuspends)) { |
| 1959 | auto Suspend = cast<CoroSuspendRetconInst>(Val: CS); |
| 1960 | |
| 1961 | // Create the clone declaration. |
| 1962 | auto Continuation = createCloneDeclaration( |
| 1963 | OrigF&: F, Shape, Suffix: ".resume." + Twine(Idx), InsertBefore: NextF, ActiveSuspend: nullptr); |
| 1964 | Clones.push_back(Elt: Continuation); |
| 1965 | |
| 1966 | // Insert a branch to the unified return block immediately before |
| 1967 | // the suspend point. |
| 1968 | auto SuspendBB = Suspend->getParent(); |
| 1969 | auto NewSuspendBB = SuspendBB->splitBasicBlock(I: Suspend); |
| 1970 | auto Branch = cast<UncondBrInst>(Val: SuspendBB->getTerminator()); |
| 1971 | |
| 1972 | // Create the unified return block. |
| 1973 | if (!ReturnBB) { |
| 1974 | // Place it before the first suspend. |
| 1975 | ReturnBB = |
| 1976 | BasicBlock::Create(Context&: F.getContext(), Name: "coro.return" , Parent: &F, InsertBefore: NewSuspendBB); |
| 1977 | Shape.RetconLowering.ReturnBlock = ReturnBB; |
| 1978 | |
| 1979 | IRBuilder<> Builder(ReturnBB); |
| 1980 | |
| 1981 | // First, the continuation. |
| 1982 | ContinuationPhi = |
| 1983 | Builder.CreatePHI(Ty: Continuation->getType(), NumReservedValues: Shape.CoroSuspends.size()); |
| 1984 | |
| 1985 | // Create PHIs for all other return values. |
| 1986 | assert(ReturnPHIs.empty()); |
| 1987 | |
| 1988 | // Next, all the directly-yielded values. |
| 1989 | for (auto *ResultTy : Shape.getRetconResultTypes()) |
| 1990 | ReturnPHIs.push_back( |
| 1991 | Elt: Builder.CreatePHI(Ty: ResultTy, NumReservedValues: Shape.CoroSuspends.size())); |
| 1992 | |
| 1993 | // Build the return value. |
| 1994 | auto RetTy = F.getReturnType(); |
| 1995 | |
| 1996 | // Cast the continuation value if necessary. |
| 1997 | // We can't rely on the types matching up because that type would |
| 1998 | // have to be infinite. |
| 1999 | auto CastedContinuationTy = |
| 2000 | (ReturnPHIs.empty() ? RetTy : RetTy->getStructElementType(N: 0)); |
| 2001 | auto *CastedContinuation = |
| 2002 | Builder.CreateBitCast(V: ContinuationPhi, DestTy: CastedContinuationTy); |
| 2003 | |
| 2004 | Value *RetV = CastedContinuation; |
| 2005 | if (!ReturnPHIs.empty()) { |
| 2006 | auto ValueIdx = 0; |
| 2007 | RetV = PoisonValue::get(T: RetTy); |
| 2008 | RetV = Builder.CreateInsertValue(Agg: RetV, Val: CastedContinuation, Idxs: ValueIdx++); |
| 2009 | |
| 2010 | for (auto Phi : ReturnPHIs) |
| 2011 | RetV = Builder.CreateInsertValue(Agg: RetV, Val: Phi, Idxs: ValueIdx++); |
| 2012 | } |
| 2013 | |
| 2014 | Builder.CreateRet(V: RetV); |
| 2015 | } |
| 2016 | |
| 2017 | // Branch to the return block. |
| 2018 | Branch->setSuccessor(idx: 0, NewSucc: ReturnBB); |
| 2019 | assert(ContinuationPhi); |
| 2020 | ContinuationPhi->addIncoming(V: Continuation, BB: SuspendBB); |
| 2021 | for (auto [Phi, VUse] : |
| 2022 | llvm::zip_equal(t&: ReturnPHIs, u: Suspend->value_operands())) |
| 2023 | Phi->addIncoming(V: VUse, BB: SuspendBB); |
| 2024 | } |
| 2025 | |
| 2026 | assert(Clones.size() == Shape.CoroSuspends.size()); |
| 2027 | |
| 2028 | for (auto [Idx, CS] : llvm::enumerate(First&: Shape.CoroSuspends)) { |
| 2029 | auto Suspend = CS; |
| 2030 | auto Clone = Clones[Idx]; |
| 2031 | |
| 2032 | coro::BaseCloner::createClone(OrigF&: F, Suffix: "resume." + Twine(Idx), Shape, NewF: Clone, |
| 2033 | ActiveSuspend: Suspend, TTI); |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | namespace { |
| 2038 | class PrettyStackTraceFunction : public PrettyStackTraceEntry { |
| 2039 | Function &F; |
| 2040 | |
| 2041 | public: |
| 2042 | PrettyStackTraceFunction(Function &F) : F(F) {} |
| 2043 | void print(raw_ostream &OS) const override { |
| 2044 | OS << "While splitting coroutine " ; |
| 2045 | F.printAsOperand(O&: OS, /*print type*/ PrintType: false, M: F.getParent()); |
| 2046 | OS << "\n" ; |
| 2047 | } |
| 2048 | }; |
| 2049 | } // namespace |
| 2050 | |
| 2051 | /// Remove calls to llvm.coro.end in the original function. |
| 2052 | static void removeCoroEndsFromRampFunction(const coro::Shape &Shape) { |
| 2053 | if (Shape.ABI != coro::ABI::Switch) { |
| 2054 | for (auto *End : Shape.CoroEnds) { |
| 2055 | replaceCoroEnd(End, Shape, FramePtr: Shape.FramePtr, /*in ramp*/ InRamp: true, CG: nullptr); |
| 2056 | } |
| 2057 | } else { |
| 2058 | for (llvm::AnyCoroEndInst *End : Shape.CoroEnds) |
| 2059 | End->eraseFromParent(); |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | static void removeCoroIsInRampFromRampFunction(const coro::Shape &Shape) { |
| 2064 | for (auto *II : Shape.CoroIsInRampInsts) { |
| 2065 | auto &Ctx = II->getContext(); |
| 2066 | II->replaceAllUsesWith(V: ConstantInt::getTrue(Context&: Ctx)); |
| 2067 | II->eraseFromParent(); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | static bool hasSafeElideCaller(Function &F) { |
| 2072 | for (auto *U : F.users()) { |
| 2073 | if (auto *CB = dyn_cast<CallBase>(Val: U)) { |
| 2074 | auto *Caller = CB->getFunction(); |
| 2075 | if (Caller && Caller->isPresplitCoroutine() && |
| 2076 | CB->hasFnAttr(Kind: llvm::Attribute::CoroElideSafe)) |
| 2077 | return true; |
| 2078 | } |
| 2079 | } |
| 2080 | return false; |
| 2081 | } |
| 2082 | |
| 2083 | void coro::SwitchABI::splitCoroutine(Function &F, coro::Shape &Shape, |
| 2084 | SmallVectorImpl<Function *> &Clones, |
| 2085 | TargetTransformInfo &TTI) { |
| 2086 | SwitchCoroutineSplitter::split(F, Shape, Clones, TTI); |
| 2087 | } |
| 2088 | |
| 2089 | static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones, |
| 2090 | coro::BaseABI &ABI, TargetTransformInfo &TTI, |
| 2091 | bool OptimizeFrame) { |
| 2092 | PrettyStackTraceFunction prettyStackTrace(F); |
| 2093 | |
| 2094 | auto &Shape = ABI.Shape; |
| 2095 | assert(Shape.CoroBegin); |
| 2096 | |
| 2097 | lowerAwaitSuspends(F, Shape); |
| 2098 | |
| 2099 | simplifySuspendPoints(Shape); |
| 2100 | |
| 2101 | normalizeCoroutine(F, Shape, TTI); |
| 2102 | ABI.buildCoroutineFrame(OptimizeFrame); |
| 2103 | replaceFrameSizeAndAlignment(Shape); |
| 2104 | |
| 2105 | bool isNoSuspendCoroutine = Shape.CoroSuspends.empty(); |
| 2106 | |
| 2107 | bool shouldCreateNoAllocVariant = |
| 2108 | !isNoSuspendCoroutine && Shape.ABI == coro::ABI::Switch && |
| 2109 | hasSafeElideCaller(F) && !F.hasFnAttribute(Kind: llvm::Attribute::NoInline); |
| 2110 | if (Shape.ABI == coro::ABI::Switch) |
| 2111 | Shape.SwitchLowering.HasCoroElideNoAllocVariant = |
| 2112 | shouldCreateNoAllocVariant; |
| 2113 | |
| 2114 | // If there are no suspend points, no split required, just remove |
| 2115 | // the allocation and deallocation blocks, they are not needed. |
| 2116 | if (isNoSuspendCoroutine) { |
| 2117 | handleNoSuspendCoroutine(Shape); |
| 2118 | } else { |
| 2119 | ABI.splitCoroutine(F, Shape, Clones, TTI); |
| 2120 | } |
| 2121 | |
| 2122 | // Replace all the swifterror operations in the original function. |
| 2123 | // This invalidates SwiftErrorOps in the Shape. |
| 2124 | replaceSwiftErrorOps(F, Shape, VMap: nullptr); |
| 2125 | |
| 2126 | // Salvage debug intrinsics that point into the coroutine frame in the |
| 2127 | // original function. The Cloner has already salvaged debug info in the new |
| 2128 | // coroutine funclets. |
| 2129 | SmallDenseMap<Argument *, AllocaInst *, 4> ArgToAllocaMap; |
| 2130 | auto DbgVariableRecords = collectDbgVariableRecords(F); |
| 2131 | for (DbgVariableRecord *DVR : DbgVariableRecords) |
| 2132 | coro::salvageDebugInfo(ArgToAllocaMap, DVR&: *DVR, UseEntryValue: false /*UseEntryValue*/); |
| 2133 | |
| 2134 | removeCoroEndsFromRampFunction(Shape); |
| 2135 | removeCoroIsInRampFromRampFunction(Shape); |
| 2136 | |
| 2137 | if (shouldCreateNoAllocVariant) |
| 2138 | SwitchCoroutineSplitter::createNoAllocVariant(F, Shape, Clones); |
| 2139 | } |
| 2140 | |
| 2141 | static LazyCallGraph::SCC &updateCallGraphAfterCoroutineSplit( |
| 2142 | LazyCallGraph::Node &N, const coro::Shape &Shape, |
| 2143 | const SmallVectorImpl<Function *> &Clones, LazyCallGraph::SCC &C, |
| 2144 | LazyCallGraph &CG, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, |
| 2145 | FunctionAnalysisManager &FAM) { |
| 2146 | |
| 2147 | auto *CurrentSCC = &C; |
| 2148 | if (!Clones.empty()) { |
| 2149 | switch (Shape.ABI) { |
| 2150 | case coro::ABI::Switch: |
| 2151 | // The resume clone's elided-frame check holds a reference to the cleanup |
| 2152 | // clone. Add the cleanup clone first, so populating the resume node does |
| 2153 | // not materialize an unregistered cleanup node. |
| 2154 | if (Shape.SwitchLowering.HasCoroElideNoAllocVariant) { |
| 2155 | assert(Clones.size() >= 3 && "expected switch coroutine clones" ); |
| 2156 | CG.addSplitFunction(OriginalFunction&: N.getFunction(), NewFunction&: *Clones[2]); |
| 2157 | CG.addSplitFunction(OriginalFunction&: N.getFunction(), NewFunction&: *Clones[1]); |
| 2158 | CG.addSplitFunction(OriginalFunction&: N.getFunction(), NewFunction&: *Clones[0]); |
| 2159 | for (Function *Clone : drop_begin(RangeOrContainer: Clones, N: 3)) |
| 2160 | CG.addSplitFunction(OriginalFunction&: N.getFunction(), NewFunction&: *Clone); |
| 2161 | } else { |
| 2162 | // Each clone in the Switch lowering is independent of the other |
| 2163 | // clones. Let the LazyCallGraph know about each one separately. |
| 2164 | for (Function *Clone : Clones) |
| 2165 | CG.addSplitFunction(OriginalFunction&: N.getFunction(), NewFunction&: *Clone); |
| 2166 | } |
| 2167 | break; |
| 2168 | case coro::ABI::Async: |
| 2169 | case coro::ABI::Retcon: |
| 2170 | case coro::ABI::RetconOnce: |
| 2171 | // Each clone in the Async/Retcon lowering references of the other clones. |
| 2172 | // Let the LazyCallGraph know about all of them at once. |
| 2173 | if (!Clones.empty()) |
| 2174 | CG.addSplitRefRecursiveFunctions(OriginalFunction&: N.getFunction(), NewFunctions: Clones); |
| 2175 | break; |
| 2176 | } |
| 2177 | |
| 2178 | // Let the CGSCC infra handle the changes to the original function. |
| 2179 | CurrentSCC = &updateCGAndAnalysisManagerForCGSCCPass(G&: CG, C&: *CurrentSCC, N, AM, |
| 2180 | UR, FAM); |
| 2181 | } |
| 2182 | |
| 2183 | // Do some cleanup and let the CGSCC infra see if we've cleaned up any edges |
| 2184 | // to the split functions. |
| 2185 | postSplitCleanup(F&: N.getFunction()); |
| 2186 | CurrentSCC = &updateCGAndAnalysisManagerForFunctionPass(G&: CG, C&: *CurrentSCC, N, |
| 2187 | AM, UR, FAM); |
| 2188 | return *CurrentSCC; |
| 2189 | } |
| 2190 | |
| 2191 | /// Replace a call to llvm.coro.prepare.retcon. |
| 2192 | static void replacePrepare(CallInst *Prepare, LazyCallGraph &CG, |
| 2193 | LazyCallGraph::SCC &C) { |
| 2194 | auto CastFn = Prepare->getArgOperand(i: 0); // as an i8* |
| 2195 | auto Fn = CastFn->stripPointerCasts(); // as its original type |
| 2196 | |
| 2197 | // Attempt to peephole this pattern: |
| 2198 | // %0 = bitcast [[TYPE]] @some_function to i8* |
| 2199 | // %1 = call @llvm.coro.prepare.retcon(i8* %0) |
| 2200 | // %2 = bitcast %1 to [[TYPE]] |
| 2201 | // ==> |
| 2202 | // %2 = @some_function |
| 2203 | for (Use &U : llvm::make_early_inc_range(Range: Prepare->uses())) { |
| 2204 | // Look for bitcasts back to the original function type. |
| 2205 | auto *Cast = dyn_cast<BitCastInst>(Val: U.getUser()); |
| 2206 | if (!Cast || Cast->getType() != Fn->getType()) |
| 2207 | continue; |
| 2208 | |
| 2209 | // Replace and remove the cast. |
| 2210 | Cast->replaceAllUsesWith(V: Fn); |
| 2211 | Cast->eraseFromParent(); |
| 2212 | } |
| 2213 | |
| 2214 | // Replace any remaining uses with the function as an i8*. |
| 2215 | // This can never directly be a callee, so we don't need to update CG. |
| 2216 | Prepare->replaceAllUsesWith(V: CastFn); |
| 2217 | Prepare->eraseFromParent(); |
| 2218 | |
| 2219 | // Kill dead bitcasts. |
| 2220 | while (auto *Cast = dyn_cast<BitCastInst>(Val: CastFn)) { |
| 2221 | if (!Cast->use_empty()) |
| 2222 | break; |
| 2223 | CastFn = Cast->getOperand(i_nocapture: 0); |
| 2224 | Cast->eraseFromParent(); |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | static bool replaceAllPrepares(Function *PrepareFn, LazyCallGraph &CG, |
| 2229 | LazyCallGraph::SCC &C) { |
| 2230 | bool Changed = false; |
| 2231 | for (Use &P : llvm::make_early_inc_range(Range: PrepareFn->uses())) { |
| 2232 | // Intrinsics can only be used in calls. |
| 2233 | auto *Prepare = cast<CallInst>(Val: P.getUser()); |
| 2234 | replacePrepare(Prepare, CG, C); |
| 2235 | Changed = true; |
| 2236 | } |
| 2237 | |
| 2238 | return Changed; |
| 2239 | } |
| 2240 | |
| 2241 | static void addPrepareFunction(const Module &M, |
| 2242 | SmallVectorImpl<Function *> &Fns, |
| 2243 | StringRef Name) { |
| 2244 | auto *PrepareFn = M.getFunction(Name); |
| 2245 | if (PrepareFn && !PrepareFn->use_empty()) |
| 2246 | Fns.push_back(Elt: PrepareFn); |
| 2247 | } |
| 2248 | |
| 2249 | static std::unique_ptr<coro::BaseABI> |
| 2250 | CreateNewABI(Function &F, coro::Shape &S, |
| 2251 | std::function<bool(Instruction &)> IsMatCallback, |
| 2252 | const SmallVector<CoroSplitPass::BaseABITy> GenCustomABIs) { |
| 2253 | if (S.CoroBegin->hasCustomABI()) { |
| 2254 | unsigned CustomABI = S.CoroBegin->getCustomABI(); |
| 2255 | if (CustomABI >= GenCustomABIs.size()) |
| 2256 | llvm_unreachable("Custom ABI not found amoung those specified" ); |
| 2257 | return GenCustomABIs[CustomABI](F, S); |
| 2258 | } |
| 2259 | |
| 2260 | switch (S.ABI) { |
| 2261 | case coro::ABI::Switch: |
| 2262 | return std::make_unique<coro::SwitchABI>(args&: F, args&: S, args&: IsMatCallback); |
| 2263 | case coro::ABI::Async: |
| 2264 | return std::make_unique<coro::AsyncABI>(args&: F, args&: S, args&: IsMatCallback); |
| 2265 | case coro::ABI::Retcon: |
| 2266 | return std::make_unique<coro::AnyRetconABI>(args&: F, args&: S, args&: IsMatCallback); |
| 2267 | case coro::ABI::RetconOnce: |
| 2268 | return std::make_unique<coro::AnyRetconABI>(args&: F, args&: S, args&: IsMatCallback); |
| 2269 | } |
| 2270 | llvm_unreachable("Unknown ABI" ); |
| 2271 | } |
| 2272 | |
| 2273 | CoroSplitPass::CoroSplitPass(bool OptimizeFrame) |
| 2274 | : CreateAndInitABI([](Function &F, coro::Shape &S) { |
| 2275 | std::unique_ptr<coro::BaseABI> ABI = |
| 2276 | CreateNewABI(F, S, IsMatCallback: coro::isTriviallyMaterializable, GenCustomABIs: {}); |
| 2277 | ABI->init(); |
| 2278 | return ABI; |
| 2279 | }), |
| 2280 | OptimizeFrame(OptimizeFrame) {} |
| 2281 | |
| 2282 | CoroSplitPass::CoroSplitPass( |
| 2283 | SmallVector<CoroSplitPass::BaseABITy> GenCustomABIs, bool OptimizeFrame) |
| 2284 | : CreateAndInitABI([=](Function &F, coro::Shape &S) { |
| 2285 | std::unique_ptr<coro::BaseABI> ABI = |
| 2286 | CreateNewABI(F, S, IsMatCallback: coro::isTriviallyMaterializable, GenCustomABIs); |
| 2287 | ABI->init(); |
| 2288 | return ABI; |
| 2289 | }), |
| 2290 | OptimizeFrame(OptimizeFrame) {} |
| 2291 | |
| 2292 | // For back compatibility, constructor takes a materializable callback and |
| 2293 | // creates a generator for an ABI with a modified materializable callback. |
| 2294 | CoroSplitPass::CoroSplitPass(std::function<bool(Instruction &)> IsMatCallback, |
| 2295 | bool OptimizeFrame) |
| 2296 | : CreateAndInitABI([=](Function &F, coro::Shape &S) { |
| 2297 | std::unique_ptr<coro::BaseABI> ABI = |
| 2298 | CreateNewABI(F, S, IsMatCallback, GenCustomABIs: {}); |
| 2299 | ABI->init(); |
| 2300 | return ABI; |
| 2301 | }), |
| 2302 | OptimizeFrame(OptimizeFrame) {} |
| 2303 | |
| 2304 | // For back compatibility, constructor takes a materializable callback and |
| 2305 | // creates a generator for an ABI with a modified materializable callback. |
| 2306 | CoroSplitPass::CoroSplitPass( |
| 2307 | std::function<bool(Instruction &)> IsMatCallback, |
| 2308 | SmallVector<CoroSplitPass::BaseABITy> GenCustomABIs, bool OptimizeFrame) |
| 2309 | : CreateAndInitABI([=](Function &F, coro::Shape &S) { |
| 2310 | std::unique_ptr<coro::BaseABI> ABI = |
| 2311 | CreateNewABI(F, S, IsMatCallback, GenCustomABIs); |
| 2312 | ABI->init(); |
| 2313 | return ABI; |
| 2314 | }), |
| 2315 | OptimizeFrame(OptimizeFrame) {} |
| 2316 | |
| 2317 | PreservedAnalyses CoroSplitPass::run(LazyCallGraph::SCC &C, |
| 2318 | CGSCCAnalysisManager &AM, |
| 2319 | LazyCallGraph &CG, CGSCCUpdateResult &UR) { |
| 2320 | // NB: One invariant of a valid LazyCallGraph::SCC is that it must contain a |
| 2321 | // non-zero number of nodes, so we assume that here and grab the first |
| 2322 | // node's function's module. |
| 2323 | Module &M = *C.begin()->getFunction().getParent(); |
| 2324 | auto &FAM = |
| 2325 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(IR&: C, ExtraArgs&: CG).getManager(); |
| 2326 | |
| 2327 | // Check for uses of llvm.coro.prepare.retcon/async. |
| 2328 | SmallVector<Function *, 2> PrepareFns; |
| 2329 | addPrepareFunction(M, Fns&: PrepareFns, Name: "llvm.coro.prepare.retcon" ); |
| 2330 | addPrepareFunction(M, Fns&: PrepareFns, Name: "llvm.coro.prepare.async" ); |
| 2331 | |
| 2332 | // Find coroutines for processing. |
| 2333 | SmallVector<LazyCallGraph::Node *> Coroutines; |
| 2334 | for (LazyCallGraph::Node &N : C) |
| 2335 | if (N.getFunction().isPresplitCoroutine()) |
| 2336 | Coroutines.push_back(Elt: &N); |
| 2337 | |
| 2338 | if (Coroutines.empty() && PrepareFns.empty()) |
| 2339 | return PreservedAnalyses::all(); |
| 2340 | |
| 2341 | auto *CurrentSCC = &C; |
| 2342 | // Split all the coroutines. |
| 2343 | for (LazyCallGraph::Node *N : Coroutines) { |
| 2344 | Function &F = N->getFunction(); |
| 2345 | LLVM_DEBUG(dbgs() << "CoroSplit: Processing coroutine '" << F.getName() |
| 2346 | << "\n" ); |
| 2347 | |
| 2348 | // The suspend-crossing algorithm in buildCoroutineFrame gets tripped up |
| 2349 | // by unreachable blocks, so remove them as a first pass. Remove the |
| 2350 | // unreachable blocks before collecting intrinsics into Shape. |
| 2351 | removeUnreachableBlocks(F); |
| 2352 | |
| 2353 | coro::Shape Shape(F); |
| 2354 | if (!Shape.CoroBegin) |
| 2355 | continue; |
| 2356 | |
| 2357 | F.setSplittedCoroutine(); |
| 2358 | |
| 2359 | // Query BFI and populate SuspendFreqs right before splitting. |
| 2360 | auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(IR&: F); |
| 2361 | for (auto *AnyS : Shape.CoroSuspends) { |
| 2362 | BasicBlock *BB = AnyS->getParent(); |
| 2363 | uint64_t Freq = BFI.getBlockFreq(BB).getFrequency(); |
| 2364 | Shape.SuspendFreqs[AnyS] = Freq; |
| 2365 | |
| 2366 | // Query BFI to get the actual estimated execution profile count of the |
| 2367 | // basic block where this suspension point resides. |
| 2368 | std::optional<uint64_t> Count = |
| 2369 | BFI.getBlockProfileCount(BB, /*AllowSynthetic=*/true); |
| 2370 | if (Count.has_value()) { |
| 2371 | if (!Shape.ResumeEntryCount.has_value()) { |
| 2372 | // For the first suspend point visited, initialize the total sum. |
| 2373 | Shape.ResumeEntryCount = Count.value(); |
| 2374 | } else { |
| 2375 | // Accumulate the absolute execution count of each subsequent suspend |
| 2376 | // point into the total sum. |
| 2377 | Shape.ResumeEntryCount.value() += Count.value(); |
| 2378 | } |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | std::unique_ptr<coro::BaseABI> ABI = CreateAndInitABI(F, Shape); |
| 2383 | |
| 2384 | SmallVector<Function *, 4> Clones; |
| 2385 | auto &TTI = FAM.getResult<TargetIRAnalysis>(IR&: F); |
| 2386 | doSplitCoroutine(F, Clones, ABI&: *ABI, TTI, OptimizeFrame); |
| 2387 | CurrentSCC = &updateCallGraphAfterCoroutineSplit( |
| 2388 | N&: *N, Shape, Clones, C&: *CurrentSCC, CG, AM, UR, FAM); |
| 2389 | |
| 2390 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(IR&: F); |
| 2391 | ORE.emit(RemarkBuilder: [&]() { |
| 2392 | return OptimizationRemark(DEBUG_TYPE, "CoroSplit" , &F) |
| 2393 | << "Split '" << ore::NV("function" , F.getName()) |
| 2394 | << "' (frame_size=" << ore::NV("frame_size" , Shape.FrameSize) |
| 2395 | << ", align=" << ore::NV("align" , Shape.FrameAlign.value()) << ")" ; |
| 2396 | }); |
| 2397 | |
| 2398 | if (!Shape.CoroSuspends.empty()) { |
| 2399 | // Run the CGSCC pipeline on the original and newly split functions. |
| 2400 | UR.CWorklist.insert(X: CurrentSCC); |
| 2401 | for (Function *Clone : Clones) |
| 2402 | UR.CWorklist.insert(X: CG.lookupSCC(N&: CG.get(F&: *Clone))); |
| 2403 | } else if (Shape.ABI == coro::ABI::Async) { |
| 2404 | // Reprocess the function to inline the tail called return function of |
| 2405 | // coro.async.end. |
| 2406 | UR.CWorklist.insert(X: &C); |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | for (auto *PrepareFn : PrepareFns) { |
| 2411 | replaceAllPrepares(PrepareFn, CG, C&: *CurrentSCC); |
| 2412 | } |
| 2413 | |
| 2414 | return PreservedAnalyses::none(); |
| 2415 | } |
| 2416 | |