From ff50f8f2be17b2de87b7361350f736491a28003d Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Fri, 29 Aug 2025 16:54:45 -0700 Subject: [PATCH 1/3] Fill in two missing functions for Embedded Swift `swift_coroFrameAlloc` is needed by `_read`/`_modify` accessors `swift_deallocUninitializedObject` which I believe is needed for failable class initializers Resolves rdar://157028375 Resolves rdar://157276375 --- stdlib/public/core/EmbeddedRuntime.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/stdlib/public/core/EmbeddedRuntime.swift b/stdlib/public/core/EmbeddedRuntime.swift index 545d67c5cda8c..a715a105a2b1b 100644 --- a/stdlib/public/core/EmbeddedRuntime.swift +++ b/stdlib/public/core/EmbeddedRuntime.swift @@ -149,6 +149,13 @@ func alignedAlloc(size: Int, alignment: Int) -> UnsafeMutableRawPointer? { return unsafe r } +@_cdecl("swift_coroFrameAlloc") +public func swift_coroFrameAlloc(_ size: Int, _ type: UInt) -> UnsafeMutableRawPointer? { + return unsafe alignedAlloc( + size: size, + alignment: _swift_MinAllocationAlignment) +} + @_cdecl("swift_slowAlloc") public func swift_slowAlloc(_ size: Int, _ alignMask: Int) -> UnsafeMutableRawPointer? { let alignment: Int @@ -178,6 +185,14 @@ func swift_allocObject(metadata: UnsafeMutablePointer, requiredSi return unsafe object } +@_cdecl("swift_deallocUninitializedObject") +public func swift_deallocUninitializedObject(object: Builtin.RawPointer, allocatedSize: Int, allocatedAlignMask: Int) { + unsafe swift_deallocObject( + object: UnsafeMutablePointer(object), + allocatedSize: allocatedSize, + allocatedAlignMask: allocatedAlignMask) +} + @_cdecl("swift_deallocObject") public func swift_deallocObject(object: Builtin.RawPointer, allocatedSize: Int, allocatedAlignMask: Int) { unsafe swift_deallocObject(object: UnsafeMutablePointer(object), allocatedSize: allocatedSize, allocatedAlignMask: allocatedAlignMask) From 73e7df9e9795949311d779d8a7b6e611ef605d8b Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Thu, 4 Sep 2025 18:05:14 -0700 Subject: [PATCH 2/3] Test for `swift_coroFrameAlloc` --- test/embedded/accessors.swift | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/embedded/accessors.swift diff --git a/test/embedded/accessors.swift b/test/embedded/accessors.swift new file mode 100644 index 0000000000000..6e10d46528fed --- /dev/null +++ b/test/embedded/accessors.swift @@ -0,0 +1,37 @@ +// RUN: %empty-directory(%t) +// RUN: %target-swift-frontend %s -Onone -parse-as-library -enable-experimental-feature Embedded -c -o %t/main.o +// RUN: %target-clang %target-clang-resource-dir-opt %t/main.o -o %t/a.out -dead_strip +// RUN: %target-run %t/a.out | %FileCheck %s + +// REQUIRES: swift_in_compiler +// REQUIRES: executable_test +// REQUIRES: optimized_stdlib +// REQUIRES: swift_feature_Embedded + +public class C { + public var x: Int { + _read { + yield(y) + } + _modify { + yield(&y) + } + } + + var y: Int = 27 +} + +@main +struct Main { + static func main() { + print("1") // CHECK: 1 + let c = C() // CHECK: 27 + print(c.y) + c.y = 28 + print(c.y) // CHECK: 28 + print("") + + print("2") // CHECK: 2 + print("") + } +} From 4368722a501ebb8fbb812e90002d3c070bebfde5 Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Fri, 5 Sep 2025 14:08:49 -0700 Subject: [PATCH 3/3] Test for swift_deallocUninitializedObject --- test/embedded/closures-heap.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/embedded/closures-heap.swift b/test/embedded/closures-heap.swift index 97f34a0200770..f9c220fe95aef 100644 --- a/test/embedded/closures-heap.swift +++ b/test/embedded/closures-heap.swift @@ -8,6 +8,10 @@ // REQUIRES: optimized_stdlib // REQUIRES: swift_feature_Embedded + + +func f() -> Bool? { return nil } + public class MyClass { var handler: (()->())? = nil func foo() { @@ -32,5 +36,12 @@ struct Main { o!.foo() // CHECK: capture local print(local == 43 ? "43" : "???") // CHECK: 43 o = nil // CHECK: deinit + + let closure = { + guard var b = f() else { print("success"); return } + let c = { b = true } + _ = (b, c) + } + closure() // CHECK: success } }