Commit Graph

393 Commits

Author SHA1 Message Date
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
Nate Chandler
fc13686403 [ObjCBridgingOpt] Look through copy_value insts. 2023-03-29 11:39:43 -07:00
Erik Eckstein
010efc1ca6 Swift Bridging: use C++ instead of C bridging for the optimizer 2023-03-21 15:33:09 +01:00
Erik Eckstein
7789b4063e Swift Bridging: remove BridgedMemoryBehavior and use swift.MemoryBehavior instead 2023-03-21 15:33:09 +01:00
Erik Eckstein
4445373808 Swift Bridging: use C++ instead of C bridging for BridgedInstruction 2023-03-21 15:33:09 +01:00
Erik Eckstein
c4f5bab5b7 Swift Bridging: use C++ instead of C bridging for BridgedBasicBlock 2023-03-21 15:33:09 +01:00
Erik Eckstein
ae7770d911 Swift Bridging: use C++ instead of C bridging for BridgedFunction 2023-03-21 15:33:09 +01:00
Erik Eckstein
598644fb92 Swift Bridging: use C++ instead of C bridging for the bridged witness table classes 2023-03-21 15:33:09 +01:00
Erik Eckstein
151f09769f Swift Bridging: use C++ instead of C bridging for BridgedVTable and BridgedVTableEntry 2023-03-21 15:33:09 +01:00
Erik Eckstein
eecea088e7 Swift Bridging: use C++ instead of C bridging for BridgedOperand and BridgedValue 2023-03-21 15:33:09 +01:00
Erik Eckstein
318c30895f Swift Optimizer: eliminate ARC operations on types which are marked as immortal
A type (mostly classes) can be attributed with `@_semantics("arc.immortal")`.
ARC operations on values of such types are eliminated.

This is useful for the bridged SIL objects in the swift compiler sources.
2023-03-14 21:07:03 +01:00
Erik Eckstein
210d1150c8 EscapeUtils: speed up hasRelevantType
The run time of `Value.hasTrivialNonPointerType` is not negligible, because it does a cache lookup of type lowerings.
Only do this check if it's really needed.
2023-03-14 21:07:03 +01: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
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
eeckstein
40f05faf03 Merge pull request #63690 from eeckstein/fix-same-metatype-simplification
Swift Optimizer: fix a crash when simplifying same-metatype comparisons of function types
2023-02-16 06:45:00 +01:00
eeckstein
04be498a47 Merge pull request #63686 from eeckstein/effects-refactoring
some refactoring in Effects and ComputeEscapeEffects
2023-02-15 22:20:51 +01:00
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