@@ -5153,6 +5153,35 @@ static llvm::Constant *getCoroAllocWrapperFn(IRGenModule &IGM) {
51535153 /* optionalLinkageOverride=*/ nullptr , llvm::CallingConv::SwiftCoro);
51545154}
51555155
5156+ static llvm::Constant *getCoroDeallocWrapperFn (IRGenModule &IGM) {
5157+ return IGM.getOrCreateHelperFunction (
5158+ " __swift_coro_dealloc_" , IGM.VoidTy ,
5159+ {IGM.CoroAllocatorPtrTy , IGM.Int8PtrTy },
5160+ [](IRGenFunction &IGF) {
5161+ auto parameters = IGF.collectParameters ();
5162+ auto *allocator = parameters.claimNext ();
5163+ auto *ptr = parameters.claimNext ();
5164+ auto *nullAllocator = IGF.Builder .CreateCmp (
5165+ llvm::CmpInst::Predicate::ICMP_EQ, allocator,
5166+ llvm::ConstantPointerNull::get (
5167+ cast<llvm::PointerType>(allocator->getType ())));
5168+ auto *bailBlock = IGF.createBasicBlock (" bail" );
5169+ auto *forwardBlock = IGF.createBasicBlock (" forward" );
5170+ IGF.Builder .CreateCondBr (nullAllocator, bailBlock, forwardBlock);
5171+ IGF.Builder .emitBlock (bailBlock);
5172+ // Emit the dynamic alloca.
5173+ IGF.Builder .CreateRetVoid ();
5174+ IGF.Builder .emitBlock (forwardBlock);
5175+ IGF.Builder .CreateCall (
5176+ IGF.IGM .getCoroDeallocFunctionPointer (), {allocator, ptr});
5177+ IGF.Builder .CreateRetVoid ();
5178+ },
5179+ /* setIsNoInline=*/ false ,
5180+ /* forPrologue=*/ false ,
5181+ /* isPerformanceConstraint=*/ false ,
5182+ /* optionalLinkageOverride=*/ nullptr , llvm::CallingConv::SwiftCoro);
5183+ }
5184+
51565185void irgen::emitYieldOnce2CoroutineEntry (IRGenFunction &IGF,
51575186 CanSILFunctionType fnType,
51585187 llvm::Value *buffer,
@@ -5164,7 +5193,9 @@ void irgen::emitYieldOnce2CoroutineEntry(IRGenFunction &IGF,
51645193 auto allocFn = IGF.IGM .getOpaquePtr (isSwiftCoroCCAvailable
51655194 ? getCoroAllocWrapperFn (IGF.IGM )
51665195 : IGF.IGM .getCoroAllocFn ());
5167- auto deallocFn = IGF.IGM .getOpaquePtr (IGF.IGM .getCoroDeallocFn ());
5196+ auto deallocFn = IGF.IGM .getOpaquePtr (isSwiftCoroCCAvailable
5197+ ? getCoroDeallocWrapperFn (IGF.IGM )
5198+ : IGF.IGM .getCoroDeallocFn ());
51685199 emitRetconCoroutineEntry (
51695200 IGF, fnType, buffer, llvm::Intrinsic::coro_id_retcon_once_dynamic,
51705201 Size (-1 ) /* dynamic-to-IRGen size*/ , IGF.IGM .getCoroStaticFrameAlignment (),
0 commit comments