Commit Graph

45 Commits

Author SHA1 Message Date
Alexander Cyon
c18a24e499 [SwiftCompilerSources] Fix typos 2024-08-08 22:22:39 -07:00
Erik Eckstein
ac3e2cb141 EscapeUtils: handle more use types for unchecked_addr_cast
Handle `apply` and `try_apply`.
Also, improve test cases
2024-08-05 17:02:07 +02:00
Erik Eckstein
6cfec91f69 EscapeInfo: handle store_borrow 2024-07-29 17:33:45 +02:00
Erik Eckstein
c6e3f51217 EscapeInfo: handle unchecked_addr_cast
It's dangerous to continue walking over an `unchecked_addr_cast` which casts between two different types.
We can only do this if the result is known to be the end of the walk, i.e. the cast result is not used in a relevant way.
2024-07-29 17:33:44 +02:00
Erik Eckstein
1440ae7b71 EscapeInfo: handle is_unique 2024-07-29 17:33:44 +02:00
Erik Eckstein
da5857d5ec EscapeInfo: improve handling of begin_applys which yield an address value
Instead of just aborting if a `begin_apply` gets an address argument, follow the yielded values and see if they actually escape
2024-07-29 17:33:44 +02:00
Erik Eckstein
9eddb24c41 EscapeUtils: replace ...byWalkingDown APIs with an initialWalkingDirection parameter
It simplifies the EscapeUtils API
NFC
2024-07-29 17:33:43 +02:00
eeckstein
d1ec089c72 Merge pull request #72088 from language-core/main
chore: fix some comments
2024-03-06 10:08:50 +01:00
Alejandro Alonso
254e4f041f Handle atomic instructions in escape analysis
Split atomic loads/stores

no more guard

invert condition
2024-03-05 08:32:51 -08:00
cui fliter
127077b3aa chore: fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 17:23:22 +08:00
Erik Eckstein
abd6ce0c9d EscapeUtils: fix escape result in case an address is stored
This unusual situation can happen if an address is converted to a raw pointer and that pointer is stored to a memory location.
In this case the walkers need to follow load instructions even if the visitor and current projection path don't say so.

Fixes a miscompile
rdar://122805546
2024-02-12 17:56:09 +01:00
Andrew Trick
2128c21106 Migrate SwiftCompilerSources to FunctionConvention.
Layers:
- FunctionConvention: AST FunctionType: results, parameters
- ArgumentConventions: SIL function arguments
- ApplyOperandConventions: applied operands

The meaning of an integer index is determined by the collection
type. All the mapping between the various indices (results,
parameters, SIL argument, applied arguments) is restricted to the
collection type that owns that mapping. Remove the concept of a
"caller argument index".
2024-01-03 12:24:50 -08:00
Erik Eckstein
899034b129 EscapeUtils: handle load_borrow 2023-11-08 17:31:28 +01:00
Erik Eckstein
7b8f433fed EscapeUtils: add EscapeVisitorWithResult.cleanupOnAbort()
Useful to destroy the result if it's not returned
2023-11-08 17:31:10 +01:00
Hamish Knight
3e3fb584c3 [SwiftCompilerSources] NFC: Workaround a compiler bug 2023-10-30 23:49:52 +00:00
Andrew Trick
a5d8aafb23 SwiftCompilerSources: Replace BlockArgument with Phi and TermResult.
All SILArgument types are "block arguments". There are three kinds:
1. Function arguments
2. Phis
3. Terminator results

In every situation where the source of the block argument matters, we
need to distinguish between these three. Accidentally failing to
handle one of the cases is an perpetual source of compiler
bugs. Attempting to handle both phis and terminator results uniformly
is *always* a bug, especially once OSSA has phi flags. Even when all
cases are handled correctly, the code that deals with data flow across
blocks is incomprehensible without giving each case a type. This
continues to be a massive waste of time literally every time I review
code that involves cross-block control flow.

Unfortunately, we don't have these C++ types yet (nothing big is
blocking that, it just wasn't done). That's manageable because we can
use wrapper types on the Swift side for now. Wrapper types don't
create any more complexity than protocols, but they do sacrifice some
usability in switch cases.

There is no reason for a BlockArgument type. First, a function
argument is a block argument just as much as any other. BlockArgument
provides no useful information beyond Argument. And it is nearly
always a mistake to care about whether a value is a function argument
and not care whether it is a phi or terminator result.
2023-09-27 18:47:46 -07:00
Erik Eckstein
e5eb15dcbe Swift SIL: replace the set_deallocating instruction with begin_dealloc_ref
Codegen is the same, but `begin_dealloc_ref` consumes the operand and produces a new SSA value.
This cleanly splits the liferange to the region before and within the destructor of a class.
2023-09-19 15:10:30 +02:00
Erik Eckstein
f6f9e75173 AliasAnalysis: use a complexity limit for the isObjReleased function
We already use a complexity limit for other functions in AliasAnalysis.
This is a workaround for quadratic complexity in ARCSequenceOpts.

Fixes a compile time problem
rdar://114352817
2023-09-04 19:52:57 +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
Erik Eckstein
8bb3e45b41 WalkUtils: more precise handling of existential projections and index_addr 2023-07-05 21:33:24 +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
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
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
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
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
8daa54a8c5 SmallProjectionPath: replace hasNoClassProjection with mayHaveClassProjection
It's less confusing.
NFC
2023-02-11 08:55:20 +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
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
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
60609eb809 Swift SIL: invert Type.isNonTrivialOrContainsRawPointer to Type.isTrivialNonPointer
This is consistent with `Type.isTrivial`.
Also, introduce corresponding properties in `Value`: `hasTrivialType` and `hasTrivialNonPointerType`, because
1. It's less to type than `Type.isTrivial(in: function)` because `Value` knows in which function it is.
2. It fixes the corner case where value is an `Undef`, which has not parent function.
2022-10-20 09:20:28 +02:00
Erik Eckstein
0d73a21c14 EscapeUtils: ignore debug_value for address types.
It has already been ignored for value types, but not for address types.
2022-10-20 09:20:28 +02:00
Egor Zhdan
94ec683ba5 Revert "Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift""
This reverts commit 69431f00
2022-10-18 18:55:57 +01:00
Erik Eckstein
dd0f360d48 EscapeUtils: add an Value.isEscapingWhenWalkingDown API 2022-10-05 07:38:11 +02:00
Erik Eckstein
86d337bc10 EscapeUtils: make hasRelevantType configurable in the EscapeVisitor 2022-10-05 07:38:11 +02:00
Erik Eckstein
66f07fe3da Swift SIL: rework function effects and add function side-effects
So far, function effects only included escape effects.
This change adds side-effects (but they are not computed, yet).
It also involves refactoring of the existing escape effects.
Also the SIL effect syntax changed a bit. Details are in docs/SIL.rst
2022-10-05 07:38:11 +02:00
eeckstein
69431f00e8 Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift" 2022-09-22 11:45:41 +02:00
Egor Zhdan
78cdf5eca6 [cxx-interop][SwiftCompilerSources] Use swift::BuiltinValueKind instead of BridgedBuiltinID
rdar://83361087
2022-09-20 12:03:48 +01:00
Erik Eckstein
9889f165ea EscapeUtils: another small refactoring of the isEscape functions
Instead of the `startWalkingDown` arguments, add separate `isEscapingWhenWalkingDown`/`visitByWalkingDown` functions
2022-09-19 16:48:10 +02:00
Erik Eckstein
a8b58735fc Swift Optimizer: improve ergonomics of EscapeUtils
Replace the `struct EscapeInfo` with a simpler API, just consisting of methods of `ProjectedValue` and `Value`:
* `isEscaping()`
* `isAddressEscaping()`
* `visit()`
* `visitAddress()`
2022-09-19 14:39:10 +02:00
Erik Eckstein
572d51364a rename EscapeInfo.swift -> EscapeUtils.swift 2022-09-19 14:39:10 +02:00