Commit Graph

387 Commits

Author SHA1 Message Date
Erik Eckstein
36c8229562 StackPromotion: fix a problem with promoted allocations in dead-end regions
Allocations in dead-end regions cannot be promoted unconditionally, because such an object could escape to another thread.

rdar://111570874
2023-07-02 18:58:02 +02:00
Erik Eckstein
e77e2bcff7 IRGen: don't initialize the object headers of bare objects
For `alloc_ref [bare] [stack]` and `global_value [bare]` omit the object header initialization.
The `bare` flag means that the object header is not used.

This was already done with a peephole optimization inside IRGen for `global_value`. But now rely on the SIL `bare` flag.
2023-06-29 06:57:05 +02:00
Erik Eckstein
55c8c433c0 SILOptimizer: add the StripObjectHeader optimization pass
It sets the `[bare]` attribute for `alloc_ref` and `global_value` instructions if their header (reference count and metatype) is not used throughout the lifetime of the object.
2023-06-29 06:57:05 +02:00
Erik Eckstein
625619ee17 SIL: add a bare attribute to global_value
The `bare` attribute indicates that the object header is not used throughout the lifetime of the value.
This means, no reference counting operations are performed on the object and its metadata is not used.
The header of bare objects doesn't need to be initialized.
2023-06-29 06:57:05 +02:00
Erik Eckstein
b08710d911 SIL: add a bare attribute to alloc_ref
The `bare` attribute indicates that the object header is not used throughout the lifetime of the object.
This means, no reference counting operations are performed on the object and its metadata is not used.
The header of bare objects doesn't need to be initialized.
2023-06-29 06:57:05 +02:00
Erik Eckstein
d0fb49e338 StackPromotion: support promoting allocations in dead-end control flow regions.
rdar://109274869
2023-06-28 20:46:40 +02:00
swift-ci
bbccbd32ba Merge pull request #66179 from Mayank-84/Refactor/Stacks
[Optimizer] Refactored the `Stack.swift` file to make it more readable and concise. 🚀
2023-06-22 12:02:23 -07: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
86f2ced581 StackProtection: treat source-operands of memcpy and memmove intrinsics as read-only
This makes `UnsafeMutableRawPointer.storeBytes` not triggering stack protection

rdar://110738333
2023-06-19 13:56:44 +02:00
Erik Eckstein
fccecbe15e MandatoryPerformanceOptimizations: remove dead metatype instructions after specialization
Generic specialization drops metatype arguments and therefore exposes opportunities to remove dead metatype instructions.
Instead of removing dead metatype instructions before specialization, try to remove them after specialization.
2023-06-15 21:42:01 +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
3ac9fc65d7 InitializeStaticGlobals: remove dead instructions in global initializer
After removing the store it's required to remove the remaining dead instructions to avoid ownership verifier errors.

rdar://109999674
2023-05-31 14:22:48 +02:00
Mayank
58ad356780 Refactored the Stack.swift file to make it more readable.
Instead of using `if` in case of checking if `index < end` in `next` function of Stack. We can use `guard` statement to make it more readable and concise.
2023-05-26 22:08:25 +05:30
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
d02765fd54 Swift SIL: some new APIs and some refactoring 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
4284dc10d0 Optimizer: implement the ObjectOutliner pass in Swift 2023-05-22 15:34:26 +02:00
Erik Eckstein
f3851f7503 Optimizer: implement load simplification in Swift 2023-05-22 15:34:26 +02:00
Erik Eckstein
61857f379b Swift Optimizer: add the ValueWorklist typealias 2023-05-22 15:34:26 +02:00
Erik Eckstein
585395b67c Swift Optimizer: add Context.lookupStdlibFunction 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
b707b5a595 Swift SIL: improve the Builder
* add new create-functions for instructions
* allow the Builder to build static initializer instructions for global variables
* some refactoring to simplify the implementation
2023-05-22 15:34:26 +02:00
Erik Eckstein
a4225a9088 Swift SIL: add a few SIL instructions and instruction APIs 2023-05-22 15:34:26 +02:00
Erik Eckstein
7bbf66ab5b ReadOnlyGlobalVariables: don't convert external global vars to lets
Because we don't know if external variables will be written

Fixes a miscompile
rdar://109476745
2023-05-19 18:28:22 +02:00
Erik Eckstein
dc3cb18029 Swift Optimizer: add the MandatoryPerformanceOptimizations pass
As a replacement for the old MandatoryGenericSpecializer

The pass it not enabled yet in the pass pipeline
2023-05-11 08:11:44 +02:00
Erik Eckstein
1355e94eca Simplification: simplify builtin "canBeClass" and builtin "assert_configuration" 2023-05-11 08:11:44 +02:00
Erik Eckstein
b9169064c6 Swift SIL: add some APIs
* `Options.assertConfiguration`
* `Argument.isIndirectResult`
* in `Function`: `selfArgument`, `isTransparent`, `performanceConstraints` and `inlineStrategy`
* `BuiltinInst.substitutionMap`
* `SubstitutionMap.replacementTypes`
* `Type.canBeClass`
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
6eff928989 Swift Optimizer: add a message to an assert 2023-05-11 08:11:44 +02:00
Erik Eckstein
92a17f8a01 Optimizer: extract the NamedReturnValueOptimization from CopyForwarding to a separate function pass
This allows to run the NamedReturnValueOptimization only late in the pipeline.
The optimization shouldn't be done before serialization, because it might prevent predictable memory optimizations in the caller after inlining.
2023-05-11 08:11:44 +02:00
Erik Eckstein
08e75f2c50 Swift SIL: add the Operand.set API
as a shortcut for `Instruction.setOperand`
2023-05-11 08:03:19 +02:00
Erik Eckstein
3d555a412a refactor two swift passes
NFC
2023-05-10 16:04:57 +02:00
Erik Eckstein
fa1ecff143 Swift Optimizer: rewrite the MemBehaviorDumper test pass in swift. 2023-05-09 08:25:09 +02:00
Erik Eckstein
960ca70def Swift Optimizer: add the module pass ReadOnlyGlobalVariables
It marks global `var` variables as `let` if they are never written.
2023-05-08 21:23:36 +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
6d6b94e430 Swift Optimizer: add the InitializeStaticGlobals function pass
It converts a lazily initialized global to a statically initialized global variable.

When this pass runs on a global initializer `[global_init_once_fn]` it tries to create a static initializer for the initialized global.
```
  sil [global_init_once_fn] @globalinit {
    alloc_global @the_global
    %a = global_addr @the_global
    %i = some_const_initializer_insts
    store %i to %a
  }
```

The pass creates a static initializer for the global:
```
  sil_global @the_global = {
    %initval = some_const_initializer_insts
  }
```

and removes the allocation and store instructions from the initializer function:
```
  sil [global_init_once_fn] @globalinit {
    %a = global_addr @the_global
    %i = some_const_initializer_insts
  }
```

The initializer then becomes a side-effect free function which let's the builtin-simplification remove the `builtin "once"` which calls the initializer.
2023-05-08 21:23:36 +02:00
Erik Eckstein
2b117fd3ee Swift Optimizer: add APIs to copy from or to a global static initializer
* `Context.copyStaticInitializer(fromInitValue:, to:)`
* `FunctionPassContext.createStaticInitializer(for:,initValue:)`
2023-05-08 21:23:36 +02:00
Erik Eckstein
78ce13dbd6 WalkUtils: Don't treat end_access as leaf-use in the AddressDefUseWalker
Instead just ignore it.
2023-05-08 21:23:36 +02:00
Erik Eckstein
3e04b8681c make ModulePassContext conform to CustomStringConvertible
and let its description return a dump of the whole module.
This is useful for debugging
2023-05-08 21:23:36 +02:00
Erik Eckstein
e95c6425f2 Swift SIL: add some APIs for global variables 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
8a8a895239 alias analysis: compute more precise memory effects of builtin "once"
* Check if the address in question is even visible from outside the function
* Return the memory effects of the called function

Also, add a new API `Instruction.memoryEffects`, which is internally used by `mayReadFromMemory` et al.
2023-05-08 21:23:36 +02:00
eeckstein
3bad9069a6 Merge pull request #65622 from eeckstein/improve-escape-utils
EscapeUtils: better handling of noescape closures and convert_function instructions
2023-05-04 07:54:23 +02:00
Erik Eckstein
5992e68d23 EscapeUtils: better handling of noescape closures and convert_function instructions
* Assume that a noescape closure argument cannot escape a called function
* Handle convert_function instructions

rdar://108837480
2023-05-03 19:53:19 +02:00
Erik Eckstein
da3f126322 CalleeAnalysis: no need to provide the PassManager to the getMemBehaviorFn
A NFC simplification
2023-05-03 14:33:45 +02:00