Commit Graph

2410 Commits

Author SHA1 Message Date
Slava Pestov
1cad5f9350 SIL: Plumb pack element captures through type lowering 2024-05-02 13:40:54 -04:00
Slava Pestov
70fb18e799 SIL: Rename misleading getFunctionInterfaceTypeWithCaptures() 2024-05-02 13:40:54 -04:00
Slava Pestov
9e13db09e0 SIL: buildThunkType() uses (new) LocalRequirementArchetypeCollector 2024-05-01 15:35:24 -04:00
Slava Pestov
865b155f2e SIL: Thunks that capture local environments must also capture the primary environment
I suspect there are latent bugs here with generic-class-constrained existentials,
same-element requirements involving packs, etc. We can't assume that the
local archetypes don't have "hidden" dependencies on the outer parameters
that are not encoded in the thunk's interface type.
2024-04-30 22:45:41 -04:00
Slava Pestov
97d95218f8 AST: Record captured generic environments in CaptureInfo 2024-04-30 21:38:15 -04:00
Emil Pedersen
f6232a937d [DebugInfo] Fix wrong scope being used in AllocBoxToStack 2024-04-30 16:29:49 -07:00
Akira Hatanaka
428fe21855 [SILGen] Fix a bug where the wrong convention was being used for computing the type of a closure thunk (#73299)
The ObjC selector family convention was being used instead of the C
function type convention.

rdar://127090209
2024-04-29 18:03:59 -07:00
Nate Chandler
5307ac666e [SIL] Permit end_borrow(begin_apply). 2024-04-25 17:03:41 -07:00
Nate Chandler
121b2faccc [NFC] SIL: Add ValueBase::isBeginApplyToken.
Forwards to the member function of the same name on
MultipleValueInstructionResult.
2024-04-25 17:03:11 -07:00
Nate Chandler
b1fbe4ea91 [BitwiseCopyable] Remove underscore. 2024-04-25 11:44:15 -07:00
Nate Chandler
ed5c7ef7ae [BitwiseCopyable] Promote to feature.
SE-0426 was accepted.
2024-04-24 15:52:20 -07:00
Michael Gottesman
025902fa99 [sil] Add a SILDeclRef field to SILFunction, have SILGen set it, and teach SILFunction how to use the field to get the SourceFile associated with the SILFunction. 2024-04-23 11:45:36 -05:00
Yuta Saito
ae74a57345 Merge pull request #73162 from kateinoigakukun/yt/indirect-err-result-conv 2024-04-23 00:50:02 +09:00
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
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
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
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
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
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
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
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
Alejandro Alonso
f5e10e2c0b Get rid of sil instruction and use a builtin for addressOfRawLayout 2024-03-27 11:36:29 -07:00