Commit Graph

2024 Commits

Author SHA1 Message Date
Aidan Hall 070f7923d9 Bridging: Rename FunctionConvention.results to resultsWithError
This matches the name of the C++ method it bridges, and avoids the ambiguity of
the previous name.
2026-05-18 14:47:05 +01:00
Ben Cohen ad8a8f7cc8 SILOptimizer: add FunctionConvention.formalResults; use it in PackSpecialization
The C++ `SILFunctionType` exposes both `getResults()` (formal results only)
and `getResultsWithError()` (formal + error). The Swift mirror previously
only had `results`, bridging to the with-error variant. Add `formalResults`
for the formal-only view, matching the C++ split.

Switch PackSpecialization's three result-iteration sites to `formalResults`.
The bridged `createSpecializedFunctionDeclaration` preserves the error
result on its own, so iterating with-error included it twice in the new
function's signature.

Also forward the original apply's `nothrow`/`noasync` flags to the
specialized apply, required for SIL verification of a plain apply calling
a function with an error result.
2026-05-18 14:45:26 +01:00
elsa 83d4291709 CSE Optimizer Pass rewrite (#88248)
Resolves rdar://173862129
2026-05-15 19:11:10 +01:00
Daniil Kovalev aa8f1d7efd [AutoDiff] Fix crash due to use after consume in differentiable_function (#88918)
The patch implements proper sil-combiner handling of
`differentiable_function` for cases when extractee has non-trivial
ownership. In such casese, it is consumed by the differentiable_function
instruction. We must copy the extractee before the consumption point so
the copy remains live afterward.

Fixes #88816
2026-05-13 21:33:13 +00:00
Erik Eckstein 8e6560aa78 SIL: fix handling of index_addr in AccessUtils
Fixes a miscompile when the index of `index_addr` is a negative integer constant. In this case two access paths were considered overlapping while in reality they reference two different array elements.

The miscompile manifested in the dead-store-elimination pass, but could potentially also show up in other passes, which use this utility.

https://github.com/swiftlang/swift/issues/77558
rdar://176820188
2026-05-12 19:59:37 +02:00
Egor Zhdan 919f56364a Merge pull request #82216 from swiftlang/egorzhdan/rm-xcode-12-workaround
[SwiftCompilerSources][build] Remove a workaround for missing libc++ in the SDK
2026-05-11 16:58:13 +01:00
Andrew Trick 2dca2e48c4 Merge pull request #88980 from atrick/fix-immortal-init
LifetimeDependenceDiagnostics: calls with no deps are immortal
2026-05-09 14:22:17 -07:00
Andrew Trick f41daa2087 Merge pull request #88968 from atrick/fix-makeborrow-escape
LifetimeDependenceDiagnostics: add Builtin.makeBorrow support
2026-05-08 17:20:46 -07:00
Andrew Trick 4845b0bf4d LifetimeDependenceDiagnostics: calls with no deps are immortal
Fix lifetime diagnostics to consider an implicit initializer of a ~Escapable
type to be implicitly immortal. Required to handle Optional<~Escapable> stored
properties, such as:

struct Foo<Element: ~Escapable>: ~Escapable {
  var element: Element?

  @_lifetime(borrow c)
  init<C>(c: borrowing C) {
    // error: Lifetime-dependent variable 'self' escapes its scope
  }
}

The fix is simply to remove a temporary safeguard that I put in place to
compensate for our incomplete closure lifetimes. We now have the complete
representation of lifetimes on closures, so don't need the safeguard.

Representationally, a function that returns a ~Escapable value but has no
dependendencies is immortal. This was always the intended design, but the
temporary safeguard treated these cases as implicitly bound to some local scope.

Removing this safeguard has the effect of:

- Variable intialization is immortal (it cannot depend on anything by
  definition). The safety of the initializer is checked inside the implementation
  of those expressions rather than the caller.

- Empty ~Escapable types have an implicit immortal initializer (why not?)

- Calls to a function with @_unsafeNonescapableResult but no @_lifetime
  annotation produce an immortal value. This is reasonable, and we want to
  deprecate this attribute as soon as possible anyway. It is not for general use.

This is currently blocking usage of BorrowingSequence, such as a hypothetical BorrowingSequenceMapSequenceIterator.

Fixes rdar://176561897 ([nonescapable] initialization of Optional fields reports
a lifetime escape)
2026-05-08 16:47:56 -07:00
Andrew Trick 1ed083be0c LifetimeDependenceDiagnostics: add Builtin.makeBorrow support
Code like this currently looks like a lifetime escape:

  struct Ref<T: ~Copyable & ~Escapable>: ~Escapable {
    private let ref: Builtin.Borrow<T>

    @_lifetime(borrow target)
    init(_ target: borrowing T) {
      self.ref = Builtin.makeBorrow(target)
    }
  }

This is blocking the implementation of `Ref<~Escapable>`.

Fixes rdar://176564359 ([nonescapable] support Builtin.makeBorrow in
lifetime diagnostics)
2026-05-08 12:26:42 -07:00
Meghana Gupta ebd4d11225 Merge pull request #88847 from meg-gupta/fixeaborrow
Update SIL utilities for address results from borrow accessors
2026-05-07 20:57:06 -07:00
Daniil Kovalev a9a74aa8fe [AutoDiff] Find partial_apply of pullback w/o loop in closure spec pass (#88911)
Currently, AutoDiff Closure Specialization pass iterates over all VJP
instructions and checks each of them against a set of conditions which
`partial_apply` of pullback must satisfy.

This logic could be re-implemented w/o loop, checking conditions in
opposite direction, starting from `return` instruction and transitively
going to defining instructions of operands (`tuple` and `partial_apply`
for the desired pullback case).
2026-05-07 22:52:48 +00:00
Erik Eckstein ba0b04559e ComputeEscapeEffects: add a complexity limit for escape analysis of function arguments
Similar to what we do in AliasAnalysis

related to rdar://175999887
2026-05-07 09:52:14 +02:00
Erik Eckstein bdc1c4d34f AliasAnalysis: use a complexity limit in getApplyEffect
Like we do in other parts of AliasAnalysis which call into escape analysis.

Fixes a compile time problem.
rdar://175937160
2026-05-07 09:52:14 +02:00
Gábor Horváth cfeda37650 Merge pull request #88622 from Xazax-hun/tsan-effects-modeled
[SILOptimizer] Correctly compute the effects of TSan instrumentation
2026-05-06 12:20:31 +01:00
Meghana Gupta ff7724900f Fix EscapeAnalysis for address results of borrow accessors
Update escape walker to recognize that the returned address from borrow accessors
are dependent on the parameter.
2026-05-05 12:28:23 -07:00
Steven Wu a87c1203fb Merge pull request #88608 from cachemeifyoucan/eng/PR-caching-swift-build
[build-script] Add --enable-caching support with clang-cache and Swift compilation caching
2026-05-05 11:36:52 -07:00
Erik Eckstein 78340fd42d Optimizer: de-virtualize deinits of non-copyable InlineArray elements
This is especially important for Embedded Swift because non de-virtualized deinits result in IRGen crashes.

Fixes a compiler crash in embedded
rdar://175984319
2026-05-05 07:27:13 +02:00
Erik Eckstein e942ae40b0 AliasAnalysis: correctly handle InlineArray in type-based alias analysis
Fixes a mis-compile where TBAA failed to recurse into `Builtin.FixedArray`'s element type.

rdar://176106882
2026-05-04 10:55:54 +02:00
Erik Eckstein 5fadeb59d9 AliasAnalysis: speed up memory effect computation for builtin instructions
If a builtin has no memory effects defined (which is the case for most builtins) we don't need to do escape analysis to compute memory effects.
2026-04-30 17:53:40 +02:00
Erik Eckstein 7204a1524b EscapeUtils: use isFullApplySite instead of listing all conforming instruction classes 2026-04-30 17:53:39 +02:00
Erik Eckstein d28549af34 Optimizer: workaround for fast type casting to the FullApplySite and ReturnInstruction protocols
Rather than doing a standard swift runtime cast to an existential, explicitly check for the conforming instruction classes, which is much faster.
The new `isFullApplySite` and `isReturnInstruction` casting utilities are used in the (very few) time critical places in the optimizer.

After toolchain builders are upgraded to a compiler version which includes the fix for this problem (https://github.com/swiftlang/swift/pull/88270), we don't need this workaround anymore and the regular `as`/`is` casts can be used again.

Now the runtime casts doesn't show up prominently in compile-time profiling data anymore - even with a host compiler which doesn't implement fast type checks, yet.

rdar://173916206
2026-04-30 17:53:39 +02:00
Gabor Horvath 12b6809813 [SILOptimizer] Correctly compute the effects of TSan instrumentation
Passing a C++ object to the TSanInOutAccess builtin resulted in an extra
temporary copy. This copy was not optimized out because the semantics of this
builtin was not understood by the optimizer. Teaching the utils that this
intrinsic does not actually modify the object, does not escape it,
and does not read it lets the optimizer eliminate this copy.

Strictly speaking, the test code that uses interop is not safe/correct,
this is why it had a lifetime issue.

rdar://173921363
2026-04-30 11:21:17 +01:00
Steven Wu 72c8c21104 [build-script] Add --enable-caching support with clang-cache and Swift compilation caching
Add a new --enable-caching option that enables compilation caching for both
C/C++ (via clang-cache as compiler launcher) and Swift code (via
-cache-compile-job flags when bootstrapping=hosttools).

New options:
- --enable-caching: main toggle, incompatible with --sccache/--distcc
- --caching-cas-path: CAS directory (default: $BUILD_ROOT/cas)
- --caching-depscan-socket: depscan daemon socket path
- --caching-plugin-path: CAS plugin library path
- --caching-plugin-option: CAS plugin options (repeatable)
- --caching-prefix-map: enable source/SDK/toolchain prefix mapping
- --caching-remote-service-path: remote caching service with auto
  plugin inference from Xcode and implied prefix mapping

The build script starts a clang-cache depscan daemon with reliable cleanup
via atexit and SIGTERM handlers. Per-product build directories get .cas-config
and compilation-prefix-map.json files written automatically.

Caching flags are applied to all Swift host compilation targets: compiler
sources, pure-swift host libraries (ASTGen, macros), swift-syntax, and
the new runtime build when --build-runtime-with-host-compiler is used.

When not using --caching-remote-service-path, enables CAS backend
(-Xfrontend -cas-backend -Xllvm -cas-friendly-debug-info) unless
SWIFT_CACHE_DISABLE_MCCAS is set.

A ninja wrapper is generated at build/<subdir>/build-utils/ninja for
cached incremental builds outside the build-script.

rdar://155876033

Assisted-By: Claude
2026-04-29 14:42:17 -07:00
Doug Gregor 08fa11ecd6 Merge pull request #88678 from DougGregor/embedded-metatypes
[Embedded] Lift the restriction on the use of metatypes in Embedded Swift
2026-04-28 13:01:27 -07:00
Joe Groff 097b0d3400 SIL: Split unchecked_*_enum_data_addr according to ownership and effects.
We cannot use spare bits or other overlapping storage layout tricks with fundamentally
address-only enums, and we can take advantage of this to do borrowing switches or other
in-place projections without copying the value. However, for resilient enums, the
implementation may use spare bit packing, but the type must be handled address-only
outside of its defining module, and we didn't have a way to express that with
borrowing switch. Optimization passes have also been running into problems with the
complexity that we were using `unchecked_take_enum_data_addr` sometimes as a pure
operation. This patch splits the instruction into three:

- `unchecked_inplace_enum_data_addr` represents a nondestructive in-place enum
  projection. It is only allowed for enums whose projection operation is
  nondestructive.
- `unchecked_take_enum_data_addr` represents a destructive enum projection,
  invalidating the enum and leaving the payload to be further consumed.
  This matches the current instruction's semantics.
- `unchecked_borrow_enum_data_addr` represents a borrowing enum projection.
  The instruction takes a second operand for "scratch" space, which the
  enum representation may be copied into in order to avoid invalidating the
  enum value, so the result is dependent on the lifetime of both the
  original enum and the scratch buffer. This allows for borrowing switches
  over resilient enums.

`unchecked_borrow_enum_data_addr` is implemented by taking advantage of the
"address-only enums can't do spare bit optimization" property at runtime.
We inspect the operand type's bitwise-borrowability from its metadata. If
the type is bitwise-borrowable, then we are allowed to bitwise-copy the
enum to the scratch space and apply the projection to the scratch space,
preserving the original value. If the type is not bitwise-borrowable, then
we cannot use spare bit optimization in its layout, so we apply the
projection in-place.

Fixes rdar://174952822.
2026-04-27 15:40:37 -07:00
Doug Gregor 46389f7d7e [Embedded] Start allowing metatypes in Embedded Swift
Now that we have generalized existentials in Embedded Swift, we also
have all of the infrastructure for metatypes. They're lazily
constructed on an as-needed basis, but otherwise work the same way as
in non-Embedded Swift.

Fixes rdar://145706221.
2026-04-27 14:02:03 -07:00
Meghana Gupta 81cc84e06f Merge pull request #88648 from meg-gupta/fixanalysisinvalidation
Correctly invalidate analyses after keypath optimization
2026-04-24 19:57:00 -07:00
Meghana Gupta 784da0a090 Correctly invalidate analyses after keypath optimization
Keypath simplification can create new basic blocks when projecting
optional chain components (OptionalChainProjector splits the block and creates new conditional branches).
However, the Swift-side tryOptimizeKeypath was not notifying the pass manager about
CFG or instruction changes, leaving analyses like the dominator tree stale.
Fix this by calling notifyBranchesChanged() and notifyInstructionsChanged()
when tryOptimizeKeypath succeeds.
2026-04-24 09:43:42 -07:00
Erik Eckstein 1203073264 Revert "SwiftCompilerSources: workaround a crash in the LoadableByAddress pass when building on Windows"
This reverts commit 338dd185e7.

The problem is already fixed and disabling the loadable-address pass causes other problems:
When the pass is disabled a `swiftself` attribute is missing for the stdlib `Hasher.finalize` declaration in LLVM IR.
This causes a mis-compile.
2026-04-24 16:39:30 +02:00
Andrew Trick 265b42d6b6 Merge pull request #88617 from atrick/simplify-load-borrow
Enable load_borrow simplification at -Onone
2026-04-23 10:20:16 -07:00
Andrew Trick aafb6ced4c Enable load_borrow simplification at -Onone
Required for Builtin.borrowAt. SILGen may generate dead borrow scopes for
loadable values used in a return expression that for a borrow access that uses
guaranteed_address convention.

rdar://175382154 (Enable load_borrow simplification at -Onone)
2026-04-22 17:23:09 -07:00
Meghana Gupta c431f654c8 Don't optimize alloc_stack with dynamic_lifetime in TempLValueElimination
The [dynamic_lifetime] attribute represents that the stack location's initialization state is tracked dynamically via a boolean flag — it may be uninitialized at certain program points. TempLValueElimination pass can replace an alloc_stack [dynamic_lifetime] with a destination alloc_stack that does not have dynamic_lifetime. This results in invalid SIL which triggers verifier errors due to lifetime mismatches in some program paths in ossa.

This PR fixes this issue by bailing out of TempLValueElimination for alloc_stack [dynamic_lifetime] in ossa.

Resolves rdar://175097584
2026-04-21 12:22:18 -07:00
Doug Gregor aad51cab01 [Embedded] Remove the ability to disable existentials in Embedded Swift
Support for existentials in Embedded Swift has been available for a
little while now and appears to be solid. Remove the ability to disable
them (via `-disable-experimental-feature EmbeddedExistentials`), both
because it simplifies the code and because it's an ABI break to
disable the feature.
2026-04-17 17:38:01 -07:00
Max Desiatov b589f05c10 Support untyped throws in Embedded Swift (#87617)
**Explanation**: We would like untyped throws to be available in Embedded Swift when system allocator is available.
**Scope**: limited to Embedded Swift.
**Risk**: low due to isolated scope, additive nature of the change, and no adoption of untyped throws in Embedded Swift so far.
**Testing**: added new lit tests.
**Issue**: rdar://171325402
2026-04-14 18:05:14 +01:00
eeckstein 668fb767b6 Merge pull request #88386 from eeckstein/fix-cond-fail-opt
Optimizer: fix optimization of unconditional `cond_fail`s
2026-04-10 07:15:53 +02:00
John McCall 58842cb584 Merge pull request #88373 from rjmccall/alloc-pack-metadata-stack-nesting
Allow alloc_pack_metadata to be marked as [non_nested]
2026-04-09 12:27:03 -04:00
Erik Eckstein 0494037b1e Optimizer: fix optimization of unconditional cond_fails
When the option `-remove-runtime-asserts` is used all `cond_fail` instructions are removed.
However, the cast optimizer inserts such unconditional fails for failing casts. This ended up in an infinite optimization loop in SILCombine.
The fix is
1. don't remove unconditional `cond_fail`s, even if with the `-remove-runtime-asserts` option. This also has the benefit that it enables later optimizations to remove all the dead code after such an unconditional `cond_fail`.
2. Don't optimize a failing cast if it is already preceded by an unconditional `cond_fail`

This bug was introduced by https://github.com/swiftlang/swift/pull/88258

Fixes a compiler hang
rdar://174185165
2026-04-09 16:42:29 +02:00
John McCall 9b6777ff59 Allow alloc_pack_metadata to be marked as [non_nested].
This is inserted by SIL passes and so may not always be properly nested
with respect to non-unreorderable allocations such as async lets.
2026-04-08 17:56:47 -04:00
Erik Eckstein b18ba61e89 Optimizer: natively implement Instruction.isDeinitBarrier in swift
No need to bridge to the C++ implementation, because the swift implementation is very simple
2026-04-08 21:34:43 +02:00
Erik Eckstein 094e6d3481 Optimizer: fix deinit barrier detection for instructions which access memory via a pointer
The original implementation of `mayAccessPointer` did look through `address_to_pointer` - `pointer_to_address` pairs and therefore not detect such pointers.

Fixes a miscompile
rdar://174268466
2026-04-08 21:34:43 +02:00
Meghana Gupta e260417aae Merge pull request #88278 from meg-gupta/fixea
Fix escape analysis for pointer_to_address
2026-04-03 12:53:30 -07:00
Andrew Trick d889cac8e5 Merge pull request #88212 from atrick/fix-builtin-borrow-ossa
Fix Builtin.Borrow<T> OSSA representation
2026-04-03 12:09:24 -07:00
Andrew Trick 7dc30f6a3d Merge pull request #88265 from atrick/fix-deinit-devirt
Fix DeinitDevirtualizer to handle non-generic deinitializer function
2026-04-03 08:40:33 -07:00
Erik Eckstein e5a259415a SIL: mark instruction protocols as @_semantics("fast_cast")
This allows fast casting and improves compile time significantly

rdar://173916206
2026-04-03 07:49:36 +02:00
Erik Eckstein e1bb4f874f cosmetic: add a missing newline in Function.swift 2026-04-03 07:49:36 +02:00
Erik Eckstein 080f9e6a81 Optimizer: add the ConformanceCheckOptimization optimization pass
Optimizes protocol conformance checking by pre-populating vtables with conformance information for "fast-cast" protocols that have superclass constraints.

This optimization works by:

1. Identifying classes that are eligible for optimization (have fixed metadata layout, are not open access, and belong to the current module)

2. Finding protocols, enabled for fast casting nad that have superclass constraints and belong to the current module

3. Pre-computing conformance checks for these protocols and storing the results directly in the vtable, eliminating the need for runtime conformance lookups
2026-04-03 07:49:35 +02:00
Erik Eckstein b97b3640f9 Optimizer: add some Context APIs
* `var isWholeModule`
* `var moduleDecl`
* `ClassDecl.hasFixedMetadataLayout`
2026-04-03 07:49:34 +02:00
Erik Eckstein 7ef83e336d AST: add ClassDecl.selfAndSuperClasses which returns a sequence to conveniently iterate over the super classes 2026-04-03 07:49:34 +02:00
Erik Eckstein 66a86e2a4e AST: bridge some APIs for class and protocol decls 2026-04-03 07:49:34 +02:00