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
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
49e66c57b8
StackProtection: ignore pointers with no stores
...
Stack protection only protects against overflows, but not against out of bounds reads.
rdar://105231457
2023-02-15 08:20:48 +01:00
Erik Eckstein
7597641104
EscapeUtils: fix handling a raw pointer during up-walk
...
When walking up we shouldn't end up at a load where followLoads is false, because going from a (non-followLoads) address to a load always involves a class indirection.
There is one exception: loading a raw pointer
2023-02-13 16:16:06 +01:00
Erik Eckstein
d602836a26
EscapeUtils: consider that a begin_apply can yield it's indirect arguments
...
This is the only apply instruction where address arguments actually can "escape"
2023-02-13 16:16:05 +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
8daa54a8c5
SmallProjectionPath: replace hasNoClassProjection with mayHaveClassProjection
...
It's less confusing.
NFC
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
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
Erik Eckstein
3f35a1d869
Swift Optimizer: add Onone simplification of branch instructions
2023-02-09 06:50:05 +01:00
Erik Eckstein
f1c6ed681d
Swift Optimizer: add Onone simplification of apply instructions
2023-02-09 06:50:05 +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
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
Nate Chandler
6caa5048d0
[WalkUtils] Walk through move_value instructions.
...
Addresses the following regressions
StackPromo 10100 14400 +42.6% **0.70x**
seen when enabling lexical lifetimes in the standard library.
2023-01-25 11:39:42 -08:00
Erik Eckstein
b5e731dde2
WalkUtils: fix a crash when visiting mismatching types
...
The path components may not be related to the current value in case mismatching types are visited, e.g. different concrete types of an existential.
This can lead to mismatching operand numbers for struct and tuple instructions.
rdar://104435056
2023-01-20 15:06:48 +01:00
Erik Eckstein
3645becada
PassManager: infrastructure to disable or enable a specific instruction simplification
...
* for testing: add the option `-simplify-instruction=<instruction-name>` to only run simplification passes for that instruction type
* on the swift side, add `Options.enableSimplification`
2023-01-16 19:00:09 +01:00
Erik Eckstein
230c93df30
SIL Optimizer: add some SIL modification APIs
...
* `MutatingContext.notifyInvalidatedStackNesting` and `MutatingContext.needFixStackNesting`
* `MutatingContext.tryDeleteDeadClosure`
* `MutatingContext.erase(block:)`
* `Undef.get`
* `BasicBlock.moveAllInstructions`
* `BasicBlock.eraseAllArguments`
* `BasicBlock.moveAllArguments`
* `TermInst.replaceBranchTarget`
2023-01-16 19:00:09 +01:00
Erik Eckstein
ea746479ad
SIL Optimizer: rename directory InstructionPasses -> InstructionSimplification
2023-01-16 16:16:51 +01:00
Erik Eckstein
22f03cdde6
Swift Optimizer: generalize the worklist data structure
...
Rename `BasicBlockWorklist.swift` to `Worklist` and make it generic for any elements.
Also, add methods `popAndForget` and `isEmpty`
2023-01-16 16:16:51 +01:00
Erik Eckstein
f3717d5127
Swift Optimizer: add the ReachableBlocks utility
2023-01-16 16:16:51 +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
eeckstein
6b33768e25
Merge pull request #62864 from eeckstein/fix-compute-side-effects
...
ComputeSideEffects: correct side effects for destroy_addr
2023-01-05 20:35:35 +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
Erik Eckstein
3d86732bea
EscapeUtils: fix a wrong handling of argument indices in walkUpApplyResult
...
Fixes a crash
rdar://103526983
2023-01-04 11:52:19 +01:00
Erik Eckstein
beb46eb624
Use the new escape and side effects in alias analysis
2022-12-21 17:41:46 +01:00
Erik Eckstein
d4d1620f28
Swift SIL: rework Instruction and BasicBlock lists to support deleting instructions during iteration
...
Replace the generic `List` with the (non-generic) `InstructionList` and `BasicBlockList`.
The `InstructionList` is now a bit different than the `BasicBlockList` because it supports that instructions are deleted while iterating over the list.
Also add a test pass which tests instruction modification while iteration.
2022-12-12 19:08:57 +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
ef302ce4ac
SILOptimizer: enable stack protection by default
...
The pass to decide which functions should get stack protection was added in https://github.com/apple/swift/pull/60933 , but was disabled by default.
This PR enables stack protection by default, but not the possibility to move arguments into temporaries - to keep the risk low.
Moving to temporaries can be enabled with the new frontend option `-enable-move-inout-stack-protector`.
rdar://93677524
2022-11-11 17:14:08 +01: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
ebc16d9062
EscapeInfoDumper: dump address-reachable-from-object information
2022-11-09 08:06:19 +01:00
Egor Zhdan
a6372e955a
Merge pull request #61618 from apple/egorzhdan/scs-reapply-enums
...
Revert "Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift""
2022-10-20 15:46:17 +01:00
Erik Eckstein
7db7065740
swift side effects: add CalleeAnalysis.getSideEffects(of: apply)
2022-10-20 09:20:28 +02: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