Commit Graph

28 Commits

Author SHA1 Message Date
Erik Eckstein
ab2345a2ed FunctionPassContext: support setting arbitrary function type representations when creating specialized functions 2025-11-26 16:23:47 +01:00
Erik Eckstein
6988b03c13 ClosureSpecialization: don't crash for recursive functions
Don't support self-recursive functions because that would result in mapping troubles when cloning.
2025-10-24 06:11:53 +02:00
Daniil Kovalev
75f80410e6 Run closure specialization only if ownership info is present in callee (#84956)
Calling `cloneRecursively` from `SpecializationInfo.cloneClosures`
requires the callee having ownership info. Otherwise, the cloner uses
`recordFoldedValue` instead of `recordClonedInstruction`, and
`postProcess` hook is not called, which leads to an assertion failure in
`BridgedClonerImpl::cloneInst`.
2025-10-17 10:51:57 -07: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
Jakub Florek
e3140e0ae0 Add new generalized cloner. 2025-08-28 20:57:57 +01:00
Daniil Kovalev
ae2b46e263 [AutoDiff][gardening] Auto-format ClosureSpecialization.swift (#83925)
Auto-formatting is done as a prerequisite for the ongoing series of
patches resolving #68944.
2025-08-27 06:12:47 +00:00
Erik Eckstein
41a6b8e257 SwiftCompilerSources: move SIL-related Context APIs from Optimizer to the SIL module 2025-07-28 14:19:11 +02:00
Daniil Kovalev
5528cf1cc4 [AutoDiff] Run AutoDiff closure spec pass for all VJPs (#81548)
Previously, AutoDiff closure specialization pass was triggered only on
VJPs containing single basic block. However, the pass logic allows
running on arbitrary VJPs. This PR enables the pass for all VJPs
unconditionally. So, if the pullback corresponding to multiple-BB VJP
accepts some closures directly as arguments, these closures might become
specialized by the pass. Closures passed via payload of branch tracing
enum are not specialized - this is subject for future changes.

The PR contains several commits.
1. The thing named "call site" in the code is partial_apply of pullback
corresponding to the VJP. This might appear only once, so we drop
support for multiple "call sites".
2. Enhance existing SILOptimizer tests for the pass.
3. Add validation-tests for single basic block case.
4. The change itself - delete check against single basic block.
5. Add validation-tests for multiple basic block case.
6. Add SILOptimizer tests for multiple basic block case.
2025-07-07 13:00:14 +00:00
Erik Eckstein
4212c611e5 Optimizer: add Context.createSpecializedFunctionDeclaration
Originally this was a "private" utility for the ClosureSpecialization pass.
Now, make it a general utility which can be used for all kind of function specializations.
2025-06-20 08:15:00 +02:00
Erik Eckstein
094b246874 SIL: FunctionArgument.copyFlags needs a MutatingContext argument
SIL may only be modified through a MutatingContext. Otherwise analysis notifications may get lost.
2025-06-20 08:14:59 +02:00
Anton Korobeynikov
87ca0f8680 Fix ownership issues with sequences of partial_apply's in AutoDiff closure specialization pass (#80662)
Each partial_apply consumes its arguments, therefore we should never release intermediate ones in the sequence of closures.

Fixes #78847
2025-04-15 07:14:44 -07:00
Erik Eckstein
d52f7d1619 AST/SIL: Refactor and simplify AST.Type, AST.CanonicalType and SIL.Type
* let `SIL.Type` conform to `TypeProperties` to share the implementation of common type properties between the AST types and `SIL.Type`
* call references to an `AST.Type` `rawType` (instead of just `type`)
* remove unneeded stuff
* add comments
2025-03-14 09:40:22 +01:00
Erik Eckstein
09f6f4f9b6 AutodiffClosureSpecialization: workaround a problem with OSSA.
Disable the closure specializer optimization for closures in OSSA until the underlying problem is fixed.
https://github.com/swiftlang/swift/issues/78847

Part of rdar://140229560
2025-01-23 16:10:32 +01:00
Erik Eckstein
cd7c533d42 Optimizer: add SingleValueInstruction.replace(with:) and use it throughout the optimizer
It replaces all uses and then erases the instruction.
2024-12-19 20:53:45 +01:00
Erik Eckstein
10782cf42b SwiftCompilerSources: introduce the AST module
As the optimizer uses more and more AST stuff, it's now time to create an "AST" module.
Initially it defines following AST datastructures:
* declarations: `Decl` + derived classes
* `Conformance`
* `SubstitutionMap`
* `Type` and `CanonicalType`

Some of those were already defined in the SIL module and are now moved to the AST module.
This change also cleans up a few things:
* proper definition of `NominalTypeDecl`-related APIs in `SIL.Type`
* rename `ProtocolConformance` to `Conformance`
* use `AST.Type`/`AST.CanonicalType` instead of `BridgedASTType` in SIL and the Optimizer
2024-10-02 07:10:29 +02:00
Erik Eckstein
701a7f7275 SwiftCompilerSources: don't make anything public in the Optimizer module
The Optimizer module is a leave module. No other modules depend on it. Therefore nothing must be public in this module.
2024-10-02 07:10:28 +02:00
Erik Eckstein
c96b196ffa SwiftCompilerSources: bridge SILLinkage
Make SILLInkage available in SIL as `SIL.Linkage`.
Also, rename the misleading Function and GlobalVariable ABI `isAvailableExternally` to `isDefinedExternally`
2024-08-22 08:56:27 +02:00
Kshitij
487648a3aa [Autodiff] Fixes bugs in closure-spec opt that were causing "optimized" test failures on Linux builds 2024-05-22 22:24:31 -07:00
Kshitij
c288d95c1e [Autodiff] Separates out changes for using non-ossa instructions in the closure-spec optimization pass
The OSSA elimination pass has not yet been moved below all high level
function passes. Until that work has been completed the Autodiff
closure-spec optimization pass cannot solely support OSSA instructions.
2024-05-21 12:02:28 -07:00
Kshitij
12faf79911 [Autodiff] Adds logic to rewrite call-sites using functions specialized by the closure-spec optimization 2024-05-21 12:02:28 -07:00
Kshitij Jain
c37d31cc64 Merge pull request #73596 from jkshtj/specialize
[Autodiff] Adds logic to generate specialized functions in the closure-spec pass
2024-05-14 18:55:23 -07:00
Kshitij
ce2161ee43 [Autodiff] Fixes build issues on Linux 2024-05-13 19:25:18 -07:00
Kshitij
74166a4ab6 [Autodiff] Moves bridging code accesses in closure-spec opt behind APIs
Addresses some other surfacial feedback as well.
2024-05-13 15:37:30 -07:00
Kshitij
ab751d57ab [Autodiff] Adds logic to generate specialized functions in the closure-spec pass 2024-05-13 11:16:42 -07:00
Nate Chandler
7bfd7110cc [Gardening] SIL: "LifeRange" -> "LiveRange" 2024-05-10 15:56:35 -07:00
Kshitij
c6330a7d3f Rev: Addressed feedback 2024-05-02 13:16:12 -07:00
Kshitij
fd609846ae Makes the swift-based closure-spec pass an experimental frontend feature 2024-05-02 09:14:05 -07:00