Commit Graph

248 Commits

Author SHA1 Message Date
Andrew Trick
8a71844456 Cleanup SIL ComputeSideEffects for partial_apply arguments 2023-08-29 16:58:56 -07:00
Andrew Trick
4ce6fcf18f Fix SIL function side effects to handle unapplied escaping
Fixes rdar://113339972 DeadStoreElimination causes uninitialized closure context

Before this fix, the recently enabled function side effect implementation
would return no side effects for a partial apply that is not applied
in the same function. This resulted in DeadStoreElimination
incorrectly eliminating the initialization of the closure context.

The fix is to model the effects of capturing the arguments for the
closure context. The effects of running the closure body will be
considered later, at the point that the closure is
applied. Running the closure does, however, depend on the captured
values to be valid. If the value being captured is addressible,
then we need to model the effect of reading from that memory. In
this case, the capture reads from a local stack slot:

    %stack = alloc_stack $Klass
    store %ref to %stack : $*Klass
    %closure = partial_apply [callee_guaranteed] %f(%stack)
      : $@convention(thin) (@in_guaranteed Klass) -> ()

Later, when the closure is applied, we won't have any reference back
to the original stack slot. The application may not even happen in a caller.

Note that, even if the closure will be applied in the current
function, the side effects of the application are insufficient to
cover the side effects of the capture. For example, the closure
body itself may not read from an argument, but the context must
still be valid in case it is copied or if the capture itself was
not a bitwise-move.

As an optimization, we ignore the effect of captures for on-stack
partial applies. Such captures are always either a bitwise-move
or, more commonly, capture the source value by address. In these
cases, the side effects of applying the closure are sufficient to
cover the effects of the captures. And, if an on-stack closure is
not invoked in the current function (or passed to a callee) then
it will never be invoked, so the captures never have effects.
2023-08-16 11:58:11 -07:00
Erik Eckstein
afc0f617e0 Optimizer: support statically initialized globals which contain pointers to other globals
For example:
```
  var p = Point(x: 10, y: 20)
  let o = UnsafePointer(&p)
```

Also support outlined arrays with pointers to other globals. For example:
```
var g1 = 1
var g2 = 2

func f() -> [UnsafePointer<Int>] {
  return [UnsafePointer(&g1), UnsafePointer(&g2)]
}
```
2023-08-10 20:50:36 +02:00
Erik Eckstein
799bd0a5f5 Swift Optimizer: some reformatting in the optimization passes
Use tail closures for all optimization passes.
NFC.
2023-08-04 10:33:52 +02:00
Erik Eckstein
c567167b66 InitializeStaticGlobals: add a peephole to merge element stores to a single store.
Sometimes structs are not stored in one piece, but as individual elements. Merge such individual stores to a single store of the whole struct.
This enables generating a statically initialized global.
2023-07-26 11:06:50 +02:00
Erik Eckstein
4d20423e00 Optimizer: re-implement the RedundantLoadElimination pass in Swift
The new implementation has several benefits compared to the old C++ implementation:

* It is significantly simpler. It optimizes each load separately instead of all at once with bit-field based dataflow.
* It's using alias analysis more accurately which enables more loads to be optimized
* It avoids inserting additional copies in OSSA

The algorithm is a data flow analysis which starts at the original load and searches for preceding stores or loads by following the control flow in backward direction.
The preceding stores and loads provide the "available values" with which the original load can be replaced.
2023-07-21 07:19:56 +02:00
Erik Eckstein
86771468fc Swift Optimizer: move StoreInst.split into OpUtils.swift
To make it available in other optimizations as well.
Also, a few problems:
* Use destructre instructions when in OSSA
* Don't split the store if it's nominal type has unreferenceable stoarge
* rename it to `trySplit` because it's not guaranteed to work

Also, add the counterpart for load instructions: `LoadInst.trySplit()`
2023-07-21 07:19:56 +02:00
Erik Eckstein
89b0de3563 StackPromotion: fix a crash due to a problem in liferange evaluation
The analysis to check if an alloc_ref outlives it's "inner" liferange had a bug which resulted in a crash in the StackPromotion pass

rdar://112275272
2023-07-17 14:55:55 +02:00
Erik Eckstein
9cb83c33eb DeadStoreElimination: some refactoring and improvements
Addresses review feedback of https://github.com/apple/swift/pull/67122
2023-07-11 22:33:03 +02: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
Kuba (Brecka) Mracek
2961cafb05 Merge pull request #66844 from kubamracek/static-init-structs
Allow using structs with trivial initializers in globals that require static initialization (e.g. @_section attribute)
2023-07-10 15:11:55 -07:00
Kuba Mracek
145f12f6a3 Allow using structs with trivial initializers in globals that require static initialization (e.g. @_section attribute)
Before this change, if a global variable is required to be statically initialized (e.g. due to @_section attribute), we don't allow its type to be a struct, only a scalar type works. This change improves on that by teaching MandatoryPerformanceOptimizations pass to inline struct initializer calls into initializer of globals, as long as they are simple enough so that we can be sure that we don't trigger recursive/infinite inlining.
2023-07-08 19:26:59 -07:00
Erik Eckstein
baaf5565b0 Optimizer: reimplement DeadStoreElimination in swift
The old C++ pass didn't catch a few cases.
Also:
* The new pass is significantly simpler: it doesn't perform dataflow for _all_ memory locations at once using bitfields, but handles each store separately. (In both implementations there is a complexity limit in place to avoid quadratic complexity)
* The new pass works with OSSA
2023-07-05 21:33:25 +02:00
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
d0fb49e338 StackPromotion: support promoting allocations in dead-end control flow regions.
rdar://109274869
2023-06-28 20:46:40 +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
Erik Eckstein
4284dc10d0 Optimizer: implement the ObjectOutliner pass 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
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
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
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
3d555a412a refactor two swift passes
NFC
2023-05-10 16:04:57 +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
Nate Chandler
fc13686403 [ObjCBridgingOpt] Look through copy_value insts. 2023-03-29 11:39:43 -07: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
490c8fa1c2 ComputeEscapeEffects: some refactoring
Mostly source-code restructuring to make the source easier to read.
NFC
2023-02-15 18:42:38 +01:00
Erik Eckstein
8984046972 Effects: remove the isExclusive flag from the escapingToArgument effect
An argument-to-argument escape always involves a store, which makes an exclusive escape impossible.
2023-02-15 18:17:32 +01:00
Erik Eckstein
7a3ae09cfc Effects: add some comments and add enum argument labels
For clarity
2023-02-15 18:17:32 +01:00
Erik Eckstein
113e23df03 ComputeEscapeEffects: correctly handle "exclusive" argument -> return effects
* Disallow stores in the return -> argument path. When walking up in the EscapeUtils, it's allowed to follow stores. Therefore stores wouldn't be handled correctly.
* Also make sure that there is a return -> argument path at all

Fixes a wrong address-escaping effect in case the called function copies an indirect argument to a newly created object.
rdar://105133434
2023-02-13 16:10:17 +01:00
Erik Eckstein
caea41a4e0 ComputeEscapeEffects: don't support exclusive argument -> argument effects.
Exclusive argument -> argument effects cannot appear because such an effect would involve a store which is not permitted for exclusive escapes.
2023-02-13 16:10:17 +01:00
Erik Eckstein
dfde580872 Effects: bail if effects are requested for not supported projection paths
Effects are only defined for operations which don't involve a load.
 In case the argument's path involves a load we need to return the global effects.
2023-02-11 08:55:20 +01:00
Erik Eckstein
cc60815bfe ComputeSideEffects: fix wrong side effect computation of releases/destroys
Only global side effects of the destructor were considered, but side effects weren't attributed to the released value.

rdar://105237110
2023-02-11 08:55:20 +01:00
Erik Eckstein
7eb2cb82e4 Swift Optimizer: add a pass to cleanup debug_step instructions
If a `debug_step` has the same debug location as a previous or succeeding instruction it is removed.
It's just important that there is at least one instruction for a certain debug location so that single stepping on that location will work.
2023-02-09 06:50:05 +01:00
Erik Eckstein
67ed6cfff8 Swift Optimizer: add Simplification passes
Those passes are a framework for instruction simplifications (which are not yet included in this commit).
Comparable to SILCombine
2023-02-09 06:49:58 +01:00
Erik Eckstein
393d1a1488 SIL Builder: rename insert(at:) -> insert(before:)
It matches with `insert(after:)` and the intent should be clearer now
2023-01-16 15:11:34 +01:00
Erik Eckstein
cc68bd98c9 Swift Optimizer: rework pass context types and instruction passes
* split the `PassContext` into multiple protocols and structs: `Context`, `MutatingContext`, `FunctionPassContext` and `SimplifyContext`
* change how instruction passes work: implement the `simplify` function in conformance to `SILCombineSimplifyable`
* add a mechanism to add a callback for inserted instructions
2023-01-16 15:11:34 +01:00
Erik Eckstein
c32d6cd0fb EscapeUitls: some refactoring 2023-01-16 15:11:34 +01:00
Erik Eckstein
eb0d6ed87e StackPromotion: some refactoring
NFC
2023-01-16 15:11:34 +01:00
Erik Eckstein
6c35258f83 Swift SIL: rename parent accessors to parentX, e.g. Instruction.parentBlock
It makes it easier to read
2023-01-16 15:11:34 +01:00
Erik Eckstein
1f0f9d65b9 ComputeSideEffects: ignore side effects of debug_value with address operands
We already ignored side effects of debug_value with non-address operands. Now also do this for address operands.
2023-01-05 09:56:11 +01:00
Erik Eckstein
e26affacbd ComputeSideEffects: correct side effects for destroy_addr
A destroy_addr also involves a read from the address. It's equivalent to a `%x = load [take]` and `destroy_value %x`.
It's also a write, because the stored value is not available anymore after the destroy.

Fixes a compiler crash in SILMem2Reg.

rdar://103879105
2023-01-05 09:56:11 +01:00
Anton Korobeynikov
dd6f468d09 Ensure that partial_apply of partial_apply does not produce conservative global side effects. (#62351)
Fixes #62249
2022-12-02 06:14:46 -08:00
Erik Eckstein
82107c5bf2 ComputeSideEffects: consider effects in dead-end blocks
It was a wrong assumption that we can ignore effects in dead-end blocks.
2022-11-09 08:06:19 +01:00
Erik Eckstein
ab2fe452f9 ComputeSideEffects: checking for unknown argument uses need to consider type which contain raw pointers 2022-11-09 08:06:19 +01:00
Erik Eckstein
dbb4d4db32 ComputeSideEffects: fix a typo in a comment 2022-11-09 08:06:19 +01:00
Erik Eckstein
b275d987b5 ComputeSideEffects: handle reference count reading instructions.
Conservatively model those effects as "destroy" effects.
2022-10-20 09:20:28 +02:00
Erik Eckstein
8961e2982e swift side effects: some additions and refactoring
add `Function.getSideEffects(forArgument:,atIndex:,withConvention:)`
2022-10-20 09:20:28 +02:00