Commit Graph

10 Commits

Author SHA1 Message Date
Meghana Gupta d4cdfad4bc Add a sanity check to ensure we don't have self with address type while optimizing bounds checks for fixed storage types
When the self operand of a fixed_storage.check_index call is an address
type (e.g. @inout Span<Int>), the value at that address can be mutated
between bounds checks. Add a bailout to prevent illegal optimization in such cases.
2026-05-20 11:49:57 -07:00
Meghana Gupta 82d99f9776 [BoundsCheckOpts] Fix merging bounds checks with fixed-storage semantics
When merging fixed-storage bounds checks with the same self value,
cloneFixedStorageIndex clones the index computation of a later check
to place it adjacent to an earlier one. The previous implementation could lead to
illegal SIL due to dominance violation in some cases by cloning an instruction
before it's operands were cloned.

Fix this with a recursive DFS that appends instructions in postorder.
Since a node is only added to the worklist after all its operands are recursively
processed, the resulting order is a valid topological sort that guarantees every
cloned instruction's operands are already available.
2026-04-21 03:19:07 -07:00
Meghana Gupta ff0557ab92 Bailout cloning indexes for non-trivial ossa instructions
In cloneFixedStorageIndex, bail out when an instruction to be cloned has
a lifetime-ending operand. Cloning such an instruction (e.g.
destructure_struct/destructure_tuple with an @owned operand, end_borrow of a begin_borrow)
may create a second consume, violating OSSA invariants.
2026-03-20 13:46:24 -07:00
Meghana Gupta c3e35843a9 Hoist bounds checks annotated with fixed storage semantics of the same self to appear together in a block
This change introduces an optimization that hoists bounds checks with fixed storage semantics when they operate on the same self value,
grouping them together within a basic block to enable downstream optimizations like partial vectorization.

Since the language guarantees the validity of underlying storage for types annotated with fixed storage semantics like Span, InlineArray etc,
we don't need to consult alias analysis to perform this optimization.

We have always hoisted cond_fail instructions over side effects in loops. This change enables hoisting over side-effects in blocks.

rdar://164947648 and rdar://166409418
2026-03-20 13:46:23 -07:00
Erik Eckstein c20593cd58 Optimizer: better support of OSSA in various optimizations
Mainly:
* look through ownership instructions in more places
* support `load_borrow` and `store_borrow` where `load` and `store`s are expected
* support `destructure_struct` where `struct_extract` is expected
* enable inout-keypath optimization for OSSA
* OSSA support in StringOptimization
2026-03-10 07:56:51 +01:00
Anthony Latsis 2bfe2bd587 SIL: Treat -1 as signed when using it to construct an "all bits set" llvm::APInt
This should enable us to revert
https://github.com/swiftlang/swift/pull/81464/commits/73c70ee338c515fba37baf3faff93c6389558a5c.
2025-08-27 15:22:30 +01:00
Anthony Latsis 26540ca439 SIL: Do not use -1 to construct a 1-bit llvm::APInt
A -1 truncated to 1 bit is just 1, so stop overcomplicating things here.
2025-08-27 12:41:10 +01:00
Erik Eckstein 8666297597 BoundsCheckOpts: handle more integer comparison builtins
So far, only negated comparisons (via xor) were handled. Now, also handle simplified comparisons
2025-07-18 07:43:51 +02: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