Commit Graph

9668 Commits

Author SHA1 Message Date
Anthony Latsis
dea291c047 Merge pull request #85971 from swiftlang/jepa-main4
SIL: Adjust pack `isa` after upstream LLVM implementation change
2025-12-12 22:20:39 +00:00
Anthony Latsis
9b2a7efbf2 SIL: Adjust pack isa after upstream LLVM implementation change
See https://github.com/llvm/llvm-project/pull/161403.

These `isa` calls with a `CanType` no longer compile with LLVM next
because the implementation now unfolds directly to `CastInfo` calls
rather than non-variadic `isa` calls that resolve to our partial
specializations here:

e293876e4f/include/swift/AST/Type.h (L600)

We should partially specialize `CastInfo` instead of `isa` et al. For
now, just use the non-variadic `isa`.
2025-12-11 14:18:47 +00:00
Joe Groff
da4e72cf2d SIL verifier: The atInstruction/atArgument parameter to require should not be optional.
The underlying C++ code expects a non-null `Instruction*` or `SILArgument*` pointer, and
most of the contextual information in a verifier error is derived from these arguments,
so it doesn't really make sense for the Swift level interface to present these arguments
as optional.
2025-12-10 10:27:56 -08:00
Egor Zhdan
ad56e061af Merge pull request #85066 from egorzhdan/egorzhdan/std-function-context
[cxx-interop] Allow initializing `std::function` from Swift capturing closures
2025-12-10 13:35:34 +00:00
Egor Zhdan
7fc815e383 [cxx-interop] Allow initializing std::function from Swift capturing closures
This introduces support for converting a Swift closure that captures variables from its surrounding context into an instance of `std::function`, which is useful for working with C++ APIs that use callbacks.

Each instantiation of `std::function` gets a synthesized Swift constructor that takes a Swift closure. Unlike the previous implementation, the closure is _not_ marked as `@convention(c)`. The body of the constructor is created lazily.

Under the hood, the closure is bitcast to a pair of a function pointer and a context pointer, which are then wrapped in a C++ object, `__SwiftFunctionWrapper`, that manages the lifetime of the context object via calls to `swift_retain`/`swift_release` from the copy constructor and the destructor. The `__SwiftFunctionWrapper` class is templated, and is instantiated by ClangImporter.

rdar://133777029
2025-12-05 15:41:59 +00:00
Erik Eckstein
332b253a2e SIL: fix vtable cache updating in SILVTable::replaceEntries
We first have to remove _all_ old entries from the cache, then add the new entries.
Otherwise we'll wrongly remove an existing entry from the cache again if a previous entry was deleted from the vtable.
2025-12-05 11:34:55 +01:00
Egor Zhdan
11d7c1ed12 Refactor: create SILFunctionType::getPointerAuthDiscriminator 2025-12-04 12:07:01 +00:00
Egor Zhdan
76c7be7cad Refactor: introduce SILModule::getMaximalTypeExpansionContext() 2025-12-04 12:06:58 +00:00
Michael Gottesman
4f29698edf Merge pull request #85766 from gottesmm/pr-6cfb709b2ef818e62904d539ba5998c638a598d2
[sil-verifier] Fix thinko around verifying joint post-dominance of begin_apply stack allocation.
2025-12-02 21:55:45 -08:00
Michael Gottesman
8a7b5f7c7f [sil-verifier] Make flow sensitive verification handle begin_apply token/allocation correctly.
Specifically:

1. We were tracking the stack allocation both in handleScopeInst and in the
Stack. We should only track it in one of them. I also used this as an
opportunity to make sure the code worked for non_nested code.

2. I made it so that we properly handle the end tracking part of the code so we
handle the token/stack part of begin_apply correctly. I generalized the code so
that we should handle non_nested stack allocations as well.

I included tests that validated that we now handle this correctly.
2025-12-02 14:17:27 -08:00
Michael Gottesman
c9076506ec [sil-verifier] Convert a if (x && isa<Inst>(x)) -> (isa_and_present<Inst>(x)). 2025-12-02 13:11:26 -08:00
Michael Gottesman
94763c8429 [sil-verifier] Convert else-if chain to if + continue to clarify control flow.
else-if chains provide the possibility that there is additional code after the
else-if chain. In this case, no such code exists, so by using early continues we
communicate that the iteration ends at the end of an if block and the programmer
does not need to read to the end of the else-if block.

This also allows for me to eliminate one level of indentation in the successor
code as well.
2025-12-02 13:10:27 -08:00
Michael Gottesman
5ef8f3b248 [sil-verifier] Add a dump method to BBState for FlowSensitiveVerification.
Just to make it easier to diagnose problems in the debugger.
2025-12-02 13:07:40 -08:00
Michael Gottesman
c739308f2b [sil-verifier] Extract the flow sensitive verifier into its own file.
This has grown large enough and complex enough that it makes sense to go into
its own file.

This is a NFCI change. The only substantial changes is I added a small require
impl based on the one in SILVerifier and I eliminated the namespace
VerifyFlowSensitiveRulesDetails.
2025-12-02 12:45:23 -08:00
Joe Groff
18faa24144 Merge pull request #85780 from jckarter/borrow-accessor-afd
Borrow accessors for addressable-for-dependencies types should pass and return by address.
2025-12-02 12:21:40 -08:00
eeckstein
8d50f20965 Merge pull request #85728 from eeckstein/print-sil-ownership
SIL: add an option `-sil-print-ownership` to print ownership
2025-12-02 07:32:11 +01:00
Joe Groff
fa2d467fc0 Borrow accessors for addressable-for-dependencies types should pass and return by address.
This ensures that a `borrow` accessor doesn't introduce temporary bitwise copies of the
representation that could foreshorten the lifetime of memory-dependent references.
2025-12-01 20:32:55 -08:00
Doug Gregor
6b3935201d [Embedded] Only strip "external" from global variables that have definitions
Embedded Swift ends up rewriting the linkage of global variables as
part of linking together all of the Swift modules. Doing so for extern
global variables (e.g., ones meant to be implemented in C) breaks the
LLVM module, because they will never have definitions. Only make this
change when the global variable is a definition.
2025-12-01 15:47:32 -08:00
Michael Gottesman
24c69c674d Merge pull request #85604 from gottesmm/alloc_stack_non_nested
[irgen] Implement support for alloc_stack non_nested.
2025-12-01 09:38:06 -08:00
Erik Eckstein
103d3c2f85 SILPrinter: print forwarding ownership for struct and tuple instructions
Print `forwarding: <ownership>` if the ownership of the result mismatches the operand ownership(s).
We already do this for all other forwarding instructions, but `struct` and `tuple` were missing.
2025-12-01 16:00:58 +01:00
Erik Eckstein
3ade98c84e SILPrinter: add an option -sil-print-ownership to print the ownership of instruction results
With this option the ownership of instruction results is printed in the comments at the end of the line, e.g.

```
  %3 = struct $S (%2, %1)                         // ownership: owned
```

And even without enabling this option, ownership comments are printed if the ownership of a result mismatches with its type.
That can happen e.g. for non-trivial enums which are constructed with a trivial case:

```
enum E {
  case A
  case B(AnyObject)
}

  %1 = enum $E, #E.A!enumelt  // type of %1 is non trivial, but ownership is "none"
```
2025-12-01 16:00:58 +01:00
Michael Gottesman
94e9166706 [sil-verifier] Use the scope result rather than the scope instruction when verifying that we end scope instructions.
We do this since we need to be able to handle instructions that may have two
different results that independently need their scopes lifetime to be ended.
2025-11-21 11:21:15 -08:00
Michael Gottesman
c876c1ee88 [sil-verifier] Split SILVerifier verificationFailure into separate functions for Instructions, Arguments, and add a variant for Values.
This also required me to change how we handled which instruction/argument we
emit an error about in the verifier. Previously we were using two global
variables that we made nullptr to control which thing we emitted an error about.
This was unnecessary. Instead I added a little helper struct that internally
controls what we will emit an error about and an external "guard" RAII struct
that makes sure we push/pop the instruction/argument we are erroring upon
correctly.
2025-11-21 11:21:15 -08:00
Michael Gottesman
682ef268d2 [optimizer] Teach SIL optimizer that stack nesting should ignore nested stack allocations. 2025-11-21 11:21:15 -08:00
Michael Gottesman
29229a9410 [sil] Add basic SIL support for alloc_stack [non_nested].
This means I just added the flag and added support for
cloning/printing/serializing the bit on alloc_stack.
2025-11-21 11:21:14 -08:00
Erik Eckstein
95d5b5da33 SIL: make getLinkageString(SILLinkage linkage) available as general API 2025-11-21 11:47:06 +01:00
Kavon Farvardin
199156b307 SILGen: ban getSILArgumentConvention
This function will give the wrong convention in SILGen when
using -enable-sil-opaque-values. In particular, it will say
arguments are indirect when they are not.
2025-11-19 17:32:20 -08:00
Yuta Saito
8748213d52 Merge pull request #85524 from kateinoigakukun/yt/fix-non-reg2mem-rle-crash 2025-11-16 23:10:20 +09:00
Yuta Saito
e259a257e6 RedundantLoadElim: Fix invalid destructure_struct emission without aggressive reg2mem
The `shouldExpand` in `OptUtils.swift` was incorrectly returning `true`
unconditionally when `useAggressiveReg2MemForCodeSize` was disabled. The
expansion might be invalid for types with addr-only types and structs
with deinit, but we didn't check them before. This could lead to invalid
`destructure_struct` instructions without `drop_deinit` being emitted.
2025-11-15 19:22:33 +00:00
Doug Gregor
8e37909131 Merge pull request #85505 from DougGregor/c-funcs-for-embedded-runtime
Use @c instead of @_cdecl in the Embedded Swift runtime
2025-11-13 23:41:03 -08:00
Doug Gregor
a1ce6e66df Merge pull request #85498 from DougGregor/retain-calling-convention-from-c
Retain the Swift calling convention when it's on an imported C declaration
2025-11-13 21:40:03 -08:00
Doug Gregor
b21485dd1f Only foreign thunks need to be serialized, not foreign definitions
This eliminates a SIL verification error with `@c` functions, which
provide definitions for foreign entrypoints. We were serializing @c
definitions when we shouldn't be, which would cause problems down the
line if those @c definitions referenced something internal that they
shouldn't.
2025-11-13 18:39:08 -08:00
Doug Gregor
e0139bcc0c Retain the Swift calling convention when it's on an imported C declaration
Fixes #69264.
2025-11-13 11:34:01 -08:00
Slava Pestov
64f2d1acce AST: Rename mapConformanceOutOfContext() => mapConformanceOutOfEnvironment(), mapReplacementTypesOutOfContext() => subs.mapReplacementTypesOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Slava Pestov
819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Andrew Trick
b28cd59dfc [NFC] TypeLowering: add CustomDeinit.
Teach SIL type lowering to recursively track custom vs. default deinit status.

Determine whether each type recursively only has default deinitialization. This
includes any recursive deinitializers that may be invoked by releasing a
reference held by this type.

If a type only has default deinitialization, then the deinitializer cannot
have any semantically-visible side effects. It cannot write to any memory
2025-11-11 17:29:45 -08:00
Anthony Latsis
8992ea82a2 Merge pull request #85433 from swiftlang/jepa-main4
AST: Rename `GenericContext::isGeneric` to `hasGenericParamList`
2025-11-11 21:12:34 +00:00
Doug Gregor
2002b90531 Merge pull request #85425 from DougGregor/irgen-multithreaded-lazy-global-var
[IRGen] Correctly assign lazily-emitted global variables in multi-threaded IRGen
2025-11-11 13:10:00 -08:00
Anthony Latsis
bda6edb85c AST: Rename GenericContext::isGeneric to hasGenericParamList
`isGeneric` is a misleading name because this method checks for the
existence of a `GenericParamList`, which is not implied by genericity.
2025-11-11 15:55:16 +00:00
Doug Gregor
bce3fa000b [IRGen] Correctly assign lazily-emitted global variables in multi-threaded IRGen
With multi-threaded IRGen, the global variables associated with "once"
initialization tokens were not getting colocated with their actual
global variables, which caused the initialization code to get split
across different files. This issue manifest as autolinking errors in
some projects.

Fixes rdar://162400654.
2025-11-10 23:08:57 -08:00
Adrian Prantl
11356acd44 Merge pull request #85397 from adrian-prantl/163167975
[SILOptimzer] Fix a crash caused by SILCombine mishandling inlined variables
2025-11-10 18:41:07 -08:00
Meghana Gupta
e2123e1b3b Merge pull request #85362 from meg-gupta/moreborrow
Add SIL verification for borrow and mutate accessors and some other minor fixes
2025-11-10 09:04:28 -08: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
Adrian Prantl
99cf35cdce [SILOptimzer] Fix a crash caused by SILCombine mishandling inlined variables
This showed up on and off again on the source-compatibility testsuite project hummingbird.

The gist of the problem is that transformations may not rewrite the
type of an inlined instance of a variable without also createing a
deep copy of the inlined function with a different name (and e.g., a
specialization suffix). Otherwise the modified inlined variable will
cause an inconsistency when later compiler passes try to create the
abstract declaration of that inlined function as there would be
conflicting declarations for that variable.

Since SILDebugScope isn't yet available in the SwiftCompilerSources
this fix just drop these variables, but it would be absolutely
possible to preserve them by using the same mechanism that SILCloner
uses to create a deep copy of the inlined function scopes.

rdar://163167975
2025-11-07 17:06:33 -08:00
Meghana Gupta
21cc1a185b Add memory lifetime verification support for borrow accessors 2025-11-06 10:55:42 -08:00
Meghana Gupta
778ad0dfb5 Add SIL verification to ensure we don't return local addresses 2025-11-06 10:55:39 -08:00
Michael Gottesman
97b0e2ebae [concurrency] Create builtins for invoking specific concurrency runtime functions.
Specifically:

1. swift_task_addCancellationHandler
2. swift_task_removeCancellationHandler
3. swift_task_addPriorityEscalationHandler
4. swift_task_removePriorityEscalationHandler
5. swift_task_localValuePush
6. swift_task_localValuePop

rdar://109850951
2025-11-05 11:03:44 -08:00
Michael Gottesman
1711ef2e62 [sil] Add support for UnsafeRawPointer to SILType and ASTSynthesis.
I am going to be using this type in some of the Builtins that I am adding. Just
chopping off part of the patch before I commit anything.
2025-11-05 11:02:44 -08:00
John McCall
13937fdb4e Merge pull request #84528 from rjmccall/async-let-runtime-realism
Model async let begin/finish as builtins in SIL
2025-11-05 10:24:46 -08:00
John McCall
a7d7970e29 Turn finishAsyncLet into a builtin.
This is necessary because we need to model its stack-allocation
behavior, although I'm not yet doing that in this patch because
StackNesting first needs to be taught to not try to move the
deallocation.

I'm not convinced that `async let` *should* be doing a stack allocation,
but it undoubtedly *is* doing a stack allocation, and until we have an
alternative to that, we will need to model it properly.
2025-11-03 16:33:40 -08:00