Commit Graph

55 Commits

Author SHA1 Message Date
Erik Eckstein
ca7facde35 SIL: add some use-list APIs
* `users`: maps from a sequence of operands to a sequence of instructions
* `users(ofType:)` : as `users`, but filters users of a certain instruction type
* `Value.users`: = `Value.uses.users`
2024-12-19 20:53:45 +01:00
Erik Eckstein
5be781a9a0 Optimizer: add a new destroy-hoisting optimization
It hoists `destroy_value` instructions  without shrinking an object's lifetime.
This is done if it can be proved that another copy of a value (either in an SSA value or in memory) keeps the referenced object(s) alive until the original position of the `destroy_value`.
```
  %1 = copy_value %0
  ...
  last_use_of %0
  // other instructions
  destroy_value %0       // %1 is still alive here
```
->
```
  %1 = copy_value %0
  ...
  last_use_of %0
  destroy_value %0
  // other instructions
```

The benefit of this optimization is that it can enable copy-propagation by moving destroys above deinit barries and access scopes.
2024-12-10 16:28:11 +01:00
Erik Eckstein
2519c343e6 Optimizer: fix simplification of unchecked_enum_data
When replacing an `enum` - `unchecked_enum_data` pair and the enum's operand is another non-trivial enum which is constructed with a trivial payload, and this happens in different basic blocks, we need to insert a compensating `destroy_value`.

Fixes a verifier crash
rdar://139787167
2024-11-14 07:21:10 +01:00
Andrew Trick
c5ab1287f3 Preserve extend_lifetime during dead instruction code elimination. 2024-11-12 23:53:56 -08:00
Erik Eckstein
1c5e185e89 Simplification: don't delete trivially dead borrowed-from instruction
A dead borrowed-from can only be removed if the argument (= operand) is also removed.

Fixes a compiler crash.
2024-11-11 14:06:16 +01:00
Arnold Schwaighofer
9093e1b0d2 Add comments to SwiftCompilerSources for shouldExpand() 2024-11-05 09:05:24 -08: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
709dfc2d21 MandatoryPerformanceOptimization: don't let not-inlinable functions to be inlined
Also refactor canInline.

Fixes a compiler crash.
rdar://137544788
2024-10-15 12:19:50 +02: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
Alexander Cyon
c18a24e499 [SwiftCompilerSources] Fix typos 2024-08-08 22:22:39 -07:00
Erik Eckstein
def3b6fab0 AccessUtils: recognize addressor calls of globals as a "global" access base
Makes e.g. alias analysis more precise in the early stage of the pass pipeline where addressor calls are not inlined, yet.
2024-08-05 17:12:46 +02:00
Andrew Trick
103c59b273 [SwiftCompilerSources] Add assert to Builder.init(after:)
This API is incorrect for terminators. Assert that it isn't used
incorrectly. Later, we should rename it.
2024-07-29 23:44:02 -07:00
Erik Eckstein
c61733f985 SwiftCompilerSources: refactor Function.mayBindDynamicSelf
Instead of bridging the whole function, just bridge `hasDynamicSelfMetadata` and do the other work in swift.
2024-06-25 17:59:23 +02:00
Ellie Shin
c3ded85c4f Update SILBridgedFunction APIs.
Add isAnySerialized() and canBeInlinedIntoCaller(SerializedKind).
2024-05-29 15:54:36 -07:00
Erik Eckstein
941a7427d8 handle upcast instructions in statically initialized globals
This allows statically initialized multi-dimensional arrays in embedded swift.
2024-05-21 13:33:06 +02:00
Erik Eckstein
af68435d90 Optimizer: support static initialization of global arrays
The buffer of global arrays could already be statically initialized.
The missing piece was the array itself, which is basically a reference to the array buffer.
For example:
```
var a = [1, 2, 3]
```
ends up in two statically initialized globals:
1. the array buffer, which contains the elements
2. the variable `a` which is a single reference (= pointer) of the array buffer

This optimization removes the need for lazy initialization of such variables.

rdar://127757554
2024-05-16 21:34:36 +02:00
Kshitij Jain
c37d31cc64 Merge pull request #73596 from jkshtj/specialize
[Autodiff] Adds logic to generate specialized functions in the closure-spec pass
2024-05-14 18:55:23 -07:00
Kshitij
ab751d57ab [Autodiff] Adds logic to generate specialized functions in the closure-spec pass 2024-05-13 11:16:42 -07:00
Nate Chandler
87e4c65e28 [Gardening] SIL: "liferange" -> "liverange" 2024-05-10 15:54:07 -07:00
Kshitij
fd609846ae Makes the swift-based closure-spec pass an experimental frontend feature 2024-05-02 09:14:05 -07:00
Kshitij
003c9082cb [Autodiff] Adds SwiftCompilerSources changes in preparation for the Autodiff closure-spec optimization pass 2024-05-02 09:14:05 -07:00
eeckstein
e871ae40c5 Merge pull request #71176 from eeckstein/borrowed-from-instruction
SIL: add the borrowed-from instruction
2024-04-10 19:33:12 +02:00
Erik Eckstein
e14c1d1f62 SIL, Optimizer: update and handle borrowed-from instructions
Compute, update and handle borrowed-from instruction in various utilities and passes.
Also, used borrowed-from to simplify `gatherBorrowIntroducers` and `gatherEnclosingValues`.
Replace those utilities by `Value.getBorrowIntroducers` and `Value.getEnclosingValues`, which return a lazily computed Sequence of borrowed/enclosing values.
2024-04-10 13:38:10 +02:00
Joe Groff
ec1023466f Optimizer/OptUtils: Don't trivially remove dead-looking unchecked_enum_data.
Under OSSA, the instruction may still be structurally responsible for consuming
its operand even if the result is dead, so we can't remove it without breaking
invariants.

More generally, this should probably apply to any instruction which consumes
one or more of its operands, has no side effects, and doesn't produce any
nontrivial results that require further consumption to keep the value alive.
I went with this targeted fix, since it addresses a problem that shows up
in practice (rdar://125381446) and the more general change appears to
disturb the optimizer pipeline while building the standard library.
2024-04-09 14:52:03 -07:00
Erik Eckstein
308b5f9ff7 Swift Optimizer: add bridging for dynamic cast utilities
* `func canDynamicallyCast`
* `var CheckedCastAddrBranchInst.dynamicCastResult`
2024-03-19 10:54:38 +01:00
Erik Eckstein
8137adf89e SwiftCompilerSources: support Undef.parentFunction and PlaceholderValue.parentFunction
After support in the C++ SIL, we can implement parentFunction for those values in swift, too.
It simplifies a few things.
2024-03-01 09:07:12 +01:00
Andrew Trick
b08f2dedf1 Add AccessBase.findSingleInitializer 2024-02-12 09:57:14 -08:00
Meghana Gupta
5d2454f3e3 Merge pull request #68180 from meg-gupta/lowerb4inline
Lower OwnershipModelEliminator to just before inlining
2024-01-10 13:53:34 -08:00
Erik Eckstein
3229749257 SwiftCompilerSources: move some private utilities of passes into OptUtils
* `var Function.initializedGlobal`
* `func getGlobalInitialization`

and add `var CollectionLikeSequence.singleElement`
2024-01-10 09:34:01 +01:00
Meghana Gupta
9dbf7ccec2 Address review comments from #68150 2024-01-05 13:20:52 -08:00
Andrew Trick
c1f0f06907 [SIL] Add FunctionConvention.swift
Bridge information about the AST function type that does not require a
SIL function body.

Support querying the function type's ResultInfo.
2023-12-18 09:43:18 -08:00
Erik Eckstein
ded20d3c11 SwiftCompilerSources: add Value.isValidGlobalInitValue and FullApplySite.isSemanticCall
`Value.isValidGlobalInitValue` was privately used by the ObjectOutliner. Make it a generally usable utility.
2023-12-09 18:49:58 +01:00
Erik Eckstein
ec4c815506 SwiftCompilerSources: move WalkUtils and AccessUtils from the Optimizer module to the SIL module
Those utilities are conceptually part of the SIL definition.
2023-12-01 19:20:13 +01:00
Erik Eckstein
9eb5da4a0f SwiftCompilerSources: add the InstructionRange.insert(borrowScopeOf:) utility
Adds the instruction range of a borrow-scope by transitively visiting all (potential) re-borrows.
2023-11-27 16:20:47 +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
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
2c84de3f4f Optimizer: add a few "lookThrough" utilities
* move `Value.lookThoughOwnershipInstructions` from the ObjCBridgingOptimization pass to OptUtils
* add `lookThroughBorrow` and `lookThroughCopy`
2023-11-09 18:34:53 +01: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
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
8223b3e210 MandatoryPerformanceOptimizations: fix some problems with inilning
* don't inline functions if it's not possible - by checking `SILInliner::canInlineApplySite`
* fix stack nesting after inlining a `begin_apply`
2023-08-10 15:01:47 +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
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
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
e95c6425f2 Swift SIL: add some APIs for global variables 2023-05-08 21:23:36 +02:00
Erik Eckstein
2d88482c9f EscapeUtils: add a computational limit to avoid quadratic complexity in some corner cases.
The `isEscaping` function is called a lot from ARCSequenceOpt and ReleaseHoisting.
To avoid quadratic complexity for large functions, limit the amount of work what the EscapeUtils are allowed to to.
This keeps the complexity linear.

The arbitrary limit is good enough for almost all functions.
It lets the EscapeUtils do several hundred up/down walks which is much more than needed in most cases.

Fixes a compiler hang
https://github.com/apple/swift/issues/63846
rdar://105795976
2023-02-24 18:58:01 +01:00
Erik Eckstein
1c1d17e18b Swift Optimizer: add some small optimization utilities
* `Instruction.isTriviallyDead` and `Instruction.isTriviallyDeadIgnoringDebugUses`
* `Instruction.hasSameDebugLocationAsPreviousOrNextInstruction`
* `UseList.singleNonDebugUse`
* `UseList.isEmptyIgnoringDebugUses`
* `removeDeadBlocks`
* `FunctionPassContext.removeTriviallyDeadInstructionsPreservingDebugInfo` and `FunctionPassContext.removeTriviallyDeadInstructionsIgnoringDebugUses`
* `BasicBlock.dropAllReferences`
* `SimplifyContext.tryReplaceRedundantInstructionPair`
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
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