Skip to content

Commit 9069d32

Browse files
Merge pull request #85055 from nate-chandler/general-coro/20251016/1
[CoroutineAccessors] Provide frame to de/allocation functions.
2 parents 25543a5 + cdf9f9d commit 9069d32

16 files changed

+445
-154
lines changed

include/swift/ABI/Coro.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,20 @@ class CoroAllocatorFlags : public FlagSet<uint32_t> {
5656
setShouldDeallocateImmediately)
5757
};
5858

59+
struct CoroAllocator;
60+
5961
using CoroAllocation = void *;
60-
using CoroAllocateFn = CoroAllocation (*)(size_t);
61-
using CoroDealllocateFn = void (*)(CoroAllocation);
62+
using CoroAllocateFn = CoroAllocation (*)(CoroAllocator *, size_t);
63+
using CoroDealllocateFn = void (*)(CoroAllocator *, CoroAllocation);
64+
using CoroAllocateFrameFn = CoroAllocation (*)(CoroAllocator *, size_t);
65+
using CoroDealllocateFrameFn = void (*)(CoroAllocator *, CoroAllocation);
6266

6367
struct CoroAllocator {
6468
CoroAllocatorFlags flags;
6569
CoroAllocateFn allocate;
6670
CoroDealllocateFn deallocate;
71+
CoroAllocateFrameFn allocateFrame;
72+
CoroDealllocateFrameFn deallocateFrame;
6773

6874
/// Whether the allocator should deallocate memory on calls to
6975
/// swift_coro_dealloc.

include/swift/ABI/MetadataValues.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,8 @@ namespace SpecialPointerAuthDiscriminators {
17971797
/// Function pointers stored in the coro allocator struct.
17981798
const uint16_t CoroAllocationFunction = 0x5f95; // = 24469
17991799
const uint16_t CoroDeallocationFunction = 0x9faf; // = 40879
1800+
const uint16_t CoroFrameAllocationFunction = 0xd251; // = 53841
1801+
const uint16_t CoroFrameDeallocationFunction = 0x5ba8; // = 23464
18001802
}
18011803

18021804
/// The number of arguments that will be passed directly to a generic

include/swift/AST/IRGenOptions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ struct PointerAuthOptions : clang::PointerAuthOptions {
257257

258258
/// Stored in a coro allocator struct, the function used to deallocate memory.
259259
PointerAuthSchema CoroDeallocationFunction;
260+
261+
/// Stored in a coro allocator struct, the function used to allocate a callee
262+
/// coroutine's fixed-size frame.
263+
PointerAuthSchema CoroFrameAllocationFunction;
264+
265+
/// Stored in a coro allocator struct, the function used to deallocate a
266+
/// callee coroutine's fixed-size frame.
267+
PointerAuthSchema CoroFrameDeallocationFunction;
260268
};
261269

262270
enum class JITDebugArtifact : unsigned {

lib/IRGen/GenCall.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5177,10 +5177,12 @@ void irgen::emitYieldOnce2CoroutineEntry(IRGenFunction &IGF,
51775177
IGF.setCoroutineAllocator(allocator);
51785178
auto allocFn = IGF.IGM.getOpaquePtr(getCoroAllocFn(IGF.IGM));
51795179
auto deallocFn = IGF.IGM.getOpaquePtr(getCoroDeallocFn(IGF.IGM));
5180+
auto allocFrameFn = IGF.IGM.getOpaquePtr(getCoroAllocFrameFn(IGF.IGM));
5181+
auto deallocFrameFn = IGF.IGM.getOpaquePtr(getCoroDeallocFrameFn(IGF.IGM));
51805182
emitRetconCoroutineEntry(
51815183
IGF, fnType, buffer, llvm::Intrinsic::coro_id_retcon_once_dynamic,
51825184
Size(-1) /*dynamic-to-IRGen size*/, IGF.IGM.getCoroStaticFrameAlignment(),
5183-
{cfp, allocator}, allocFn, deallocFn, {});
5185+
{cfp, allocator}, allocFn, deallocFn, {allocFrameFn, deallocFrameFn});
51845186
}
51855187
void irgen::emitYieldOnce2CoroutineEntry(
51865188
IRGenFunction &IGF, LinkEntity coroFunction, CanSILFunctionType fnType,
@@ -5266,17 +5268,19 @@ StackAddress irgen::emitAllocCoroStaticFrame(IRGenFunction &IGF,
52665268
// TODO: Avoid swift_task_alloc (async) and malloc (yield_once) if the
52675269
// suspension doesn't span an apply of an async function or a yield
52685270
// respectively.
5269-
auto retval =
5270-
IGF.emitDynamicAlloca(IGF.IGM.Int8Ty, size, Alignment(MaximumAlignment),
5271-
/*allowTaskAlloc*/ true, "caller-coro-frame");
5271+
auto retval = IGF.emitDynamicAlloca(
5272+
IGF.IGM.Int8Ty, size, Alignment(MaximumAlignment), AllowsTaskAlloc,
5273+
IsForCalleeCoroutineFrame_t(IGF.isCalleeAllocatedCoroutine()),
5274+
"callee-coro-frame");
52725275
IGF.Builder.CreateLifetimeStart(retval.getAddress(),
52735276
Size(-1) /*dynamic size*/);
52745277
return retval;
52755278
}
52765279
void irgen::emitDeallocCoroStaticFrame(IRGenFunction &IGF, StackAddress frame) {
52775280
IGF.Builder.CreateLifetimeEnd(frame.getAddress(), Size(-1) /*dynamic size*/);
52785281
IGF.emitDeallocateDynamicAlloca(frame, /*allowTaskAlloc*/ true,
5279-
/*useTaskDeallocThrough*/ true);
5282+
/*useTaskDeallocThrough*/ true,
5283+
/*forCalleeCoroutineFrame*/ true);
52805284
}
52815285

52825286
llvm::Value *irgen::emitYield(IRGenFunction &IGF,

0 commit comments

Comments
 (0)