Commit Graph

7787 Commits

Author SHA1 Message Date
Jakub Florek
6955bdf564 Merge pull request #84173 from MAJKFL/fix-licm-not-projecting-load-path
Fix licm not projecting load path before load splitting.
2025-09-09 22:43:53 +01:00
Jakub Florek
be72ad726a Fix licm not projecting load path before load splitting. 2025-09-09 17:02:29 +01:00
eeckstein
2b6565d051 Merge pull request #84101 from eeckstein/simplify-explicit-copy
Optimizer: replace `explicit_copy_value` and `explicit_copy_addr` with their non-explicit counterparts
2025-09-05 01:21:50 +02:00
Andrew Trick
592642087f Merge pull request #84010 from atrick/lifedep-mutating-default
@_lifetime defaults
2025-09-04 15:44:46 -07:00
Erik Eckstein
844217d58c Optimizer: replace explicit_copy_value and explicit_copy_addr with their non-explicit counterparts
The `explicit_copy_value` and `explicit_copy_addr` instructions are only used for non-copyable diagnostics in the mandatory pipeline.
After that we can replace them by their non-explicit counterparts so that optimizations (which only know of `copy_value` and `copy_addr`) can do their work.

rdar://159039552
2025-09-04 19:40:53 +02:00
Arnold Schwaighofer
6da200f047 Merge pull request #84009 from aschwaighofer/assembly_vision_all_the_things
Add a flag to annotate all functions with the `optremark` (`@_assemblyVision`) attribute
2025-09-04 08:57:44 -07:00
Erik Eckstein
ddea9f6aa0 Optimizer: switch to the new ConstantCapturePropagation pass in the pass pipeline 2025-09-04 08:15:46 +02:00
Erik Eckstein
efa9f9ebc0 Optimizer: rewrite and improve the ConstantCapturePropagation pass
(old name: CapturePropagation)

The pass is now rewritten in swift which makes the code smaller and simpler.
Compared to the old pass it has two improvements:

* It can constant propagate whole structs (and not only builtin literals). This is important for propagating "real" Swift constants which have a struct type of e.g. `Int`.
* It constant propagates keypaths even if there are other non-constant closure captures which are not propagated. This is something the old pass didn't do.

rdar://151185177
2025-09-04 08:15:46 +02:00
Andrew Trick
91ab0b79bf Update unit tests for improved inference rules. 2025-09-03 22:42:13 -07:00
JanBaig
48cedb5e72 Fix RawSILInstLowering test 2025-09-03 10:30:57 -04:00
Janat Baig
f21eb5375e Merge branch 'main' into temp-branch 2025-09-02 20:23:25 -04:00
Jakub Florek
e24ef9dcf5 Merge pull request #84055 from MAJKFL/fix-read-apply-hoisted-with-conflicting-write-apply-bug
Fix read only apply hoisting with conflicting write apply in the new LICM
2025-09-02 21:46:18 +01:00
Jakub Florek
e905df42f2 Fix read apply hoisted with conflicting write apply. 2025-09-02 15:53:53 +01:00
Pavel Yaskevich
96900f9bd0 Merge pull request #84012 from xedin/rdar-158063151
[CSOptimizer] Don't match `nil` to `_OptionalNilComparisonType`
2025-09-02 07:05:21 -07:00
Pavel Yaskevich
df962a83c6 [CSOptimizer] Don't match nil to _OptionalNilComparisonType
This type is only intended for pattern matching against `nil`
and the solver shouldn't early attempt to infer this type for
`nil` for arguments of `==` and `!=` operators it should instead
be inferred from other argument or result.

Resolves: rdar://158063151
2025-09-01 20:47:56 -07:00
nate-chandler
b3af244c46 Merge pull request #84022 from nate-chandler/rdar158139991
[Mem2Reg] Add end_lifetime for more trivial case non-trivial enums.
2025-09-01 06:56:26 -07:00
Jakub Florek
eae7864370 Merge pull request #83988 from MAJKFL/new-sil-licm-pass-copy
New SIL LICM pass
2025-09-01 10:28:17 +01:00
eeckstein
c2cd172860 Merge pull request #83999 from eeckstein/access-enforcement-improvements
Optimizer: some small fixes and improvements for exclusivity checking
2025-08-31 07:22:00 +02:00
eeckstein
50b5ecf042 Merge pull request #84013 from eeckstein/canonical-substitution-maps
SIL: only use canonical substitution maps in SIL
2025-08-30 20:30:07 +02:00
Erik Eckstein
c790052590 AllocBoxToStack: convert access checks from "dynamic" to "static"
Once we have promoted the box to stack, access violations can be detected statically by the DiagnoseStaticExclusivity pass (which runs after MandatoryAllocBoxToStack).
Therefore we can convert dynamic accesses to static accesses.

rdar://157458037
2025-08-30 07:29:15 +02:00
Erik Eckstein
b6e9c4c5e4 TempLValueElimination: don't propagate alloc_stack which have access scopes
If exclusivity is checked for the alloc_stack we must not replace it with the copy-destination.
If the copy-destination is also in an access-scope this would result in an exclusivity violation which was not there before.

Fixes a miscompile which results in a wrong exclusivity violation error at runtime.
https://github.com/swiftlang/swift/issues/83924
rdar://159220436
2025-08-30 07:29:15 +02:00
Erik Eckstein
86076e2ada RawSILInstLowering: Don't insert an access scope for assign_or_init if there is already one.
This avoids inserting a dynamic access check when the parent is static (and therefore can be statically enforced).
2025-08-30 07:29:14 +02:00
Erik Eckstein
667de83339 Optimizer: move DiagnoseStaticExclusivity after MandatoryAllocBoxToStack
This is needed because MandatoryAllocBoxToStack can convert dynamic accesses to static accesses.
Also, it improves diagnostics for closure captures.
2025-08-30 07:28:33 +02:00
Kavon Farvardin
511971fb1d Merge pull request #83959 from kavon/rdar159079818-subscript-read-nc
silgen: allow borrow of subscript for noncopyables
2025-08-29 20:52:23 -07:00
Nate Chandler
1c52364d28 [Mem2Reg] Complete single-block trivial enum cases
Add `end_lifetime` for values of non-trivial enum type which aren't
destroyed in non-dead-end blocks.
2025-08-29 15:47:10 -07:00
Nate Chandler
1f7432dedb [Mem2Reg] Don't skip write-only enum locations.
The lifetimes of the values stored into these locations may need to be
completed: if a trivial case of a non-trivial enum is written to the
location, its lifetime must be completed.

rdar://158139991
2025-08-29 15:45:14 -07:00
Erik Eckstein
6df033b298 SIL: only use canonical substitution maps in SIL
* When constructing instructions which have substitution maps: initialize those with the canonical SubstitutionMap
* Also initialize SILFunction::ForwardingSubMap with the canonical one

Non-canonical substitution maps may prevent generic specializations.
This fixes a problem in Embedded Swift where an error is given because a function cannot be specialized, although it should.

https://github.com/swiftlang/swift/issues/83895
rdar://159065157
2025-08-29 22:31:04 +02:00
Kavon Farvardin
906a4cbcdb silgen: allow borrow of subscript for noncopyables
If a subscript uses a read accessor to yield a noncopyable value,
we'd emit an `end_apply` that's too tightly scoped to allow for
a subsequent borrowing access on the yielded value.

resolves rdar://159079818
2025-08-29 13:30:31 -07:00
Arnold Schwaighofer
df5a8cd3cc Add a flag to annotate all functions with the optremark (@_assemblyVision) attribute
rdar://159291169
2025-08-29 09:26:13 -07:00
Jakub Florek
bab00113b2 Fix licm handling of unreferenceable storage. 2025-08-29 12:46:27 +01:00
eeckstein
05ddeae3c7 Merge pull request #83977 from eeckstein/fix-array-property-opt
Optimizer: fix an ownership violation when duplicating loops
2025-08-29 13:43:18 +02:00
nate-chandler
9fe43836f6 Merge pull request #83907 from nate-chandler/rdar158149082
[AllocBoxToStack] Don't destroy in dead-ends.
2025-08-28 13:36:28 -07:00
Jakub Florek
07ac8b3478 Add new loop invariant code motion. 2025-08-28 21:00:33 +01:00
Erik Eckstein
87e7250750 Optimizer: fix an ownership violation when duplicating loops
If a guaranteed value is used in a dead-end exit block and the enclosing value is _not_ destroyed in this block, we end up missing the enclosing value as phi-argument after duplicating the loop.
TODO: once we have complete lifetimes we can remove this check again.

rdar://159125605
2025-08-28 18:40:54 +02:00
eeckstein
8cea77eac0 Merge pull request #83913 from eeckstein/fix-deinit-devirtualization-for-arrays
Optimizer: de-virtualize deinits of `builtin "destroyArray"`
2025-08-28 18:21:07 +02:00
Erik Eckstein
e5081a511f AliasAnalysis: fix aliasing of ref_tail_addr access bases
In contrast to `ref_element_addr`, tail addresses can also be obtained via a superclass (in case the derived class doesn't add any stored properties).
Therefore if the instance types differ by sub-superclass relationship, they may alias.

Fixes a miscompile
2025-08-28 08:09:39 +02:00
Erik Eckstein
8e33487471 DeinitDevirtualizer: de-virtualize deinits of builtin "destroyArray" 2025-08-28 08:05:28 +02:00
JanBaig
4effb83bc0 Refactor SILOptimizer test to use assign_or_init 2025-08-27 20:35:26 -04:00
Nate Chandler
1eafced6a2 [AllocBoxToStack] Don't destroy in dead-ends.
It is valid to leak a value on paths into dead-end regions.
Specifically, it is valid to leak an `alloc_box`.  Thus, "final
releases" in dead-end regions may not destroy the box and consequently
may not release its contents.  Therefore it's invalid to lower such final
releases to `dealloc_stack`s, let alone `destroy_addr`s.  The in-general
invalidity of that transformation results in miscompiling whenever a box
is leaked and its projected address is used after such final releases.

Fix this by not treating final releases as boundary markers of the
`alloc_box` and not lowering them to `destroy_addr`s and
`dealloc_stack`s.

rdar://158149082
2025-08-27 17:03:48 -07:00
Nate Chandler
f94168c875 [AllocBoxToStack] Restore isDeadEnd check.
The rewrite was missing the intentional omission of `dealloc_stack`s
corresponding to `[dead_end]` `dealloc_box`es.  Add the necessary
bridging to get to parity with the original.

Without this check, `dealloc_box [dead_end]`s are promoted to
`dealloc_stack`s but the memory projected out of such `alloc_box`s need
not be valid.

rdar://159271158
2025-08-27 17:02:27 -07:00
Arnold Schwaighofer
07248c70b8 Merge pull request #83904 from aschwaighofer/mark_dep_closure_inline_heuristic
ConstantTracker: Look through mark_dependence instructions
2025-08-27 14:50:28 -07:00
Aidan Hall
53028abea9 Merge pull request #83665 from aidan-hall/fix-effects
ComputeSideEffects: compute properties even if function has an effect attribute
2025-08-27 10:34:53 +01:00
eeckstein
ff8058da99 Merge pull request #83898 from eeckstein/fix-simplify-alloc-stack-apply
Optimizer: fix handling of dependent existential archetypes in `alloc_stack` and `apply` simplification
2025-08-27 07:37:33 +02:00
Allan Shortlidge
3919fe1459 Tests: Mark SILOptimizer/propagate_opaque_return_type.swift as unsupported for back deployment.
It requires a deployment target that is too new for back deployment runtime testing.

Resolves rdar://159026084.
2025-08-26 15:42:54 -07:00
Erik Eckstein
56521f0c3d Optimizer: fix handling of dependent existential archetypes in alloc_stack and apply simplification
When replacing an opened existential type with the concrete type, we didn't consider that the existential archetype can also be a "dependent" type of the root archetype.
For now, just bail in this case. In future we can support dependent archetypes as well.

Fixes a compiler crash.
rdar://158594365
2025-08-26 18:01:10 +02:00
Erik Eckstein
87bf92494a SILCombine: fix convert_function -> apply peephole for generic function types
Currently we cannot deal with generic arguments/returns. Bail in this case.

Fixes a compiler crash
rdar://158809851
2025-08-26 10:16:49 +02:00
Arnold Schwaighofer
2fd904808c ConstantTracker: Look through mark_dependence instructions
An interposed mark_dependence should not block the heuristic that adds a
benefit when inlining a function that takes a closure argument.
2025-08-25 13:57:23 -07:00
Erik Eckstein
833560f08f SIL: fix printing of dependent existential archetypes
We were missing parentheses:
```
  $@opened("...", any P2) Self.A
```
->
```
  $(@opened("...", any P2) Self).A
```
2025-08-25 19:38:26 +02:00
Erik Eckstein
bdea6063d7 Mem2Reg: Fix lifetime completion for enum case values.
Enum types may have incomplete lifetimes in address form for trivial case values. When promoted to value form, they will end up with incomplete ossa lifetimes.
Because we know that the incomplete enum values are trivial enum cases we complete their lifetimes with `end_lifetime` instead of `destroy_value`.
This is especially important for Embedded Swift where we are not allowed to insert destroys which were not there before.

Fixes a compiler crash in Embedded Swift caused by de-virtualizing such an inserted destroy and ending up with a non-specialized generic function.

rdar://158807801
2025-08-25 09:35:46 +02:00
Aidan Hall
6a263f8ddb ComputeSideEffects: Compute for functions with effect attributes
After computing side effects, we also remove any global or argument effects that
are computed to happen, but are defined not to, based on the effect attribute.

This allows us to compute the deinit_barrier effect for such functions, fixing
the test case here: rdar://155870190

This supercedes #38324.
2025-08-22 11:42:09 +01:00