Skip to content

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

Open
@filip-sakel

Description

@filip-sakel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions