Commit Graph

11008 Commits

Author SHA1 Message Date
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
Nate Chandler
8e7dae560d [OSSACanOwned] Bail on escaping inner pointers.
If there is a pointer escape, we cannot safely rewrite its lifetime.
2025-02-27 07:20:27 -08:00
Nate Chandler
f1bc3b5205 [OSSACanOwned] Dead-end extend base lifetime.
Regardless of consumes of copies, if the original lexical value is not
consumed on a dead-end path, it must remain live to that dead-end.

rdar://145226757
2025-02-27 07:20:27 -08:00
Nate Chandler
5ffe092fa5 [OSSACanOwned] Extend all lifetimes to dead-ends.
Uses of borrows may occur between the borrow and a dead-end regardless
of lexicality, so always extend lifetimes to dead-ends.
2025-02-27 07:20:25 -08:00
Slava Pestov
541757a491 AST: Remove TypeBase::isOpenedExistential()
At one point, OpenedArchetypeType did not exist as a separate subclass
of ArchetypeType, so this method did something. Now, it's just
equivalent to calling is<> or isa<>.

I also removed a couple of asserts that were obvious no-ops as a result.
2025-02-27 09:55:36 -05:00
Meghana Gupta
199482a623 Fix hoisting array semantics call with non-dominant self
If we have a self value that does not dominate loop preheader,
and the array semantics call does not consume the self value,
that means there will be instructions that consume the self value
with the loop.

In ossa, we cannot hoist such semantic calls because there is no
support for creating destroys for them in the preheader.
Add a bailout to avoid the ownership error.

rdar://145673368
2025-02-26 12:11:59 -08:00
Andrew Trick
b3a445fb55 Fix BorrowingOperand::getBorrowIntroducingUserResult()
To handle borrowing operands that produce a dependence value but do not create a
nested borrow scope. This includes non-reborrow borrowed-from and guaranteed
mark_dependence [nonescaping].
2025-02-25 23:08:54 -08:00
Andrew Trick
f1792d80b3 Fix BorrowingOperand.visitScopeEndingUses
Only return false if the visitor returns false. Clients were ignoring the
result.

If the BorrowingOperand does not create a borrow scope, call visitUnknownUse
instead.

Until we have complete lifetimes, to avoid breaking code that cannot handle dead
defs, consider a dead borrow scope to be an unknown use.
2025-02-25 23:08:54 -08:00
Michael Gottesman
f01d27514d [rbi] Convert an assert to an if check.
This also fixes a case where we would have inferred the wrong isolation (which
the assert caught). I think we missed this since it only comes up with final
classes.

rdar://142568522
2025-02-25 13:39:09 -08:00
Michael Gottesman
929891807c Merge pull request #79611 from gottesmm/pr-1af1f4e53db03b01babcd0470c532fe00f6a7a11
[rbi] Respect nonisolated(unsafe) when assigning or merging into a sending out parameter.
2025-02-25 13:35:23 -08:00
eeckstein
d9489d7757 Merge pull request #79604 from eeckstein/fix-licm
LICM: fix handling of stores of Optional.none in OSSA
2025-02-25 22:13:43 +01:00
Michael Gottesman
dab324080b [rbi] Make convert_escape_to_no_escape and convert_function lookthrough if their result/operand is non-Sendable.
In these cases, we want to lookthrough so we propagate through
nonisolated(unsafe) and make it easier to discover that we are processing
keypaths (the reason I am making this change).
2025-02-25 08:39:39 -08:00
Dario Rexin
f5dd557307 Merge pull request #79506 from drexin/wip-145095088
[SILOptimizer] Don't apply CMO to key paths that reference inaccessib…
2025-02-25 07:08:52 -08:00
Erik Eckstein
bfdeb57863 LICM: fix handling of stores of Optional.none in OSSA
Currently we don't support hoisting ownership instructions.
But the check was missing a store of Optional.none because such a value has no ownership even if the optional is not trivial.

Fixes a SIL verifier crash.
https://github.com/swiftlang/swift/issues/79491
2025-02-25 09:34:59 +01:00
Dario Rexin
bde7daa2f2 [SILOptimizer] Don't apply CMO to key paths that reference inaccessible properties
rdar://145095088
2025-02-24 23:07:18 -08:00
Michael Gottesman
b286373631 [rbi] Make a confusing comment clearer.
From talking with @dgregor, it became clear that this comment was easily
interpreted as saying that AssignFresh always introduced a disconnected value...
which is not the case. Instead, AssignFresh just introduces a new value that
could have any form of isolation. The actual isolation of the value is assigned
via tryToTrackValue and eventually SILIsolationInfo::get().
2025-02-24 10:09:32 -08:00