Commit Graph

11224 Commits

Author SHA1 Message Date
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
Meghana Gupta
4c46faad26 Fix mem2reg for load_borrows with reborrows
StackAllocationPromoter::pruneAllocStackUsage substitutes loads/stores of alloc_stack
with values. For some reason isLoadFromStack was bailing out for load_borrows with
reborrows leaving them to be fixed up by fixBranchesAndUses which uses live in value
from predecessors for substitution which is obviosly incorrect the block containing
the load_borrow has a store before it.

Fixes rdar://145834542
2025-03-04 10:18:08 -08:00
Susana Monteiro
593f320460 Merge pull request #79325 from swiftlang/susmonteiro/copy-constructor-default-args
[cxx-interop] Prevent usage in Swift of C++ copy constructor with default args
2025-03-04 15:40:22 +00:00
Gábor Horváth
c91e295234 Merge pull request #79576 from swiftlang/gaborh/fix-consume-calling-conv
[cxx-interop] Fix calling convention for rvalue reference params
2025-03-04 02:31:49 -08:00
Meghana Gupta
cf6c4c9422 Merge pull request #79699 from meg-gupta/prboundscheck
Add support for bounds check optimization of Span and InlineArray
2025-03-03 14:32:27 -08:00
susmonteiro
bc6573ec32 [cxx-interop] Prevent usage in Swift of C++ copy constructor with default args 2025-03-03 13:22:11 +00:00
Gabor Horvath
00fa738209 [cxx-interop] Fix calling convention for rvalue reference params
In C++, we always expected to invoke the dtor for moved-from objects.
This is not the case for swift. Fortunately, @inCxx calling convention
is already expressing that the caller supposed to destroy the object.
This fixes the missing dtor calls when calling C++ functions taking
rvalue references. Fixes #77894.

rdar://140786022
2025-03-03 11:47:12 +00:00
Andrew Trick
19c8bd625f Add FIXMEs for calls to liveness that fail to check pointer escapes. 2025-03-02 23:51:34 -08:00
Andrew Trick
727aed3a86 Fix ClosureLifetimeFixup to handle pointer escapes.
Fix insertDeallocOfCapturedArguments to conservatively sink deallocs over
pointer escapes.
2025-03-02 23:51:34 -08:00
Andrew Trick
7760dbc983 Fix ClosureLifetimeFixup handling of incomplete liveness.
Correctly handle pointer escapes and dependent uses to avoid miscompiles.
2025-03-02 23:51:34 -08:00
Meghana Gupta
2b072a56c1 Allow duplicating mark_dependence [nonescaping] on addresses 2025-02-28 17:11:59 -08:00
Meghana Gupta
26277c8363 Add support for bounds check optimization of Span and InlineArray 2025-02-28 17:11:52 -08:00
Pavel Yaskevich
4e51155f64 Merge pull request #78995 from jamieQ/DI-add-stats
[NFC]: add a FrontendStatsTracer for definite init
2025-02-28 16:05:35 -08:00
Meghana Gupta
6908e9b776 Annotate some Span and InlineArray's methods with semantics 2025-02-28 09:52:00 -08:00
Meghana Gupta
3fe1029ef8 [NFC] Reorganize and rename ArrayBoundsCheckOpts.cpp 2025-02-28 09:50:58 -08:00
Slava Pestov
d8a1649722 Merge pull request #79666 from slavapestov/fix-rdar144644342
Sema: Fix Sendable generic parameter fixit
2025-02-27 18:37:50 -05:00
nate-chandler
39acb9ac8a Merge pull request #79521 from nate-chandler/rdar145154549
[OSSACanonicalizeOwned] Dead-end extend base lifetime.
2025-02-27 14:36:43 -08:00
Slava Pestov
74222a62b8 Merge pull request #79659 from slavapestov/pointless-predicate
AST: Remove TypeBase::isOpenedExistential()
2025-02-27 14:46:18 -05:00
Meghana Gupta
b7952284eb Merge pull request #79644 from meg-gupta/arraypropoptfix
Fix hoisting array semantics call with non-dominant self in ossa
2025-02-27 10:40:23 -08:00
Slava Pestov
e37a2d065d Sema: Fix Sendable generic parameter fixit
Fixes rdar://problem/144644342.
2025-02-27 13:15:13 -05:00