Commit Graph

11193 Commits

Author SHA1 Message Date
Andrew Trick
762cdc4c94 Fix PredictableDeadAllocationElimination ownership for empty structs
Preserve ownership for empty non-trivial structs. This currently applies to
~Escapable structs. People often use empty structs to investigate language
behavior. They should behave just like a struct that wraps a
pointer.

Previously, this would crash later during OSSA lifetime completion:

Assertion failed: (isa<UnreachableInst>(block->getTerminator())),
function computeRegion, file OSSALifetimeCompletion.cpp.
2025-01-13 16:36:40 -08:00
Erik Eckstein
9d0fc8b614 embedded: In IRGen, don't force generate code for functions which are imported from other modules.
Only generate code lazily for such functions, i.e. only if such a function is referenced from already generated code.
This is achieved by converting the SILLinkage for `public_external` functions to `internal` instead of `public in IRGenPrepare.
2025-01-13 14:08:35 +01:00
Meghana Gupta
15733446ed Merge pull request #78581 from meg-gupta/outlinerfix
Outliner: Do not outline if the BridgedArg value is not available at the BridgedCall
2025-01-13 04:15:20 -08:00
Nate Chandler
afe95135a0 [NFC] SILCombine: Ennamespace canonicalize class. 2025-01-12 15:32:54 -08:00
eeckstein
999669c860 Merge pull request #78441 from stzn/improve-use-before-initialized-diagnostic
[Diagnostics] A self argument implicitly passed as an inout parameter is diagnosed as used before initialized
2025-01-12 16:44:48 +01:00
Meghana Gupta
abc37ce218 Remove copy added by outliner for guaranteed bridged value
This copy may remain unoptimized, avoid outlining this case.
2025-01-11 08:15:24 -08:00
Meghana Gupta
c56573e424 Outliner: Do not outline if the BridgedArg value is not available at the BridgedCall
Avoid outlining this scenario instead of creating a copy to make the value available
2025-01-11 07:40:35 -08:00
nate-chandler
50c4861a04 Merge pull request #78552 from nate-chandler/rdar142520491
[OSSACanonicalizeGuaranteed] Don't rewrite consuming uses of move-only values.
2025-01-10 17:21:32 -08:00
Meghana Gupta
2c256180ce Add new utility swift::areUsesWithinValueLifetime 2025-01-10 16:15:50 -08:00
Nate Chandler
6dd45a70f9 [OSSACanonicalizeGuaranteed] Don't copy MO values.
When visiting a consuming use of a move-only value (which can be
produced by a forwarding operation), the inner rewriter must bail out.
Otherwise, it would produce a copy of that move-only value.

rdar://142520491
2025-01-10 10:14:21 -08:00
Nate Chandler
3b4803028d [NFC] OSSACanonicalizeGuaranteed: Args can bail.
Allow rewriting of arguments to bail out.  This is necessary because not
all forwarding instructions allow rewriting of forward(copy) as
copy(forward) (e.g. when forward produces a move-only value).
2025-01-10 10:14:21 -08:00
Nate Chandler
8803b612ac [NFC] OSSACanonicalizeGuaranteed: Flip condition.
Replace large nested condition with early exit.
2025-01-10 10:14:20 -08:00
Nate Chandler
0f6283c5dd [Gardening] OSSACanGuaranteed: Tweak comment. 2025-01-10 10:14:20 -08:00
eeckstein
b0bb19c001 Merge pull request #78524 from eeckstein/fix-codemotion
Fix two bugs which cause SIL verification errors
2025-01-10 10:48:04 +01:00
nate-chandler
d92ce1f436 Merge pull request #78507 from nate-chandler/rdar142570727
[SILCombine] Fix apply(convert_function(@guaranteed)) with an @owned operand.
2025-01-09 11:13:34 -08:00
Erik Eckstein
b80a08ee6c CodeMotion: check the ownership of arguments when sinking arguments
So far we only checked the ownership of incoming values.
But even if the incoming instruction has no ownership, the argument may have.
This can happen with enums which are constructed with a non-payload case:

   %1 = enum $Optional<C>, #Optional.none!enumelt
   br bb3(%1)
 bb1(%3 : @owned $Optional<C>):

Fixes an ownership verification error:
rdar://142506300
2025-01-09 19:56:25 +01:00
Erik Eckstein
db96d63e9d SimplifyCFG: correctly handle borrowed-from values when removing a redundant phi argument
Fixes a verifier crash
2025-01-09 19:56:25 +01:00
eeckstein
6b8c341b64 Merge pull request #78464 from eeckstein/sil-linker-error
SILLinker: convert an assert to a compiler error message
2025-01-09 07:44:47 +01:00
Nate Chandler
482e18b90f [SILCombine] Fix apply(convert_func(@guaranteed)).
The operands to the original apply are cast via an ownership forwarding
instruction to the appropriate type for the rewritten apply.

```
%converted = convert_function %original to $(NewTy) -> ()
apply %converted(%operand)
```
->
```
%cast = cast %operand to $OriginalTy
apply %original(%cast)
```

Previously, when an original operand is owned but the new apply does not
consume that operand, the newly added cast would consume the original
operand (an owned value)--something the original code being replaced did
not do.

```
%converted = convert_function %original to $(NewTy) -> ()
apply %converted(%operand : @guaranteed)
// %operand remains available
```
->
```
%cast = cast %operand to $OriginalTy // consumes %operand!
apply %original(%cast : @guaranteed)
// %operand is not available!
```

This is incorrect for the complementary reasons that the result of the
cast is leaked and any uses of the original operand subsequent to the
new apply are uses-after-consume.

Here, this is fixed by borrowing the original operand before casting in
this case.

rdar://142570727
2025-01-08 20:33:49 -08:00
Nate Chandler
f78c57daef [SILCombine] Rewrite try_apply(convert_function).
A partial revert of 428ab472ac.
2025-01-08 20:33:49 -08:00
Meghana Gupta
8397fc364c Merge pull request #78460 from meg-gupta/removeossa
Fix build option for Synchronization and Distributed
2025-01-08 15:20:07 -08:00
Michael Gottesman
2dbffa247b Merge pull request #78479 from gottesmm/pr-fadbda958eb1024880fd290cdb1b5b17ddc46b99
[rbi] Ensure that we infer isolation correctly for direct sending results
2025-01-08 14:56:18 -08:00
Meghana Gupta
0634babbe6 Merge pull request #78412 from meg-gupta/fixsilcombine
Fix SILCombine of existential applies
2025-01-08 12:12:36 -08:00
Erik Eckstein
67b8c08ef1 SILLinker: convert an assert to a compiler error message
Having a wrong linkage can happen if the user "re-defines" an existing function with a wrong linkage, e.g. using `@_cdecl`.
2025-01-08 11:00:27 +01:00
Daniil Kovalev
0ba886d409 [AutoDiff] Fix adjoint for move_value (#78286)
Since `move_value` is a destroying operation, the adjoint of `y = move_value x` should be `adj[x] += adj[y]; adj[y] = 0` instead of just `adj[x] += adj[y]`.
2025-01-08 00:42:54 -08:00
Michael Gottesman
c061ee72ec [rbi] Ensure that we infer isolation correctly for direct sending results.
I did the correct thing for indirect parameters, but did not do the correct
thing for direct parameters. This is now fixed with some tests to boot.

rdar://141631655
2025-01-07 14:33:39 -08:00
Meghana Gupta
ed0483b4ce Fix SILCombine of existential applies
Don't use previously found owned concrete values in ossa. They will consumed by
forwarding operations like init_existential_ref. Instead create an unconditional cast
of the opened existential to concrete type and use that to create a concrete apply.
2025-01-07 10:38:25 -08:00
Meghana Gupta
d55cb2ad6c Bailout of PredictableMemOpt utilities when in non-ossa
These utilities are called from MandatorPerformanceOptimizations which can run
on serialized non-ossa functions. Add a bailout to handle this case.
2025-01-07 01:19:09 -08:00
Daniil Kovalev
95f34ebaca [AutoDiff] Fix derivative for array literal with tuple_element_addr elts (#78355)
The `adjIndex` was not incremented due to missed `remapType`.

Fixes #54214
2025-01-06 16:11:54 -08:00
stzn
d429a90ebc [Diagnostics] A self argument implicitly passed as an inout parameter is diagnosed as used before initialized 2025-01-06 05:05:26 +09:00
Allan Shortlidge
d0f63a0753 AST: Split Availability.h into multiple headers.
Put AvailabilityRange into its own header with very few dependencies so that it
can be included freely in other headers that need to use it as a complete type.

NFC.
2025-01-03 18:36:04 -08:00
Meghana Gupta
8494f68899 Remove complex bailouts and fix replacing open_existential_ref
This code was previously creating illegal unchecked_ref_cast
 with same source and destination.
2025-01-03 16:15:34 -08:00
Michael Gottesman
b74be1433a [concurrency] Fix a small bug in RBI that caused us to not recognize that an @MainActor SILIsolationInfo is from an @MainActor. 2025-01-02 13:18:55 -08:00
Michael Gottesman
5d4239af57 [concurrency] Add new isolation kind CallerIsolationInheriting.
Right now it is basically a version of nonisolated beyond a few simple cases
like constructors/destructors where we are pretty sure we want to not support
this.

This is part of my bringup strategy for changing nonisolated/unspecified to be
caller isolation inheriting.
2025-01-02 13:18:30 -08:00
eeckstein
f096787871 Merge pull request #78384 from eeckstein/enable-code-motion-in-ossa
Optimizer: enable SILCodeMotion in OSSA, but only for trivial values.
2025-01-02 20:36:29 +01:00
eeckstein
efe8aeb718 Merge pull request #78317 from eeckstein/pointer-to-address-simplification
Optimizer: re-implement the `pointer_to_address` SILCombine peephole optimizations in swift
2025-01-02 12:54:26 +01:00
Erik Eckstein
53d78abd01 Optimizer: enable SILCodeMotion in OSSA, but only for trivial values.
to-do: we should eventually remove code motion of retain and release instructions and implement them as semantic ARC optimizations in OSSA.
2025-01-02 11:01:39 +01:00
Erik Eckstein
48b913af4b Optimizer: make the hasOwnershipOperandsOrResults utility available in OwnershipOptUtils 2025-01-02 10:42:01 +01:00
Meghana Gupta
ed2f43e67d Merge pull request #78354 from meg-gupta/enablesilcombine
Enable silcombine to propagate concrete type of existentials in ossa
2025-01-02 00:01:13 -08:00
eeckstein
aa2cfd3ed1 Merge pull request #78361 from eeckstein/dead-obj-elimination
DeadObjectElimination: handle OSSA instructions when analyzing class destructors
2025-01-02 08:41:15 +01:00
eeckstein
95f199212d Merge pull request #78351 from eeckstein/fix-specializer
GenericSpecializer: fix an ownership verification failure when removing a partial_apply of a thunk
2025-01-02 08:40:52 +01:00
eeckstein
1bdb51edc3 Merge pull request #78318 from eeckstein/fix-access-enforcement-opts
AccessEnforcementOpts: fix a SIL verifier error caused by a wrong bail-out condition
2025-01-02 08:31:28 +01:00
Erik Eckstein
41dd3dc453 DeadObjectElimination: handle OSSA instructions when analyzing class destructors
Handle typical patterns which are generated by SILGen.
2024-12-24 12:00:22 +01:00
Meghana Gupta
5e1f9a37ef Enable silcombine to propagate concrete type of existentials in ossa 2024-12-23 08:34:13 -08:00
Meghana Gupta
571498289a [NFC] Print final ossa module when enabled 2024-12-23 08:31:31 -08:00
Erik Eckstein
e6f161c948 GenericSpecializer: fix an ownership verification failure when removing a partial_apply of a thunk
rdar://141490629
2024-12-23 09:45:21 +01:00
Erik Eckstein
9aff288be4 Optimizer: re-implement the pointer_to_address SILCombine peephole optimizations in swift
Which consists of
* removing redundant `address_to_pointer`-`pointer_to_address` pairs
* optimize `index_raw_pointer` of a manually computed stride to `index_addr`
* remove or increase the alignment based on a "assumeAlignment" builtin

This is a big code cleanup but also has some functional differences for the `address_to_pointer`-`pointer_to_address` pair removal:

* It's not done if the resulting SIL would result in a (detectable) use-after-dealloc_stack memory lifetime failure.
* It's not done if `copy_value`s must be inserted or borrow-scopes must be extended to comply with ownership rules (this was the task of the OwnershipRAUWHelper).

Inserting copies is bad anyway.
Extending borrow-scopes would only be required if the original lifetime of the pointer extends a borrow scope - which shouldn't happen in save code. Therefore this is a very rare case which is not worth handling.
2024-12-21 08:28:22 +01:00
Ben Barham
8111fe9343 Merge pull request #78262 from bnbarham/skip-non-wmo-diag
[Embedded] Do not produce `cannot_specialize_class` for live issues
2024-12-20 10:40:07 -08:00
Erik Eckstein
413c78e33a AccessEnforcementOpts: fix a SIL verifier error caused by a wrong bail-out condition
In case of a bail-out on failed ownership extension the optimization didn't remove old instructions from previously merged access pairs.
2024-12-20 15:51:37 +01:00
Ben Barham
a2fda1d9f3 [Embedded] Do not produce cannot_specialize_class for live issues
SourceKit explicitly disables WMO, silence the diagnostic in this case
(but leave it enabled for explicit non-WMO builds otherwise).
2024-12-19 15:31:41 -08:00