Commit Graph

11193 Commits

Author SHA1 Message Date
Andrew Trick
e7000e4668 SIL: Add mark_dependence_addr 2025-03-25 23:02:42 -07:00
nate-chandler
17952d4d6f Merge pull request #80221 from nate-chandler/general-coro/20250320/1
[CoroutineAccessors] Default overrides of new symbols.
2025-03-25 12:22:40 -07:00
Pavel Yaskevich
5af7e0d09b Merge pull request #80236 from xedin/is-self-recursive-cache
[SILOptimizer] Turn "is self-recursive" check into analysis
2025-03-25 10:37:07 -07:00
Nate Chandler
4951e9d091 [DFE] Default overrides are alive.
They are used externally and can't be eliminated as dead when there are
no uses from SIL (of which in fact there never are any).
2025-03-25 07:22:43 -07:00
Pavel Yaskevich
41c88f864a [SILOptimizer] Turn "is self-recursive" check into analysis
The body of a function has to be re-analyzed for every call
site of the function, which is very expensive and if the
body is not changed would produce the same result.

This takes about ~10% from swift-syntax overall build time
in release configuration.
2025-03-24 00:25:13 -07:00
Doug Gregor
731f58443c Address review feedback on AbstractConformance in ProtocolConformanceRef 2025-03-23 20:54:39 -07:00
Kuba (Brecka) Mracek
033f6679e8 Merge pull request #80185 from kubamracek/embedded/used
[embedded] Link in @_used declarations from other modules in SILLinker
2025-03-22 12:01:56 -07:00
Arnold Schwaighofer
7da2333017 Merge pull request #80167 from aschwaighofer/print_cond_fail_message_strings
SIL: Add an option to print all the cond_fail message strings encountered during IRGenPrepare
2025-03-21 15:45:46 -07:00
eeckstein
962be95c75 Merge pull request #79511 from swiftlang/78848-fix
Ensure we're commuting instructions only when internal one has a single use.
2025-03-21 09:16:34 +01:00
Kuba Mracek
9a293ed29a [embedded] Link in @_used declarations from other modules in SILLinker 2025-03-20 15:52:15 -07:00
Arnold Schwaighofer
e11e02675c SIL: Add an option to print all the cond_fail message strings encountered during IRGenPrepare
Run the swift-frontend in a mode that will generate LLVM IR adding the
option `-print-cond-fail-messages` will dump all cond_fail message strings
encountered in the SIL.

```
% swift-frontend -Xllvm -print-cond-fail-messages -emit-ir/-c ...
cond_fail message encountered: Can't take a prefix of negative length from a collection
cond_fail message encountered: Range out of bounds
cond_fail message encountered: Array index is out of range
```
2025-03-20 09:46:16 -07:00
Arnold Schwaighofer
69e9dd34c2 Merge pull request #80127 from aschwaighofer/disable_cond_fail_by_message_comment
Add comment for cond-fail-config-file option
2025-03-20 08:06:55 -07:00
eeckstein
29876fd491 Merge pull request #80137 from eeckstein/fix-cast-optimizer
CastOptimizer: fix optimization of casts from non-existentials to existentials
2025-03-20 07:26:18 +01:00
Amritpan Kaur
3c30d68d2e Merge pull request #78823 from amritpan/method-keypaths
[Sema/SILGen/IRGen] Implement method & initializer keypaths.
2025-03-19 18:59:17 -07:00
Erik Eckstein
21264019cd CastOptimizer: fix optimization of casts from non-existentials to existentials
We need to consider that archetypes (generic types) can be existentials if they conform to self-conforming protocols.

Fixes a miscompile
rdar://147269904
2025-03-19 20:30:48 +01:00
Amritpan Kaur
39b48bb218 [SIL] Handle KeyPathComponentKind::Method in helpers. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
e7e354d989 [NFC] Generalize subscript code for methods. 2025-03-19 10:54:09 -07:00
Arnold Schwaighofer
1e15a85378 Add comment for cond-fail-config-file option 2025-03-19 10:28:57 -07:00
Arnold Schwaighofer
2f30161563 Merge pull request #80097 from aschwaighofer/remove_cond_fails_by_message
SIL: Allow to selectively disabled cond_fails by cond_fail message
2025-03-19 10:26:21 -07:00
Erik Eckstein
cf8c8561ca Optimizer: move optimizer bridging code from PassManager.cpp into its own file.
The bridging code was in PassManager.cpp only for historical reasons.
It's now in OptimizerBridging.cpp.

NFC
2025-03-19 09:28:53 +01:00
eeckstein
c28279f3f1 Merge pull request #80095 from eeckstein/cleanup-passes-def
PassManager: cleanup Passes.def
2025-03-19 06:56:26 +01:00
Arnold Schwaighofer
1fd53d23a3 SIL: Allow to selectively disabled cond_fails by cond_fail message
The standard library uses `_precondition` calls which have a message argument.

Allow disabling the generated cond_fail by these message arguments.

For example:

 _precondition(source >= (0 as T), "Negative value is not representable")

Results in a `cond_fail "Negative value is not representable"`.

This commit allows for specifying a file that contains these messages on
each line.

/path/to/disable_cond_fails:
```
Negative value is not representable
Array index is out of range
```

The optimizer will remove these cond_fails if the swift frontend is invoked with
`-Xllvm -cond-fail-config-file=/path/to/disable_cond_fails`.
2025-03-18 13:04:03 -07:00
Meghana Gupta
30a364d3f0 Merge pull request #80031 from meg-gupta/closurelifetimefixup
Fix ClosureLifetimeFixup to insert destroys at leaking blocks
2025-03-18 10:40:31 -07:00
Erik Eckstein
62ea5b1c81 PassManager: cleanup Passes.def
* move the "SILCombine passes" into a separate file `Simplifications.def` which lives in the SILCombiner directory
* group passes by kind
* rename PASS -> LEGACY_PASS and add a comment to make clear that new passes should be implemented in Swift

NFC
2025-03-18 18:34:52 +01:00
Doug Gregor
9c2c4ea07f Merge pull request #79707 from DougGregor/transparent-integer-conversions
[Stdlib performance] Make integer conversion operations transparent
2025-03-17 16:37:16 -07:00
Meghana Gupta
c528afccf2 Fix ClosureLifetimeFixup to insert destroys at leaking blocks 2025-03-14 15:48:44 -07:00
Erik Eckstein
37455b6ab6 Optimizer: use formal types instead of SIL types for classifying dynamic casts.
Casts always work with formal rather than lowered types.
This fixes a potential bug when lowered types are different than formal types, like function types.
2025-03-14 09:45:27 +01:00
Shubham Sandeep Rastogi
72867f30bc Merge pull request #79924 from rastogishubham/DroppedVarStatsFix
Fix bug in dropped variable statistics for SIL.
2025-03-13 10:51:54 -07:00
Andrew Trick
74452825fd Merge pull request #79969 from atrick/fix-comment
Remove an incorrect PassPipeline comment.
2025-03-12 19:41:25 -07:00
Konrad `ktoso` Malawski
8c4dea9802 Revert "[concurrency] Add support for HopToMainActorIfNeededThunk." (#79938)
* Revert "[concurrency] Add support for HopToMainActorIfNeededThunk."

This reverts commit 0e0665bfbd.

* remove some last bits of 0e0665b
2025-03-13 06:48:03 +09:00
Andrew Trick
619eb42a24 Remove an incorrect PassPipeline comment.
A pass was deleted from the PassPipeline without removing its comments!
2025-03-12 14:47:33 -07:00
Ravi Kandhadai
cbf804e9f3 [SILOpt][OSLog] Skip constant folding StaticString in the
OSLogOptimization pass

The OSLogOptimization pass constant evaluates and folds SIL instructions
that are inlined from the construction of the string interpolations passed
to the log calls, which enables replacing the dynamic format string construction
with a static format string. In addition to folding constant strings, it also folds
constant integers and arrays whose elements are constants. This change makes it
skip folding static strings, since they are already efficiently represented.

rdar://146028438
2025-03-12 07:40:07 -07:00
Shubham Sandeep Rastogi
b80045a5c1 Fix bug in dropped variable statistics for SIL.
When checking for false positives, we want to make sure that if a
debug_value is dropped, we also find a real instruction that shares
the same scope as the debug_value or has a scope that is a child
of the scope of the debug_value, and has an inlinedAt equal to the
inlinedAt of the debug_value or it's inlinedAt chain contains the
inlinedAt of the debug_value. However, this instruction shouldn't be
another debug_value.

The check was supossed to check if(!I.isDebugInstruction())
but it checked if(I.isDebugInstruction())

This patch fixes that bug.
2025-03-11 14:32:53 -07:00
Erik Eckstein
d225c47d25 AST: rename OpenArchetypeType -> ExistentialArchetypeType
NFC
2025-03-11 20:21:46 +01:00
Doug Gregor
24f4e6486d Merge pull request #79885 from DougGregor/remove-region-isolation-assert
[Region analysis] Remove an assertion that doesn't always hold
2025-03-11 01:26:04 -07:00
Doug Gregor
25e1a00dbe [Region analysis] Remove an assertion that doesn't always hold
It appears that we can end up breaking this assertion when inlining
SIL from modules with strict concurrency enabled into modules that
don't. That's not a assertion-worth condition.
2025-03-10 11:19:16 -07:00
Erik Eckstein
2ce9ddeb46 ConditionForwarding: don't violate ownership
Instructions in a block, which is moved, must not use any (non-trivial) value because we don't do liveness analysis.
When moving a block, there is no guarantee that the operand value is still alive at the new location.

Fixes an ownership violation error
rdar://146630743
2025-03-10 12:48:14 +01:00
eeckstein
737b5ec924 Merge pull request #79674 from eeckstein/simplify-open-existential-metatype
Optimizer: Rework peephole optimizations to replace existential (archetypes) with concrete types
2025-03-10 08:18:52 +01:00
eeckstein
cbba008ac3 Merge pull request #79835 from eeckstein/fix-generic-specializer
GenericSpecializer: avoid an infinite loop when looking up GenericSpecializationInformation
2025-03-10 08:15:13 +01:00
Meghana Gupta
b6b4a7bfdb Merge pull request #79841 from meg-gupta/fixsemanticarc
Fix two optimizer issues
2025-03-07 19:45:00 -08:00
Meghana Gupta
6c2d3eb27d Bailout of CopyValueOpts when there is a pointer escape
Copy propagation does these optimizations more generally. It should be
able to replace this optimization. Fixing it and not just deleting CopyValueOpts
because it happens to be called in the mandatory pipeline.
2025-03-07 10:40:37 -08:00
Erik Eckstein
46035305aa Optimizer: improve simplification of alloc_stack
* Reimplement most of the logic in Swift as an Instruction simplification and remove the old code from SILCombine
* support more cases of existential archetype replacements:

For example:
```
  %0 = alloc_stack $any P
  %1 = init_existential_addr %0, $T
  use %1
```
is transformed to
```
  %0 = alloc_stack $T
  use %0
```

Also, if the alloc_stack is already an opened existential and the concrete type is known,
replace it as well:
```
  %0 = metatype $@thick T.Type
  %1 = init_existential_metatype %0, $@thick any P.Type
  %2 = open_existential_metatype %1 : $@thick any P.Type to $@thick (@opened("X", P) Self).Type
  ...
  %3 = alloc_stack $@opened("X", any P) Self
  use %3
```
is transformed to
```
  ...
  %3 = alloc_stack $T
  use %3
```
2025-03-07 15:59:34 +01:00
Erik Eckstein
71de3d90e0 Optimizer: replace existential archetypes with concrete types in apply instructions
If an apply uses an existential archetype (`@opened("...")`) and the concrete type is known, replace the existential archetype with the concrete type
  1. in the apply's substitution map
  2. in the arguments, e.g. by inserting address casts
For example:
```
  %5 = apply %1<@opend("...")>(%2) : <τ_0_0> (τ_0_0) -> ()
```
->
```
  %4 = unchecked_addr_cast %2 to $*ConcreteType
  %5 = apply %1<ConcreteType>(%4) : <τ_0_0> (τ_0_0) -> ()
```
2025-03-07 15:59:34 +01:00
Erik Eckstein
35097b5a71 Optimizer: simplify unconditional_checked_cast to existential metatypes.
Replace `unconditional_checked_cast` to an existential metatype with an `init_existential_metatype`, it the source is a conforming type.
Note that init_existential_metatype is better than unconditional_checked_cast because it does not need to do any runtime casting.
2025-03-07 15:59:33 +01:00
Erik Eckstein
df81283b97 SILCombine: make it possible to add a Swift simplification to an existing SILCombine visit function.
So far a `SILCombineSimplifiable` could only replace a SILCombine visit implementation.
With the `SWIFT_SILCOMBINE_PASS_WITH_LEGACY` (to be used in Passes.def) it's possible to keep an existing C++ implementation and on top of that add a Swift Simplification pass.
2025-03-07 15:59:33 +01:00
Erik Eckstein
43cb79dc10 GenericSpecializer: avoid an infinite loop when looking up GenericSpecializationInformation
fixes a compiler hang
rdar://146330284
2025-03-07 15:09:11 +01:00
Erik Eckstein
25873407ad CapturePropagation: handle existential keypaths
Handle open_existential_ref instructions which cast keypath instructions before they are passed to a partial_apply.
In Swift language mode 6 keypaths are existentials (e.g. `any WritableKeyPath<Str, Int> & Sendable`) and we need to deal with that in CapturePropagation.

rdar://141370412
2025-03-06 16:48:01 +01:00
Meghana Gupta
5851118f3a Merge pull request #79775 from meg-gupta/fixtemplvalue
Fix TempLValueOpt for init_enum_data_addr
2025-03-05 04:07:10 -08:00
Meghana Gupta
3addadc503 Merge pull request #79766 from meg-gupta/fixmem2reg
Fix mem2reg for load_borrows with reborrows
2025-03-04 18:29:17 -08:00
Meghana Gupta
8438e165fa Fix TempLValueOpt for init_enum_data_addr
TempLValueOpt eliminates copies from a temporary to destination and
supports hoisting projections of the destination.

An enum is fully initialized with the pair init_enum_data_addr and
inject_enum_addr. Calling destroy_addr only before inject_enum_addr
can cause a runtime crash.

This optimization can eliminate copy_addr and hoist init_enum_data_addr
such that enum is not fully initialized before it's use.

Disable this case for now.
Fixes rdar://145941433

To correctly do this optimization we have to find the corresponding inject_enum_addr
and hoist it as well or ensure the source is not used until fully initialized by inject_enum_addr.
2025-03-04 16:23:09 -08:00