Commit Graph

1652 Commits

Author SHA1 Message Date
Kavon Farvardin
47a6f2acfd Merge pull request #85392 from kavon/borrow-expr-implicit-infra
SILGen: add RValue emission for BorrowExpr
2025-11-12 11:23:52 -08:00
Slava Pestov
522a6b7c80 SILGen: Fix break/continue inside 'for ... in ... repeat' loop
We were creating the JumpDests too early, so lowering a 'break' or 'continue'
statement would perform cleanups that were recorded while evaluating the
pack expansion expression, which would cause SIL verifier errors and
runtime crashes.

- Fixes https://github.com/swiftlang/swift/issues/78598
- Fixes rdar://131847933
2025-11-10 20:51:45 -05:00
Doug Gregor
020b69d4b6 [SE-0497] Implement @export attribute syntax
Implement the @export(implementation) and @export(interface) attributes
to replace @_alwaysEmitIntoClient and @_neverEmitIntoClient. Provide a
warning + Fix-It to start staging out the very-new
@_neverEmitIntoClient. We'll hold off on pushing folks toward
@_alwaysEmitIntoClient for a little longer.
2025-11-07 22:00:40 -08:00
Kavon Farvardin
314093f426 SILGen: add RValue emission for BorrowExpr 2025-11-07 14:01:35 -08:00
Doug Gregor
1b2e6ad473 Merge pull request #84989 from DougGregor/c-only-c-entrypoint 2025-10-18 00:59:00 -07:00
Michael Gottesman
a9d409d526 Merge pull request #83346 from gottesmm/pr-df6aafa41c61b9a9a6ee4965a1564ec2946a0dc9
[concurrency] Change #isolated to mask out the TBI bits of the witness pointer of the implicit isolated any Actor pointer so we can do optimizations on TBI supporting platforms in the future.
2025-10-17 17:02:43 -07:00
Doug Gregor
4be74c714f [SE-0495, SILGen] Only emit C-compatible entrypoints for @c definitions.
When defining a C-compatible function with `@c`, we were emitting a
Swift function along with a C-compatible thunk. Stop doing that, and
instead only produce the C-compatible function. All uses of the
function will go through that C interface, just like if the function
were declared in C.

This also applies to `@c @implementation` functions, which are
declared in C but implemented in Swift. It does *not* apply to
`@_cdecl`, which will continue to produce both the Swift function and
C thunk to prevent an ABI break.

Fixes rdar://158888024.
2025-10-17 16:34:42 -07:00
Doug Gregor
79fc0f1b2e [extern] Give @_extern(c) functions @convention(c) type
This ensures that when we reference them, we get an appropriately-typed
function value. SILGen can then thunk to whatever function type is
needed.
2025-10-16 20:04:34 -07:00
Michael Gottesman
390afe3e7d [concurrency] Implement bit masking for TBI when available or in tagged pointer bits otherwise.
Specifically, when TBI is available we use the bottom two bits of the top nibble
(bits 60,61). On platforms without TBI, we use the bottom two tagged pointer
bits (bits 0, 1).

rdar://156525771
2025-10-16 10:52:05 -07:00
Michael Gottesman
788abd0b96 [silgen] Use Builtin.ImplicitActor instead of Optional<any Actor> to represent the implicit isolated parameter.
NOTE: We are not performing any bitmasking at all now. This is so that we can
transition the code base/tests to expect Builtin.ImplicitActor instead
of Optional<any Actor>.

NOTE: The actual test changes are in the next commit. I did this to make it
easier to review the changes.

This should not have any user visible changes.
2025-10-16 10:52:05 -07:00
Tim Kientzle
41f680cb93 Merge pull request #84826 from tbkka/tbkka-revert-revert-floatingpointdescription
Re-land new Floating-poing `debugDescription`
2025-10-12 20:19:12 -07:00
Tim Kientzle
4c0b58096e Make _InlineArray subject to the same optimizations as InlineArray 2025-10-10 14:06:14 -07:00
Erik Eckstein
3309809429 SILGen: improve code generation for array literals
It used to be done with a library intrinsic which returns the array and an element address pointer.
A `mark_dependence` was used to "connect" the two results.
But this resulted in completely wrong OSSA after inlining.
It just didn't get caught be the verifier because the SIL was obfuscated by address-pointer conversions.

Now we don't use the second result (= the element address) of the intrinsic but generate a correct borrow scope from the first (= array) result. Within that borrow scope we project out the element address with a `ref_tail_addr` instruction.

This relies on knowledge of the internal Array data structures. However those data structures are baked into the ABI since a long time and will not change.
2025-10-10 14:20:36 +02:00
Kavon Farvardin
8458226347 Merge pull request #84779 from kavon/opaque-values/unreachable-expr-rdar162239557
SILGen: revise emission of UnreachableExpr
2025-10-09 16:00:37 -07:00
Kavon Farvardin
1e28b0a312 SILGen: revise emission of UnreachableExpr
The prior emission strategy produced some scary SIL where
we have a copy of uninitialized memory:

```
  ignored_use %14
  %16 = alloc_stack $T
  unreachable

bb1:
  copy_addr [take] %16 to [init] %0
  dealloc_stack %16
```

I assume this was done to dodge the SILVerifier, which
will catch a take of an uninitialized address, had the
alloc_stack been in any reachable predecessor.

We already have a representation for an undefined value
in SIL, so I'd rather use that than try to sneak one
past the verifier.

This adjusted emission also is opaque values friendly,
as it doesn't assume the result value is an address.

resolves rdar://162239557
2025-10-09 10:38:54 -07:00
Gábor Horváth
7159b12936 Merge pull request #83752 from Xazax-hun/fix-inline-namespaces
[cxx-interop] Support calling functions in inline namespaces
2025-10-08 18:41:38 +01:00
Max Desiatov
b545a28bd3 Add missing // REQUIRES: swift_feature_ForExpressions 2025-10-07 12:34:49 +01:00
Gabor Horvath
0e56809834 [cxx-interop] Support calling functions in inline namespaces
The overload resolution generated a constraint that tried to bind
outer.inline_inner to outer. This constraint failed. This PR attempts to
recognize this scenario and make the constraint succeed.

rdar://158401346
2025-10-07 11:39:07 +01:00
Max Desiatov
b985f3a6be Fix SILGen and make it work with all RangeReplaceableCollections 2025-10-07 11:16:54 +01:00
Gábor Horváth
e8784b8c10 Merge pull request #83827 from Xazax-hun/const-ref-crash-take-4
[SILGen] Fix the type of closure thunks that are passed const references
2025-10-01 09:56:06 +01:00
Kavon Farvardin
e93ffb762d Merge pull request #84504 from kavon/manual-ownership/usability-fixes-3
Usability improvements for ManualOwnership (part 3)
2025-09-25 03:17:14 -07:00
Kavon Farvardin
0734267053 ManualOwnership: fix simple var assign
Was mistakenly counting a 'store' as
a copying instruction, when it's only a
consuming one.

SILGen was not handling lvalues that are
addresses for loadable types correctly,
when emitting a CopyExpr in ManualOwnership.
2025-09-24 17:00:24 -07:00
Kavon Farvardin
341d5310e1 nfc: simplify ManualOwnership check 2025-09-24 13:40:33 -07:00
Kavon Farvardin
da4f27e31b ManualOwnership: adjust CopyExpr emission
We were getting an address-based emission for loadable types,
if the result of a LoadExpr was explicit-copied. This emission
had extra hidden copies that could not be silenced.
2025-09-24 12:11:15 -07:00
Kavon Farvardin
a44bf30141 Merge pull request #84476 from kavon/manual-ownership/usability-fixes-1
Usability improvements for ManualOwnership (aka "explicit copies mode")
2025-09-23 22:47:22 -07:00
Kavon Farvardin
b3b9d28a54 OpaqueValues: support SILGen for copy expr
This doesn't cover AddressLowering of `explicit_copy_value`.
2025-09-23 12:01:33 -07:00
Slava Pestov
90bfc1676d Sema: Remove some unreachable code from CSApply
I believe these code paths could only be reached by re-typechecking
invalid code in the old CSDiag implementation.
2025-09-17 16:11:53 +01:00
John McCall
8301d5383b Fix the evaluation of #isolation in non-AFD contexts, most importantly
closures.

The fixes for initializers are just setting the stage for doing this
properly: we should be able to just change the isolation computation
in Sema and fix up the tests.
2025-09-09 14:26:57 -04:00
John McCall
5fb314b475 At the SIL level, allow synchronous functions to be nonisolated(nonsending). 2025-09-09 14:26:57 -04:00
Gabor Horvath
f26749245b [SILGen] Fix the type of closure thunks that are passed const reference structs
This PR is another attempt at landing #76903. The changes compared to
the original PR:
* Instead of increasing the size of SILDeclRef, store the necessary type
  information in a side channel using withClosureTypeInfo.
* Rely on SGFContext to get the right ClangType
* Extend BridgingConversion with an AbstractionPattern to store the
  original clang type.
* The PR above introduced a crash during indexing system modules that
  references foreign types coming from modules imported as
  implementation only. These entities are implementation details so they
  do not need to be included during serialization. This PR adds a test
  and adds logic to exclude such clang types in the serialization
  process.

rdar://131321096&141786724
2025-09-09 12:07:52 +01:00
Kavon Farvardin
511971fb1d Merge pull request #83959 from kavon/rdar159079818-subscript-read-nc
silgen: allow borrow of subscript for noncopyables
2025-08-29 20:52:23 -07:00
Kavon Farvardin
4964a66277 silgen: inherit eval scope for OpenExistentialExpr
We were not able to use an existential as the base
of an access that strictly borrows the existential,
because SILGen's RValue emission would establish
a fresh evaluation scope just for the existential's
opening, and then copy the opened value out.

This is problematic for noncopyable existentials.

So this patch moves & adds FormalEvaluationScope's
around so they're broad enough to enable a
borrow of an existential. The idea behind this
refactoring is to establish top-level
FormalEvaluationScopes when initially creating
RValue's for Expr's in SILGen. Any more-tightly
scoped operations will already establish their own
nested scope, so this is mostly adding safe-guards.

I've limited the existentials fix to noncopyables
for now.

part of rdar://159079818
2025-08-29 13:30:42 -07:00
Kavon Farvardin
906a4cbcdb silgen: allow borrow of subscript for noncopyables
If a subscript uses a read accessor to yield a noncopyable value,
we'd emit an `end_apply` that's too tightly scoped to allow for
a subsequent borrowing access on the yielded value.

resolves rdar://159079818
2025-08-29 13:30:31 -07:00
Slava Pestov
11f727e143 SIL: Add no-arg overload of SILFunction::getLoweredFunctionTypeInContext() 2025-08-25 16:45:45 -04:00
John McCall
6c3fd0eaec Fix projecting a tuple element out of a tuple with packs.
At the same time, make sure we propagate contextual initializations.

I'm actually not sure this is meant to be supported, but it's fine,
we should implement it.

Fixes #80937
2025-08-02 02:13:25 -04:00
John McCall
bee053f1f0 Switch InitializationPtr to use PossiblyUniquePtr so that we can just
forward existing initializations around when necessary.
2025-08-02 02:13:25 -04:00
Anthony Latsis
bbd84429a0 Merge pull request #83400 from swiftlang/jepa-main
Address `llvm::PointerUnion::{is,get}` deprecations
2025-07-30 17:19:58 +01:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
John McCall
92b9857e6a Rewrite the type of nonisolated(nonsending) closures.
The constraint solver does not reliably give closures a function type
that includes `nonisolated(noncaller)`, even when the immediate context
requires a conversion to such a type. We were trying to work around this
in SILGen, but the peephole only kicked in if the types matched exactly,
so a contextual conversion that e.g. added `throws` was still emitting
the closure as `@concurrent`, which is of course the wrong semantics.
It's relatively easy to avoid all this by just rewriting the closure's
type to include `nonisolated(nonsending)` at a point where we can reliably
decide that, and then SILGen doesn't have to peephole anything for
correctness.

Fixes rdar://155313349
2025-07-28 22:20:38 -04:00
Pavel Yaskevich
785e7b1571 Merge pull request #82858 from gottesmm/rdar154969621
[concurrency] Perform some fixes so that transfernonsendable_closureliterals_isolationinference.swift now passes.
2025-07-08 00:04:14 -07:00
Michael Gottesman
648bb8fe30 [concurrency] Perform some fixes so that transfernonsendable_closureliterals_isolationinference.swift now passes.
The reason why this failed is that concurrently to @xedin landing
79af04ccc4, I enabled
NonisolatedNonsendingByDefault on a bunch of other tests. That change broke the
test and so we needed to fix it.

This commit fixes a few issues that were exposed:

1. We do not propagate nonisolated(nonsending) into a closure if its inferred
context isolation is global actor isolated or if the closure captures an
isolated parameter. We previously just always inferred
nonisolated(nonsending). Unfortunately since we do not yet have capture
information in CSApply, this required us to put the isolation change into
TypeCheckConcurrency.cpp and basically have function conversions of the form:

```
(function_conversion_expr type="nonisolated(nonsending) () async -> Void"
   (closure_expr type="() async -> ()" isolated_to_caller_isolation))
```

Notice how we have a function conversion to nonisolated(nonsending) from a
closure expr that has an isolation that is isolated_to_caller.

2. With this in hand, we found that this pattern caused us to first thunk a
nonisolated(nonsending) function to an @concurrent function and then thunk that
back to nonisolated(nonsending), causing the final function to always be
concurrent. I put into SILGen a peephole that recognizes this pattern and emits
the correct code.

3. With that in hand, we found that we were emitting nonisolated(nonsending)
parameters for inheritActorContext functions. This was then fixed by @xedin in

With all this in hand, closure literal isolation and all of the other RBI tests
with nonisolated(nonsending) enabled pass.

rdar://154969621
2025-07-07 17:21:41 -07:00
Konrad 'ktoso' Malawski
b1ca06d79d [Concurrency] #isolation+nonsending nonisolated - review followup and SIL test 2025-07-07 11:37:16 +09:00
Konrad 'ktoso' Malawski
b0d2ca9be6 [Concurrency] Fix nonisolated(nonsending) interaction with #isolation 2025-07-04 10:20:08 +09:00
Joe Groff
761faaa169 SILGen: Handle struct fields and addressors as addressable storage.
When accessing stored properties out of an addressable variable or parameter
binding, the stored property's address inside the addressable storage of the
aggregate is itself addressable. Also, if a computed property is implemented
using an addressor, treat that as a sign that the returned address should be
used as addressable storage as well. rdar://152280207
2025-06-16 20:23:47 -07:00
Slava Pestov
28af54a778 Merge pull request #80913 from stzn/fix-crash-self-keypath-root
[SILGen]Check if baseType is DynamicSelfType
2025-06-06 22:35:47 -04:00
Kavon Farvardin
32f1ebcfab SILGen: avoid double copies with 'copy' expr 2025-05-29 19:47:26 -07:00
Erik Eckstein
d28384dbd7 SILGen: use vector_base_addr in InlineArray literals 2025-05-12 19:25:12 +02:00
Alejandro Alonso
02d9779ba3 Merge pull request #81184 from Azoy/no-more-is-array-type
[AST] Rename isArrayType and split the InlineArray portion
2025-05-08 20:52:51 -07:00
Slava Pestov
6ffa8fd489 AST: Change signature of LookupConformanceFn
Instead of passing in the substituted type, we pass in the
InFlightSubstitution. This allows the substituted type to be
recovered if needed, but we can now skip computing it for
the common case of LookUpConformanceInSubstitutionMap.
2025-04-30 13:42:20 -04:00
Alejandro Alonso
e68b398d41 Rename isArrayType and split the InlineArray portion 2025-04-29 15:57:10 -07:00