Commit Graph

1725 Commits

Author SHA1 Message Date
Aidan Hall
6aa55785f0 LifetimeDependence: Tests for function type attribute
Includes:
- rdar://166912068 test case
- Nested type SIL tests
- rdar://160894371 test
- Many more
2026-02-05 14:50:44 +00:00
Alejandro Alonso
6c754561a9 Fix serialization of init_borrow_addr 2026-01-27 16:20:49 -08:00
Erik Eckstein
7df75c8b80 SIL: add the InstructionIndices utility and use it in the SILVerifier
InstructionIndices caches instruction indices per basic block using a NodeBitfield.
This is much more efficient than a DenseMap.
2026-01-27 08:52:41 +01:00
Joe Groff
0f3ddfbcc8 Merge pull request #86545 from jckarter/builtin-borrow
`Builtin.Borrow` implementation
2026-01-26 07:32:31 -08:00
Tim Kientzle
925ec400f6 Merge pull request #86761 from tbkka/tbkka-force-unwind-off
[SE-0474] Force unwind off for new yielding accessors
2026-01-24 02:13:30 -08:00
eeckstein
4ae970e634 Merge pull request #86741 from eeckstein/argument-effects
SIL: assume a read-effect for unused indirect arguments
2026-01-24 07:56:21 +01:00
Tim Kientzle
7e32732649 [SE-0474] Force unwind off for new yielding accessors
In the context of coroutine/yielding accessors, "unwind" means
that the second half of the coroutine (code _after_ the `yield`
statement) will not run if an exception is thrown during the access.

Unwinding was the default behavor for legacy `_read`/`_modify` coroutine
accessors.
For the new `yielding borrow`/`yielding mutate` accessors, unwinding
was optional behind a separate feature flag.
But the final SE-0474 dictates that unwinding is always _disabled_ for
the new yielding accessors.  That is, the new yielding accessors always
run to completion, regardless of whether there are thrown exceptions within
the access scope.  This was deemed essential to ensure that authors of
data structures could guarantee consistency.

This PR permanently disables unwinding behavior for the new accessors.
The feature flag still exists but has no effect.
A handful of tests that verified the unwinding behavior have been
edited to ensure that unwinding does _not_ happen even when the feature
flag is specified.
2026-01-23 16:01:48 -08:00
Erik Eckstein
47fc8cc789 add an option -print-no-uuids to suppress printing UUIDs in SIL output
To to make the output better diffable
2026-01-23 19:20:19 +01:00
Joe Groff
b11e8c985a SIL: Handle dereference_borrow returns in SILGenCleanup.
Make sure an `end_borrow` is emitted on the `dereference_borrow` so that
SILGenCleanup recognizes this pattern and lowers it to a `return_borrow`
as it does for returned `load_borrow`s. Add support to the Swift implementation
of `BorrowingInstruction` for `DereferenceBorrow`.
2026-01-23 08:02:09 -08:00
Joe Groff
2f02c7cda3 SIL: Introduce instructions for forming and dereferencing Builtin.Borrow.
Since after address lowering, `Borrow` can remain loadable with a known-
layout address-only referent, we need instructions that handle three
forms:

- borrow and referent are both loadable values
- borrow is a value, but referent is address-only
- borrow and referent are both address-only
2026-01-23 08:02:01 -08:00
Erik Eckstein
535a58e3fb SIL: assume a read-effect for unused indirect arguments
Even if an indirect argument is unused, pretend that the function reads from it.
If the unused argument is passed to another generic function and that function is specialized,
the argument may be re-abstracted and the specializer inserts a load from the indirect argument.
Therefore we must be prepared that unused indirect argument might get loaded from at some time.

Fixes a compiler crash
rdar://168623362
2026-01-23 15:58:23 +01:00
Erik Eckstein
18063707b5 Optimizer: enable complete OSSA lifetimes throughout the pass pipeline
This new OSSA invariant simplifies many optimizations because they don't have to take care of the corner case of incomplete lifetimes in dead-end blocks.

The implementation basically consists of these changes:
* add the lifetime completion utility
* add a flag in SILFunction which tells optimization that they need to run the lifetime completion utility
* let all optimizations complete lifetimes if necessary
* enable the ownership verifier to check complete lifetimes
2026-01-22 17:41:48 +01:00
Erik Eckstein
0f0aa0c17b Optimizer: require that there are no unreachable blocks and infinite loops in OSSA
These two new invariants eliminate corner cases which caused bugs if optimization didn't handle them.
Also, it will significantly simplify lifetime completion.

The implementation basically consists of these changes:
* add a flag in SILFunction which tells optimization if they need to take care of infinite loops
* add a utility to break infinite loops
* let all optimizations remove unreachable blocks and break infinite loops if necessary
* add verification to check the new SIL invariants

The new `breakIfniniteLoops` utility breaks infinite loops in the control flow by inserting an "artificial" loop exit to a new dead-end block with an `unreachable`.
It inserts a `cond_br` with a `builtin "infinite_loop_true_condition"`:
```
bb0:
  br bb1
bb1:
  br bb1              // back-end branch
```
->
```
bb0:
  br bb1
bb1:
  %1 = builtin "infinite_loop_true_condition"() // always true, but the compiler doesn't know
  cond_br %1, bb2, bb3
bb2:                  // new back-end block
  br bb1
bb3:                  // new dead-end block
  unreachable
```
2026-01-22 17:41:23 +01:00
Erik Eckstein
5b201c9416 MemoryLifetimeVerifier: fix a false alarm for function calls reading from an argument location
If a struct field is not referenced in a function and that field is not read from a called function's argument, the verifier complained that the field must be initialized.

rdar://168051370
https://github.com/swiftlang/swift/issues/86511
2026-01-18 22:19:27 +01:00
Michael Gottesman
56d6b60c36 Merge pull request #86312 from gottesmm/rdar166081666
[rbi] Convert Sendable immutable weak var captures to weak let to fix RegionIsolation checking
2026-01-17 11:38:12 -08:00
Michael Gottesman
581cfcd7b6 [sil] Add @inferredImmutable flag to SILFunctionArgument for captured boxes
Introduce a new optional inferred-immutable flag on SILFunctionArgument to mark
closure-captured box parameters that are never written to despite being mutable.

This flag will enable in future commits:

- Marking captured mutable boxes as immutable when interprocedural analysis
  proves they are never modified
- Treating these captures as Sendable when they contain Sendable types
- Improving region-based isolation analysis for concurrent code

This complements the inferred-immutable flag on alloc_box by allowing
immutability information to flow through closure boundaries.
2026-01-16 08:22:21 -08:00
Pavel Yaskevich
c6ee92e5d2 [SILParser] Adjust ownership requirement for ImplicitActorToOpaqueIsolationCast
Similarly to SILVerifier condition, it has to use `isCompatibleWith`
to allow for "none" as well.
2026-01-15 11:03:23 -08:00
Pavel Yaskevich
500cf4cd25 [SILVerifier] Adjust ownership requirement for ImplicitActorToOpaqueIsolationCast
This check should use `isCompatibleWith` because sometimes the
operands don't have ownership i.e. when the actor is `nil`.

Resolves: rdar://164993540
2026-01-15 10:34:45 -08:00
Erik Eckstein
87dc1c5d8b SIL verifier: accept "owned" ownership for borrowed-from's enclosing values
"none" is like "owned" and can happen e.g. if a non-trivial enum is constructed with a trivial case:
```
  %1 = enum $Optional<AnyObject>, #Optional.none!enumelt   // ownership: none
  ...
  %3 = borrowed %phi from (%1)
```

Fix a false verification error
https://github.com/swiftlang/swift/issues/86407
rdar://167833469
2026-01-12 08:56:31 +01:00
Tim Kientzle
4955bc4568 [SE-0474] use new "yielding borrow" terminology in SIL printing
To help reduce cognitive burden for future Swift compiler engineers,
let's use the same terminology for coroutine accessors in SIL dumps
as we use in the surface language and inside the compiler.

This really just changes two lines in SILPrinter.cpp and updates
a lot of tests.  I've also copied one test to preserve the old syntax
to make sure that SIL parsing still accepts it.  That should hopefully
prevent unfortunate round-tripping issues while these changes settle.
2026-01-09 10:10:18 -08:00
Tim Kientzle
104dba920b [SE-0474] Implement yielding borrow and yielding mutate syntax
This does not rename all the internal variables, functions, and types
whose names were based on the old syntax.

I think it adds new syntax support everywhere it's needed while
retaining enough of the old syntax support that early adopters will
see nice deprecation messages guiding them to the new syntax.
2026-01-03 15:07:10 -08:00
Kavon Farvardin
829ddafa69 Merge pull request #86117 from kavon/disable-manualownership-on-simulator 2026-01-02 09:20:23 -08:00
eeckstein
5bb895f65f Merge pull request #86176 from eeckstein/fix-ownership-verifier
SIL: Fix the ownership verifier for guaranteed phi arguments
2026-01-02 06:44:34 +00:00
Erik Eckstein
503ccc8097 MemoryLifetimeVerifier: fix verification of init_enum_data_addr
`init_enum_data_addr` is a pure address projection instruction, similar to `struct_element_addr`.
It doesn't matter if the underlying memory is initialized or not at the point of this instruction.

Fixes a false memory-lifetime verification error
rdar://166962673
2025-12-29 08:08:34 +01:00
Erik Eckstein
6ce2df2ed1 SIL Verifier: workaround a MoveOnlyChecker pass bug
The MoveOnlyChecker is inserting a `destroy_addr` in a read-only access scope, which is illegal.
Relax the verification of read-only access scope by only looking at dynamic scopes.

Fixes a verifier crash.
rdar://166896665
2025-12-22 10:57:41 +01:00
Erik Eckstein
15636a197c SIL: Fix the ownership verifier for guaranteed phi arguments
The verifier reported a false error in case an unowned (trivial) value was used in an guaranteed phi argument outside the liverange of a borrow scope.
For example:

```
  %2 = begin_borrow %0
  %3 = struct_extract %2 , #KlassAndInt.int                // %3 is trivial and can outlive the borrow scope
  end_borrow %2
  %6 = struct $KlassAndInt (%someOtherGuaranteedValue, %3)
  br bb2(%6)

bb2(%8 : @guaranteed $KlassAndInt):      // false error reported at the uses of %8
```
2025-12-22 07:54:39 +01:00
Erik Eckstein
e265eb0a72 SILVerifier: don't crash on verifier errors in the function header
The thing with `optional<VerifierErrorEmitterGuard>` didn't work as expected.
This resulted in a null-pointer de-reference when printing a verifier error.
2025-12-19 17:44:01 +01:00
Erik Eckstein
ceafe65500 SILVerifier: print a readable error message when referencing an undefined SIL function
This error was not printed because the verifier complained before the error handling was done.
2025-12-19 17:44:01 +01:00
Kavon Farvardin
e1e9478fbf Test: disable flaky bit of manual_ownership.swift
The diagnostic messages are not yet stabilized, so we're
getting an extra diagnostic depending on whether we're building
the stdlib in debug-asserts mode, i.e., when

```
preset=buildbot,tools=RA,stdlib=DA
```

rdar://164852821
2025-12-18 11:31:04 -08:00
Erik Eckstein
c822615dc2 Optimizer: convert the UpdateBorrowedFromPass pass to a test
And remove the now empty TestPasses directory.
2025-12-15 10:01:51 +01:00
Erik Eckstein
bff873dea0 Optimizer: convert the SILPrinter and TestInstructionIteration passes to tests 2025-12-15 10:01:41 +01:00
Erik Eckstein
9ceb8b83c1 SIL-Verifier: Don't verify that there are no stores in read-only access scopes if there is a conflicting scope
This is a programming error, but the compiler should not crash. The violation is caught at runtime.
2025-12-04 21:12:32 +01:00
Michael Gottesman
4f29698edf Merge pull request #85766 from gottesmm/pr-6cfb709b2ef818e62904d539ba5998c638a598d2
[sil-verifier] Fix thinko around verifying joint post-dominance of begin_apply stack allocation.
2025-12-02 21:55:45 -08:00
Michael Gottesman
8a7b5f7c7f [sil-verifier] Make flow sensitive verification handle begin_apply token/allocation correctly.
Specifically:

1. We were tracking the stack allocation both in handleScopeInst and in the
Stack. We should only track it in one of them. I also used this as an
opportunity to make sure the code worked for non_nested code.

2. I made it so that we properly handle the end tracking part of the code so we
handle the token/stack part of begin_apply correctly. I generalized the code so
that we should handle non_nested stack allocations as well.

I included tests that validated that we now handle this correctly.
2025-12-02 14:17:27 -08:00
eeckstein
8d50f20965 Merge pull request #85728 from eeckstein/print-sil-ownership
SIL: add an option `-sil-print-ownership` to print ownership
2025-12-02 07:32:11 +01:00
Erik Eckstein
103d3c2f85 SILPrinter: print forwarding ownership for struct and tuple instructions
Print `forwarding: <ownership>` if the ownership of the result mismatches the operand ownership(s).
We already do this for all other forwarding instructions, but `struct` and `tuple` were missing.
2025-12-01 16:00:58 +01:00
Erik Eckstein
3ade98c84e SILPrinter: add an option -sil-print-ownership to print the ownership of instruction results
With this option the ownership of instruction results is printed in the comments at the end of the line, e.g.

```
  %3 = struct $S (%2, %1)                         // ownership: owned
```

And even without enabling this option, ownership comments are printed if the ownership of a result mismatches with its type.
That can happen e.g. for non-trivial enums which are constructed with a trivial case:

```
enum E {
  case A
  case B(AnyObject)
}

  %1 = enum $E, #E.A!enumelt  // type of %1 is non trivial, but ownership is "none"
```
2025-12-01 16:00:58 +01:00
Michael Gottesman
94e9166706 [sil-verifier] Use the scope result rather than the scope instruction when verifying that we end scope instructions.
We do this since we need to be able to handle instructions that may have two
different results that independently need their scopes lifetime to be ended.
2025-11-21 11:21:15 -08:00
Michael Gottesman
c876c1ee88 [sil-verifier] Split SILVerifier verificationFailure into separate functions for Instructions, Arguments, and add a variant for Values.
This also required me to change how we handled which instruction/argument we
emit an error about in the verifier. Previously we were using two global
variables that we made nullptr to control which thing we emitted an error about.
This was unnecessary. Instead I added a little helper struct that internally
controls what we will emit an error about and an external "guard" RAII struct
that makes sure we push/pop the instruction/argument we are erroring upon
correctly.
2025-11-21 11:21:15 -08:00
Michael Gottesman
29229a9410 [sil] Add basic SIL support for alloc_stack [non_nested].
This means I just added the flag and added support for
cloning/printing/serializing the bit on alloc_stack.
2025-11-21 11:21:14 -08:00
Andrew Trick
b28cd59dfc [NFC] TypeLowering: add CustomDeinit.
Teach SIL type lowering to recursively track custom vs. default deinit status.

Determine whether each type recursively only has default deinitialization. This
includes any recursive deinitializers that may be invoked by releasing a
reference held by this type.

If a type only has default deinitialization, then the deinitializer cannot
have any semantically-visible side effects. It cannot write to any memory
2025-11-11 17:29:45 -08:00
Meghana Gupta
21cc1a185b Add memory lifetime verification support for borrow accessors 2025-11-06 10:55:42 -08:00
Meghana Gupta
778ad0dfb5 Add SIL verification to ensure we don't return local addresses 2025-11-06 10:55:39 -08:00
Doug Gregor
38c23bcdda [Serialization] Allow near-misses when matching imported Clang declarations
Fixes the rest of rdar://137014448.
2025-11-02 07:21:15 -08:00
Kavon Farvardin
2d881bdc9c ManualOwnership: provide ability to apply to entire compilation unit
With this patch, I'm flipping the polarity of things.

The flag `-enable-experimental-feature ManualOwnership` now turns on the diagnostics,
but they're all silenced by default. So, you need to add -Wwarning or -Werror to
your build settings to turn on the specific diagnostics you care about.

These are the diagnostic groups relevant to the feature:

- SemanticCopies aka "explicit copies mode"
- DynamicExclusivity

For example, the build setting `-Werror SemanticCopies` now gives you errors about
explicit copies, just as before, but now you can make them just warnings with -Wwarning.

To opt-out a declaration from everything when using the feature, use @_noManualOwnership.

@_manualOwnership is no longer an attribute as a result.

resolves rdar://163372569
2025-10-24 18:54:07 -07:00
Kavon Farvardin
135e02c1d9 ManualOwnership: improve diagnostics & testing 2025-10-24 16:59:44 -07:00
Kavon Farvardin
6b858b411c ManualOwnership: introduce 'DynamicExclusivity'
We can add warnings about dynamic exclusivity
checks that may happen on an access, with
explainers about why they happen for safety.
2025-10-24 16:59:44 -07:00
Kavon Farvardin
95a6719117 ManualOwnership: introduce 'SemanticCopies' diagnostic group
This includes documentation to explain how to understand
these diagnostics.
2025-10-24 16:59:41 -07:00
Kuba (Brecka) Mracek
eb23d3bc0a Merge pull request #85074 from kubamracek/section
SE-0492: Stabilize @_section/@_used into @section/@used
2025-10-24 12:29:48 -07:00
Meghana Gupta
cc0e75ca03 Merge pull request #85040 from meg-gupta/returnborrowpr
Introduce return_borrow and unchecked_ownership instructions for use with borrow accessors
2025-10-23 10:45:55 -07:00