Commit Graph

2685 Commits

Author SHA1 Message Date
Meghana Gupta
16a2d7e18d Merge pull request #86812 from meg-gupta/fixsimplifycfg
Fix SimplifyCFG::simplifyTryApplyBlock for owned arguments in ossa
2026-01-29 00:16:30 -08:00
Meghana Gupta
1144aa33d4 Fix SimplifyCFG::simplifyTryApplyBlock for owned arguments in ossa 2026-01-28 19:21:18 -08:00
Erik Eckstein
144b4b910d SROA: don't try to destructure a non-copyable type with a deinit
This check was only done for the top-level alloc_stack, but not for any nested types.

rdar://169022052
2026-01-28 13:10:30 +01:00
Meghana Gupta
501eada8ef [NFC] Reduce indentation in SimplifyCFG::simplifyTryApplyBlock 2026-01-27 05:18:58 -08:00
eeckstein
d01f9da13b Merge pull request #86788 from eeckstein/check-incremental-2
CSE/check_incremental: add some more logging to detect non-determinisms in CSE
2026-01-27 12:42:53 +01:00
Joe Groff
0f3ddfbcc8 Merge pull request #86545 from jckarter/builtin-borrow
`Builtin.Borrow` implementation
2026-01-26 07:32:31 -08:00
Erik Eckstein
903142d7cf CSE/check_incremental: add some more logging to detect non-determinisms in CSE 2026-01-26 15:43:49 +01:00
Meghana Gupta
bfdfe8f948 Remove DCE support for end_lifetime 2026-01-23 14:04:12 -08:00
Meghana Gupta
13a044dc05 [NFC] Remove unused variable 2026-01-23 09:07:05 -08:00
Meghana Gupta
db77c83cad Remove unnecessary calls to DCE::endLifetimeOfLiveValue 2026-01-23 09:01:49 -08:00
Joe Groff
bc53d4ecb4 SIL: Handle Borrow instructions in more utility visitors. 2026-01-23 08:02:09 -08:00
Meghana Gupta
02d0ce598d Remove unnecessary code from DeadCodeElimination
DeadCodeElimination stopped deleting dead copy_value and hoisting destroy_value
instructions.

LifetimeCompletion of valuesToComplete was necessary since DCE could delete blocks
that contain only destroy_value instructions or forwarding instructions with
destroy_value uses only.

Since this is not done in DeadCodeElimination, remove this support.
2026-01-23 06:50:49 -08:00
Erik Eckstein
18063707b5 Optimizer: enable complete OSSA lifetimes throughout the pass pipeline
This new OSSA invariant simplifies many optimizations because they don't have to take care of the corner case of incomplete lifetimes in dead-end blocks.

The implementation basically consists of these changes:
* add the lifetime completion utility
* add a flag in SILFunction which tells optimization that they need to run the lifetime completion utility
* let all optimizations complete lifetimes if necessary
* enable the ownership verifier to check complete lifetimes
2026-01-22 17:41:48 +01:00
Erik Eckstein
0f0aa0c17b Optimizer: require that there are no unreachable blocks and infinite loops in OSSA
These two new invariants eliminate corner cases which caused bugs if optimization didn't handle them.
Also, it will significantly simplify lifetime completion.

The implementation basically consists of these changes:
* add a flag in SILFunction which tells optimization if they need to take care of infinite loops
* add a utility to break infinite loops
* let all optimizations remove unreachable blocks and break infinite loops if necessary
* add verification to check the new SIL invariants

The new `breakIfniniteLoops` utility breaks infinite loops in the control flow by inserting an "artificial" loop exit to a new dead-end block with an `unreachable`.
It inserts a `cond_br` with a `builtin "infinite_loop_true_condition"`:
```
bb0:
  br bb1
bb1:
  br bb1              // back-end branch
```
->
```
bb0:
  br bb1
bb1:
  %1 = builtin "infinite_loop_true_condition"() // always true, but the compiler doesn't know
  cond_br %1, bb2, bb3
bb2:                  // new back-end block
  br bb1
bb3:                  // new dead-end block
  unreachable
```
2026-01-22 17:41:23 +01:00
Erik Eckstein
e3e8c24201 SIL: add the infinite_loop_true_condition builtin
The value of the builtin is true, but the compiler doesn't know.
This builtin is used to break infinite loops.
2026-01-22 17:41:23 +01:00
Arnold Schwaighofer
a81a18755d Merge pull request #86491 from aschwaighofer/embedded_force_witness_table_deserialization_after_specialization
[embedded] Link newly created specialized functions to bring in witness tables
2026-01-15 13:48:26 -08:00
Arnold Schwaighofer
1fa08f7ba7 [embedded] Link newly created specialized functions to bring in witness tables
Specialization might create new references to conformances. A
specialized init_existential_ref might now refer to a concrete
conformance.
In Embedded swift protocol witness tables are emitted lazily. Therefore
deserialization of the sil_witness_table becomes mandatory if it is
referenced because we can't rely on it being defined in the originating
module.

rdar://167843592
2026-01-12 12:44:00 -08:00
Aidan Hall
0ef2094279 [DebugInfo] Salvage more during SIL optimization passes
This reverts commit b1eb70bf45.

The original PR (#85244) was reverted (#85836) due to rdar://165667449

This version fixes the aforementioned issue, and (potentially) improves overall
debug info retention by salvaging info in erase(instructionIncludingDebugUses:),
rather than inserting many explicit salvageDebugInfo calls throughout the code
to make the test cases pass.

Bridging: Make salvageDebugInfo a method of MutatingContext

This feels more consistent than making it a method of Instruction.

DebugInfo: Salvage from trivially dead instructions

This handles the case we were checking in constantFoldBuiltin, so we do not need to salvage debug info there any more.
2026-01-06 12:01:20 +00:00
Meghana Gupta
76b3374e81 Merge pull request #86155 from meg-gupta/removeborrowoptbailouts
Enable GenericSpecializer for borrow/mutate accessors and fix it for conditionally copyable types
2026-01-02 04:26:05 -08:00
Meghana Gupta
d165f735e3 Fix Mem2Reg of multiple store_borrow to a lexical stack location 2025-12-30 19:18:49 -08:00
Meghana Gupta
b467c5ec3c [NFC] Remove unused DominanceInfo from OSSACompleteLifetime and InteriorLiveness 2025-12-17 13:30:06 -08:00
Meghana Gupta
59c588fd39 Remove bailout from GenericSpecializer for borrow accessors 2025-12-17 13:29:53 -08:00
Erik Eckstein
c0017a2701 DeadCodeElimination: don't insert destroys for removed values in dead-end blocks
When an owned value has no lifetime ending uses it means that it is in a dead-end region.
We must not remove and inserting compensating destroys for it because that would potentially destroy the value too early.
Initialization of an object might be cut off and removed after a dead-end loop or an `unreachable`.
In this case a class destructor would see uninitialized fields.

Fixes a mis-compile
https://github.com/swiftlang/swift/issues/85851
rdar://165876726
2025-12-05 17:08:51 +01:00
Aidan Hall
b1eb70bf45 Revert "[DebugInfo] Salvage more in -O builds"
This reverts commit a95d2979f9.

rdar://165667449
2025-12-04 15:39:39 +00:00
Erik Eckstein
e603f52757 DeadCodeElimination: don't hoist destroy_value instructions
DeadCodeElimination can remove instructions including their destroys and then insert compensating destroys at a new place.
This is effectively destroy-hoisting which doesn't respect deinit-barriers.
Disable removing and re-creating `destroy_value` instructions. This is done by other optimizations.
2025-12-03 15:53:56 +01:00
Aidan Hall
96dca43eb9 Merge pull request #85244 from aidan-hall/fixing-debug-info-rebase
Retain more debug info in optimized builds
2025-12-01 20:49:40 +00:00
Aidan Hall
a95d2979f9 [DebugInfo] Salvage more in -O builds
Specifically, improved debug info retention in:
* tryReplaceRedundantInstructionPair,
* splitAggregateLoad,
* TempLValueElimination,
* Mem2Reg,
* ConstantFolding.

The changes to Mem2Reg allow debug info to be retained in the case tested by
self-nostorage.swift in -O builds, so we have just enabled -O in that file
instead of writing a new test for it.

We attempted to add a case to salvageDebugInfo for unchecked_enum_data, but it
caused crashes in Linux CI that we were not able to reproduce.
2025-11-28 17:42:18 +00:00
Erik Eckstein
dbc50633b9 DeadCodeElimination: don't remove empty access scopes
Empty access scopes can be a result of e.g. redundant-load-elimination.
It's still important to keep those access scopes to detect access violations.
Even if the load is physically not done anymore, in case of a conflicting access a propagated load is still wrong and must be detected.

rdar://164571252
2025-11-24 14:49:45 +01:00
Slava Pestov
819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
eeckstein
552b665a9e Merge pull request #85334 from eeckstein/mandatory-destroy-hoisting
Optimizer: make destroy hoisting a mandatory pass
2025-11-07 06:45:03 +01:00
Erik Eckstein
04688a69ec Optimizer: Always respect deinit barriers when hoisting destroys.
Also for non-lexical lifetimes
2025-11-06 21:00:45 +01:00
Michael Gottesman
97b0e2ebae [concurrency] Create builtins for invoking specific concurrency runtime functions.
Specifically:

1. swift_task_addCancellationHandler
2. swift_task_removeCancellationHandler
3. swift_task_addPriorityEscalationHandler
4. swift_task_removePriorityEscalationHandler
5. swift_task_localValuePush
6. swift_task_localValuePop

rdar://109850951
2025-11-05 11:03:44 -08:00
John McCall
13937fdb4e Merge pull request #84528 from rjmccall/async-let-runtime-realism
Model async let begin/finish as builtins in SIL
2025-11-05 10:24:46 -08:00
John McCall
a7d7970e29 Turn finishAsyncLet into a builtin.
This is necessary because we need to model its stack-allocation
behavior, although I'm not yet doing that in this patch because
StackNesting first needs to be taught to not try to move the
deallocation.

I'm not convinced that `async let` *should* be doing a stack allocation,
but it undoubtedly *is* doing a stack allocation, and until we have an
alternative to that, we will need to model it properly.
2025-11-03 16:33:40 -08:00
John McCall
cd67912a50 Remove the deprecated and unused endAsyncLet builtin. 2025-11-03 13:45:18 -08:00
John McCall
1d95fe14de Remove the deprecated and unused startAsyncLet builtin 2025-11-03 13:44:58 -08:00
Aidan Hall
b353bb302f DebugUtils: Remove eraseFromParentWithDebugInsts
There is another near-identical function in DebugOptUtils.h that can be used
everywhere this function is used, and offers more flexibility in its callback
interface.
2025-10-31 13:47:22 +00:00
Meghana Gupta
6734d4a4e6 Disable GenericSpecializer for @guaranteed results 2025-10-23 05:19:15 -07:00
Meghana Gupta
e116df3628 Introduce return_borrow instruction 2025-10-23 05:18:59 -07:00
eeckstein
28535a6865 Merge pull request #85006 from eeckstein/fix-simplify-cfg2
SimplifyCFG: insert compensating `end_lifetime` when replacing a `switch_enum`
2025-10-21 07:25:05 +02:00
eeckstein
9074a1eec3 Merge pull request #85005 from eeckstein/remove-copy-forwarding
Optimizer: remove the CopyForwarding pass
2025-10-21 07:24:42 +02:00
Erik Eckstein
7a7004927c Optimizer: remove the CopyForwarding pass
This pass removes `copy_addr` instructions.
However, it has some problems which causes compiler crashes.
It's not worth fixing these bugs because
1. Most copy_addrs can be eliminated by TempRValueElimination and TempLValueElimination.
2. Once we have opaque value we don't need copy_addr elimination, anyway.

rdar://162212460
2025-10-20 20:19:54 +02:00
Meghana Gupta
0bd6825aa2 Update SIL utilities for borrow and mutate accessors under opaque values mode 2025-10-20 09:05:38 -07:00
Erik Eckstein
a2a57dff0c SimplifyCFG: insert compensating end_lifetime when replacing a switch_enum
This is a follow-up of https://github.com/swiftlang/swift/pull/84905, which handles non-copyable enums with a deinit correctly.
Also, for copyable enums it's more efficient to use `end_lifetime` than `destroy_value`, because we already know that the enum contains a trivial case.
Therefore no destroy operation is needed.
2025-10-20 17:09:59 +02:00
Erik Eckstein
eb7120048b SimplifyCFG: insert compensating destroy when replacing a switch_enum
When replacing a `switch_enum` of an owned enum value with a branch to a non-payload case destination,
we need to insert a destroy of the enum value.

Fixes a SIL ownership verification failure.
https://github.com/swiftlang/swift/issues/84552
rdar://161482601
2025-10-15 11:24:12 +02:00
Arnold Schwaighofer
95358e85a1 Merge pull request #84856 from aschwaighofer/fix_assembly_vision_tests
AssemblyVision: Add assemblyvisionremarkgen-diagnose-copy-destroy-addr option and use it
2025-10-13 13:03:06 -07:00
Arnold Schwaighofer
bd2d6284bd AssemblyVision: Add assemblyvisionremarkgen-diagnose-copy-destroy-addr option and use it
rdar://162443309
2025-10-13 07:44:43 -07:00
Erik Eckstein
65e4c10113 Optimizer: remove the obsolete SpeculativeDevirtualization pass
This pass has been disabled since a very long time (because it's terrible for code size).
It does not work for OSSA. Therefore it cannot be enabled anymore (as is) once we have OSSA throughout the pipeline.
So it's time to completely remove it.
2025-10-13 10:49:17 +02:00
Arnold Schwaighofer
895cd0181d Merge pull request #84796 from aschwaighofer/assembly_vision_copy_destroy
AssemblyVision: Emit remarks for generic copies and destroys
2025-10-11 08:10:43 -07:00
Erik Eckstein
1cbed39f32 Optimizer: support the new array literal initialization pattern in the COWOpts pass 2025-10-10 14:20:59 +02:00