Commit Graph

20 Commits

Author SHA1 Message Date
Kavon Farvardin
32263bb9b8 ManualOwnership: add workaround for vars
For a proof-of-concept quality feature,
we can just skip the pass causing problems until
a proper solution is implemented.
2025-09-25 15:53:18 -07:00
Erik Eckstein
8f39c3cad8 Swift SIL: rename isTakeOfSrc -> isTakeOfSource and isInitializationOfDest -> isInitializationOfDestination
Trying to avoid abbreviations in ABI names
2025-05-06 12:35:21 +02:00
Erik Eckstein
8cf4e34cc1 RedundantLoadElimination: support replacing a redundant copy_addr with a store
For example:
```
  %0 = load %1
  copy_addr %1 to %2
```
->
```
  %0 = load %1
  store %0 to %2
```

This is important for MandatoryRedundantLoadElimination to be able to create statically initialized globals in the mandatory pipeline.
For example:
```
public struct MyStruct {
  public static let r: Range<Int> = 1 ..< 3
}

```
gets a statically initialized global, even at Onone, with this improvement.

rdar://149356742
2025-04-23 11:57:28 +02:00
Erik Eckstein
ce73deebc4 RedundantLoadElimination: add a "mandatory" redundant load elimination pass
And make `eliminateRedundantLoads` callable from other optimizations
2025-02-07 11:30:35 +01:00
Erik Eckstein
2af24703ab RedundantLoadElimination: insert mark_dependence instructions for a removed load
If the memory location depends on something, insert a dependency for the loaded value:
```
  %2 = mark_dependence %1 on %0
  %3 = load %2
```
->
```
  %2 = mark_dependence %1 on %0 // not needed anymore, can be removed eventually
  %3 = load %2
  %4 = mark_dependence %3 on %0
  // replace %3 with %4
```
2025-02-07 11:30:35 +01:00
Erik Eckstein
5acccf1802 RedundantLoadElimination: ignore memory effects of begin_access
Memory effects of begin_access are only defined to prevent the optimizer moving loads and stores across a begin_access.
But those memory effects are not relevant for RedundantLoadElimination
2025-02-07 11:30:35 +01:00
Erik Eckstein
ad2462f7f2 RedundantLoadElimination: optimize a load from a memory location which was written by copy_addr
For example:
```
  %1 = alloc_stack $B
  copy_addr %0 to [init] %1
  %3 = load [take] %1
  dealloc_stack %1
```
->
```
  %3 = load [copy] %0
```
2025-02-07 11:30:34 +01:00
Erik Eckstein
cd7c533d42 Optimizer: add SingleValueInstruction.replace(with:) and use it throughout the optimizer
It replaces all uses and then erases the instruction.
2024-12-19 20:53:45 +01:00
Erik Eckstein
cea7da8d26 RedundantLoadElimination: correctly handle end_borrow instructions
Scope-ending instructions, like `end_borrow` are only irrelevant for RLE if the preceding load is not changed.
If it is changed from `load [copy]` -> `load [take]` the memory effects of those scope-ending instructions prevent that the `load [take]` will illegally mutate memory which is protected from mutation by the scope.

Fixes a memory verifier crash
rdar://139824805
2024-11-14 18:25:11 +01:00
Arnold Schwaighofer
9093e1b0d2 Add comments to SwiftCompilerSources for shouldExpand() 2024-11-05 09:05:24 -08:00
Erik Eckstein
7786596a48 RedundantLoadElimination: support redundant loads of array elements with non-constant index
For example:
```
func test3(_ arr: Array<Int>, index: Int) -> Int {
  return arr[index] + arr[index]
}
```

rdar://138519664
2024-11-04 19:26:45 +01:00
Arnold Schwaighofer
787c996394 LargeTypesReg2Mem: Add a new heuristic that trys harder to keep large
values on the stack

This heuristic can be enabled by passing -Xfrontend
-enable-aggressive-reg2mem.

rdar://123916109
2024-10-31 13:22:06 -07:00
Erik Eckstein
10782cf42b SwiftCompilerSources: introduce the AST module
As the optimizer uses more and more AST stuff, it's now time to create an "AST" module.
Initially it defines following AST datastructures:
* declarations: `Decl` + derived classes
* `Conformance`
* `SubstitutionMap`
* `Type` and `CanonicalType`

Some of those were already defined in the SIL module and are now moved to the AST module.
This change also cleans up a few things:
* proper definition of `NominalTypeDecl`-related APIs in `SIL.Type`
* rename `ProtocolConformance` to `Conformance`
* use `AST.Type`/`AST.CanonicalType` instead of `BridgedASTType` in SIL and the Optimizer
2024-10-02 07:10:29 +02:00
Nate Chandler
71368be8d4 [Gardening] SIL: "Liferange" -> "Liverange" 2024-05-10 15:54:09 -07:00
Nate Chandler
87e4c65e28 [Gardening] SIL: "liferange" -> "liverange" 2024-05-10 15:54:07 -07:00
Michael Gottesman
11f0ff6e32 [sil] Ensure that all SILValues have a parent function by making it so that SILUndef is uniqued at the function instead of module level.
For years, optimizer engineers have been hitting a common bug caused by passes
assuming all SILValues have a parent function only to be surprised by SILUndef.
Generally we see SILUndef not that often so we see this come up later in
testing. This patch eliminates that problem by making SILUndef uniqued at the
function level instead of the module level. This ensures that it makes sense for
SILUndef to have a parent function, eliminating this possibility since we can
define an API to get its parent function.

rdar://123484595
2024-02-27 13:14:47 -08:00
Erik Eckstein
54d254f100 Optimizer: better handling of the complexity budget in redundant-load-elimination and dead-store-elimination.
Instead of having a budget for each optimized load or store, provide a budget for the whole function.
Fixes a build time problem.

rdar://116877696
2023-10-16 14:44:34 +02:00
Erik Eckstein
d457368014 SIL Optimizer: move some projection path utilities from RedundantLoadElimination and AccessUtils into OptUtils
NFC
2023-09-19 15:10:30 +02:00
Erik Eckstein
51cb7575ea RedundantLoadElimination: don't crash when loading a non-trivial enum which was stored as trivial non-payload case.
rdar://114648847
2023-08-31 09:16:17 +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