Commit Graph

11645 Commits

Author SHA1 Message Date
eeckstein
11964e5a25 Merge pull request #84766 from eeckstein/struct-simplification
Optimizer: eliminate `struct_extract`s of an owned `struct` where the `struct_extract`s are inside a borrow scope
2025-10-09 06:42:10 +02:00
Kavon Farvardin
a528c5c19c Merge pull request #84734 from kavon/opaque-values/fixes-2
OpaqueValues: support typed throws
2025-10-08 21:17:23 -07:00
Kavon Farvardin
3b0e77390e AddressLowering: support typed throws
Out of SILGen, we'll get the non-indirect SSA for throwing
the error. AddressLowering then converts a `throw` into
`throw_addr` to match the function convention. Similarly, a
try_apply gets rewritten to pass the error address to the
error successor block.

resolves rdar://158171053
2025-10-08 15:59:13 -07:00
Gábor Horváth
0959bce25e Merge pull request #84612 from swiftlang/fix-overrelease-destroy-hoist
[cxx-interop] Delay lowering unowned convention until ownership elimination
2025-10-08 18:42:02 +01:00
Erik Eckstein
181b2f1f6d Optimizer: eliminate struct_extracts of an owned struct where the struct_extracts are inside a borrow scope
This is done by splitting the `begin_borrow` of the whole struct into individual borrows of the fields (for trivial fields no borrow is needed).
And then sinking the `struct` to it's consuming use(s).

```
  %3 = struct $S(%nonTrivialField, %trivialField)  // owned
  ...
  %4 = begin_borrow %3
  %5 = struct_extract %4, #S.nonTrivialField
  %6 = struct_extract %4, #S.trivialField
  use %5, %6
  end_borrow %4
  ...
  end_of_lifetime %3
```
->
```
  ...
  %5 = begin_borrow %nonTrivialField
  use %5, %trivialField
  end_borrow %5
  ...
  %3 = struct $S(%nonTrivialField, %trivialField)
  end_of_lifetime %3
```

This optimization is important for Array code where the Array buffer is constantly wrapped into structs and then extracted again to access the buffer.
2025-10-08 17:48:37 +02:00
Erik Eckstein
f57278f941 SILCombine: be careful about deleting trivially dead destructure_struct instructions
A dead `destructure_struct` with an owned argument can appear for a non-copyable or non-escapable struct which has only trivial elements.
The instruction is not trivially dead because it ends the lifetime of its operand.

Fixes an ownership verification error.
2025-10-08 08:23:40 +02:00
Andrew Trick
4a7895e15b SwiftCompilerSources: bridge Function.isAddressor() 2025-10-07 10:44:42 -07:00
Gabor Horvath
8b3b16cb5b Fix some fallout. 2025-10-07 12:18:23 +01:00
eeckstein
401a2ac2bc Merge pull request #84704 from eeckstein/closure-specialization
ClosureSpecialization: support for OSSA and a big overhaul
2025-10-07 06:59:08 +02:00
Daniil Kovalev
b46a095d66 [AutoDiff][Gardening] Use SILFunction::getSourceFile instead of dup logic (#84709)
In 025902fa99,
`SILFunction::getSourceFile()`
is introduced. This patch makes use of that to avoid duplicating the
logic in `static SourceFile &getSourceFile(SILFunction *f)` in
lib/SILOptimizer/Differentiation/ADContext.cpp.
2025-10-06 09:57:20 -07:00
Erik Eckstein
8efafc7e3b Optimizer: remove the -experimental-swift-based-closure-specialization option 2025-10-06 12:02:48 +02:00
Erik Eckstein
df20d36255 ClosureSpecialization: support for OSSA and a big overhaul
Beside supporting OSSA, this change significantly simplifies the pass.
The main change is that instead of starting at a closure (e.g. `partial_apply`) and finding all call sites, we now start at a call site and look for closures for all arguments. This makes a lot of things much simpler, e.g. not so many intermediate data structures are required to track all the states.

I needed to remove the 3 unit tests because the things those tests were testing are not there anymore. However, the pass is tested with a lot of sil tests (and I added quite a few), which should give good test coverage.

The old ClosureSpecializer pass is still kept in place, because at that point in the pipeline we don't have OSSA, yet. Once we have that, we can replace the old pass withe the new one.
However, the autodiff closure specializer already runs in the OSSA pipeline and there the new changes take effect.
2025-10-06 12:02:48 +02:00
Erik Eckstein
89bba668e2 Mangling: add a closure specialization mangling for arguments which specialize for the same closure as a previous argument
For example:
```
  %1 = partial_apply %closure
  apply %f(%1, %1)    // first argument: `.closure(%1)`
                      // second argument: `.previousArgumentIndex(0)`
```
2025-10-06 09:47:41 +02:00
eeckstein
8edfd9c83f Merge pull request #84662 from eeckstein/fix-dead-alloc-elimination
DeadAllocElimination: fix a bug when promoting mark_dependence instructions
2025-10-04 18:44:04 +02:00
Allan Shortlidge
270ea8b921 SILOptimizer: Fix mandatory switch dispatch elimination for unreachable cases.
https://github.com/swiftlang/swift/pull/67920 introduced a mandatory SIL
optimization that eliminates switch statement dispatch for enum elements that
are provably unavailable at runtime. This optimization helps eliminate
unreachable code, but it's also load bearing because it removes references to
enum element symbols that may not preset at link time. The optimization was
checking the wrong condition to determine whether an enum element is
unavailable at runtime and so the optimization wasn't working for code using
custom availability domains.

Resolves rdar://161846311.
2025-10-03 17:32:12 -07:00
Meghana Gupta
ea0e0173b4 Merge pull request #84595 from meg-gupta/moreborrowaccessors
Extend borrow accessor support
2025-10-03 11:14:49 -07:00
Gabor Horvath
8c197ebd8e [cxx-interop] Delay lowering unowned convention until ownership elimination
Unowned result conventions do not work well with OSSA. Retain the result
right after the call when we come out of OSSA so we can treat the
returned value as if it was owned when we do optimizations.

This fix a miscompilation due to the DestroyAddrHoisting pass hoisting
destroys above copies with unowned sources. When the destroyed object
was the last reference to the pointed memory the copy is happening too
late resulting in a use after free.

rdar://160462854
2025-10-03 13:15:51 +01:00
Erik Eckstein
214be2dda0 DeadAllocElimination: fix a bug when promoting mark_dependence instructions
This was a wrong use of the InstructionDeleter.
When replacing a mark_dependence with a new one the old one has to be deleted _without_ fixing its lifetime.
Otherwise destroy_value instructions are inserted, which is obviously wrong.

Fixes a SIL ownership verification error.
rdar://161831308
2025-10-03 10:48:42 +02:00
Mishal Shah
03a599c5be Merge pull request #84606 from swiftlang/rebranch
Merge clang 21.x rebranch into main
2025-10-02 20:17:05 -07:00
Kavon Farvardin
d58dd6e2c4 Merge pull request #84593 from kavon/manual-ownership/usability-fixes-5
ManualOwnership: don't diagnose unknown callees
2025-10-02 09:44:37 -07:00
Meghana Gupta
75d040f257 Update VariableNameInferrer for borrow/mutate accessors 2025-10-02 07:18:25 -07:00
Meghana Gupta
d54044c231 Update MoveOnlyObjectChecker for borrow accessors 2025-10-02 07:18:24 -07:00
Arnold Schwaighofer
7853ba0a7f Merge pull request #84178 from aschwaighofer/inline_always
Add experimental feature `@inline(always)`
2025-10-01 07:23:24 -07:00
Kavon Farvardin
0a81f7fbec AddressLowering: handle ignored_use
resolves rdar://158175192
2025-09-30 13:59:30 -07:00
Kavon Farvardin
f63117e01c ManualOwnership: don't diagnose unknown callees
Since the annotation is non-viral, we don't want to emit an error
when we counter a callee that is unknown.

resolves rdar://161407417
2025-09-30 10:13:45 -07:00
Arnold Schwaighofer
25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
swift-ci
5fd12cd32f Merge remote-tracking branch 'origin/main' into rebranch 2025-09-26 22:55:46 -07:00
nate-chandler
3535225df0 Merge pull request #84543 from nate-chandler/rdar161371112
[OwnershipDiagnosticLowering] Visit autorelease_value insts.
2025-09-26 22:49:24 -07:00
swift-ci
d5a03fa0e4 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-26 22:36:42 -07:00
eeckstein
a1e5a06c9e Merge pull request #84530 from eeckstein/remove-enable-oss-module-option
Remove the `-enable-ossa-modules` option
2025-09-27 07:35:40 +02:00
Nate Chandler
d844cc3b97 [ODL] Visit unchecked_ref_cast insts.
Eliminate them if they're just identity casts.
2025-09-26 17:06:42 -07:00
Nate Chandler
d0cd8acd64 [ODL] Visit autorelease_value insts.
No update is needed for the values they produce.  This pass should
really be refactored not to crash on instructions that aren't explicitly
listed or at least not to compile if not every instruction is listed.

rdar://161371112
2025-09-26 17:06:37 -07:00
Nate Chandler
715abbe057 [OwnershipDiagnosticLowering] Sorted these lines. 2025-09-26 11:54:49 -07:00
swift-ci
4ca69968db Merge remote-tracking branch 'origin/main' into rebranch 2025-09-26 06:14:25 -07:00
Erik Eckstein
7217722c83 Optimizer: remove the now unused NonTransparentFunctionOwnershipModelEliminator pass
Also remove the `skipStdlibModule` flag from the OwnershipModelEliminator, because it's always false
2025-09-26 08:01:09 +02:00
Erik Eckstein
2f124cf564 Remove the -enable-ossa-modules option.
OSSA modules are enabled by default.
The compiler still accepts this option but it has no effect.
2025-09-26 08:01:08 +02:00
Kavon Farvardin
a96e494c87 ManualOwnership: reword diagnostics
The term 'demand' didn't pass the hallway test.
2025-09-25 16:56:03 -07:00
Kavon Farvardin
e943c91158 ManualOwnership: improve diagnostics for address types
We can find the name of the source of a copy_addr to
give less confusing messages.
2025-09-25 16:26:16 -07:00
Kavon Farvardin
58a4a21c27 ManualOwnership: copy_addr [take] doesn't copy 2025-09-25 15:05:58 -07:00
swift-ci
34c7948328 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-25 03:35:26 -07:00
Kavon Farvardin
e93ffb762d Merge pull request #84504 from kavon/manual-ownership/usability-fixes-3
Usability improvements for ManualOwnership (part 3)
2025-09-25 03:17:14 -07:00
Kavon Farvardin
0734267053 ManualOwnership: fix simple var assign
Was mistakenly counting a 'store' as
a copying instruction, when it's only a
consuming one.

SILGen was not handling lvalues that are
addresses for loadable types correctly,
when emitting a CopyExpr in ManualOwnership.
2025-09-24 17:00:24 -07:00
swift-ci
e04ee0fc2d Merge remote-tracking branch 'origin/main' into rebranch 2025-09-23 22:54:52 -07:00
Kavon Farvardin
a44bf30141 Merge pull request #84476 from kavon/manual-ownership/usability-fixes-1
Usability improvements for ManualOwnership (aka "explicit copies mode")
2025-09-23 22:47:22 -07:00
Kavon Farvardin
80a61dd9dd ManualOwnership: variable names in diagnostics
Also tailor the messages it a bit based on uses, such
as closure captures.
2025-09-23 16:35:08 -07:00
Kavon Farvardin
bfce1170d4 ManualOwnership: diagnose copies of generics
copy_addr often is `Releasing`, which is `RefCounting | Deallocating`
2025-09-23 16:35:07 -07:00
swift-ci
2fffb15836 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-22 06:31:59 -07:00
eeckstein
1a8d6a8329 Merge pull request #83866 from jamieQ/remove-old-di-comments
[NFC][SILOptimizer]: update outdated docs in DefiniteInit & DRY
2025-09-22 13:54:09 +02:00
swift-ci
2deb2f9a87 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-19 22:17:53 -07:00
Kavon Farvardin
889baf7f2c Merge pull request #84411 from kavon/copyprop-onone
sil: provide ability to run CopyPropagation in -Onone
2025-09-19 22:09:59 -07:00