Skip to content

[SILGen] Remove subtle identity function call. #67846

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

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Aug 9, 2023

Back in 33f4f57 of #28044 fame, non-CaptureKind::Constant: uses of Entry.val in SILGenFunction::emitCaptures were replaced with a use of the newly added lambda getAddressValue applied at Entry.val.

The replacement of Entry.value with getAddressValue(Entry.value) in the case of CaptureKind::Box had no effect.

Back then, the reason was that the condition

SGM.Types
    .getTypeLowering(
        valueType,
        TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(
            expansion.getResilienceExpansion()))
        .isAddressOnly() &&
    !entryValue->getType().isAddress()

under which something other than the input was returned would never hold: CaptureKind::Box is used for LValues and those never satisfy !entryValue->getType().isAddress().

Since that PR, the getAddressValue lambda has grown. There are two additional aspects to consider: (1) forceCopy, (2) isPack. (1) is not relevant because false was being passed for the CaptureKind::Box case. (2) can not currently happen because pack lvalues haven't been implemented. But even if they were implemented, the argument passed to the lambda would still need to be an address, and it still wouldn't be desired to convert that address (a project_box) into an on-stack temporary (what the isPack branch of getAddressValue presently does).

The same all holds for the CaptureKind::ImmutableBox case which only differs from the CaptureKind::Box by a couple of lines.

Way back in

commit d0bb027
Author: Joe Groff <[email protected]>
Date:   Mon Nov 23 11:47:09 2015 -0800

the convention was changed such that LValues were passed only by box
rather than by box and address.  Delete the comment that says that both
are passed.
Back in 33f4f57 of
swiftlang#28044 fame,
non-`CaptureKind::Constant:` uses of `Entry.val` in
`SILGenFunction::emitCaptures` were replaced with a use of the newly
added lambda `getAddressValue` applied at `Entry.val`.

The replacement of `Entry.value` with `getAddressValue(Entry.value)` in
the case of `CaptureKind::Box` had no effect.

Back then, the reason was that the condition

    SGM.Types
        .getTypeLowering(
            valueType,
            TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(
                expansion.getResilienceExpansion()))
            .isAddressOnly() &&
        !entryValue->getType().isAddress()

under which something other than the input was returned would never
hold: CaptureKind::Box is used for LValues and those never satisfy
`!entryValue->getType().isAddress()`.

Since that PR, the getAddressValue lambda has grown.  There are two
additional aspects to consider: (1) forceCopy, (2) isPack.  (1) is not
relevant because `false` was being passed for the `CaptureKind::Box`
case.  (2) can not currently happen because pack lvalues haven't been
implemented.  But even if they were implemented, the argument passed to
the lambda would still need to be an address.

The same all holds for the `CaptureKind::ImmutableBox` case which only
differs from the `CaptureKind::Box` by a couple of lines.
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please test source compatibility

@nate-chandler nate-chandler merged commit 9d5b175 into swiftlang:main Aug 10, 2023
@nate-chandler nate-chandler deleted the nfc/20230809/1/silgen-closure-simplification branch August 10, 2023 13:57
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.

2 participants