Commit Graph

6754 Commits

Author SHA1 Message Date
Joe Groff
5ad260315b Use the BorrowingSwitch implementation for all noncopyable switches.
It works well enough now that it should be an acceptable replacement for both
borrowing and consuming switches that works in more correct situations than the
previous implementation. This does however expose a few known issues that I'll
try to fix in follow ups:

- overconsumes cause verifier errors instead of raising diagnostics (rdar://125381446)
- cases with multiple pattern labels aren't yet supported (rdar://125188955)
- copyable types with the `borrowing` or `consuming` modifiers should probably use
  noncopyable pattern matching.

The `BorrowingSwitch` flag is still necessary to enable the surface-level syntax
changes (switches without `consume` and the `_borrowing` modifier, for instance).
2024-04-09 16:31:01 -07:00
Ellie Shin
3bb25dec11 Merge pull request #72606 from apple/elsh/pkg-serialize-tables 2024-04-09 10:06:58 -07:00
Ellie Shin
c44b22a188 Serialize SIL witness-tables and v-tables and their entries if package cmo is
enabled. If two modules are in the same package and package cmo is enabled,
v-table or witness-table calls should not be generated at the use site in the
client module. Modified conformance serialization check to allow serializing
witness thunks.

Also reordered SIL functions bottom-up so the most nested referenced functions
can be serialized first. Allowed serializing a function if a shared definition
(e.g. function `print`). Added a check for resilient mode wrt struct instructions.

Added tests for SIL tables and resilient mode on/off.

rdar://124632670
2024-04-08 13:36:17 -07:00
Allan Shortlidge
ee915ddfd4 SILGen: Ignore placeholders and missing methods during conformance emission.
Builds on https://github.com/apple/swift/pull/72286.

Use a more forgiving strategy when handling potentially invalid conformances
during SILGen. Conformances may be erroneously marked invalid during witness
resolution, even when they can still be successfully emitted, so we can't bail
out of witness table emission if the invalid bit is set. Instead, just ignore
placeholders and missing methods in the witness table emitter and trust that if
an error was diagnosed during resolution that compilation will be aborted.

Resolves rdar://125947349
2024-04-08 13:20:57 -07:00
Slava Pestov
5a546ed6b0 Merge pull request #72762 from slavapestov/fix-rdar125460667
Sema: Fix existential-metatype-to-Any conversion
2024-04-02 08:30:33 -04:00
Michael Gottesman
19c72acff6 [silgen] Emit the location of the original function def if SILGen is erroring on a duplicate symbol definition.
This diagnostic is useful around silgen_name where it validates that we do not
have any weird collisions. Sadly, it just points where one of the conflicting
elements is... with this patch, we also emit an error on the other function if
we have a SILLocation.
2024-04-01 22:01:08 -07:00
Slava Pestov
90ab23ed6f SILGen: Fix invariant violation with existential conformances
When erasing a pseudogeneric value in SILGen, we actually treat
the value as if it had the type `any AnyObject`, while in Sema,
it's an archetype. This requires SILGen to look up conformances
again instead of using the ones in the ErasureExpr.
2024-04-01 23:05:35 -04:00
Michael Gottesman
1cf4e99454 Propagate global actor-ness of functions/closures.
I fixed a bunch of small issues around here that resulted in a bunch of radars
being fixed. Specifically:

1. I made it so that we treat function_refs that are from an actor isolated
function as actor isolated instead of sendable.

2. I made it so that autoclosures which return global actor isolated functions
are treated as producing a global actor isolated function.

3. I made it so that we properly handle SILGen code patterns produced by
Sendable GlobalActor isolated things.

rdar://125452372
rdar://121954871
rdar://121955895
rdar://122692698
2024-03-29 14:39:34 -07:00
Alejandro Alonso
36bf2ae3bf Merge pull request #72592 from Azoy/raw-layout-inst
[IRGen] Add Builtin.addressOfRawLayout
2024-03-29 07:38:37 -07:00
Joe Groff
233c1675cf Merge pull request #72647 from jckarter/borrowing-switch-expr-based-ownership
Alternative noncopyable switch design based on expression kind.
2024-03-28 14:44:16 -07:00
Joe Groff
ba3494802a Alternative noncopyable switch design based on expression kind.
If an expression refers to noncopyable storage, then default to performing
a borrowing switch, where `let` bindings in patterns borrow out of the
matched value. If an expression refers to a temporary value or explicitly
uses the `consume` keyword, then perform a consuming switch, where
`let` bindings take ownership of corresponding parts of the matched value.
Allow `_borrowing` to still be used to explicitly bind a pattern variable
as a borrow, with no-implicit-copy semantics for copyable values.
2024-03-28 08:32:48 -07:00
Anton Korobeynikov
d84847ac9d Reland Allow normal function results of @yield_once coroutines (#71645)
* Allow normal function results of @yield_once coroutines

* Address review comments

* Workaround LLVM coroutine codegen problem: it assumes that unwind path never returns.
This is not true to Swift coroutines as unwind path should end with error result.
2024-03-27 13:09:02 -07:00
Alejandro Alonso
5d0deaa4a3 Add Builtin.addressOfRawLayout 2024-03-27 09:47:01 -07:00
Michael Gottesman
8243b5cb74 [region-isolation] If a value is dynamically actor isolated, do not consider it transferred if the transfer statically was to that same actor isolation.
This issue can come up when a value is initially statically disconnected, but
after we performed dataflow, we discovered that it was actually actor isolated
at the transfer point, implying that we are not actually transferring.

Example:

```swift
@MainActor func testGlobalAndGlobalIsolatedPartialApplyMatch2() {
  var ns = (NonSendableKlass(), NonSendableKlass())
  // Regions: (ns.0, ns.1), {(mainActorIsolatedGlobal), @MainActor}

  ns.0 = mainActorIsolatedGlobal
  // Regions: {(ns.0, ns.1, mainActorIsolatedGlobal), @MainActor}

  // This is not a transfer since ns is already main actor isolated.
  let _ = { @MainActor in
    print(ns)
  }

  useValue(ns)
}
```

To do this, I also added to SILFunction an actor isolation that SILGen puts on
the SILFunction during pre function visitation. We don't print it or serialize
it for now.

rdar://123474616
2024-03-25 22:58:17 -07:00
Doug Gregor
14fc015d51 Ensure that we map the capture isolation into context
Failing to map the type of an isolation capture into context
caused assertions in type lowering. Fixes the build of the
swift-distributed-actors package.
2024-03-25 17:13:33 -07:00
nate-chandler
ec6d207c89 Merge pull request #72538 from nate-chandler/opaque-values/20240322/1
[OpaqueValues] Pass direct to willThrowTyped.
2024-03-25 07:06:17 -07:00
Konrad `ktoso` Malawski
6132386371 [Distributed] Complete handling of protocol calls and witnesses using adjusted mangling scheme (#72416) 2024-03-23 23:54:23 +09:00
John McCall
40d9ea598e Merge pull request #72514 from rjmccall/isolated-any-optional-conversion
Correctly apply `@isolated(any)` even when converting to an optional type
2024-03-23 05:04:18 -04:00
Nate Chandler
7887912a94 [OpaqueValues] Pass direct to willThrowTyped.
The runtime function `swift_willThrowTyped` takes its argument
`@in_guaranteed`.  In opaque values SIL, that's passed directly.  Don't
store non-address errors before passing them to the function.
2024-03-22 18:01:12 -07:00
John McCall
15b5dcb870 Simplify the representation of conversions to make it easier to extract
the source/result/lowered-result types.
2024-03-22 17:04:25 -04:00
John McCall
79d1d684be Just replace the inner abstraction pattern when combining a subtype conversion
into a reabstraction.
2024-03-22 17:03:04 -04:00
Adrian Prantl
9b0c4104ef Merge pull request #72397 from Snowy1803/verifier-conflicting-debug-value-types
[SILVerifier] Add detection of conflicting debug variables
2024-03-22 11:31:05 -07:00
nate-chandler
d0825adaa5 Merge pull request #72494 from nate-chandler/rdar125182396
[SILGen] Load trivial in consuming switch.
2024-03-22 07:41:56 -07:00
John McCall
3e64f9d69f Push isolated(any) conversions down through multiple levels of conversion.
I'm not sure there's any way to test this right now.  We don't naturally
emit multiple function conversions on a single operand, and the only way
to get a similar effect is to coerce, which interrupts the application of
`@_inheritActorContext`.  So I think this is dead code until we add closure
isolation controls, and even then it might be dead unless we allow coercion
of isolated closures, which maybe we won't.  But it's the right thing to do
in the abstract, and I was thinking of it now.
2024-03-22 02:20:44 -04:00
John McCall
a040df9aa8 Teach optional injection to peephole into more converting contexts
This allows us to propagate abstraction patterns from optional parameters
all the way to closure emission, which optimizes some code patterns but
(more importantly) propagates our knowledge that we're converting to an
`@isolated(any)` function type down to closure emission, allowing us to
set the isolation correctly.

There are still some conversion cases missing --- if we can't combine
conversions for whatever reason, we should at least shift our knowledge
that we need to produce an `@isolated(any)` type down, the same way that
we shift it when emitting a closure that we can't directly emit in the
target abstraction pattern.  But this completes the obvious cases of
peepholing for closure emission.
2024-03-22 01:19:29 -04:00
John McCall
783dd2050f [NFC] Set up the conversion peephole to turn back into normal conversions. 2024-03-21 18:10:52 -04:00
Joe Groff
f5128d57b9 Merge pull request #72464 from jckarter/init-conditionally-copyable-empty-type
SILGen: Emit empty type initialization for conditionally-copyable type inits.
2024-03-21 15:08:41 -07:00
Nate Chandler
12c91d8dc0 [SILGen] Load trivial in consuming switch.
When emitting a consuming switch, the load of a trivial payload must be
trivial.

rdar://125182396
2024-03-21 11:51:17 -07:00
John McCall
16093848fc [NFC] Work in preparation of generalizing how we combine conversions 2024-03-21 12:47:56 -04:00
John McCall
c7d3e8f559 [NFC] Generalize how Conversion works with reabstraction conversions and
force callers to specify the input lowered type as well.
2024-03-21 12:47:56 -04:00
Emil Pedersen
04ab38056a [DebugInfo] Remove conflicting debug info in switch
For address-only types, a temporary was emitted with the same debug variable
and same scope as the instruction it is moved to after entering a shared case,
but it would have a different type, which would create a conflict.
The better way to fix this would probably to use a different scope for both,
but the variable is moved immediately anyway.
2024-03-20 15:35:52 -07:00
Emil Pedersen
0cff76e82c [DebugInfo] [SILGen] Remove invalid debug info from intermediates in prolog 2024-03-20 15:35:33 -07:00
Joe Groff
d9bd92e226 SILGen: Emit empty type initialization for conditionally-copyable type inits.
Fix the check here to go by `isMoveOnly` on the lowered type rather than
`canBeCopyable` on the declaration so that we correctly emit the
semantic initialization for types that are `~Copyable` in their most
generic form. Fixes rdar://125101955.
2024-03-20 14:14:25 -07:00
Holly Borla
51c1543eb2 Merge pull request #72332 from hborla/isolated-default-value-crash
[Concurrency] Don't attempt to hop to executor inside default argument generators and stored property initializers.
2024-03-14 18:44:08 -07:00
Meghana Gupta
3d4457b632 Merge pull request #72303 from meg-gupta/implicitreturn
Updates to initializers with explicit lifetime dependence
2024-03-14 10:11:59 -07:00
Holly Borla
2260957ac5 [Concurrency] Don't attempt to hop to executor inside default argument
generators and stored property initializers.
2024-03-14 09:33:55 -07:00
Holly Borla
0a627bc44c Merge pull request #72324 from hborla/extract-function-isolation-expr
[Concurrency] Add a `.isolation` member on dynamically isolated function values.
2024-03-14 06:56:43 -07:00
Holly Borla
7e0f534a5a [Concurrency] SILGen for ExtractFunctionIsolationExpr. 2024-03-13 22:23:31 -07:00
Holly Borla
78384d596d [Concurrency] Add ExtractFunctionIsolationExpr to represent the isolation
of a dynamically isolated function value in the AST.
2024-03-13 19:55:15 -07:00
Doug Gregor
685b493ac3 Merge pull request #72305 from DougGregor/silgen-thrown-error-closures
[SILGen] Correctly determine the thrown error type for closures
2024-03-13 18:28:29 -07:00
Doug Gregor
f54782eab5 [SILGen] Correctly determine the thrown error type for closures
When a closure throws a generic error type, we were retrieving the
substituted error type (involving archetypes) when we needed to be
working with the interface type.

Fixes rdar://124484012.
2024-03-13 12:51:48 -07:00
eeckstein
a24539171c Merge pull request #72265 from eeckstein/fix-mandatory-inlining
MandatoryInlining and ConstExpr: look through sendable function conversions
2024-03-13 18:52:43 +01:00
Meghana Gupta
c0847e33ac Remove diagnostic error when 'return self' is missing in an initializer with explicit lifetime dependence
Also, handle it similar to other initializers in SILGen and ASTVerifier
2024-03-13 10:34:37 -07:00
Allan Shortlidge
d29f6a1a85 Merge pull request #72286 from tshortli/silgen-lazy-typecheck-conformance-errors
SILGen: Avoid crashing for invalid conformances
2024-03-13 09:25:18 -07:00
Joe Groff
b0655407c6 Merge pull request #72281 from jckarter/moveonly-optional-chain-3
SIL: Optimizer fixes for noncopyable optional chains.
2024-03-13 08:11:39 -07:00
Erik Eckstein
3c76464c1c rename withConcurrent -> withSendable
That was missed when "concurrent" was renamed to "sendable"
2024-03-13 09:58:31 +01:00
Allan Shortlidge
f6dd5245b0 SILGen: Increase test coverage for lazy typechecking.
None of these additional test cases actually uncovered any problems but it
seems valuable to have them.

Also, add pretty stack traces for a couple paths through SILGen.
2024-03-12 22:15:22 -07:00
Allan Shortlidge
64dc2e9f33 SILGen: Avoid crashing for invalid conformances.
Force resolution of value witnesses and check the conformance for validity
before proceeding to witness table emission in SILGen to avoid crashing because
of unexpected errors in the AST.

Resolves rdar://123027739
2024-03-12 21:57:35 -07:00
Joe Groff
8a65c9be94 SIL: Optimizer fixes for noncopyable optional chains.
Don't attempt a SILCombine transform on `select_enum` that inserts a copy when an
enum is noncopyable. Adjust the cleanup handling for a consuming optional chain to
ensure a `destroy_value` still gets emitted on the `none` path; this shouldn't
actually matter since `none` is a trivial case, but the memory verifier isn't
that fancy during OSSA, and we can optimize it later. Fixes rdar://124426918.
2024-03-12 18:11:21 -07:00
John McCall
27e0edfacc Merge pull request #72244 from rjmccall/builtin-initial-serial-executor
Add builtin support for starting a task on a specific executor
2024-03-12 18:54:29 -04:00