Skip to content

Commit 2412ac3

Browse files
committed
[CoroutineAccessors] Dedicate bit to dealloc time.
Don't rely on the kind for this. Allow each allocator to decide for itself for future compatibility.
1 parent dbcd038 commit 2412ac3

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

include/swift/ABI/Coro.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class CoroAllocatorFlags : public FlagSet<uint32_t> {
3939
Kind = 0,
4040
Kind_width = 8,
4141

42+
ShouldDeallocateImmediately = 8,
43+
4244
// 24 reserved bits
4345
};
4446
// clang-format on
@@ -49,6 +51,9 @@ class CoroAllocatorFlags : public FlagSet<uint32_t> {
4951

5052
FLAGSET_DEFINE_FIELD_ACCESSORS(Kind, Kind_width, CoroAllocatorKind, getKind,
5153
setKind)
54+
FLAGSET_DEFINE_FLAG_ACCESSORS(ShouldDeallocateImmediately,
55+
shouldDeallocateImmediately,
56+
setShouldDeallocateImmediately)
5257
};
5358

5459
using CoroAllocation = void *;
@@ -62,16 +67,16 @@ struct CoroAllocator {
6267

6368
/// Whether the allocator should deallocate memory on calls to
6469
/// swift_coro_dealloc.
65-
constexpr bool shouldDeallocateImmediately() {
66-
// Only the "mallocator" should immediately deallocate in
70+
bool shouldDeallocateImmediately() {
71+
// Currently, only the "mallocator" should immediately deallocate in
6772
// swift_coro_dealloc. It must because the ABI does not provide a means for
6873
// the callee to return its allocations to the caller.
6974
//
7075
// Async allocations can be deferred until the first non-coroutine caller
7176
// from where swift_task_dealloc_through can be called and passed the
7277
// caller-allocated fixed-per-callee-sized-frame.
7378
// Stack allocations just pop the stack.
74-
return flags.getKind() == CoroAllocatorKind::Malloc;
79+
return flags.shouldDeallocateImmediately();
7580
}
7681
};
7782

lib/IRGen/GenCall.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5230,16 +5230,17 @@ Address irgen::emitAllocYieldManyCoroutineBuffer(IRGenFunction &IGF) {
52305230
getYieldManyCoroutineBufferAlignment(IGF.IGM));
52315231
}
52325232

5233-
static llvm::Constant *getAddrOfGlobalCoroAllocator(IRGenModule &IGM,
5234-
CoroAllocatorKind kind,
5235-
llvm::Constant *allocFn,
5236-
llvm::Constant *deallocFn) {
5233+
static llvm::Constant *getAddrOfGlobalCoroAllocator(
5234+
IRGenModule &IGM, CoroAllocatorKind kind, bool shouldDeallocateImmediately,
5235+
llvm::Constant *allocFn, llvm::Constant *deallocFn) {
52375236
auto entity = LinkEntity::forCoroAllocator(kind);
52385237
auto taskAllocator = IGM.getOrCreateLazyGlobalVariable(
52395238
entity,
52405239
[&](ConstantInitBuilder &builder) -> ConstantInitFuture {
52415240
auto allocator = builder.beginStruct(IGM.CoroAllocatorTy);
5242-
allocator.addInt32(CoroAllocatorFlags(kind).getOpaqueValue());
5241+
auto flags = CoroAllocatorFlags(kind);
5242+
flags.setShouldDeallocateImmediately(shouldDeallocateImmediately);
5243+
allocator.addInt32(flags.getOpaqueValue());
52435244
allocator.add(allocFn);
52445245
allocator.add(deallocFn);
52455246
return allocator.finishAndCreateFuture();
@@ -5249,10 +5250,12 @@ static llvm::Constant *getAddrOfGlobalCoroAllocator(IRGenModule &IGM,
52495250
}
52505251
llvm::Constant *IRGenModule::getAddrOfGlobalCoroMallocAllocator() {
52515252
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Malloc,
5253+
/*shouldDeallocateImmediately=*/true,
52525254
getMallocFn(), getFreeFn());
52535255
}
52545256
llvm::Constant *IRGenModule::getAddrOfGlobalCoroAsyncTaskAllocator() {
52555257
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Async,
5258+
/*shouldDeallocateImmediately=*/false,
52565259
getTaskAllocFn(), getTaskDeallocFn());
52575260
}
52585261
llvm::Value *

test/IRGen/coroutine_accessors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// CHECK-SAME: }>
1515

1616
// CHECK-LABEL: _swift_coro_malloc_allocator = linkonce_odr hidden constant %swift.coro_allocator {
17-
// CHECK-SAME: i32 2,
17+
// CHECK-SAME: i32 258,
1818
// CHECK-SAME: malloc,
1919
// CHECK-SAME: free
2020
// CHECK-SAME: }

test/IRGen/coroutine_accessors_popless.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// CHECK-SAME: swift_task_dealloc
2424
// CHECK-SAME: }
2525
// CHECK-LABEL: _swift_coro_malloc_allocator = linkonce_odr hidden constant %swift.coro_allocator {
26-
// CHECK-SAME: i32 2,
26+
// CHECK-SAME: i32 258,
2727
// CHECK-SAME: malloc,
2828
// CHECK-SAME: free
2929
// CHECK-SAME: }

0 commit comments

Comments
 (0)