Skip to content

Missed optimization combining pointers and parameter packs #82000

Open
@jrose-signal

Description

@jrose-signal

Description

In the following code, both the "non-pack" and "direct" variants optimize down to return 54, but the indirect one that uses both pointer-based indirection and variadic packs still writes the value to the stack, loads it twice using InitializeWithCopy, and then adds the result.

Reproduction

func applyPointer<each T>(input: repeat UnsafePointer<each T>, op: (repeat each T) -> Int) -> Int {
  op(repeat (each input).pointee)
}

func applyPointerNonPack<T, U>(input: UnsafePointer<T>, _ input2: UnsafePointer<U>, op: (T, U) -> Int) -> Int {
  op(input.pointee, input2.pointee)
}

func applyDirect<each T>(input: repeat each T, op: (repeat each T) -> Int) -> Int {
  op(repeat (each input))
}


func test() -> Int {
    withUnsafePointer(to: 27) {
        applyPointer(input: $0, $0) { ($0 + $1) }
    }
}

func testNonPack() -> Int {
    withUnsafePointer(to: 27) {
        applyPointerNonPack(input: $0, $0) { ($0 + $1) }
    }
}

func testDirect() -> Int {
    applyDirect(input: 27, 27) { ($0 + $1) }
}

Expected behavior

Ideally, the first variant would optimize down to return 54 like the other two.

Environment

Swift 6.1 on Compiler Explorer, also nightly version Swift version 6.2-dev (LLVM 5e7837820b1c886, Swift d5ef256).

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