Skip to content

[CoroutineAccessors] Store CFPs in descriptors. #80135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/swift/ABI/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,16 @@ struct TargetMethodDescriptor {
union {
TargetCompactFunctionPointer<Runtime, void> Impl;
TargetRelativeDirectPointer<Runtime, void> AsyncImpl;
TargetRelativeDirectPointer<Runtime, void> CoroImpl;
};

// TODO: add method types or anything else needed for reflection.

void *getImpl() const {
if (Flags.isAsync()) {
return AsyncImpl.get();
} else if (Flags.isCalleeAllocatedCoroutine()) {
return CoroImpl.get();
} else {
return Impl.get();
}
Expand Down Expand Up @@ -665,13 +668,16 @@ struct TargetMethodOverrideDescriptor {
union {
TargetCompactFunctionPointer<Runtime, void, /*nullable*/ true> Impl;
TargetRelativeDirectPointer<Runtime, void, /*nullable*/ true> AsyncImpl;
TargetRelativeDirectPointer<Runtime, void, /*nullable*/ true> CoroImpl;
};

void *getImpl() const {
auto *baseMethod = Method.get();
assert(baseMethod && "no base method");
if (baseMethod->Flags.isAsync()) {
return AsyncImpl.get();
} else if (baseMethod->Flags.isCalleeAllocatedCoroutine()) {
return CoroImpl.get();
} else {
return Impl.get();
}
Expand Down Expand Up @@ -5170,6 +5176,7 @@ class DynamicReplacementDescriptor {
union {
TargetCompactFunctionPointer<InProcess, void, false> replacementFunction;
TargetRelativeDirectPointer<InProcess, void, false> replacementAsyncFunction;
TargetRelativeDirectPointer<InProcess, void, false> replacementCoroFunction;
};
RelativeDirectPointer<DynamicReplacementChainEntry, false> chainEntry;
uint32_t flags;
Expand All @@ -5179,6 +5186,8 @@ class DynamicReplacementDescriptor {
void *getReplacementFunction() const {
if (replacedFunctionKey->isAsync()) {
return replacementAsyncFunction.get();
} else if (replacedFunctionKey->isCalleeAllocatedCoroutine()) {
return replacementCoroFunction.get();
} else {
return replacementFunction.get();
}
Expand Down
79 changes: 79 additions & 0 deletions test/IRGen/run-coroutine_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
public protocol ResilientWrapping {
associatedtype Wrapped
var wrapped: Wrapped { read set }
var wrapped2: Wrapped { read set }
}
extension ResilientWrapping {
public var wrapped2: Wrapped {
read {
yield wrapped
}
modify {
yield &wrapped
}
}
}

public struct ResilientBoxtional<T> : ResilientWrapping {
Expand All @@ -80,6 +91,7 @@ public struct ResilientBoxtional<T> : ResilientWrapping {
}

open class ResilientWrappingClass<Wrapped> {
public init() {}
open var wrapped: Wrapped {
read {
fatalError()
Expand Down Expand Up @@ -162,6 +174,39 @@ struct MaybePtrBox<T> {
}
}

struct Boxtional<T> : ResilientWrapping {
var storage: T?
init(_ t: T?) {
self.storage = t
}
typealias Wrapped = T?

var wrapped : T? {
read {
yield storage
}
modify {
yield &storage
}
}
}

class NonresilientResilientWrappingSubclass<X : ResilientWrapping> : ResilientWrappingClass<X.Wrapped> {
init(_ impl: X) {
self.impl = impl
super.init()
}
var impl: X
override var wrapped: X.Wrapped {
read {
yield impl.wrapped
}
modify {
yield &impl.wrapped
}
}
}

protocol AsyncMutatable {
mutating func mutate() async
}
Expand Down Expand Up @@ -333,6 +378,24 @@ struct M {
// CHECK: "hihi"
print(v2.wrapped)
}
static func mutateResilientWrapped2<T : ResilientWrapping>(_ t: inout T) where T.Wrapped : Mutatable {
t.wrapped2.mutate()
}
static func resilient_proto_default_main() {
var v1 = Boxtional(Optional<Stringgg>.none)
// CHECK: nil
print(v1.wrapped)
mutateResilientWrapped(&v1)
// CHECK: nil
print(v1.wrapped)

var v2 = Boxtional(Stringgg(value: "hi"))
// CHECK: "hi"
print(v2.wrapped)
mutateResilientWrapped(&v2)
// CHECK: "hihi"
print(v2.wrapped)
}
static func mutateWrappedInResilientClass<T : Mutatable>(_ t: ResilientWrappingClass<T>) {
t.wrapped.mutate()
}
Expand All @@ -350,12 +413,28 @@ struct M {
// CHECK: "hihi"
print(v2.wrapped)
}
static func resilient_subclass_main() {
let v1 = MaybePtrBox(Optional<Stringgg>.none)
// CHECK: nil
print(v1.wrapped)
mutateWrappedInResilientClass(NonresilientResilientWrappingSubclass(v1))
// CHECK: nil
print(v1.wrapped)
let v2 = MaybePtrBox(Stringgg(value: "hi"))
// CHECK: "hi"
print(v2.wrapped)
mutateWrappedInResilientClass(NonresilientResilientWrappingSubclass(v2))
// CHECK: "hihi"
print(v2.wrapped)
}
static func main() async {
sync_main()
await async_main()
proto_main()
class_main()
resilient_proto_main()
resilient_proto_default_main()
resilient_class_main()
resilient_subclass_main()
}
}