Skip to content

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Oct 9, 2025

It used to be done with a library intrinsic which returns the array and an element address pointer. A mark_dependence was used to "connect" the two results.
But the pointer was obtained from a wrong borrow scope and from a wrong copy of the buffer. This resulted in completely wrong OSSA after inlining. It just didn't get caught by the verifier because the SIL was obfuscated by address-pointer conversions.

Now we don't use the second result (= the element address) of the intrinsic but generate a correct borrow scope from the first (= array) result. Within that borrow scope we project out the element address with a ref_tail_addr instruction.

This relies on knowledge of the internal Array data structures. However those data structures are baked into the ABI since a long time and will not change.

@eeckstein
Copy link
Contributor Author

@swift-ci smoke test

@eeckstein
Copy link
Contributor Author

@swift-ci apple silicon benchmark

// DEBUG-NEXT: [AD] Adjoint for the following value is a projection, skipping: %[[#D4:]] = pointer_to_address %[[#D5:]] : $Builtin.RawPointer to [strict] $*Float
// DEBUG-NEXT: [AD] The following active value is loop-local, zeroing its adjoint value in loop header: %[[#D6:]] = apply %[[#]]<Float>(%[[#D2]]) : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0>
// DEBUG-NEXT: [AD] Setting adjoint value for %[[#D6]] = apply %[[#]]<Float>(%[[#D2]]) : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kovdan01 Will you please double check that this is expected result here given the change of SIL for array literals?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kovdan01 Will you please double check that this is expected result here given the change of SIL for array literals?

@asl Thanks for mentioning, I'll double-check and let you know if this is expected

@eeckstein eeckstein force-pushed the array-initialization branch from 338bbd6 to 221d2e8 Compare October 10, 2025 07:18
@eeckstein eeckstein requested a review from ktoso as a code owner October 10, 2025 07:18
@eeckstein eeckstein force-pushed the array-initialization branch from 221d2e8 to 5fa3183 Compare October 10, 2025 08:15
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci apple silicon benchmark

It used to be done with a library intrinsic which returns the array and an element address pointer.
A `mark_dependence` was used to "connect" the two results.
But this resulted in completely wrong OSSA after inlining.
It just didn't get caught be the verifier because the SIL was obfuscated by address-pointer conversions.

Now we don't use the second result (= the element address) of the intrinsic but generate a correct borrow scope from the first (= array) result. Within that borrow scope we project out the element address with a `ref_tail_addr` instruction.

This relies on knowledge of the internal Array data structures. However those data structures are baked into the ABI since a long time and will not change.
…_based_sendability.swift

TODO: fix handling of Array initialization in the SendNonSenable pass
This benchmark just wants to test if stack promotion of an array literal works.
This is so simple that it's better tested with a lit test.
@eeckstein eeckstein force-pushed the array-initialization branch from 5fa3183 to 60efd32 Compare October 10, 2025 12:21
@eeckstein
Copy link
Contributor Author

@swift-ci smoke test

@meg-gupta
Copy link
Contributor

LGTM

Please update the PR description as discussed:

A mark_dependence was used to "connect" the two results. But this resulted in completely wrong OSSA after inlining. It just didn't get caught be the verifier because the SIL was obfuscated by address-pointer conversions.

OSLogOptimization also generates the previous SILGen pattern -

// Call the _allocateUninitializedArray function with numElementsSIL. The

This may need to be updated as well.

@eeckstein
Copy link
Contributor Author

This may need to be updated as well.

As this is not actually causing a miscompile, I'll leave that open for future work

@eeckstein eeckstein merged commit ee6d801 into swiftlang:main Oct 10, 2025
3 checks passed
@eeckstein eeckstein deleted the array-initialization branch October 10, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants