Commit Graph

103 Commits

Author SHA1 Message Date
Erik Eckstein
94b6e511ba Optimizer: add simplification for switch_enum
Replace
```
    %1 = enum $E, #someCase, %payload
    switch_enum %1, case #someCase: bb1, ...
  bb1(%payloadArgument):
```
->
```
  br bb1(%payload)
  bb1(%payloadArgument):
```
2023-12-15 17:11:49 +01:00
Erik Eckstein
dd9ce40ba1 add the allocVector builtin 2023-12-09 18:49:57 +01:00
Erik Eckstein
c829d5bd77 SimplifyBeginCOWMutation: don't eliminate end_cow_mutation instructions with the [keep_unique] flag
This flag is important for following optimizations. Therefore such `end_cow_mutation` instructions must not be removed.
Also, keep the flag in a SILCombine transformation.

rdar://119178823
2023-12-06 08:59:58 +01:00
Erik Eckstein
fc534e1c28 SwiftCompilerSources: better APIs for handling resilient nominal types
* add `NominalTypeDecl.isResilient`

* make the return type of `Type.getNominalFields` optional and return nil in case the nominal type is resilient.
This forces users of this API to think about what to do in case the nominal type is resilient.
2023-11-27 09:21:33 +01:00
Erik Eckstein
3bfabf6e8a SimplifyBeginBorrow: don't remove begin_borrow instructions which have the [point_escape] flag set. 2023-11-14 12:26:23 +01:00
Erik Eckstein
4f09d29afd Optimizer: add simplification for begin_borrow
Try to replace a begin_borrow with its owned operand.

This is either possible if the borrowed value (or a forwarded value if it) is copied:

```
  %1 = begin_borrow %0
  %2 = struct_extract %1     // a chain of forwarding instructions
  %3 = copy_value %1
  // ... uses of %3
  end_borrow %1
```
->
```
  %1 = copy_value %0
  %3 = destructure_struct %0 // owned version of the forwarding instructions
  // ... uses of %3
```

Or if the borrowed value is destroyed immediately after the borrow scope:

```
  %1 = begin_borrow %0
  %2 = struct_extract %1     // a chain of forwarding instructions
  // ... uses of %2
  end_borrow %1
  destroy_value %1           // the only other use of %0 beside begin_borrow
```
->
```
  %2 = destructure_struct %0 // owned version of the forwarding instructions
  // ... uses of %2
  destroy_value %2
```
2023-11-13 20:18:07 +01:00
Erik Eckstein
0509a056fb SwiftCompilerSources: improve APIs for UseList
Make filter APIs for UseList chainable by adding them to Sequence where Element == Operand

For example, it allows to write:
```
let singleUse = value.uses.ignoreDebugUses.ignoreUsers(ofType: EndAccessInst.self).singleUse
```

Also, add `UseList.getSingleUser(notOfType:)`
2023-11-13 20:18:07 +01:00
Erik Eckstein
6ff3b91fe4 SimplifyDestructure: support simplifying a destructure_struct with a copy_value in place
Replace
```
  %s = struct (%0, %1, %2)
  %c = copy_value %s           // can also be a chain of multiple copies
  (%3, %4, %5) = destructure_struct %c
```
with
```
  %c0 = copy_value %0
  %c1 = copy_value %1
  %c2 = copy_value %2
  %s = struct (%0, %1, %2)    // keep the original struct
```
and replace the results %3, %4, %5 with %c0, %c1, %c2, respectively.
2023-11-09 18:34:53 +01:00
Erik Eckstein
e718bfe8ed Optimizer: reimplement simplifications for copy_value and destroy_value in swift
So that they can run in the OnoneSimplification pass
2023-10-27 10:47:07 +02:00
Erik Eckstein
b6938475b9 Optimizer: add simplification for the convert_escape_to_noescape instruction
Including the required bridging stuff.

  %2 = thin_to_thick_function %1 to $() -> ()
  %3 = convert_escape_to_noescape %2 : $() -> () to $@noescape () -> ()
->
  %3 = thin_to_thick_function %1 to $@noescape () -> ()
2023-10-27 10:47:07 +02:00
Erik Eckstein
8b9f2a73d0 SimplifyApply: optimize thick calls where the callee is a thin_to_thick_function
```
   %2 = thin_to_thick_function %1
   %3 = apply %2(...) : @callee_guaranteed
 ->
   %2 = thin_to_thick_function %1
   %3 = apply %1(...): @convention(thin)
```
2023-10-27 10:47:07 +02:00
Erik Eckstein
2dbd6cc56b SwiftCompilerSources: rework bridging
Introduce two modes of bridging:
* inline mode: this is basically how it worked so far. Using full C++ interop which allows bridging functions to be inlined.
* pure mode: bridging functions are not inlined but compiled in a cpp file. This allows to reduce the C++ interop requirements to a minimum. No std/llvm/swift headers are imported.

This change requires a major refactoring of bridging sources. The implementation of bridging functions go to two separate files: SILBridgingImpl.h and OptimizerBridgingImpl.h.
Depending on the mode, those files are either included in the corresponding header files (inline mode), or included in the c++ file (pure mode).

The mode can be selected with the BRIDGING_MODE cmake variable. By default it is set to the inline mode (= existing behavior). The pure mode is only selected in certain configurations to work around C++ interop issues:
* In debug builds, to workaround a problem with LLDB's `po` command (rdar://115770255).
* On windows to workaround a build problem.
2023-10-09 09:52:52 +02:00
Kuba Mracek
5d8c55eacb [embedded] Initial Swift Concurrency for embedded Swift 2023-10-06 20:04:03 -07:00
Andrew Trick
a5d8aafb23 SwiftCompilerSources: Replace BlockArgument with Phi and TermResult.
All SILArgument types are "block arguments". There are three kinds:
1. Function arguments
2. Phis
3. Terminator results

In every situation where the source of the block argument matters, we
need to distinguish between these three. Accidentally failing to
handle one of the cases is an perpetual source of compiler
bugs. Attempting to handle both phis and terminator results uniformly
is *always* a bug, especially once OSSA has phi flags. Even when all
cases are handled correctly, the code that deals with data flow across
blocks is incomprehensible without giving each case a type. This
continues to be a massive waste of time literally every time I review
code that involves cross-block control flow.

Unfortunately, we don't have these C++ types yet (nothing big is
blocking that, it just wasn't done). That's manageable because we can
use wrapper types on the Swift side for now. Wrapper types don't
create any more complexity than protocols, but they do sacrifice some
usability in switch cases.

There is no reason for a BlockArgument type. First, a function
argument is a block argument just as much as any other. BlockArgument
provides no useful information beyond Argument. And it is nearly
always a mistake to care about whether a value is a function argument
and not care whether it is a phi or terminator result.
2023-09-27 18:47:46 -07:00
Kuba Mracek
6a0bfa267d [embedded] Handle thick->thin metatype conversion on IsPOD builtin too 2023-09-20 15:49:13 -07:00
Kuba Mracek
51792d8a31 [embedded] Simplify optimizeArrayBuiltin, avoid needing a single use of the metatype inst 2023-09-15 10:40:19 -07:00
Kuba Mracek
350107e153 [embedded] Also cover DestroyArray builtin in embedded Swift mode 2023-09-15 06:56:11 -07:00
Kuba Mracek
50f6430a27 [embedded] Convert thick->thin metatypes on array builtins (copy, take, assign) in mandatory optimizations 2023-09-14 18:09:08 -07:00
Erik Eckstein
d9826f6043 SimplifyBranch: don't leave an empty block after merging two basic blocks
Although empty blocks are cleaned up at the end of every Simplification pass, it's not legal SIL to have empty blocks and subsequent simplification passes may crash because of this.

rdar://115169880
2023-09-11 14:11:46 +02:00
Erik Eckstein
89d3326ff4 Swift Optimizer: add a simplification for pointer_to_address
For a redundant pair of pointer-address conversions, e.g.

   %2 = address_to_pointer %1
   %3 = pointer_to_address %2 [strict]

replace all uses of %3 with %1.
2023-08-10 20:49:20 +02:00
Kuba (Brecka) Mracek
8510bdd7f0 Merge pull request #67587 from kubamracek/move-target-const-folding-to-swift-simplification
Move target const folding to swift simplification
2023-08-01 15:19:26 -07:00
Kuba Mracek
5dac59ce71 Move TargetConstantFolding pass to the simplification passes in Swift, enable using MemoryLayout's .size, .stride, .alignment fields in forced-const global initializers 2023-07-31 10:54:07 -07:00
Nate Chandler
e5d87f75a8 [SIL] Add source formal type to checked_cast_br.
It is necessary for opaque values where for casts that will newly start
out as checked_cast_brs and be lowered to checked_cast_addr_brs, since
the latter has the source formal type, IRGen relies on being able to
access it, and there's no way in general to obtain the source formal
type from the source lowered type.
2023-07-27 15:04:15 -07:00
Erik Eckstein
393711d5f4 SimplifyBuiltin: ignore debug_step and debug_value when checking for side-effects in the callee of a builtin.once 2023-07-26 11:06:50 +02:00
Kuba (Brecka) Mracek
e34c20fdf5 Merge pull request #67243 from kubamracek/section-ump
Allow forced constant-folding of global initializers to work on UnsafeMutablePointer types
2023-07-13 20:07:05 -07:00
Kuba Mracek
c09710bc9a Enhance enum simplifications in SILOptimizer to allow forced constant-folding of global initializers to work on UnsafeMutablePointer types 2023-07-13 08:31:53 -07:00
Erik Eckstein
efcd90af7d Swift SIL: rename ownership enums and properties in LoadInst and StoreInst
`ownership` is a bad name in `LoadInst`, because it hides `Value.ownership`.
Therefore rename it to `loadOwnership`.
Do the same for ownership in StoreInst to be consistent.
2023-07-11 22:33:02 +02:00
Erik Eckstein
9474b9d1f0 Optimizer: add simplifications for checked_cast_br and unchecked_ref_cast
Look through `upcast` and `init_existential_ref` instructions and replace the operand of this cast instruction with the original value.
For example:
```
  %2 = upcast %1 : $Derived to $Base
  %3 = init_existential_ref %2 : $Base : $Base, $AnyObject
  checked_cast_br %3 : $AnyObject to Derived, bb1, bb2
```

This makes it more likely that the cast can be constant folded because the source operand's type is more accurate.
In the example above, the cast reduces to
```
  checked_cast_br %1 : $Derived to Derived, bb1, bb2
```
which can be trivially folded to always-succeeds.

Found while looking at `_SwiftDeferredNSDictionary.bridgeValues()`
2023-06-20 16:45:58 +02:00
Erik Eckstein
50c23a1640 Optimizer: implement the SILCombine peephole optimizations for retain_value and release_value in Swift 2023-06-07 14:18:38 +02:00
Erik Eckstein
3747a2830e Optimizer: add simplification for init_enum_data_addr
Optimize the sequence
```
  %1 = init_enum_data_addr %enum_addr, #someCaseWithPayload
  store %payload to %1
  inject_enum_addr %enum_addr, #someCaseWithPayload
```
to
```
  %1 = enum  $E, #someCaseWithPayload, %payload
  store %1 to %enum_addr
```
This sequence of three instructions must appear in consecutive order.
But usually this is the case, because it's generated this way by SILGen.
2023-05-25 16:28:41 +02:00
Erik Eckstein
5c0ae940eb Optimizer: add simplification for the value_to_bridge_object instruction
This comes up in the code for constructing an empty string literal.
With this optimization it's possible to statically initialize empty string global variables.
2023-05-25 16:28:41 +02:00
Erik Eckstein
f3851f7503 Optimizer: implement load simplification in Swift 2023-05-22 15:34:26 +02:00
Erik Eckstein
38de5b1ab5 Swift SIL/Optimizer: implement cloning of static init values of globals in Swift
* add the StaticInitCloner utility
* remove bridging of `copyStaticInitializer` and `createStaticInitializer`
* add `Context.mangleOutlinedVariable` and `Context.createGlobalVariable`
2023-05-22 15:34:26 +02:00
Erik Eckstein
1355e94eca Simplification: simplify builtin "canBeClass" and builtin "assert_configuration" 2023-05-11 08:11:44 +02:00
Erik Eckstein
82734b6ac2 Swift Optimizer: simplification for apply, try_apply, begin_apply and partial_apply
* move the apply of partial_apply transformation from simplify-apply to simplify-partial_apply
* delete dead partial_apply instructions
* devirtualize apply, try_apply and begin_apply
2023-05-11 08:11:44 +02:00
Erik Eckstein
88a4a9769e Swift Optimizer: add simplification for load
Replace loads of global let variables with there static initializer values.
2023-05-08 21:23:36 +02:00
Erik Eckstein
c4096bc723 Swift Optimizer: simplify builtin "once"
If the callee is side effect-free we can remove the whole builtin "once".
2023-05-08 21:23:36 +02:00
Erik Eckstein
9b51e69dac Swift Optimizer: constant fold builtins in the simplification passes 2023-05-08 21:23:36 +02:00
Erik Eckstein
88973a3cd5 Swift Optimizer: add simplification for tuple_extract
Replace tuple_extract(tuple(x)) -> x
2023-05-08 21:23:36 +02:00
Erik Eckstein
5a3ab6ee2e Swift Optimizer: add simplifications for destructure_struct and destructure_tuple
Eliminate the redundant instruction pair
```
  %t = tuple (%0, %1, %2)
  (%3, %4, %5) = destructure_tuple %t
```
and replace the results %3, %4, %5 with %0, %1, %2, respectively.
The same for structs.
2023-05-08 21:23:36 +02:00
Erik Eckstein
0a05124023 Swift Optimizer: add simplification of debug_step
When compiling with optimizations, unconditionally remove `debug_step`
2023-05-08 21:23:36 +02:00
Erik Eckstein
a8c9aae1e0 Swift Optimizer: add simplification for cond_fail 2023-05-08 21:23:36 +02:00
Erik Eckstein
318c30895f Swift Optimizer: eliminate ARC operations on types which are marked as immortal
A type (mostly classes) can be attributed with `@_semantics("arc.immortal")`.
ARC operations on values of such types are eliminated.

This is useful for the bridged SIL objects in the swift compiler sources.
2023-03-14 21:07:03 +01:00
Erik Eckstein
f1095556c9 Swift SIL: let var UnaryInstruction.operand return an Operand and not a Value
To avoid confusion. Instead add specific getters for unary instructions with dedicated names.

NFC
2023-02-21 17:57:29 +01:00
Erik Eckstein
40ed0fb388 Swift Optimizer: fix a crash when simplifying same-metatype comparisons of function types
The instance type of a metatype instruction is not necessarily a legal lowered SIL Type.
Lower the type before converting it to a SILType.

rdar://105502403
2023-02-15 21:14:32 +01:00
Erik Eckstein
85210a4e91 Swift Optimizer: make some SILCombine optimizations available for the "Simplification" pass
* begin_cow_mutation
* global_value
* strong_retain and strong_release

So far, those simplifications did only run in SILCombine. Now they are also considered in the swift Simplification pass.
2023-02-09 06:50:05 +01:00
Erik Eckstein
1c70060e01 Swift Optimizer: add Onone simplification of struct_extract instructions 2023-02-09 06:50:05 +01:00
Erik Eckstein
20edeb6a8c Swift Optimizer: add Onone simplification of unchecked_enum_data instructions 2023-02-09 06:50:05 +01:00
Erik Eckstein
4cf62696b6 Swift Optimizer: add Onone simplification of cond_br instructions 2023-02-09 06:50:05 +01:00
Erik Eckstein
d56ed65718 Swift Optimizer: add Onone simplification of some builtin instructions
* `Builtin.isConcrete`
* `Builtin.is_same_metatype`
2023-02-09 06:50:05 +01:00