Daniil Kovalev
1f77138afe
[AutoDiff] Closure specialization: specialize branch tracing enums ( #85757 )
...
This patch contains part of the changes intended to resolve #68944 .
1. Closure info gathering logic.
2. Branch tracing enum specialization logic.
3. Specialization of branch tracing enum basic block arguments in VJP.
4. Specialization of branch tracing enum payload basic block arguments
in pullback.
Note that mangling-related logic is implemented in C++ since at this
moment we have no Swift bridged for that.
Here is a simplified example of how branch tracing enum (BTE)
specialization looks like.
Before specialization:
```
enum $_AD__xxx {
case bb0(((Float) -> Float))
}
func vjp(...) {
// ...
%foo = function_ref $foo : (Float, Float) -> Float
%pa1 = partial_apply %foo(%arg1) : (Float) -> Float
%payload1 = tuple (%pa1) : ((Float) -> Float)
%bte = enum $_AD__xxx.bb0!enumelt, %payload1
// ...
}
func pullback(%bte, ...) {
// ...
%payload2 = unchecked_enum_data %bte, $_AD__xxx.bb0!enumelt : ((Float) -> Float)
%pa2 = tuple_extract %payload2, 0 : (Float) -> Float
%res = apply %pa2(%arg2) : Float
// ...
}
```
After specialization:
```
enum $_AD__xxx_spec_bb0_0 {
case bb0(((Float)))
}
func vjp(...) {
// ...
%captured1 = tuple (%arg1) : (Float)
%payload1 = tuple (%captured1) : ((Float))
%bte_spec = enum $_AD__xxx_spec_bb0_0.bb0!enumelt, %payload1
// ...
}
func pullback_spec(%bte_spec, ...) {
// ...
%payload2 = unchecked_enum_data %bte, $_AD__xxx_spec_bb0_0.bb0!enumelt : ((Float))
%captured2 = tuple_extract %payload2, 0 : (Float)
%arg1 = tuple_extract %captured2, 0 : Float
%foo = function_ref $foo : (Float, Float) -> Float
%res = apply %foo(%arg2, %arg1) : Float
// ...
}
```
2025-12-21 00:33:50 +00:00
Erik Eckstein
7ace2c5736
CopyToBorrowOptimization: need to update borrowed-from instructions when changes are made
...
When ownership is changed from owned to guaranteed, the enclosing values of a guaranteed value can change.
Fixes a SIL verifier error.
2025-12-19 17:44:00 +01:00
Erik Eckstein
fb5015832e
SimplifyDestructure: convert destructure_tuple/destructure_struct with guaranteed ownership to to tuple_extract/struct_extract
2025-12-19 17:44:00 +01:00
Erik Eckstein
d5afd16fbf
Optimizer: rename SimplifyMisc.swift -> SimplifyTypeValue.swift
2025-12-19 17:43:59 +01:00
Erik Eckstein
9631d6cdf8
SimplifyLoad/SimplifyLoadBorrow: add two peephole optimizations
...
* Replace address casts of heap objects
```
%1 = unchecked_addr_cast %0 : $*SomeClass to $*OtherClass
%2 = load [copy] %1
```
with ref-casts of the loaded value
```
%1 = load [copy] %0
%2 = unchecked_ref_cast %1 : $SomeClass to $OtherClass
```
* Replace a `load_borrow` of a `store_borrow` with a `begin_borrow`:
```
%1 = alloc_stack $T
%2 = store_borrow %0 to %1
...
%3 = load_borrow %2
// ... uses of %3
end_borrow %3
```
->
```
%1 = alloc_stack $T
%2 = store_borrow %0 to %1
...
%3 = begin_borrow %0
// ... uses of %3
end_borrow %3
```
2025-12-19 17:43:59 +01:00
Erik Eckstein
439bda67c8
SIL: add Type.isHeapObjectReferenceType
2025-12-19 17:43:59 +01:00
Erik Eckstein
a250a30e8c
SimplifyBeginBorrow: remove inner borrow scopes and other improvements
...
* remove borrow scopes which are borrowing an already guaranteed value
* allow optimizing lexical `begin_borrows` outside the mandatory pipeline
* fix: don't remove `begin_borrow [lexical]` of a `thin_to_thick_function` in the mandatory pipeline
2025-12-19 17:43:58 +01:00
Erik Eckstein
0271a4fd03
SimplifyBeginBorrow: ignore type-dependent operands when converting borrowed -> owned
2025-12-19 17:43:58 +01:00
Erik Eckstein
2868663963
Optimizer: handle debug_value in begin_borrow simplification
...
When trying to remove a borrow scope by replacing all guaranteed uses with owned uses, don't bail for values which have debug_value uses.
Also make sure that the debug_value instructions are located within the owned value's lifetime.
2025-12-19 17:43:58 +01:00
Erik Eckstein
ec8fe839c5
SimplifyBeginBorrow: small refactoring
...
* rename `lookThroughSingleForwardingUses` -> `lookThroughOwnedConvertibaleForwardingChain`
* fix the comment of `replaceGuaranteed`
2025-12-19 17:43:58 +01:00
Erik Eckstein
f67a310861
Optimizer: split SimplifyBeginAndLoadBorrow.swift into SimplifyBeginBorrow.swift and SimplifyLoadBorrow.swift
2025-12-19 17:43:57 +01:00
Meghana Gupta
5c4ce2f942
Merge pull request #86023 from meg-gupta/miscborrowfixes
...
Minor fixes to borrow accessors
2025-12-16 20:03:19 -08:00
Meghana Gupta
0c5fe56b7f
Bailout on address apply result while computng argument escaping effects
2025-12-15 13:02:13 -08:00
eeckstein
f092709b72
Merge pull request #85883 from aeu/ast_suppress_compiler_warning
...
[AST] suppress compiler warning
2025-12-15 15:44:34 +00:00
eeckstein
fd8c3ea046
Merge pull request #86042 from eeckstein/sil-unit-tests
...
SwiftCompilerSources: convert all unit-test passes to real tests
2025-12-15 14:38:50 +00:00
Erik Eckstein
c822615dc2
Optimizer: convert the UpdateBorrowedFromPass pass to a test
...
And remove the now empty TestPasses directory.
2025-12-15 10:01:51 +01:00
Erik Eckstein
bff873dea0
Optimizer: convert the SILPrinter and TestInstructionIteration passes to tests
2025-12-15 10:01:41 +01:00
Erik Eckstein
c639c716b9
Optimizer: convert the RangeDumper pass to a test
2025-12-15 10:01:41 +01:00
Erik Eckstein
d8a85693c8
Optimizer: convert the FunctionUsesDumper pass to a test
2025-12-15 10:01:41 +01:00
Erik Eckstein
6ef1064a92
Optimizer: add ModuleTest
2025-12-15 10:01:41 +01:00
Erik Eckstein
751e52cdc3
Optimizer: convert the DeadEndBlockDumper pass to a test
2025-12-15 10:01:40 +01:00
Mahdi Bahrami
d1bea5b184
Minor fix in HasShortDescription doc-comments
2025-12-15 11:54:41 +03:30
Erik Eckstein
dc38a2226e
Optimizer: convert the AliasInfoDumper and MemBehaviorDumper passes to tests
2025-12-15 09:09:41 +01:00
Erik Eckstein
edacd4c4e3
Optimizer: convert the AccessDumper from a pass to a test
2025-12-15 09:09:41 +01:00
Erik Eckstein
abc40b3b55
Optimizer: convert the EscapeInfoDumper from a pass to a test
2025-12-15 09:09:40 +01:00
Andrew Trick
d609889e87
Merge pull request #85934 from atrick/fix-closure-liveness
...
Fix InteriorUseWalker: consider partial_apply [on_stack] an escape
2025-12-11 15:05:21 -08:00
Joe Groff
560eb9deaa
Merge pull request #85954 from jckarter/sil-require-require-instruction
...
SIL verifier: The `atInstruction`/`atArgument` parameter to `require` should not be optional.
2025-12-11 09:43:43 -08:00
Joe Groff
4479b49fd8
SIL: Handle address ReturnInsts from borrow accessors as yielding uses.
...
This avoids a spurious lifetime error when an address-only borrow accessor returns a
non-`Escapable` value from a non-`Escapable` aggregate.
2025-12-10 11:44:41 -08:00
Joe Groff
da4e72cf2d
SIL verifier: The atInstruction/atArgument parameter to require should not be optional.
...
The underlying C++ code expects a non-null `Instruction*` or `SILArgument*` pointer, and
most of the contextual information in a verifier error is derived from these arguments,
so it doesn't really make sense for the Swift level interface to present these arguments
as optional.
2025-12-10 10:27:56 -08:00
Arnold Schwaighofer
be78127c23
Merge pull request #85923 from aschwaighofer/embedded_existentials_requires_embedded
...
[embedded] Feature::EmbeddedExistentials requires Feature::Embedded
2025-12-10 07:13:45 -08:00
Andrew Trick
f7c3c6f437
Fix InteriorUseWalker: consider partial_apply [on_stack] an escape
...
Fixes a bug in MandatoryDestroyHoisting where a captured value is destroyed
before a copy of the closure.
On-stack closures can be copied, and all copied uses must be within the borrow
scope of the captured operand. This is just like any other non-Escapable value,
so treat it as such by checking `Value.mayEscape` rather than `Type.Escapable`.
Originally, I wanted to make it illegal to copy of partial_apply [on_stack], but
it looks like we still allow it.
I would rather not complicate any logic yet with special handling for this
case. To fix any performance concerns, we might be able to simplify the
representation instead by banning copy_value on on-stack closures.
Fixes rdar://165850554 swift-frontend crash: While running "CopyPropagation" -
Invalid SIL provided to OSSACompleteLifetime?!)
2025-12-09 14:38:04 -08:00
Arnold Schwaighofer
4d879967a7
[embedded] Feature::EmbeddedExistentials requires Feature::Embedded
2025-12-09 10:21:51 -08:00
Alfonso Urdaneta
527ce4ef4a
suppress AST compiler warning
2025-12-06 16:26:33 +00:00
eeckstein
d0f8ccef28
Merge pull request #85857 from eeckstein/fix-vtable-specialization
...
Embedded: allow generic class constructors
2025-12-05 20:17:52 +01:00
Aidan Hall
21aa6b2ea2
Merge pull request #85836 from aidan-hall/revert-debuginfo
...
Revert "[DebugInfo] Salvage more in -O builds"
2025-12-05 14:14:17 +00:00
Erik Eckstein
8b496f668f
Embedded: allow generic class constructors
...
For example:
```
public class C: MyClass {
public init(p: some P) {
// ...
}
}
```
Constructors are not called via the vtable (except "required" constructors).
Therefore we can allow generic constructors.
https://github.com/swiftlang/swift/issues/78150
rdar://138576752
2025-12-05 11:35:00 +01:00
Erik Eckstein
7eedc18965
Swift AST: add ConstructorDecl.isInheritable
...
and make `AbstractFunctionDecl.isOverridden` final
2025-12-05 11:34:54 +01:00
Erik Eckstein
9ceb8b83c1
SIL-Verifier: Don't verify that there are no stores in read-only access scopes if there is a conflicting scope
...
This is a programming error, but the compiler should not crash. The violation is caught at runtime.
2025-12-04 21:12:32 +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
eeckstein
4ebdcf27a6
Merge pull request #85814 from eeckstein/no-destroy-hoisting-in-dce
...
Optimizer: don't hoist destroys in DeadCodeElimination and the InstructionDeleter
2025-12-04 14:22:14 +01:00
Daniil Kovalev
b73676ee68
Bridging: Implement several optional- and enum-related bridges ( #85756 )
...
In #85757 , part of the changes resolving #68944 is submitted. Most
bridges required for #85757 were previously implemented in #84648 . After
#82653 got merged, we have demand for several new bridges in order to
properly support optimizing derivatives of throwing functions via
AutoDiff Closure Specialization pass.
This patch implements:
- **AST:**
* `var optionalObjectType: Type` property of `Type` struct
* `var optionalType: Type` property of `Type` struct
- **SIL:**
* `let name: StringRef` property of `EnumCase` struct
* `func createOptionalSome(operand: Value, type: Type) -> EnumInst`
method of `Builder`
* `func createOptionalNone(type: Type) -> EnumInst` method of `Builder`
2025-12-04 08:26:44 +00:00
Erik Eckstein
8aa911ba2f
Optimizer: add simplifications for destroy_value
...
Attempt to optimize by forwarding the destroy to operands of forwarding instructions.
```
%3 = struct $S (%1, %2)
destroy_value %3 // the only use of %3
```
->
```
destroy_value %1
destroy_value %2
```
The benefit of this transformation is that the forwarding instruction can be removed.
Also, handle `destroy_value` for phi arguments.
This is a more complex case where the destroyed value comes from different predecessors via a phi argument.
The optimization moves the `destroy_value` to each predecessor block.
```
bb1:
br bb3(%0)
bb2:
br bb3(%1)
bb3(%3 : @owned T):
... // no deinit-barriers
destroy_value %3 // the only use of %3
```
->
```
bb1:
destroy_value %0
br bb3
bb2:
destroy_value %1
br bb3
bb3:
...
```
2025-12-03 15:53:55 +01:00
Aidan Hall
90e12147e0
Merge pull request #85456 from aidan-hall/pack-opt-fix-weather-swb
...
PackSpecialization: Fix result & type parameter handling
2025-12-02 15:38:32 +00:00
Arnold Schwaighofer
36a3c6e611
Merge pull request #85644 from aschwaighofer/wip_embedded_exits_with_eriks_changes_v1
...
[embedded] Fix associated type conformances for specialized witness tables
2025-12-01 16:40:31 -08: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
Michael Gottesman
24c69c674d
Merge pull request #85604 from gottesmm/alloc_stack_non_nested
...
[irgen] Implement support for alloc_stack non_nested.
2025-12-01 09:38:06 -08:00
Arnold Schwaighofer
0c882d42ce
Merge pull request #85625 from aschwaighofer/wip_embedded_cast_tuples
...
[embedded] Allow casting to tuples and tighten the check which existentials we support in embedded with existentials
2025-12-01 08:27:43 -08:00
eeckstein
9304ce951c
Merge pull request #85707 from eeckstein/embedded-witness-method-specialization
...
embedded: change the function representation of directly called witness methods
2025-12-01 09:36:45 +01:00
eeckstein
46c69e40c1
Merge pull request #85533 from eeckstein/fix-access-simplification
...
SILOptimizer: don't remove empty conflicting access scopes
2025-12-01 09:35:48 +01:00
eeckstein
c8b5df5129
Merge pull request #85647 from eeckstein/fix-embedded-cpp-classes
...
embedded: don't try to specialize vtables of C++ imported reference-counted classes
2025-12-01 09:35:19 +01:00