Commit Graph

339 Commits

Author SHA1 Message Date
Erik Eckstein
783f90b76c LICM: handle memory dependency for store sinking correctly
Prevent sinking of stores if there are instructions other than `load` which may read from memory.
This kind of memory dependencies were ignored.
Fixes SIL verifier crashes or - in worst case - miscompiles.

rdar://150205299
2025-04-29 17:46:56 +02:00
Evan Wilde
bca1378fdb SILOptimizer: Disable invalid passes in C++-only compiler
The SimplifyCFG and LoopRotate passes result in verification failures
when built in a compiler that is not built with Swift sources enabled.

Fixes: rdar://146357242
2025-04-21 12:46:01 -07:00
Andrew Trick
d9dd93560d Support mark_dependence_addr in SIL passes. 2025-03-25 23:02:45 -07:00
Pavel Yaskevich
41c88f864a [SILOptimizer] Turn "is self-recursive" check into analysis
The body of a function has to be re-analyzed for every call
site of the function, which is very expensive and if the
body is not changed would produce the same result.

This takes about ~10% from swift-syntax overall build time
in release configuration.
2025-03-24 00:25:13 -07:00
Meghana Gupta
26277c8363 Add support for bounds check optimization of Span and InlineArray 2025-02-28 17:11:52 -08:00
Meghana Gupta
3fe1029ef8 [NFC] Reorganize and rename ArrayBoundsCheckOpts.cpp 2025-02-28 09:50:58 -08:00
Erik Eckstein
bfdeb57863 LICM: fix handling of stores of Optional.none in OSSA
Currently we don't support hoisting ownership instructions.
But the check was missing a store of Optional.none because such a value has no ownership even if the optional is not trivial.

Fixes a SIL verifier crash.
https://github.com/swiftlang/swift/issues/79491
2025-02-25 09:34:59 +01:00
Meghana Gupta
03038d201e Fix loop rotate when header has instructions producing ownership results
rdar://145086395
2025-02-18 14:42:28 -08:00
Erik Eckstein
83aaccc188 remove the "array.copy_into_vector" array-semantic
It's not needed anymore, because the "FixedArray" experimental feature is replaced by inline-arrays.
2025-02-12 10:51:14 +01:00
Meghana Gupta
6a47193e0c Fix LICM for begin_access/end_access in OSSA
LICM in OSSA is enabled only for instructions involving trvial values.

begin_access/end_access is eligible for LICM in OSSA. However we need to
collect applies to check for conflicts.

rdar://143835241
2025-01-29 12:54:35 -08:00
Erik Eckstein
5dc80f62dc LoopRotate: don't rotate loops where the header block branches to a dead-end block.
Incomplete liveranges in the dead-end exit block can cause a missing adjacent phi-argument for a re-borrow if there is a borrow-scope is in the loop.
But even when we have complete lifetimes, it's probably not worth rotating a loop where the header block branches to a dead-end block.

Fixes a SIL verifier crash
2025-01-21 19:08:08 +01:00
Erik Eckstein
48b913af4b Optimizer: make the hasOwnershipOperandsOrResults utility available in OwnershipOptUtils 2025-01-02 10:42:01 +01:00
Erik Eckstein
8439d0bc46 LoopRotate: remove redundant phis after ssa-update
Fixes an ownership error
2024-12-12 09:22:20 +01:00
Erik Eckstein
301ab4e112 LoopRotate: remove the replace-arg-with-struct peephole optimization
This does not belong to loop-rotate and does not work with OSSA.
This peephole is covered by other optimizations.
2024-12-12 08:35:48 +01:00
Erik Eckstein
1bd74d1fc3 LICM: (limited) support for OSSA
The first step: allow hoisting instructions which only have trivial operands and results.
2024-12-11 12:32:33 +01:00
Erik Eckstein
074a99cc39 LoopRotate: don't rotate a loop if the new header is loop exiting as well
This doesn't give any performance benefit.
2024-12-11 12:32:33 +01:00
Erik Eckstein
8000802481 LoopRotate: handle copy_value and begin_borrow correctly 2024-12-11 12:32:32 +01:00
Erik Eckstein
7e8410ebc1 COWArrayOpt: handle load_borrow 2024-12-11 12:32:32 +01:00
Erik Eckstein
51e3e5ed80 Optimizer: rename BorrowArgumentsUpdater -> GuaranteedPhiUpdater
NFC
2024-11-12 09:26:59 +01:00
Erik Eckstein
626b40c329 LoopRotate: don't verify the optimized function within the pass
With the new re-borrow flag computation, verification can fail before the re-borrow flags are updated at the end of the pass.
2024-11-12 09:26:59 +01:00
Erik Eckstein
6b8c6a3c3b SIL: rename updateBorrowedFrom to updateBorrowArguments
NFC
2024-11-12 09:26:58 +01:00
Kavon Farvardin
7203a4fa73 ColdBlockInfo: overhaul analysis pass
The old analysis pass doesn't take into account profile data, nor does
it consider post-dominance. It primarily dealt with _fastPath/_slowPath.

A block that is dominated by a cold block is itself cold. That's true
whether it's forwards or backwards dominance.

We can also consider a call to any `Never` returning function as a
cold-exit, though the block(s) leading up to that call may be executed
frequently because of concurrency. For now, I'm ignoring the concurrency
case and assuming it's cold. To make use of this "no return" prediction,
use the `-enable-noreturn-prediction` flag, which is currently off by
default.
2024-09-03 15:41:10 -07:00
Meghana Gupta
49bb19d27e Fix debug info in LICM
Use RegularLocation::getAutoGeneratedLocation() while hoisting a load.
Previously the source location of the preheader's terminator was used,
this need not be a RegularLocationKind triggering verification errors.
2024-08-14 01:49:58 +05:30
Erik Eckstein
8f2531348d LICM: make sure that getMemoryBehavior is only called for address values
It was called for non-address operands of fix_lifetime.
Just exclude such fix_lifetime instructions from moving. It's not important anyway.
2024-07-29 17:33:44 +02:00
Meghana Gupta
06faf98251 Update Array bounds check optimization's isIdentifiedUnderlyingArrayObject 2024-07-10 03:32:27 -07:00
Meghana Gupta
aad321c566 Improve pattern matching range check in ArrayBoundsCheck optimizations
Current pattern matching misses when range checks are of the form:

%cond = %end < %start
cond_fail %cond, "Range requires lowerBound <= upperBound"

instead of
%cond =  (%start <= %end) ^ 1
cond_fail %cond, "Range requires lowerBound <= upperBound"
2024-07-03 10:39:15 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Erik Eckstein
35017dbe42 ArrayPropertyOpt: update borrowed-from instructions
Fixes a verifier crash when building with OSSA modules

rdar://129796888
2024-06-14 14:07:54 +02:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
nate-chandler
49dd95fae3 Merge pull request #73240 from nate-chandler/rdar127013278
[LICM] Bail on unreferenceable storage.
2024-04-25 07:00:39 -07:00
Nate Chandler
7753b0fa78 [LICM] Bail on unrefable storage.
When computing an access path, if a struct with unreferenceable storage
is encountered, bail out.  Otherwise, an attempt will be made to
construct such a struct via the struct instruction which isn't possible.

rdar://127013278
2024-04-24 18:04:21 -07: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
Michael Gottesman
b85f75f6b5 Delete some unused variables 2024-02-27 13:14:47 -08:00
Michael Gottesman
11f0ff6e32 [sil] Ensure that all SILValues have a parent function by making it so that SILUndef is uniqued at the function instead of module level.
For years, optimizer engineers have been hitting a common bug caused by passes
assuming all SILValues have a parent function only to be surprised by SILUndef.
Generally we see SILUndef not that often so we see this come up later in
testing. This patch eliminates that problem by making SILUndef uniqued at the
function level instead of the module level. This ensures that it makes sense for
SILUndef to have a parent function, eliminating this possibility since we can
define an API to get its parent function.

rdar://123484595
2024-02-27 13:14:47 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Rick van Voorden
f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -08:00
Erik Eckstein
7c46a4d9e2 LICM: don't let cond_fail prevent hoisting initializations of global variables
And fix the corresponding test file
2024-01-02 09:42:59 +01:00
nate-chandler
680c737ddb Merge pull request #64789 from nate-chandler/more-move-values
[SILGen] Used move_value for more lexical values.
2023-12-15 07:07:54 -08:00
Nate Chandler
2a5aafe9d7 [ForEachLoopUnroll] Fix move_value handling. 2023-12-14 09:10:05 -08:00
Meghana Gupta
90c5b80b0b [NFC] Add some debug msgs in LoopUnroll 2023-12-13 10:38:12 -08:00
Erik Eckstein
cc83a7d414 add the "array.copy_into_vector" array-semantic 2023-12-09 18:49:57 +01:00
Meghana Gupta
86b651330b Revert "Merge pull request #69807 from apple/revert-69450-uninarrayfix"
This reverts commit cabb5e109f, reversing
changes made to 09688abb02.
2023-12-06 08:29:50 -08:00
Erik Eckstein
0897d8a720 MemoryLifetimeVerifier: use CalleeCache instead of AliasAnalysis
To verify if a function may read from an indirect argument, don't use AliasAnalysis.
Instead use the CalleeCache to get the list of callees of an apply instruction.
Then use a simple call-back into the swift Function to check if a callee has any relevant memory effect set.

This avoids a dependency from SIL to the Optimizer.
It fixes a linker error when building some unit tests in debug.
2023-12-01 19:20:18 +01:00
Erik Eckstein
0d596fd8c7 ABCOpts: fix a miscompile in case a value is added to the array index
For example: `array[i + x]`. In this case the addition of `x` was misinterpreted as pre-increment of the induction variable

rdar://118026862
2023-11-20 11:20:55 +01:00
Doug Gregor
8c079c3e43 Teach ForEachLoopUnroll to deal with error basic blocks without arguments 2023-11-13 14:51:14 -08:00
Mishal Shah
e8de333daf Revert "Add a mark_dependence while emitting SIL for uninitialized array allocation " 2023-11-12 09:43:13 -08:00
Meghana Gupta
61ce3a3af2 Handle mark_dependence in ForEachLoopUnroll 2023-10-27 14:19:21 -07:00
Erik Eckstein
4db8130625 LoopRotate: ignore single-block loops where the "backedge" block only contains trivial instructions
This avoids that LoopRotate increases code size without any benefit.

rdar://117362048
2023-10-24 11:19:28 +02:00
Evan Wilde
309aed4925 Add SmallSetVector replacement
llvm::SmallSetVector changed semantics
(https://reviews.llvm.org/D152497) resulting in build failures in Swift.
The old semantics allowed usage of types that did not have an
`operator==` because `SmallDenseSet` uses `DenseSetInfo<T>::isEqual` to
determine equality. The new implementation switched to using
`std::find`, which internally uses `operator==`. This type is used
pretty frequently with `swift::Type`, which intentionally deletes
`operator==` as it is not the canonical type and therefore cannot be
compared in normal circumstances.

This patch adds a new type-alias to the Swift namespace that provides
the old semantic behavior for `SmallSetVector`. I've also gone through
and replaced usages of `llvm::SmallSetVector` with the
`Swift::SmallSetVector` in places where we're storing a type that
doesn't implement or explicitly deletes `operator==`. The changes to
`llvm::SmallSetVector` should improve compile-time performance, so I
left the `llvm::SmallSetVector` where possible.
2023-07-25 12:28:27 -07:00
Saleem Abdulrasool
55531ea5d9 Merge pull request #67085 from hjyamauchi/licm
Don't try to hoist/sink {begin,end}_access when the loop has no exits
2023-07-05 20:58:05 -07:00