Commit Graph

1501 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
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
Erik Eckstein
d802b5955f SIL: Fix ResultInfo.init(bridged: OptionalBridgedResultInfo)
The initializer has to be optional because OptionalBridgedResultInfo can be nil
2025-09-04 08:15:46 +02:00
Erik Eckstein
8e86b5ce3b Optimizer: add a preserveGenericSignature flag to FunctionPassContext.createSpecializedFunctionDeclaration 2025-09-04 08:15:46 +02:00
Erik Eckstein
45b1a21e74 Optimizer: make ModulePassContext.specialize() also available in FunctionPassContext and add two argument flags
add `convertIndirectToDirect` and `isMandatory`
2025-09-04 08:15:45 +02:00
Erik Eckstein
b8a49692eb Optimizer: add TypeSubstitutionCloner and func cloneAndSpecializeFunction
Also move `func cloneFunction` from ContextCommon.swift to OptUtils.swift
2025-09-04 08:15:45 +02:00
Erik Eckstein
231042b9a8 SIL: some Cloner cleanups and improvements
* move some Cloner utilities from ContextCommon.swift directly into Cloner.swift
* add an `cloneRecursively` overload which doesn't require the `customGetCloned` closure argument
* some small cleanups
2025-09-04 08:15:45 +02:00
Erik Eckstein
65d69fe965 SIL/AST: add some APIs
* `GenericSignature.isEmpty`
* `Builder.emitDestroy`
* `Function.abi`
* `KeyPathInst.substitutionMap`
* `KeyPathInst.hasPattern`
2025-09-04 08:15:44 +02:00
Erik Eckstein
1a4bd76f95 Mangling: add specialization mangling for more complex constant propagated function arguments
So far, constant propagated arguments could only be builtin literals.
Now we support arbitrary structs (with constant arguments), e.g. `Int`.
This requires a small addition in the mangling scheme for function specializations.
Also, the de-mangling tree now looks a bit different to support a "tree" of structs and literals.
2025-09-04 08:15:44 +02:00
Erik Eckstein
85381a580e Optimizer: move notifyNewFunction from Context to FunctionPassContext 2025-09-04 08:15:44 +02:00
Andrew Trick
ed9283e1ad [NFC] drive-by comment 2025-09-03 22:42:13 -07:00
Janat Baig
f21eb5375e Merge branch 'main' into temp-branch 2025-09-02 20:23:25 -04:00
Jakub Florek
e905df42f2 Fix read apply hoisted with conflicting write apply. 2025-09-02 15:53:53 +01: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
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
3de5d6a13e SIL: add get+set for enforcement in BeginAccessInst 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
Jakub Florek
bab00113b2 Fix licm handling of unreferenceable storage. 2025-08-29 12:46:27 +01: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
Jakub Florek
3514b2b9df Bridge array semantics call and loop tree 2025-08-28 21:00:09 +01:00
Jakub Florek
e3140e0ae0 Add new generalized cloner. 2025-08-28 20:57:57 +01: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
Erik Eckstein
49d3960c68 MandatoryPerformanceOptimization: de-virtualize deinits of builtin "destroyArray"
This is required for embedded swift.
rdar://157131184
2025-08-28 08:05:27 +02:00
Erik Eckstein
3da2c1476b SIL: add Builder.createCondBranch 2025-08-28 08:04:50 +02:00
Erik Eckstein
ab10dc23a5 SIL: add Context.getTupleType 2025-08-28 08:04:49 +02: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
74528eef8c [NFC] OptUtils: Promote isInLoop to internal.
It will be used by both AllocBoxToStack in addition to StackPromotion
and for the same reason.
2025-08-27 17:03:47 -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
Slava Pestov
66dc1cc2e8 Merge pull request #83902 from slavapestov/lowered-type-in-context
Cache SILFunction::getLoweredTypeInContext()
2025-08-27 08:19:13 -04: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
Daniil Kovalev
ae2b46e263 [AutoDiff][gardening] Auto-format ClosureSpecialization.swift (#83925)
Auto-formatting is done as a prerequisite for the ongoing series of
patches resolving #68944.
2025-08-27 06:12:47 +00: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
0a8c60290f AST: add Type.interfaceTypeOfArchetype and some related APIs 2025-08-26 16:38:19 +02:00
Slava Pestov
6c60f03f9e SIL: Split up Function.loweredFunctionType/InContext 2025-08-25 16:45:45 -04:00
Janat Baig
798c0f51a4 Merge branch 'main' into temp-branch 2025-08-23 11:11:04 -04:00
JanBaig
4c61096be7 [SIL] Remove AssignByWrapper handling from analysis and utils 2025-08-22 23:15:16 -04:00
JanBaig
c2850c33c9 [SIL] Remove AssignByWrapper definition and registration 2025-08-22 23:06:59 -04: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
Doug Gregor
7a196ac3ba Merge pull request #83739 from DougGregor/embedded-weak-definitions-of-imports
[Embedded] Emit weak definitions for imported symbols
2025-08-17 20:25:02 -07:00
Doug Gregor
c91a4822d3 Serialize the "markedAsUsed" bit for SILGlobalVariable 2025-08-17 15:25:50 -07:00
Andrew Trick
288cef1532 LifetimeDependenceScopeFixup: extend store_borrow allocations
Extend temporary allocations (sink dealloc_stacks) initialized by a store_borrow
across lifetime dependent uses.

Fixes rdar://143159873 ([nonescapable] extend rvalue lifetimes when they are the source of a dependency)
2025-08-14 10:13:55 -07:00
Andrew Trick
b3e169be5e [NFC] Rename a variable for clarity. 2025-08-14 09:08:11 -07:00
Andrew Trick
eb1d5f484c [NFC] SwiftCompilerSources: add a correctly named filterUsers API
Rename existing filterUsers to filterUses.
2025-08-14 09:08:11 -07:00