Commit Graph

682 Commits

Author SHA1 Message Date
Erik Eckstein
85228f2c75 Optimizer: do mark_dependence simplification at Onone
* re-implement the SILCombine peephole as a Swift instruction simplification
* run this simplification also in the OnoneSimplification pass
2025-05-23 18:53:57 +02:00
Hamish Knight
edca7c85ad Adopt ABORT throughout the compiler
Convert a bunch of places where we're dumping to stderr and calling
`abort` over to using `ABORT` such that the message gets printed to
the pretty stack trace. This ensures it gets picked up by
CrashReporter.
2025-05-19 20:55:01 +01:00
Erik Eckstein
85a49dcbbc Optimizer: make salvageDebugInfo optional when deleting instructions
Add a boolean parameter `salvageDebugInfo` to `Context.erase(instruction:)`.
Sometimes it needs to be turned off because the caller might require that after erasing the original instruction the operands no users anymore.
2025-05-13 07:37:45 +02:00
Erik Eckstein
bc4310b0eb Optimizer: simplify unchecked_addr_cast for vectors
Reimplement the simplification in swift and add a new transformation:
```
  %1 = unchecked_addr_cast %0 : $*Builtin.FixedArray<N, Element> to $*Element
```
->
```
  %1 = vector_base_addr %0 : $*Builtin.FixedArray<N, Element>
```
2025-05-12 19:25:12 +02:00
Erik Eckstein
8dd76769bb SILCombine: handle mark_dependence in apply-of-partial_apply optimization
rdar://150686370
2025-05-06 19:42:01 +02:00
Slava Pestov
cd7a12ec2d SILOptimizer: Use ReplaceExistentialArchetypesWithConcreteTypes in SILCombiner 2025-04-28 11:49:50 -04:00
Erik Eckstein
199eb1d6e7 Optimizer: remove redundant copy_block instructions
Removes a `copy_block` if its only uses, beside ownership instructions, are callees of function calls
```
  %2 = copy_block %0
  %3 = begin_borrow [lexical] %2
  %4 = apply %3() : $@convention(block) @noescape () -> ()
  end_borrow %3
  destroy_value %2
```
->
```
  %4 = apply %0() : $@convention(block) @noescape () -> ()
```

rdar://118521396
2025-04-15 11:02:39 +02:00
Erik Eckstein
dff855034c SILCombine: fix a wrong assert in the SingleBlockOwnedForwardingInstFolder 2025-04-02 17:33:30 +02:00
Erik Eckstein
f7f5f9ad96 SILCombine: fix an ownership violation when replacing open existential apply arguments
The value of the opened existential must outlive the apply. Otherwise the optimization creates invalid SIL.

rdar://148259849
2025-04-02 17:33:30 +02:00
Arnold Schwaighofer
0222f90cf0 Merge pull request #80256 from aschwaighofer/disable_cond_fail_by_function
Disable cond_fail instructions by filtering the instruction's parent function
2025-03-28 13:18:19 -07:00
Arnold Schwaighofer
cc76edea15 Add a -print-cond-fail-messages-include-function-name option
Additionally, also consider the function name as the key to base cond_fail
removal on under `-cond-fail-config-file`.
2025-03-26 08:12:35 -07:00
Andrew Trick
d9dd93560d Support mark_dependence_addr in SIL passes. 2025-03-25 23:02:45 -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
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
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
Amritpan Kaur
39b48bb218 [SIL] Handle KeyPathComponentKind::Method in helpers. 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
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
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
Erik Eckstein
d225c47d25 AST: rename OpenArchetypeType -> ExistentialArchetypeType
NFC
2025-03-11 20:21:46 +01: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
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
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
Anton Korobeynikov
4310507d35 Ensure we're commuting instructions only when internal one has a single
use. Fix couple of similar single use predicates while there.

Fixes #78848
2025-02-24 13:59:31 -08:00
Meghana Gupta
d2936a15bc Merge pull request #78824 from meg-gupta/fixmdi
Fix operand ownership of mark_dependence [nonescaping] of address values
2025-01-24 14:59:36 -08:00
Meghana Gupta
79b649854b Fix operand ownership of mark_dependence [nonescaping] of address values 2025-01-24 00:49:32 -08:00
Erik Eckstein
51628dde12 SILCombine: workaround a problem with differential_function in OSSA
Disable this peephole optimization until the underlying problem is fixed.
https://github.com/swiftlang/swift/issues/78848

Part of rdar://140229560
2025-01-23 16:10:32 +01:00
Nate Chandler
af5cd31f3c [Test] Added test for SILCombiner::processInst. 2025-01-13 17:00:23 -08:00
Nate Chandler
6deba02792 [Test] Renamed test case. 2025-01-13 16:57:36 -08:00
Nate Chandler
5ce6c6cf14 [NFC] SILCombine: Extract processing function.
The pass is structured to drain an instruction worklist and perform a
sequence of operations on each popped instruction.  Extract that
sequence of operations into a new processInstruction function. Enables
testing the sequence on a single instruction.
2025-01-13 16:57:23 -08:00
Nate Chandler
afe95135a0 [NFC] SILCombine: Ennamespace canonicalize class. 2025-01-12 15:32:54 -08: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
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
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
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
Meghana Gupta
5e1f9a37ef Enable silcombine to propagate concrete type of existentials in ossa 2024-12-23 08:34:13 -08: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
Erik Eckstein
69b16faa04 SILCombine: enable alloc_stack optimization for OSSA 2024-12-17 09:55:39 +01:00
Andrew Trick
e56dbb4695 Improve SILCombine mark_dependence: handle address dependence. 2024-12-14 16:26:58 -08:00
Erik Eckstein
bf496aa4f6 Optimizer: add simplification for fix_lifetime
Canonicalize a `fix_lifetime` from an address to a `load` + `fix_lifetime`:
```
   %1 = alloc_stack $T
   ...
   fix_lifetime %1
```
->
```
   %1 = alloc_stack $T
   ...
   %2 = load %1
   fix_lifetime %2
```

This transformation is done for `alloc_stack` and `store_borrow` (which always has an `alloc_stack` operand).
The benefit of this transformation is that it enables other optimizations, like mem2reg.

This peephole optimization was already done in SILCombine, but it didn't handle store_borrow.
A good opportunity to make an instruction simplification out of it.

This is part of fixing regressions when enabling OSSA modules:
rdar://140229560
2024-12-13 12:06:20 +01:00
Erik Eckstein
6b38f2aab4 Optimizer: simplify load_borrow
* Remove dead `load_borrow` instructions (replaces the old peephole optimization in SILCombine)
* If the `load_borrow` is followed by a `copy_value`, combine both into a `load [copy]`
2024-12-11 12:32:33 +01:00
Slava Pestov
47156e006b AST: Introduce ProtocolConformanceRef::forAbstract() 2024-11-16 16:16:06 -05:00
Erik Eckstein
51e3e5ed80 Optimizer: rename BorrowArgumentsUpdater -> GuaranteedPhiUpdater
NFC
2024-11-12 09:26:59 +01:00
Erik Eckstein
6b8c6a3c3b SIL: rename updateBorrowedFrom to updateBorrowArguments
NFC
2024-11-12 09:26:58 +01:00
Meghana Gupta
d58d97d97a Fix SILCombine of inject_enum_addr of ~Copyable values
Fixes rdar://138798467
2024-10-29 14:09:57 -07:00