Skip to content

[5.9] Fix ManagedValue::isPlusOne for addresses #65644

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

Merged
merged 4 commits into from
May 4, 2023

Conversation

atrick
Copy link
Contributor

@atrick atrick commented May 4, 2023

  • Explanation: Fix ManagedValue::isPlusOne for addresses.

    In some cases, SILGen would reuse an address value after it was
    consumed. This is incorrect according to SIL's memory
    rules. In-memory values cannot be reused after deinitialization.

    Change this API to return false for address values that don't have a
    cleanup and therefore cannot be destroyed.

  • Scope of Issue: Before this fix, memory verification catches the
    incorrect SIL that SILGen produces for resilient enums that require
    a protocol conformance thunk for initialization. For example:

    public protocol ObjectProtocol {
    static func entityReference(_ id: EntityIdentifier) -> Self
    }

    public enum Object : ObjectProtocol {
    case entityReference(EntityIdentifier)
    }

    If we don't fix this, then SIL verification will continue to assert
    for real-world code. SIL verification is currently disabled by
    default for release compilers, but we rely on it for testing and
    triage.

  • Risk: SILGen could potentially generate more copies, but they would
    be cheap copies. We don't know of any cases where this would be a
    problem. This was discused among the team and determined that we
    don't want to allow SILGen to perform optimizations where memory is
    reused after it is deinitialized. Performance improved after this
    change in many more benchmarks that it regressed.

  • PR to main: Fix SILGen ManagedValue::isPlusOne for addresses #65579

  • Reviewed By: Joe Groff and Michael Gottesman

  • Resolves: rdar://108001491 (SIL verification failed: Found mutating or
    consuming use of an in_guaranteed parameter?!:
    !ImmutableAddressUseVerifier().isMutatingOrConsuming(fArg))

atrick added 4 commits May 3, 2023 22:13
In-memory values cannot be reused after deinitialization.

Add ManagedValue::isPlusOneOrTrivial() for situations where we want to
forward a value without destroying the original memory.

Fixes rdar://108001491 (SIL verification failed: Found mutating or
consuming use of an in_guaranteed parameter?!:
!ImmutableAddressUseVerifier().isMutatingOrConsuming(fArg))

(cherry picked from commit dc95b11)
Whenever we want to forward to a +1 value but don't need to destroy
the original memory, use isPlusOneOrTrivial.

This follows the existing naming scheme.

Fixes rdar://108001491 (SIL verification failed: Found mutating or
consuming use of an in_guaranteed parameter?!:
!ImmutableAddressUseVerifier().isMutatingOrConsuming(fArg))

(cherry picked from commit ea7b805)
Tests rdar://108001491 (SIL verification failed: Found mutating
or consuming use of in_guaranteed parameter?!: !ImmutableAddressUseVerifier().isMutatingOrConsuming(fArg))

(cherry picked from commit 298880d)
@atrick atrick requested a review from a team as a code owner May 4, 2023 05:44
@atrick
Copy link
Contributor Author

atrick commented May 4, 2023

@swift-ci test

@atrick
Copy link
Contributor Author

atrick commented May 4, 2023

@tbkka @gottesmm If we're going to take this SILGen fix in 5.9 it's probably better sooner than later just for testing purposes. If there are any issues we can certainly revert.

@atrick atrick merged commit 26e3d7d into swiftlang:release/5.9 May 4, 2023
@atrick atrick deleted the 5.9-fix-address-consume branch May 4, 2023 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants