Commit Graph

9332 Commits

Author SHA1 Message Date
Slava Pestov
e342a38b87 Sema: Convert TypeChecker::computeCaptures() into two requests
We now compute captures of functions and default arguments
lazily, instead of as a side effect of primary file checking.

Captures of closures are computed as part of the enclosing
context, not lazily, because the type checking of a single
closure body is not lazy.

This fixes a specific issue with the `-experimental-skip-*` flags,
where functions declared after a top-level `guard` statement are
considered to have local captures, but nothing was forcing these
captures to be computed.

Fixes rdar://problem/125981663.
2024-04-20 22:16:25 -04:00
Yuta Saito
eeb62b056b [SILGen] Create thunks for non-throwing -> indirect error result conversions
When a non-throwing function is passed to a function that expects a
throwing function with an indirect error result, we need to create a
thunk because the indirect error result pointer is passed as an extra
trailing parameter. Extra trailing parameters except for `swifterror`
and `swiftself` are not allowed on some platforms like WebAssembly, so
they need to be called as exactly the same function signature.

```swift
func passThrough<X>(_ c: () throws(X) -> Void) throws(X) {
  try c()
}
func neverThrow() {}
passThrough(neverThrow)
```
2024-04-20 06:45:15 +00:00
Nate Chandler
d11d97d210 [FSPL] Use autogen'd loc. 2024-04-19 12:38:11 -07:00
Nate Chandler
d28a12d356 [MoveOnlyAddressChecker] Handle enum deinits.
Previously, the enum representation was fixed to represent the different
cases payloads separately with the unchecked_take_enum_data_addr
instruction consuming all fields but that whose address is obtained.
In a few places, handling for enum deinits was left undone with an
assertion that the enum not have one.

Here, the deinit bit of the enum is shifted to the end.  And the
assertions are replaced with handling.  Finally, the logic for inserting
destroys after switch_enum_addr instructions is fixed.
2024-04-19 12:37:34 -07:00
Slava Pestov
3e637b0afb Merge pull request #73129 from slavapestov/local-function-isolation-change
Local functions inherit isolation from context instead of captures
2024-04-19 08:32:46 -04:00
Slava Pestov
05bcc25f7b SIL: Local functions should inherit isolation from context
If the outer context is actor isolated, we need to force a capture the
actor variable, since otherwise it might not appear in the capture list.
2024-04-18 23:26:17 -04:00
Slava Pestov
24857abf73 SIL: Remove unused DeclContext in getLoweredLocalCaptures() 2024-04-18 23:07:06 -04:00
Slava Pestov
d222470a0b AST: Remove unused methods from CaptureInfo 2024-04-18 22:47:23 -04:00
Ellie Shin
0c0d8c0316 Merge branch 'main' into elsh/pkg-sil-verify 2024-04-18 13:34:29 -07:00
nate-chandler
b00b5aad4f Merge pull request #72646 from nate-chandler/bitwise-copyable/20240327/1
[BitwiseCopyable] Allow suppression via ~.
2024-04-18 07:05:10 -07:00
Ellie Shin
45e8454a9e Merge branch 'main' into elsh/pkg-sil-verify 2024-04-17 22:46:55 -07:00
Ellie Shin
fbb3382e21 During Package CMO, SIL cloning happens during which
SILOptions::EnableSerializePackage info is lost.

SILVerifier needs this info to determine whether resilience
can be bypassed for decls serialized in a resiliently
built module when Package CMO optimization enabled.

This PR adds SerializePackageEnabled bit to Module format
and uses that in SILVerifier.

Resolves rdar://126157356
2024-04-17 22:37:48 -07:00
Akira Hatanaka
d76dbb1a64 [SILGen] Fix a bug where the wrong convention was being used for lowering a closure to a C++ function pointer (#73039)
Use the C function pointer convention instead of the block convention.

rdar://122977380
2024-04-16 15:18:50 -07:00
Adrian Prantl
6357d12cfc Merge pull request #73009 from Snowy1803/debuginfo-small-fixes
[Debuginfo] Small fixes
2024-04-17 00:03:53 +02:00
Emil Pedersen
96cf2331b5 [SILPrinter] Hide VarInfo debug info if printing debuginfo is disabled 2024-04-16 10:28:13 -07:00
Nate Chandler
eb1f0ac68a [BitwiseCopyable] Suppress via ~.
In addition to the existing language mechanism of
`@available(*, unavailable)`.
2024-04-15 16:46:24 -07:00
Erik Eckstein
96cf03fe95 OwnershipUtils: fix a warning
Removed a left over from the work on the borrowed-from instruction
2024-04-15 12:18:05 +02:00
eeckstein
e871ae40c5 Merge pull request #71176 from eeckstein/borrowed-from-instruction
SIL: add the borrowed-from instruction
2024-04-10 19:33:12 +02:00
Kuba (Brecka) Mracek
6e4ccca2db Merge pull request #72817 from kubamracek/embedded-print-on-assert
[embedded] In -assert-config Debug, print errors in assertions, preconditions, fatalErrors
2024-04-10 07:52:02 -07:00
Erik Eckstein
e14c1d1f62 SIL, Optimizer: update and handle borrowed-from instructions
Compute, update and handle borrowed-from instruction in various utilities and passes.
Also, used borrowed-from to simplify `gatherBorrowIntroducers` and `gatherEnclosingValues`.
Replace those utilities by `Value.getBorrowIntroducers` and `Value.getEnclosingValues`, which return a lazily computed Sequence of borrowed/enclosing values.
2024-04-10 13:38:10 +02:00
Erik Eckstein
ac4bc89c9a SIL: add the borrowed-from instruction.
It declares from which enclosing values a guaranteed phi argument is borrowed from.
2024-04-10 13:38:10 +02:00
Andrew Trick
38b78ed057 Merge pull request #72866 from atrick/fix-nonescapable-lowering
Fix type lowering of ~Copyable and ~Escapable generics.
2024-04-09 21:56:15 -07:00
Kuba Mracek
1bf38aa780 [embedded] Relax assert in SILLinkerVisitor::maybeAddFunctionToWorklist to allow external forward-declarations even in regular Swift, add tests 2024-04-09 11:36:37 -07:00
Kuba Mracek
757e4e56b0 [embedded] In -assert-config Debug, print errors in assertions, preconditions, fatalErrors 2024-04-09 11:36:21 -07:00
Ellie Shin
3bb25dec11 Merge pull request #72606 from apple/elsh/pkg-serialize-tables 2024-04-09 10:06:58 -07:00
Erik Eckstein
1b1d5ed020 IRGen: support the @sensitive attribute
Call `swift_clearSensitive` after destroying or taking "sensitive" struct types.

Also, support calling C-functions with "sensitive" parameters or return values. In SIL, sensitive types are address-only and so are sensitive parameters/return values.
Though, (small) sensitive C-structs are passed directly to/from C-functions. We need re-abstract such parameter and return values for C-functions.
2024-04-09 12:01:11 +02: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
Andrew Trick
a90b515df9 Fix type lowering of ~Escapable generics.
Factor AbstractionPattern::conformsToKnownProtocol and lower ~Escapable using the same logic as ~Copyable.

Adds support for conditionally Escapable enums.

Correctly sets the SILType::isTrivial flags for conditionally escapable structs and enums in environments (extensions)
that provide an Escapable conformance, such as:

    struct NE<E: ~Escapable> : ~Escapable {}

    extension NE: Escapable {
      func foo() -> Self {
        // Self is both Escapable and trivial here.
        self
      }
    }

Fixes rdar://125950218 ([nonescapable] support conditionally escapable enums)
2024-04-07 22:58:28 -07:00
Andrew Trick
22646bd07c Fix AbstractionPattern::isNoncopyable to handle archetypes.
This fixes TypeLowering for ~Copyable generics, such as:

struct S<T: ~Copyable>: ~Copyable {
  var x: T
}

extension S: Copyable where T: Copyable  {}

func foo<T>(s: S<T>) -> ()

Previously, TypeLowering would ignore the implicit Copyable
requirement on the archetype 'T'.
2024-04-07 20:31:55 -07:00
Emil Pedersen
512ac6cc31 Merge pull request #72860 from Snowy1803/salvage-debug-info-tuple
[DebugInfo] Salvage debug info for tuples
2024-04-05 20:16:24 -07:00
Slava Pestov
4a27afa79f Merge pull request #72885 from slavapestov/parameter-packs-and-parameterized-existentials
Two fixes for parameter packs and parameterized existentials
2024-04-05 22:09:11 -04:00
Slava Pestov
b073165c75 SIL: Correctly unwrap one-element tuples in TypeConverter::computeLoweredRValueType()
Fixes rdar://problem/123978349.
2024-04-05 17:02:48 -04:00
Slava Pestov
480592a7d5 Merge pull request #72847 from slavapestov/fix-rdar123645784
Fix two problems with opaque return types -vs- parameter packs
2024-04-05 07:31:34 -04:00
Anton Korobeynikov
c7a216058f [AutoDiff] First cut of coroutines differentiation (#71461)
This PR implements first set of changes required to support autodiff for coroutines. It mostly targeted to `_modify` accessors in standard library (and beyond), but overall implementation is quite generic.

There are some specifics of implementation and known limitations:
 - Only `@yield_once` coroutines are naturally supported
 - VJP is a coroutine itself: it yields the results *and* returns a pullback closure as a normal return. This allows us to capture values produced in resume part of a coroutine (this is required for defers and other cleanups / commits)
 - Pullback is a coroutine, we assume that coroutine cannot abort and therefore we execute the original coroutine in reverse from return via yield and then back to the entry
 - It seems there is no semantically sane way to support `_read` coroutines (as we will need to "accept" adjoints via yields), therefore only coroutines with inout yields are supported (`_modify` accessors). Pullbacks of such coroutines take adjoint buffer as input argument, yield this buffer (to accumulate adjoint values in the caller) and finally return the adjoints indirectly.
 - Coroutines (as opposed to normal functions) are not first-class values: there is no AST type for them, one cannot e.g. store them into tuples, etc. So, everywhere where AST type is required, we have to hack around.
 - As there is no AST type for coroutines, there is no way one could register custom derivative for coroutines. So far only compiler-produced derivatives are supported
 - There are lots of common things wrt normal function apply's, but still there are subtle but important differences. I tried to organize the code to enable code reuse, still it was not always possible, so some code duplication could be seen
 - The order of how pullback closures are produced in VJP is a bit different: for normal apply's VJP produces both value and pullback closure via a single nested VJP apply. This is not so anymore with coroutine VJP's: yielded values are produced at `begin_apply` site and pullback closure is available only from `end_apply`, so we need to track the order in which pullbacks are produced (and arrange consumption of the values accordingly – effectively delay them)
 - On the way some complementary changes were required in e.g. mangler / demangler

This patch covers the generation of derivatives up to SIL level, however, it is not enough as codegen of `partial_apply` of a coroutine is completely broken. The fix for this will be submitted separately as it is not directly autodiff-related.

---------

Co-authored-by: Andrew Savonichev <andrew.savonichev@gmail.com>
Co-authored-by: Richard Wei <rxwei@apple.com>
2024-04-04 17:24:55 -07:00
Emil Pedersen
efa09088ac Revert "[DebugInfo][SIL] Introduce the 'implicit' attribute for debug variable"
This reverts commit e63632fda8.

rdar://125939953
2024-04-04 17:23:32 -07:00
Slava Pestov
082246094b AST: Pass SubstFlags::PreservePackExpansionLevel in a few more places 2024-04-04 19:36:25 -04:00
Andrew Trick
6aa07ff9bf Merge pull request #72777 from atrick/fix-lifedep-throw
LifetimeDependence: handle dependent values in throwing calls.
2024-04-04 08:06:31 -07:00
Andrew Trick
581010d00c SIL: Make drop_deinit a ForwardingInstruction.
drop_deinit forwards ownership while effectively stripping the deinitializer. It is similar to a type cast.

Fixes rdar://125590074 ([NonescapableTypes] Nonescapable types
cannot have deinits)
2024-04-03 18:53:18 -07:00
Meghana Gupta
0cddd69415 Merge pull request #72780 from meg-gupta/enablesilcombineossa
Enable inject_enum_addr silcombine in ossa
2024-04-03 15:59:22 -07:00
Meghana Gupta
b6ed0d41a7 Handle switch_value in MemoryLifetimeVerifier 2024-04-03 10:30:55 -07:00
Meghana Gupta
c30eea156f Verify switch_value has int operands only 2024-04-03 10:30:55 -07:00
Kuba (Brecka) Mracek
7553c41705 Merge pull request #72690 from kubamracek/embedded-subclassing-across-modules
[embedded] Relax verifier check on hidden-external functions in embedded mode
2024-04-02 10:47:49 -07:00
Andrew Trick
44a2661644 Merge pull request #72764 from atrick/fix-address-yield
Fix LifetimeDependenceDefUseWalker for address yields.
2024-04-02 08:32:51 -07:00
Andrew Trick
a1bb9f401a Fix SILVerifier and AddressUtils handling of addr casts.
These core utils did not handle UnconditionalCheckedCastAddrInst or
UncheckedRefCastAddrInst correctly.
2024-04-01 21:46:10 -07:00
Slava Pestov
32dab83420 SIL: Stronger assertions in InitExistential{Addr,Value,Ref}Inst 2024-04-01 23:05:35 -04: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
Kuba Mracek
a54dfe8b8e [embedded] Relax verifier check on hidden-external functions in embedded mode 2024-03-28 17:57:54 -07:00
nate-chandler
a91bfcc376 Merge pull request #72600 from nate-chandler/rdar125443922
[BitwiseCopyable] Loosen validation assertion.
2024-03-27 15:27:19 -07:00
Pavel Yaskevich
d3540b4838 Merge pull request #71376 from simanerush/pack-argument-fix
SIL: Adjust `isTypeMetadataForLayoutAccessible()` to visit more lowered positions
2024-03-27 13:44:22 -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