Skip to content

Embedded KeyPath Optimization Limited to Types Internal to the Module #81588

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

Open
filip-sakel opened this issue May 17, 2025 · 0 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@filip-sakel
Copy link
Contributor

Description

This PR added support for optimizing key paths out of Embedded code, but it seems to work solely on types declared in the current module. Trying something like Box<String>().count fails for example.

Reproduction

Building with the following flags: -Xfrontend -enable-experimental-feature -Xfrontend Embedded -wmo -Xfrontend -enable-experimental-feature -Xfrontend SymbolLinkageMarkers:

@dynamicMemberLookup
public struct MyBox<T>: ~Copyable {
    init() {
        self.value = UnsafeMutablePointer<T>.allocate(capacity: 1)
    }

    subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> U {
        @_transparent
        get { return self.value.pointer(to: member)!.pointee }

        @_transparent
        set { self.value.pointer(to: member)!.pointee = newValue }
    }

    subscript<U>(dynamicMember member: KeyPath<T, U>) -> U {
        @_transparent
        get { return self.value.pointer(to: member)!.pointee }
    }

    var value: UnsafeMutablePointer<T>
}

struct MyType {
    var computedProp: Int { 0 }
}

@_used
func useVector() -> Int {
    let myBox = MyBox<MyType>()

    // Works fine :)
    return myBox.computedProp
}


@_used
func useString() -> Int {
    let myBox = MyBox<MyType>()

    // Error: cannot use key path in embedded Swift
    return myBox.count
}

Or see live in Godbolt

Expected behavior

I would expect key paths to external types to also get optimized out.

Environment

Swift version 6.1-dev (LLVM 83bb915697f5992, Swift 1e211b4)
Target: x86_64-unknown-linux-gnu

Additional information

No response

@filip-sakel filip-sakel added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant