Commit Graph

112 Commits

Author SHA1 Message Date
Nate Chandler
aa85694237 [NFC] OSSACompleteLifetime: Renamed. 2025-08-18 09:45:19 -07:00
Nate Chandler
fdd896e021 Revert "[DCE] Verify liveness of completed lifetimes."
This reverts commit 3ec9b269f5.

rdar://149896608
2025-04-23 16:59:26 -07:00
Nate Chandler
3ec9b269f5 [DCE] Verify liveness of completed lifetimes. 2025-04-11 14:58:34 -07:00
Nate Chandler
b405c8b23c [DCE] Keep insts which consume escaping values.
When DCE deletes instructions as dead, if the instruction ends one of
its operands lifetimes, it must insert a compensating lifetime end.
When the def block of the value and the parent block of the instruction
are different, it uses lifetime completion.  Lifetime completion relies
on complete liveness, which doesn't and can't exist for values with
pointer escapes.  The result is ending lifetimes too early.

Avoid this scenario by marking such instructions live.

In the fullness of time, it may be possible to track the deleted
instruction's "location" even in the face of deletions of adjacent
instructions and parent blocks and to insert the lifetime end at that
location.

rdar://149007151
2025-04-11 14:58:33 -07:00
Nate Chandler
6d9ae19629 [DCE] Don't complete lifetimes of erased values.
DCE may enqueue a value for lifetime completion and later on erase the
instruction that defines that value.  When erasing an instruction, erase
each of its results from the collection of values to complete.

rdar://141560546
2024-12-19 15:16:32 -08:00
Nate Chandler
f75f12b681 [DCE] Add delete handler for phi erasure.
Set CallsChanged when appropriate and increment NumDeletedInsts.
2024-12-19 15:12:24 -08:00
Nate Chandler
00116edcfb [NFC] DCE: TermInsts increment NumDeletedInsts. 2024-12-19 15:12:24 -08:00
Nate Chandler
1851e712f8 [Gardening] DCE: Use bound variable in branch.
Rather than reusing the source of the dyn_cast.
2024-12-19 15:12:24 -08:00
Meghana Gupta
50bde829b4 Look through borrowed from instructions before calling lifetime completion
Lifetime completion will insert end_borrows only on borrow introducers.

Look through "borrowed from" instructions before calling it.

rdar://141490551
2024-12-15 01:53:19 -08:00
Meghana Gupta
3701f01de4 Mark ownership fixup instructions as live 2024-12-15 01:53:17 -08:00
Meghana Gupta
daa9b161d0 Fix DCE for ownership forwarding instructions
DCE deletes ownership forwarding instructions when it doesn’t have useful users.
It inserts destroy_value/end_borrow for its operands to compensate their lifetimes.

DCE also deletes branches when its successor blocks does not have useful instructions.
It deletes blocks and creates a jump to the nearest post dominating block.

When DCE needs to delete a forwarding instruction in a dead block, it cannot just create
lifetime ends of its operands at its position. Use LifetimeCompletion utility in such cases.

rdar://140428721
2024-12-09 03:22:57 -08:00
Meghana Gupta
adf1eae287 Bring in DominanceInfo and DeadEndBlocks to DCE 2024-12-09 03:00:18 -08:00
Meghana Gupta
434461d8f4 Fix DCE of load_borrow when it is derived from another borrow scope
Fixes rdar://138663452
2024-11-14 12:09:11 -08:00
eeckstein
6d4db0e649 Merge pull request #77583 from eeckstein/fix-dce
DeadCodeElimination: don't remove end_lifetime instructions with address operands
2024-11-13 18:51:45 +01:00
Erik Eckstein
64698ca6bb DeadCodeElimination: don't remove end_lifetime instructions with address operands
DCE cannot reason about values in memory.

Fixes a memory lifetime verification error
rdar://139779406
2024-11-13 12:04:41 +01:00
Meghana Gupta
fd6d29d10b Insert end_borrow for dead phi operands only when required
DCE inserts end_borrow at phi operands when a guaranteed phi becomes dead.
This should be done for reborrows which end the lifetime of the incoming value.
The existing check was not accurate and ended up inserting end_borrow for forwarded values as well.

Fixes rdar://139283745
2024-11-12 14:05:23 -08:00
Meghana Gupta
359f6d14bf DCE unconditional_checked_cast 2024-10-16 16:55:22 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07: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
224d36c576 [DCE] Insts that define lexical values seem useful
Don't delete as dead instructions that define lexical values such as
`move_value [lexical]`.

rdar://129299803
2024-06-05 13:04:43 -07:00
Meghana Gupta
505d84fe74 Don't delete allocations in DCE 2024-05-23 12:59:00 -07:00
Emil Pedersen
c2c16f53dd [DebugInfo] Fix undef debug values being removed 2024-04-26 16:31:16 -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
Erik Eckstein
44f4ea9099 DeadCodeElimination: fix debug logging of to-instruction when adding a reverse dependency 2024-04-10 13:38:10 +02: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
Nate Chandler
ae6868a296 [DCE] Process instructions added during deletion.
In cea0f00598, `InstructionDeleter` began
deleting `load [take]` instructions.  Analogous to how it creates a
`destroy_value` when deleting an instruction which consumes a value, in
the case of deleting a `load [take]` the `InstructionDeleter` inserts a
compensating `destroy_addr`.

Previously, `DeadCodeElimination` did not observe the creation of any
instructions created by the `InstructionDeleter`.  In the case of the
newly created `destroy_addr`, DCE didn't mark that the `destroy_addr`
was live and so deleted it.  The result was a leak.

Here, this is fixed by passing an `InstModCallbacks`--with an
`onCreateNewInst` implementation--down into `erasePhiArgument` that
eventually invokes the `InstructionDeleter`.  When the
`InstructionDeleter` creates a new instruction, DCE marks it live.
2023-11-15 15:03:24 -08:00
Slava Pestov
05ccd9734c SIL: Introduce ThrowAddrInst 2023-10-31 16:58:54 -04:00
Meghana Gupta
031255c2d8 Rename hasPointerEscape -> findPointerEscape 2023-06-20 21:13:55 -07:00
Nate Chandler
38cbc6fde1 [DCE] Destroys of live values are live.
As is done for `end_borrow`s, add a dependency from a `destroy_value`s
to its operand so that it is not eliminated if its operand isn't.
2023-04-06 13:43:57 -07:00
Nate Chandler
5f5692c3ba [Gardening] Separated debug output lines. 2023-04-06 13:35:01 -07:00
swift-ci
be620b8ada Merge pull request #64709 from meg-gupta/dcefix
Don't DCE @owned phis that may have escaped
2023-03-29 14:26:45 -07:00
Meghana Gupta
246e9c819b Don't DCE @owned phis that may have escaped 2023-03-29 11:38:13 -07:00
Meghana Gupta
a135f4fc50 Check lexicality at reborrow instead of borrow while disabling DCE 2023-03-28 20:31:32 -07:00
Meghana Gupta
b77a3f8a38 Use the updated hasPointerEscape api in DCE 2023-03-28 20:31:32 -07:00
Meghana Gupta
df90e2ac46 Don't DCE lexical phis with only destroy users 2023-03-10 00:27:48 -08:00
Erik Eckstein
b741aa0018 SILOptimizer: fix a stack overflow in DCE
For very large control flow graphs the markControllingTerminatorsLive can stack overflow.
Fix this by doing the work iteratively instead of recursively.

rdar://106198943
2023-03-08 12:20:45 +01:00
Meghana Gupta
0c29ebb57d Fix typo 2023-01-08 00:15:17 -08:00
Meghana Gupta
cdeb55a20a Fix DCE of begin_borrow with @guaranteed operand
For the lower most begin_borrows with @guaranteed operands, there
was no @guaranteed phi computation, restructure the code and fix this case.
2023-01-06 09:23:42 -08:00
Nate Chandler
e1cb0b5522 [MemAccessUtils] Look thru nested for guar roots.
Previously, findGuaranteedReferenceRoots always stopped searching when
finding a begin_borrow, because it's not an ownership-forwarding
instruction.  Here, it is conditionally allowed to keep search through
the borrowee of that begin_borrow if it itself is guaranteed.
2022-12-08 18:29:14 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Meghana Gupta
d6399e2626 Don't generate end_borrow for GuaranteedForwardingPhi in DCE 2022-10-21 11:53:50 -07:00
Meghana Gupta
5ede08ca3c Update DCE for @guaranteed forwarding phi support 2022-10-19 19:54:28 -07:00
Meghana Gupta
a1bcf17b26 Avoid optimizing lexical borrow scopes in DCE 2022-09-15 16:42:23 -07:00
Meghana Gupta
6c0c3dd1e1 Rename hasEscaped -> hasPointerEscape, and don't consider OperandOwnership::BitwiseEscape as a pointer escape 2022-09-15 16:40:39 -07:00
Meghana Gupta
88782ac939 Don't shrink escaped borrow scopes in DCE 2022-09-14 21:02:02 -07:00
Michael Gottesman
1e6187c4f4 [sil] Update all usages of old API SILValue::getOwnershipKind() in favor of new ValueBase::getOwnershipKind().
Andy some time ago already created the new API but didn't go through and update
the old occurences. I did that in this PR and then deprecated the old API. The
tree is clean, so I could just remove it, but I decided to be nicer to
downstream people by deprecating it first.
2022-07-26 11:46:23 -07:00
Erik Eckstein
777b42180d SILOptimizer: use the new set data structures in various optimizations 2022-07-13 14:27:50 +02:00
Andrew Trick
2fd4de411e [SIL-opaque] Removed [Unconditional]CheckedCastValue 2022-03-22 17:04:13 -07:00
Andrew Trick
83b01d8ebe Improve the SILPhiArgument API
This subclass of SILArgument should be eliminated--it's not always a
phi, and whether it is a "phi argument" has nothing whatsoever to do
with the opcode. That is a property of a value's uses, not a property of the
value.

Until then, provide a logical and useful API within the type. This
often avoids the need to explicitly cast to a SILPhiArgument type and
avoids a lot of boilerplate in code that deals with phis.

Note: PhiOperand and PhiValue are improved abstractions on top of this
API. But the SILArgument-level API is still an important bridge
between SILArgument and other phi abstractions.
2022-02-15 13:28:46 -08:00
Nate Chandler
e3fbadf7b7 [SILOptimizer] Let visitTransitiveEndBorrows take SILValues.
Previously, visitTransitiveEndBorrows took BorrowedValues.  However,
there is at least one kind of borrow--namely,
unchecked_ownership_conversion insts--that is not currently permitted by
the BorrowedValue API.  The long term fix is to make BorrowedValue
handle such instructions.  For now, change visitTransitiveEndBorrows to
take SILValues so that unchecked_ownership_conversion can be passed to
the API.

rdar://87985420
2022-01-25 15:55:08 -08:00