Commit Graph

804 Commits

Author SHA1 Message Date
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
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
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
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
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
Ben Barham
f292ec9784 Use the new template deduction guides rather than makeArrayRef
LLVM has removed `make*ArrayRef`, migrate all references to their
constructor equivalent.
2024-02-23 20:04:51 -08:00
John McCall
5c2cd18a5b Teach SILGen to try to peephole a function conversion into the emission of
a closure expression, then don't actually do it.  The long term plan is
to actually do this, which should just be a matter of taking some of the
code out of reabstraction thunk emission and using it in prolog/epilog/return
emission.  In the short term, the goal is just to get the conversion
information down to the closure emitter so that we can see that we're
erasing into an `@isolated(any)` type and then actually erase the
closure's isolation properly instead of relying on type-based erasure,
which can't handle parameter/capture isolation correctly.
2024-02-23 02:59:39 -05:00
Erik Eckstein
3488cd364f SIL: remove instruction leaks checking
It's not thread safe and can cause false alarms in case multiple modules exist in different threads. E.g. when building swiftmodules from interfaces.
The leaking check is not important anymore because the builder APIs enforce that instructions are not leaking.
I.e. it's not possible to create an instruction without inserting it into a basic block. Also, it's not possible to remove an instruction from a block without deleting it.

rdar://122169263
2024-02-22 14:09:11 +01:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Doug Gregor
81ffafdc6a Merge pull request #70602 from ApolloZhu/macro/expression-as-default-argument
[Macros] Expression macro as caller-side default argument
2024-02-14 16:10:11 -08:00
Ellie Shin
f5150e7265 Remove AbstractStorageDecl::isFormallyResilient.
Update DeclContext::bypassResilienceInPackage.
2024-02-13 19:09:31 -08:00
Slava Pestov
06b1aee360 Evaluator: Cache circular evaluation to avoid redundant diagnostics
Previously, if a request R evaluated itself N times, we would emit N
"circular reference" diagnostics. These add no value, so instead let's
cache the user-provided default value on the first circular evaluation.

This changes things slightly so that instead of returning an
llvm::Expected<Request::OutputType>, various evaluator methods take
a callback which can produce the default value.

The existing evaluateOrDefault() interface is unchanged, and a new
evaluateOrFatal() entry point replaces
llvm::cantFail(ctx.evaluator(...)).

Direct callers of the evaluator's operator() were updated to pass in
the callback. The benefit of the callback over evaluateOrDefault() is
that if the default value is expensive to constuct, like a dummy
generic signature, we will only construct it in the case where a
cycle actually happened, otherwise we just delete the callback.

(cherry picked from commit b8fcf1c709efa6cd28e1217bd0efe876f7c0d2b7)
2024-02-09 16:02:24 -08:00
Apollo Zhu
b09a22a9a0 Somewhat working
Test shadowed variable of same type

Fully type check caller side macro expansion

Skip macro default arg caller side expr at decl primary

Test macro expand more complex expressions

Set synthesized expression as implicit

Add test case for with argument, not compiling currently

Test with swiftinterface

Always use the string representation of the default argument

Now works across module boundary

Check works for multiple files

Make default argument expression work in single file

Use expected-error

Disallow expression macro as default argument

Using as a sub expression in default argument still allowed as expression macros behave the same as built-in magic literals
2024-02-06 15:02:11 -08:00
Ellie Shin
72a7760027 Support package SIL linkage.
Decls with a package access level are currently set to public SIL
linkages. This limits the ability to have more fine-grained control
and optimize around resilience and serialization.
This PR introduces a separate SIL linkage and FormalLinkage for
package decls, pipes them down to IRGen, and updates linkage checks
at call sites to include package linkage.

Resolves rdar://121409846
2024-02-06 01:23:14 -08:00
Slava Pestov
af3ab14986 SILGen: Clean up substitution map construction to deal with Copyable 2024-01-31 21:55:52 -05:00
Ian Anderson
288f37b2c1 [Concurrency] [shims] Don't declare exit in the concurrency shims
Don't delete the OS declaration of `exit` because the concurrency shims aren't always imported, and so the shim declaration might not always be available.
Don't override the OS declaration of `exit` in the concurrency shims since we can't just delete the OS one. Instead, set up internal shims just for building Concurrency that forward declares `exit`.
2024-01-29 16:25:46 -08:00
Alex Lorenz
7b2d7124b3 [SILGen] do not clear parent module when emitting stored initializer function
When SILGen was emitting the stored initializer function, it updated the location to a new autogenerated location tied to the initialization expression. That in turn reset the function's parent module to null, as the initialization expression is not a decl, and thus it didn't produce a decl context SIL could use when looking for the parent module. That lead to a this function having no parent module when it was serialized, and thus a user that used this function from code in another Swift module thought that this function should use 'dllimport' on Windows, even when the parent module was built with as the static library (i.e. using the -static flag). This lead to spurious LNK4217 diagnostics when building the Swift package manager using CMake on windows. This change fixes that by preserving the parent module for such function.
2024-01-25 15:09:44 -08:00
Kavon Farvardin
b4985f7fde [NFC] replace canBeNoncopyable
First, "can have an absence of Copyable" is a rather confusing notion,
so the query is flipped to "can be Copyable". Next, it's more robust to
ask if a conformance exists for the TypeDecl to answer that question,
rather than trying to replicate what happens within that conformance
lookup.

Also renames `TypeDecl::isEscapable` to match.
2024-01-23 22:42:38 -08:00
Pavel Yaskevich
b4dbd97c34 Merge pull request #71064 from xedin/refactor-intrinsics-lookup-into-request
[SILGen] NFC: Convert intrinsic function lookup into a request
2024-01-23 09:18:04 -08:00
Pavel Yaskevich
9e056a1fa2 [SILGen] NFC: Convert intrinsic function lookup into a request 2024-01-22 11:25:49 -08:00
Slava Pestov
be3539474e Merge pull request #70835 from jkshtj/main
Skip setting type expansion context for captures of skipped functions
2024-01-19 12:50:33 -05:00
Kshitij
580e83e48e Skip setting type expansion context for captures of skipped functions
Type-checking and SILGen for non-inlinable functions is skipped in the
presence of `-experimental-skip-non-inlinable-function-bodies` and
`-experimental-skip-non-inlinable-function-bodies-without-types` flags.

Such functions may be top-level and may contain captures (if they appear
after a `guard` statement), for which we were setting the type expansion
context during SILGen. Setting type expansion context however, relied on
the capture info being computed -- which was never happening because of
the abovementioned flags.

Changes in this commit make setting type expansion context, for
captures, conditional on a flag that ensures that the function has already
been typechecked.

Fixes #57646
2024-01-11 06:59:37 -08:00
Egor Zhdan
494474b021 [cxx-interop] Support C++ default arguments
This allows calling a C++ function with default arguments from Swift without having to explicitly specify the values of all arguments.

rdar://103975014
2024-01-10 16:37:42 +00:00
Rintaro Ishizaki
f7e35cb835 [AST] Requestify local type declarations
Instead of recording local type declarations while parsing, make a
request to collect them from the AST. This reduces the side effect of
parsing.
2024-01-05 13:26:55 -08:00
Allan Shortlidge
9b6b4b956d SILGen: Consistently use the buildMainActorExecutorRef built-in.
SILGen and IRGen would disagree on the return type of the
`swift_task_getMainExecutor()` runtime function if
`SILGenModule::getGetMainExecutor()` had ever been called. To address this,
consistently use the `buildMainActorExecutorRef` built-in and get rid of
`SILGenModule::getGetMainExecutor()`.

Resolves rdar://116472583
2023-12-14 15:13:46 -08:00
Allan Shortlidge
5b95737a69 SILGen: Skip emitting non-exportable property wrapper backing inits.
When `-experimental-skip-non-exportable-decls` is specified, skip emitting
property wrapper backing inits since they cannot be referenced directly by
module clients.

Resolves rdar://119469651
2023-12-10 16:33:34 -08:00
Allan Shortlidge
13a1861a1f SILGen: Don't skip functions nested in closures in inlinable functions.
The logic for walking up the tree of DeclContexts did not handle closures
correctly.

Resolves rdar://119350220
2023-12-07 18:24:37 -08:00
Kuba Mracek
434a0b9872 [embedded] Consider 'skipped' decls when SILGen-ing accessors, fix compiler crash on unavailable accessors 2023-11-27 12:03:47 -08:00
Allan Shortlidge
84098f9fb1 Sema: Always mark initializers of lazy vars as subsumed.
Previously, the initializer expressions of lazy vars would only be marked as
subsumed when the getter body for the var was synthesized. This didn't work
with `-experimental-lazy-typechecking` since accessor synthesis was not
guaranteed to happen. Consequently, SILGen would emit the initializer even
though it was already subsumed and then assert/crash since the init had also
not been checked and contextualized. Now lazy var inits are marked subsumed in
the request creating storage.

Resolves rdar://118421753
2023-11-16 18:01:54 -08:00
Allan Shortlidge
8d7cf970a5 AST: Refactor pattern binding initializer expression state representation.
As a follow up to https://github.com/apple/swift/pull/69841, clarify the
possible states that initializer expression of a pattern can be in. The
possible states are not checked, checked, and "checked and contextualized"
(which is the new state that was introduced and requestified in the previous
PR). This refactoring encodes the states more explicitly and renames a few
compiler APIs to better align with the new naming. NFC.
2023-11-16 10:06:47 -08:00
Allan Shortlidge
111eea7f5d AST/SILGen: Requestify var initializer expression typechecking.
Allow initializer expressions to be emitted during SILGen when
`-experimental-lazy-typecheck` is specified by introducing a new request that
fully typechecks the init expressions of pattern binding declarations
on-demand.

There are still a few rough edges, like missing support for wrapped properties
and incomplete handling of subsumed initializers. Fixing these issues is not an
immediate priority because in the short term `-experimental-lazy-typecheck`
will always be accompanied by `-enable-library-evolution` and
`-experimental-skip-non-exportable-decls`. This means that only the
initializers of properties on `@frozen` types will need to be emitted and
property wrappers are not yet fully supported on properties belonging to
`@frozen` types.

Resolves rdar://117448868
2023-11-14 11:54:01 -08:00
Allan Shortlidge
29c04a78ff SILGen: Skip emitting non-exportable stored property inits.
When -experimental-skip-non-exportable-decls is specified, the SIL for the
initializers of stored properties is unneeded and should be skipped so long as
the property does not belong to a `@frozen` type.
2023-11-11 14:07:09 -08:00
Kavon Farvardin
a9c64baaa3 [Sema] refactor TypeDecl::isNoncopyable
In preparation for reporting whether the inverse marking on a TypeDecl
was inferred instead of explicit.
2023-11-06 15:40:12 -08:00
Allan Shortlidge
d0e2ca4e0c SILGen: Skip emitting distributed thunks for skipped accessors.
Generalizes https://github.com/apple/swift/pull/68917 to cover accessors in
addition to methods.

Resolves rdar://117226130
2023-10-26 16:32:51 -07:00
Allan Shortlidge
114a46aa88 SILGen: Handle nested functions in shouldSkipDecl().
Functions that are nested within other functions are not exportable to clients
since they cannot be called directly by clients. However, they need still to be
emitted during SILGen when -experimental-skip-non-exportable-decls is specified
so walk the decl context hierarchy when determining whether to skip emitting a
function.

Resolves rdar://117440503
2023-10-24 22:58:51 -07:00
Kavon Farvardin
148897ac1a [nfc] refactor ValueDecl::isMoveOnly
I've renamed the method to `TypeDecl::isNoncopyable`, because the query
doesn't make sense for many other kinds of `ValueDecl`'s beyond the
`TypeDecl`'s. In fact, it looks like no one was relying on that anyway.

Thus, we now have a distinction where in Sema, you ask whether
a `Type` or `TypeDecl` is "Noncopyable". But within SIL, we still
preserve the notion of "move-only" since there is additionally the
move-only type wrapper for types that otherwise support copying.
2023-10-18 13:45:50 -07:00
Allan Shortlidge
b8bddb58fe SILGen: Honor -experimental-skip-non-exportable-decls.
When the flag is specified, only emit SIL for declarations that are exposed to
clients.

Resolves rdar://116774565
2023-10-11 22:41:31 -07:00
Allan Shortlidge
17f6f4e7a4 SILGen: Introduce and adopt SILGenModule::shouldSkipDecl().
This method centralizes the logic for determining whether to skip emission of
SIL associated with a Decl.
2023-10-11 22:41:30 -07:00
nate-chandler
a9e03df58c Revert "[Diagnostics] Require explicit releasenone." 2023-10-04 14:19:15 -07:00
Allan Shortlidge
a200333a2d SILGen: Skip emitting distributed thunks for skipped functions. 2023-10-02 11:04:07 -07:00
Allan Shortlidge
1d8fc104c3 AST/SILGen: Requestify function body skipping.
Function bodies are skipped during typechecking when one of the
-experimental-skip-*-function-bodies flags is passed to the frontend. This was
implemented by setting the "body kind" of an `AbstractFunctionDecl` during decl
checking in `TypeCheckDeclPrimary`. This approach had a couple of issues:

- It is incompatible with skipping function bodies during lazy typechecking,
  since the skipping is only evaluated during a phase of eager typechecking.
- It prevents skipped function bodies from being parsed on-demand ("skipped" is
  a state that is distinct from "parsed", when they ought to be orthogonal).
  This needlessly prevented complete module interfaces from being emitted with
  -experimental-skip-all-function-bodies.

Storing the skipped status of a function separately from body kind and
requestifying the determination of whether to skip a function solves these
problems.

Resolves rdar://116020403
2023-09-28 19:18:35 -07:00
Pavel Yaskevich
c75aea3654 Merge pull request #68390 from xedin/checked-continuations-for-foreign-callbacks
Provide ability to use CheckedContinuation when suspending for an async ObjC call
2023-09-08 18:52:04 -07:00
Pavel Yaskevich
6e48d449e8 [SILGen] NFC: Introduce intrinsics for construction and resumption of CheckedContinuation 2023-09-07 17:35:38 -07:00
Nate Chandler
63bdb4ab1f [Gardening] Fixed variable name.
In SILGen, it's an SGM not an IGM.
2023-09-05 08:18:02 -07:00
Zak Kent
6aafeec181 [Immediate] [SILGen] Implement on-demand compilation of globals 2023-08-17 11:03:11 -07:00
zachary0kent
25f078adb5 Merge branch 'main' into lazy-immediate 2023-08-16 19:45:27 -07:00
Pavel Yaskevich
23d838af32 [SIL/SILGen] [AST] NFC: Remove @runtimeMetadata related code 2023-08-15 12:17:31 -07:00
Zak Kent
17fedabb5e [Immediate] Implemented SwiftMaterializationUnit
Implemented SwiftMaterializationUnit, supporting
lazy compilation of individual Swift functions.
2023-08-14 13:28:22 -07:00
Zak Kent
a459c97d74 [SILGen] [Test] Address code review comments 2023-08-08 11:25:11 -07:00
Zak Kent
03526c3ad0 [SILGen] Remove TODO referring to unified entry point emission 2023-08-08 11:25:11 -07:00