Commit Graph

27 Commits

Author SHA1 Message Date
Allan Shortlidge
02dbb96b94 AST: Rename AvailabilityContext to AvailabilityRange.
The generality of the `AvailabilityContext` name made it seem like it
encapsulates more than it does. Really it just augments `VersionRange` with
additional set algebra operations that are useful for availability
computations. The `AvailabilityContext` name should be reserved for something
pulls together more than just a single version.
2024-09-13 16:25:18 -07:00
Allan Shortlidge
f7ff3f0001 SILGen: Introduce macCatalyst support for @backDeployed.
Upstreams the necessary changes to compile references to `@backDeployed`
declarations correctly when a `macabi` target triple or a `-target-variant` is
specified.
2024-07-23 17:00:10 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -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
cui fliter
127077b3aa chore: fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 17:23:22 +08:00
Nate Cook
e317febc9d Revert "Allow normal function results of @yield_once coroutines (#69843)"
This reverts commit aa5b505014.
2024-02-07 14:57:31 -06:00
Anton Korobeynikov
aa5b505014 Allow normal function results of @yield_once coroutines (#69843)
This adds SIL-level support and LLVM codegen for normal results of a coroutine.

The main user of this will be autodiff as VJP of a coroutine must be a coroutine itself (in order to produce the yielded result) and return a pullback closure as a normal result.

For now only direct results are supported, but this seems to be enough for autodiff purposes.
2024-02-06 22:13:15 -08:00
Doug Gregor
76a89500d6 [SILGen] Handle indirect errors in other thunk kinds 2023-11-10 15:30:17 -08:00
Slava Pestov
581776b997 SILGen: Emit indirect error result argument 2023-10-27 17:37:43 -04:00
Doug Gregor
7d7c726efe [Typed throws] Teach associated type inference to infer from thrown errors
When comparing a requirement that uses typed throws and uses an
associated type for the thrown error type against a potential witness,
infer the associated type from the thrown error of the
witness---whether explicitly specified, untyped throws (`any Error`),
or non-throwing (`Never`).
2023-10-09 21:21:21 -07:00
Doug Gregor
b6b999abd4 [Typed throws] Basic SIL lowering and SIL generation for typed throws
Lower the thrown error type into the SIL function type. This requires
very little code because the thrown error type was already modeled as
a SILResultInfo, which carries type information. Note that this
lowering does not yet account for error types that need to passed
indirectly, but we will need to do so for (e.g.) using resilient error
types.

Teach a few places in SIL generation not to assume that thrown types
are always the existential error type, which primarily comes down to
ensuring that rethrow epilogues have the thrown type of the
corresponding function or closure.

Teach throw emission to implicitly box concrete thrown errors in the
error existential when needed to satisfy the throw destination. This
is a temporary solution that helps translate typed throws into untyped
throws, but it should be replaced by a better modeling within the AST
of the points at which thrown errors are converted.
2023-09-29 10:51:55 -07:00
Michael Gottesman
543063ef0e [silgen] Make ManagedValue::forUnmanaged private and change users to use other more specific APIs.
I have been doing this over the past couple of days in preparation for changing
ManagedValue to specify the type of scope its cleanup is connected to.
2023-08-19 14:11:44 -07:00
Allan Shortlidge
d062489344 SILGen: Don't reference external property descriptors for @backDeployed properties.
The property descriptors of `@backDeployed` properties aren't available on OSes
prior to the "back deployed before" OS version, so the descriptors cannot be
referenced by clients that may run against older versions of the library
that defines the property.

See https://github.com/apple/swift/pull/59214 for prior art.

Resolves rdar://106517386
2023-08-16 23:19:15 -07:00
John McCall
939270c573 [NFC] More prep work for sharing code between param and result reabstraction 2023-08-14 19:06:49 -04:00
Allan Shortlidge
d2524a6de8 AST: Implement parsing support for the accepted spelling of @backDeployed for SE-0376.
For source compatibility `@_backDeploy` continues to be accepted as a spelling.

rdar://102792909
2023-02-01 22:04:33 -08:00
Allan Shortlidge
5b942eb18b SILGen/Sema: Add @_backDeploy support for constructors.
Resolves rdar://94436652
2023-01-21 18:04:54 -08:00
Allan Shortlidge
9f54e9cd1f SILGen: Fix double-free of __owned parameters of functions with @_backDeploy.
The following program crashed when compiled with the Swift 5.7 and 5.8 compilers:

```
@available(macOS 12, *)
@_backDeploy(before: macOS 99)
public func foo<T>(_ t: __owned T) {
  print("foo")
}

class C {
  deinit {
    print("deinit")
  }
}

foo(C())
print("done")
```

```
> ./test
foo
deinit
[1]    49162 segmentation fault  ./test
```

The root cause is that generated SIL for the back deployment thunk for `foo(_:)` included its own `destroy_addr` instruction for the value of `t`, but didn't copy the parameter before passing it to the real function implementation which also destroys the value. The fix is to forward ownership of the parameter values to the called function, which causes cleanup generation to be skipped.

Resolves rdar://104436515
2023-01-19 13:12:02 -08:00
Nate Chandler
921c6a0e34 [OpaqueValues] Map back-deploy thunk results.
When a throwing function which returns a generic (this is the simplest
example) is back deployed, it gets a back deploy thunk.  This back
deploy thunk calls one of two variations of the function, depending on
availability, both which have the same signature and in particular both
return a generic.  The result type of that function signature is an
unbound generic.

Previously, that result type was used as is.  Consequently the success
blocks for the try_apply instructions in these thunks had arguments of
unbound generic type.

Here, that type is mapped into the context of the function being
emitted.  The thunks now have the appropriate bound generic type.
2023-01-04 16:11:49 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
swift-ci
fd6fd76dae Merge pull request #42513 from jsoref/spelling-silgen
Spelling silgen
2022-11-09 23:28:43 -08:00
Josh Soref
9a6bf46c0f Spelling silgen
* actually
* arbitrary
* cargo-culted
* clazz
* constrained
* continuation
* coordinator
* coroutine
* derivative
* destroyer
* given
* have
* imported
* initialization
* items
* necessarily
* occurring
* omitting
* overridden
* parameter
* possible
* predecessor
* preparation
* resilience
* should
* struct
* that
* the
* throwing
* unexpectedly
* uniqueness
* using
* value
* villain

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2022-11-09 21:44:17 -05:00
Allan Shortlidge
6b61e37364 SILGen: Function availability for linkage should use @_backDeploy OS version if present.
When computing the availability of a `SILFunction` for linkage the OS version specified in the `@_backDeploy` attribute should be preferred over the version in the `@available` attribute. This ensures that the corresponding symbol is weakly linked when deploying to older OSes than the back deploy "before" version.

Resolves rdar://99962885
2022-09-16 17:05:35 -07:00
Allan Shortlidge
2777040ecc SILGen: Fix potential use-after-free in @_backDeploy coroutines.
The SIL verifier has identified an issue with the SIL generated for property accessors structured like this:

```
public struct S {
  @available(macOS, introduced: 12.0)
  @_backDeploy(before: macOS 13.0)
  public var x: String {
    _read {
      yield "x"
    }
  }
}
```

The emitted SIL is invalid because the value `%9` is used after `end_apply` may have ended the lifetime of the value:

```
bb1:
  %8 = function_ref @$s4test1SV1xSSvrTwB : $@yield_once @convention(method) (S) -> @yields @guaranteed String9
  (%9, %10) = begin_apply %8(%0) : $@yield_once @convention(method) (S) -> @yields @guaranteed String
  end_apply %10
  yield %9 : $String, resume bb3, unwind bb2
```

The fix is to move the `end_apply` to the resume and unwind blocks, after the value has been yielded to the caller.

Resolves rdar://96879247
2022-07-12 11:49:21 -07:00
Allan Shortlidge
019a345932 SILGen: Generate appropriate SIL for @_backDeploy thunks that wrap coroutine functions (such as the _read and _modify accessors). Remove the temporary diagnostics that rejected use of @_backDeploy on decls that would result in coroutines. 2022-03-24 15:42:14 -07:00
Allan Shortlidge
d203876d36 SILGen: Use the OS version specified in the @_backDeploy attribute instead of the version specified in the @available attribute when emitting the if #available condition in back deployment thunks. 2022-02-25 17:51:12 -08:00
Allan Shortlidge
5339dae08e SILGen: Emit a fallback variant of a back deployable function and call it when the original function is not available at run time. 2022-02-23 18:07:18 -08:00
Allan Shortlidge
97e8e31b5c SILGen: Emit a thunk that wraps the call to a back deployed function. The thunk calls the original function if it is available at runtime and otherwise falls back to calling a copy that is emitted into the client (not yet implemented). 2022-02-17 11:28:12 -08:00