Commit Graph

2874 Commits

Author SHA1 Message Date
Meghana Gupta
96cd2e1e10 Merge pull request #85987 from meg-gupta/undefcapture
Tolerate undef closure captures in ClosureLifetimeFixup
2025-12-12 16:31:09 -08:00
Meghana Gupta
f30b5841c4 Tolerate undef closure captures in ClosureLifetimeFixup
We may see undef closure captures in ClosureLifetimeFixup since
it is a mandatory pass that runs on invalid code as well.

This could sometimes hang the compiler while running `insertDeallocOfCapturedArguments`
in release builds.

This change adds a bailout when we see an undef closure capture to avoid running into this issue.
2025-12-11 12:16:12 -08:00
Andrew Trick
8cdef92334 Add a FIXME to CopyPropagation to highlight an incomplete fix. 2025-12-05 11:16:08 -08: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
98805c9141 Optimizer: let the InstructionDeleter respect deinit-barriers by default
The InstructionDeleter 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. Therefore it's not done for lexical lifetimes.
However, since https://github.com/swiftlang/swift/pull/85334, the optimizer should treat _all_ lifetimes as fixed and not only lexical lifetimes.

This change adds a `assumeFixedLifetimes` flag to InstructionDeleter which is on by default.
Only mandatory passes (like OSLogOptimization) should turn this off.
2025-12-03 15:53:56 +01: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
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
62a13962a9 Merge pull request #85724 from eeckstein/fix-ossa-canonicalization
Optimizer: correctly handle bit-wise escaping values in OSSA canonicalization
2025-12-01 09:36:17 +01: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
633d42b1b4 Optimizer: correctly handle bit-wise escaping values in OSSA canonicalization
We cannot compute the liverange of a value if it bit-wise escapes.
This fixes a mis-compile in copy-propagation which hoists a destroy_value over a use of the escaped value when lexical liveranges are disabled.
The test case is a simplified SIL sequence from the stdlib core where this problem shows up, because we build the stdlib core with disabled lexical liveranges.
2025-11-27 22:07:11 +01:00
Erik Eckstein
17ca4d9787 Optimizer: add FunctionPassContext.mangle(withChangedRepresentation original: Function) 2025-11-26 16:23:47 +01:00
Erik Eckstein
ab2345a2ed FunctionPassContext: support setting arbitrary function type representations when creating specialized functions 2025-11-26 16:23:47 +01:00
Erik Eckstein
2ac69f3c55 SILCombine: fix propagation of concrete existentials in enums
This peephole optimization didn't consider that an alloc_stack of an enum can be overridden by another value.
The fix is to remove this peephole optimization at all because it is already covered by `optimizeEnum` in alloc_stack simplification.

Fixes a miscompile
https://github.com/swiftlang/swift/issues/85687
rdar://165374568
2025-11-25 09:42:19 +01:00
Michael Gottesman
682ef268d2 [optimizer] Teach SIL optimizer that stack nesting should ignore nested stack allocations. 2025-11-21 11:21:15 -08:00
Erik Eckstein
60a28cd570 embedded: don't re-abstract witness method calls for existentials
Don't convert indirect to direct arguments. This is needed to support general existentials in embedded swift.
2025-11-20 10:54:37 -08:00
Kavon Farvardin
7c6d54efa6 OpaqueValues: fix emitDistributedActorSystemWitnessCall
This code was not consulting SILFunctionConventions, so it was
passing things indirect when they are not in sil-opaque-values.

This fixes callers as well to ensure they pass arguments and
results correctly in both modes.
2025-11-19 17:35:14 -08:00
Slava Pestov
819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Michael Gottesman
1bb65d8def Merge pull request #85165 from gottesmm/rdar153207557
[sil] Change SILIsolationInfo inference for classmethods to use SILDeclRef instead of using the AST directly.
2025-11-07 02:02:50 -08:00
Michael Gottesman
91444dd0bd Delete dead code. 2025-11-06 20:25:23 -08:00
Michael Gottesman
8016bf2332 [sil] Change SILIsolationInfo inference for classmethods to use SILDeclRef instead of using the AST directly.
We are creating/relying on a contract between the AST and SIL... that SILDeclRef
should accurately describe the method/accessor that a class_method is from. By
doing this we eliminate pattern matching on the AST which ties this code too
tightly to the AST and makes it brittle in the face of AST changes. This also
fixes an issue where we were not handling setters correctly.

I am doing this now since it is natural to fix it along side fixing the
ref_element_addr issue in the previous commit since they are effectively doing
the same thing.

rdar://153207557
2025-11-06 20:25:23 -08:00
Michael Gottesman
dfbe7c1e7a [rbi] Refactor out code for handling ref_element_addr so I can use it for class_methods as well.
The two pieces of code are fundamentally doing the same thing so I can reuse the
code. I am doing the refactoring as a separate change so that it is easier to
review.
2025-11-06 19:31:41 -08:00
Michael Gottesman
f0a4571fdd [rbi] Ensure that we properly handle nonisolated(nonsending) for forward declared vars.
rdar://164042741
2025-11-05 13:01:08 -08:00
John McCall
ba47e23f51 Merge pull request #84688 from rjmccall/stack-nesting-2
Rewrite `StackNesting` to use a single-pass algorithm
2025-11-03 20:47:16 -08:00
John McCall
8d231d20c6 Rewrite StackNesting to be a non-iterative single-pass algorithm.
The previous algorithm was doing an iterative forward data flow analysis
followed by a reverse data flow analysis. I suspect the history here is that
it was a reverse analysis, and that didn't really work for infinite loops,
and so complexity accumulated.

The new algorithm is quite straightforward and relies on the allocations
being properly jointly post-dominated, just not nested. We simply walk
forward through the blocks in consistent-with-dominance order, maintaining
the stack of active allocations and deferring deallocations that are
improperly nested until we deallocate the allocations above it. The only
real subtlety is that we have to delay walking into dead-end regions until
we've seen all of the edges into them, so that we can know whether we have
a coherent stack state in them. If the state is incoherent, we need to
remove any deallocations of previous allocations because we cannot talk
correctly about what's on top of the stack.

The reason I'm doing this, besides it just being a simpler and hopefully
faster algorithm, is that modeling some of the uses of the async stack
allocator properly requires builtins that cannot just be semantically
reordered. That should be somewhat easier to handle with the new approach,
although really (1) we should not have runtime functions that need this and
(2) we're going to need a conservatively-correct solution that's different
from this anyway because hoisting allocations is *also* limited in its own
way.

I've attached a rather pedantic proof of the correctness of the algorithm.

The thing that concerns me most about the rewritten pass is that it isn't
actually validating joint post-dominance on input, so if you give it bad
input, it might be a little mystifying to debug the verifier failures.
2025-11-03 11:51:17 -08:00
Michael Gottesman
760a6fa776 Merge pull request #82427 from gottesmm/pr-9d8b2e21000560a9c3a3b0143c3fb3b22a0ca75a
[rbi] Remove code that caused us to misidentify certain captured parameters as sending.
2025-10-31 20:28:59 -07:00
Aidan Hall
c7af4c584e Bridging: APIs for PackSpecialization pass 2025-10-26 13:44:34 +00:00
Kavon Farvardin
03d61ed75a Merge pull request #85090 from kavon/manual-ownership/usability-fixes-6
ManualOwnership: provide ability to apply to entire compilation unit
2025-10-25 00:15:03 -07:00
nate-chandler
b4777083b0 Merge pull request #85128 from nate-chandler/rdar162922634
[CastOptimizer] Use TypeConverter to lower type.
2025-10-24 23:47:03 -07:00
Kavon Farvardin
6b858b411c ManualOwnership: introduce 'DynamicExclusivity'
We can add warnings about dynamic exclusivity
checks that may happen on an access, with
explainers about why they happen for safety.
2025-10-24 16:59:44 -07:00
Nate Chandler
41a7d7cab9 [CastOptimizer] Use TypeConverter to lower type, 2 2025-10-24 15:34:35 -07:00
Nate Chandler
ef6bd64e7f [CastOptimizer] Use TypeConverter to lower type.
rdar://162922634
2025-10-24 15:34:09 -07:00
Michael Gottesman
4946256c61 Merge pull request #85064 from gottesmm/pr-a4b0eaddab3318c277ebf3a9080a8bcfc79a3ba6
[rbi] Prefer field specific isolation over nominal type isolation when inferring isolation.
2025-10-23 18:17:51 -07:00
Meghana Gupta
1dc5c9611c Intoduce unchecked_ownership instruction in raw SIL
This instruction can be used to disable ownership verification on it's result and
will be allowed only in raw SIL.

Sometimes SILGen can produce invalid ownership SSA, that cannot be resolved until
mandatory passes run. We have a few ways to piecewise disable verification.
With unchecked_ownership instruction we can provide a uniform way to disable ownership
verification for a value.
2025-10-23 05:19:08 -07:00
Meghana Gupta
a3aed17d63 Disable inlining borrow accessors with return_borrow instruction 2025-10-23 05:19:06 -07:00
Meghana Gupta
e116df3628 Introduce return_borrow instruction 2025-10-23 05:18:59 -07:00
Michael Gottesman
c14e4b0376 [rbi] Prefer field specific isolation over nominal type isolation when inferring isolation.
Specifically given a nominal type like the following:

```swift
@MainActor
struct Foo {
  @CustomActor var ns: NonSendableKlass
}
```

the isolation for ns should be CustomActor not MainActor.

rdar://160603379
2025-10-22 09:33:44 -07:00
Michael Gottesman
6ab8b569c4 Merge pull request #84879 from gottesmm/pr-9c51dc994a742cf2eaf0f068c64f5e3ed57a22f9
[rbi] Ensure that we error when two 'inout sending' parameters are in the same region at end of function.
2025-10-17 17:31:20 -07:00
Michael Gottesman
828bf45e4e [rbi] Ensure that we error when two 'inout sending' parameters are in the same region at end of function.
The caller is allowed to assume that the 'inout sending' parameters are not in
the same region on return so can be sent to different isolation domains safely.
To enforce that we have to ensure on return that the two are /actually/ not in
the same region.

rdar://138519484
2025-10-17 12:07:09 -07:00
Michael Gottesman
fe9c21fd87 [sil] Add a new instruction cast_implicit_actor_to_optional_actor.
This instruction converts Builtin.ImplicitActor to Optional<any Actor>. In the
process of doing so, it masks out the bits we may have stolen from the witness
table pointer of Builtin.ImplicitActor. The bits that we mask out are the bottom
two bits of the top nibble of the TBI space on platforms that support TBI (that
is bit 60,61 on arm64). On platforms that do not support TBI, we just use the
bottom two tagged pointer bits (0,1).

By using an instruction, we avoid having to represent the bitmasking that we are
performing at the SIL level and can instead just make the emission of the
bitmasking an IRGen detail. It also allows us to move detection if we are
compiling for AArch64 to be an IRGen flag instead of a LangOpts flag.

The instruction is a guaranteed forwarding instruction since we want to treat
its result as a borrowed projection from the Builtin.ImplicitActor.
2025-10-16 10:52:04 -07:00
Michael Gottesman
cff1c77a0b [rbi] Teach RBI how to infer nonisolated(unsafe) of closure captures.
The previous commit in this PR exposed that we were not handling this correctly.
Specifically, we incorrectly started to error in SwiftFoundation.

rdar://162629359
2025-10-14 14:00:35 -07:00
Michael Gottesman
97c3bf3b99 [rbi] Remove code that caused us to misidentify certain captured parameters as sending.
Specifically, this code was added because otherwise we would in swift 5 +
strict-concurrency mode emit two warnings, one at the AST level and one at the
SIL level. Once we are in swift-6 mode, this does not happen since we stop
compiling at the AST level since we will emit the AST level diagnostic as an
error.

To do this, we tried to pattern match what the AST was erroring upon and treat
the parameter as disconnected instead of being isolated. Sadly, this resulted in
us treating certain closure cases incorrectly and not emit a diagnostic
(creating a concurrency hole).

Given that this behavior results in a bad diagnostic only to avoid emitting two
diagnostics in a mode which is not going to last forever... it really doesn't
make sense to keep it. We really need a better way to handle these sorts of
issues. Perhaps a special semantic parameter put on the function that squelches
certain errors. But that is something for another day. The specific case it
messes up is:

```
class NonSendable {
    func action() async {}
}

@MainActor
final class Foo {
    let value = NonSendable()

    func perform() {
        Task { [value] in
            await value.action() // Should emit error but do not.
        }
    }
}
```

In this case, we think that value is sending... when it isnt and we should emit
an error.

rdar://146378329
2025-10-14 14:00:29 -07:00
Erik Eckstein
71381fab3c ConstExpr: support the new array literal initialization pattern 2025-10-10 14:20:58 +02:00
Andrew Trick
d653b0ccd0 Merge pull request #84739 from atrick/lifedep-unsafeaddress
LifetimeDependenceDefUseWalker: store to unsafeMutableAddress
2025-10-09 18:40:32 -07: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
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
Meghana Gupta
75d040f257 Update VariableNameInferrer for borrow/mutate accessors 2025-10-02 07:18:25 -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