Commit Graph

387 Commits

Author SHA1 Message Date
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
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
0a2d233d1e compute effects: Don't modify the effects if they didn't change
This avoids sending a change notification which can trigger unnecessary other invalidations.
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
Erik Eckstein
ecbcacdecf SIL Analysis: Rename InvalidationKind::FunctionData to InvalidationKind::Effects
This invalidation kind is used when a compute-effects pass changes function effects.
Also, let optimization passes which don't change effects only invalidate the `FunctionBody` and not `Everything`.
2022-10-20 09:20:28 +02:00
Nate Chandler
4476088ab5 [ComputeSideEffects] Track deinit-barrier-ness.
Functions "are deinit barriers" (more pedantically, applies of functions
are deinit barriers) if any of their instructions are deinit barriers.
During side-effect analysis, when walking a function's instructions for
other global effects, also check for the deinit-barrier effect.  If an
instruction is found to be a deinit barrier, mark the function's global
effects accordingly.

Add SILFunction::isDeinitBarrier to conveniently access the effects
computed during ComputeSideEffects.

Update the isBarrierApply predicate to iterate over the list of callees,
if complete, to check whether any is a deinit barrier.  If none is, then
the apply is not a deinit barrier.
2022-10-18 21:23:22 -07:00
Nate Chandler
7ea336367d [NFC] Port isDeinitBarrier to Swift.
Added new C++-to-Swift callback for isDeinitBarrier.

And pass it CalleeAnalysis so it can depend on function effects.  For
now, the argument is ignored.  And, all callers just pass nullptr.

Promoted to API the mayAccessPointer component predicate of
isDeinitBarrier which needs to remain in C++.  That predicate will also
depends on function effects.  For that reason, it too is now passed a
BasicCalleeAnalysis and is moved into SILOptimizer.

Also, added more conservative versions of isDeinitBarrier and
maySynchronize which will never consider side-effects.
2022-10-18 21:23:22 -07: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
5208d6d467 Swift Optimizer: make the RunUnitTests a module pass
Which makes more sense. At the time RunUnitTests was added, there were no module passes, yet.
2022-10-17 11:45:30 +02:00
Erik Eckstein
741c6c38df Swift Optimizer: add the ComputeSideEffects pass.
Computes the side effects for a function, which consists of argument- and global effects.
This is similar to the ComputeEscapeEffects pass, just for side-effects.
2022-10-05 07:38:11 +02: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
e1c65bd1d6 Swift Optimizer: rename the ComputeEffects pass to ComputeEscapeEffects 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
Erik Eckstein
99079ec673 ValueDefUseWalker: fix a problem with cond_br
Don't crash when visiting the cond_br condition operand.
2022-10-05 07:37:41 +02:00
Erik Eckstein
872013959d Swift AccessUtils: remove AccessStoragePathWalker
Instead, place it's one and only API `visitAccessStorageRoots` into an extension of `ValueUseDefWalker`.
2022-10-05 07:37:41 +02:00
Erik Eckstein
0a59e02bb4 Swift AccessUtils: make AccessPathWalker private
And simplify it.
This struct is not really needed by clients. It's just needed internally in 'Value.accessPath` (and similar properties) to compute the access path.
2022-10-05 07:37:41 +02:00
Erik Eckstein
53363c7c3b WalkUtils: add UnusedWalkingPath
Which is a walking path which matches everything.

Useful for walkers which don't care about the path and unconditionally walk to all defs/uses.
2022-10-05 07:37:41 +02:00
Erik Eckstein
81ac311c83 Swift Optimizer: add the DeadEndBlocks utility for finding dead-end blocks.
Dead-end blocks are blocks from which there is no path to the function exit (`return`, `throw` or unwind).
These are blocks which end with an unreachable instruction and blocks from which all paths end in "unreachable" blocks.
2022-10-05 07:37:41 +02:00
Erik Eckstein
aea26bbc6e Swift SIL: add a utility protocol NoReflectionChildren for better debug output with lldb po
Let's lldb's `po` command not print any "internal" properties of the conforming type.
This is useful if the `description` already contains all the information of a type instance.
2022-10-05 07:37:41 +02:00
eeckstein
69431f00e8 Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift" 2022-09-22 11:45:41 +02:00
Egor Zhdan
5f2a641513 [cxx-interop][SwiftCompilerSources] Use swift::SILWitnessTable::WitnessKind instead of `
SILWitnessTableEntryKind`

rdar://83361087
2022-09-20 12:03:48 +01:00
Egor Zhdan
3f1a30c52a [cxx-interop][SwiftCompilerSources] Use swift::SILAccessKind instead of BridgedAccessKind
rdar://83361087
2022-09-20 12:03:48 +01: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
Erik Eckstein
eec201ca07 AccessUtils: Replace the struct PointerIdentification with a computed property var PointerToAddressInst.originatingAddress
It's a simpler API.
2022-09-19 11:30:35 +02:00
Erik Eckstein
2137e41502 Swift SIL: add ProjectedValue.
A projected value consists of the original value and a projection path.
For example, if the `value` is of type `struct S { var x: Int }` and `path` is `s0`, then the projected value represents field `x` of the original value.

Also, use ProjectedValue instead of AccessStoragePath.
2022-09-19 11:30:35 +02:00
Erik Eckstein
e06d1c68a6 WalkUtils: add two inline elements into the WalkerCache to avoid memory allocations in the common case.
If there are no more than 2 elements in the cache, we can avoid using the `cache` Dictionary, which avoids memory allocations.
Fortunately this is the common case by far (about 97% of all walker invocations).
2022-09-19 11:30:35 +02:00
Erik Eckstein
4554939dc4 SwiftCompilerSources: consistently use assert instead of precondition
It's better to use the new assert implementation (defined in the "Basic" module) than Swift's `precondition`
2022-09-14 14:16:26 +02:00
Erik Eckstein
984ae9e4f1 Define our own assert implementation for the swift compiler sources
For two reasons:
* We also like to check for assert failures in release builds. Although this could be achieved with `precondition`, it's easy to forget about it and use `assert` instead.
* We need to see the error message in crashlogs of release builds. This is even not the case for `precondition`.

Also, re-export the "Basic" module in "SIL" so that the new assert implementation is also available in the Optimizer module (where all files import SIL).
2022-09-14 14:16:26 +02:00
Erik Eckstein
024ec094d1 Swift SIL: simplify the representation of argument effects
Just refactoring, it's a NFC.
2022-09-09 21:50:10 +02:00
Michael Gottesman
925a211ed8 Merge pull request #60989 from gottesmm/pr-d5933fd70a08a0acd36e29c39312cf34cc50f904
[move-only] Fix a few small issues around mark must check.
2022-09-08 13:19:53 -07:00
Erik Eckstein
b2b44c0d83 Swift Optimizer: add the StackProtection optimization
It decides which functions need stack protection.

It sets the `needStackProtection` flags on all function which contain stack-allocated values for which an buffer overflow could occur.

Within safe swift code there shouldn't be any buffer overflows.
But if the address of a stack variable is converted to an unsafe pointer, it's not in the control of the compiler anymore.
This means, if there is any `address_to_pointer` instruction for an `alloc_stack`, such a function is marked for stack protection.
Another case is `index_addr` for non-tail allocated memory.
This pattern appears if pointer arithmetic is done with unsafe pointers in swift code.

If the origin of an unsafe pointer can only be tracked to a function argument, the pass tries to find the root stack allocation for such an argument by doing an inter-procedural analysis.
If this is not possible, the fallback is to move the argument into a temporary `alloc_stack` and do the unsafe pointer operations on the temporary.

rdar://93677524
2022-09-08 08:42:25 +02:00
Erik Eckstein
0eddce0dca Swift SIL: add BuiltinInst.ID.stackAlloc
And make all the case identifiers lowercase.
2022-09-08 08:42:25 +02:00