Commit Graph

667 Commits

Author SHA1 Message Date
Doug Gregor
88fc259605 Merge pull request #85776 from DougGregor/extern-global-variables-embedded
[Embedded] Only strip "external" from global variables that have definitions
2025-12-01 22:25:50 -08:00
Arnold Schwaighofer
36a3c6e611 Merge pull request #85644 from aschwaighofer/wip_embedded_exits_with_eriks_changes_v1
[embedded] Fix associated type conformances for specialized witness tables
2025-12-01 16:40:31 -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
Arnold Schwaighofer
0c882d42ce Merge pull request #85625 from aschwaighofer/wip_embedded_cast_tuples
[embedded] Allow casting to tuples and tighten the check which existentials we support in embedded with existentials
2025-12-01 08:27:43 -08:00
Rauhul Varma
27ebe4dd95 Merge pull request #85492 from swiftlang/rauhul/cxx-exception-personality 2025-12-01 08:25:25 -08:00
eeckstein
9304ce951c Merge pull request #85707 from eeckstein/embedded-witness-method-specialization
embedded: change the function representation of directly called witness methods
2025-12-01 09:36:45 +01:00
Erik Eckstein
64dd574bea embedded: change the function representation of directly called witness methods
This is needed in Embedded Swift because the `witness_method` convention requires passing the witness table to the callee.
However, the witness table is not necessarily available.
A witness table is only generated if an existential value of a protocol is created.

This is a rare situation because only witness thunks have `witness_method` convention and those thunks are created as "transparent" functions, which means they are always inlined (after de-virtualization of a witness method call).
However, inlining - even of transparent functions - can fail for some reasons.

This change adds a new EmbeddedWitnessCallSpecialization pass:
If a function with `witness_method` convention is directly called, the function is specialized by changing the convention to `method` and the call is replaced by a call to the specialized function:

```
  %1 = function_ref @callee : $@convention(witness_method: P) (@guaranteed C) -> ()
  %2 = apply %1(%0) : $@convention(witness_method: P) (@guaranteed C) -> ()
...
sil [ossa] @callee : $@convention(witness_method: P) (@guaranteed C) -> () {
  ...
}
```
->
```
  %1 = function_ref @$e6calleeTfr9 : $@convention(method) (@guaranteed C) -> ()
  %2 = apply %1(%0) : $@convention(method) (@guaranteed C) -> ()
...
// specialized callee
sil shared [ossa] @$e6calleeTfr9 : $@convention(method) (@guaranteed C) -> () {
  ...
}
```

Fixes a compiler crash
rdar://165184147
2025-11-26 16:23:47 +01:00
Arnold Schwaighofer
b9f6454386 [embedded] Add support for storing/casting function types 2025-11-21 14:55:32 -08:00
Rauhul Varma
ce02872adf Add Embedded Swift Cxx exception personality
Users frequently run into a missing runtime symbol for Cxx exceptions
(`_swift_exceptionPersonality`) when mixing Embedded Swift and Cxx with
exceptions enabled. This leads to a confusing an hard to debug linker
error. This commit adds an implementation of this function to the
Embedded Swift runtime which simply fatal errors if a cxx exception is
caught in a Swift frame.

Issue: rdar://164423867
Issue: #85490
2025-11-21 11:12:10 -08:00
Erik Eckstein
fa3bfa3b10 embedded: don't try to specialize vtables of C++ imported reference-counted classes
Fixes a false compiler error

rdar://165209061
2025-11-21 16:33:49 +01:00
Arnold Schwaighofer
16206fad9f [embedded] Enable all tests in general-existentials1.swift 2025-11-21 06:48:51 -08:00
Arnold Schwaighofer
a5aa929d1f [embedded] Existentials: enable tests that now pass 2025-11-21 06:37:47 -08:00
Arnold Schwaighofer
003273c12e Test on macOS only 2025-11-20 13:27:24 -08:00
Arnold Schwaighofer
ffca3b4623 [embedded] Fix associate type conformances involving specialized conformances 2025-11-20 13:10:44 -08:00
Erik Eckstein
4191543855 MandatoryPerformanceOptimizations: specialize witness tables for general existentials 2025-11-20 10:56:22 -08:00
Arnold Schwaighofer
920def9319 [embedded] One more test 2025-11-20 10:16:19 -08:00
Arnold Schwaighofer
8beaad60bd [embedded] Allow casting to tuples 2025-11-20 10:03:39 -08:00
Arnold Schwaighofer
f7aae22e43 [embedded] A few more test cases for existential casting 2025-11-20 07:34:33 -08:00
Arnold Schwaighofer
3cff05d540 [embedded] Implement swift_dynamicCast suport for casts from existential to concrete type 2025-11-19 14:41:37 -08:00
Arnold Schwaighofer
84501bd6e2 Also test in optimized mode 2025-11-17 12:48:48 -08:00
Arnold Schwaighofer
62ac48a17e Complete support for outline existential storage
... or so I believe
2025-11-17 12:48:45 -08:00
Arnold Schwaighofer
eda5eadfd4 Start outline storage support: Add swift_allocBox/deallocBox
Code using the outline heap storage path will crash and burn because
support is incomplete. But at least inline storage existential
inhabitants should compile and run.
2025-11-17 12:46:35 -08:00
Arnold Schwaighofer
f38ad4b09f Start support for protocol witness tables 2025-11-17 12:46:35 -08:00
Arnold Schwaighofer
4285a2169d IRGen: Start support for embedded existentials
Allow storing struct, enum, and tuple types in an any.
2025-11-17 12:46:35 -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
Michael Gottesman
ab272fd429 Disable test when asan is enabled to unblock asan build.
rdar://159992660
2025-11-11 09:19:17 -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
Doug Gregor
ba507ab822 Merge pull request #85203 from DougGregor/clang-decl-asmname 2025-11-01 20:46:50 -07:00
Doug Gregor
f267f62f65 [SILGen] Consistently use SIL asmname for foreign function/variable references
Whenever we have a reference to a foreign function/variable in SIL, use
a mangled name at the SIL level with the C name in the asmname
attribute. The expands the use of asmname to three kinds of cases that
it hadn't been used in yet:

* Declarations imported from C headers/modules
* @_cdecl @implementation of C headers/modules
* @_cdecl functions in general

Some code within the SIL pipeline makes assumptions that the C names of
various runtime functions are reflected at the SIL level. For example,
the linking of Embedded Swift runtime functions is done by-name, and
some of those names refer to C functions (like `swift_retain`) and
others refer to Swift functions that use `@_silgen_name` (like
`swift_getDefaultExecutor`). Extend the serialized module format to
include a table that maps from the asmname of functions/variables over
to their mangled names, so we can look up functions by asmname if we
want. These tables could also be used for checking for declarations
that conflict on their asmname in the future. Right now, we leave it
up to LLVM or the linker to do the checking.

`@_silgen_name` is not affected by these changes, nor should it be:
that hidden feature is specifically meant to affect the name at the
SIL level.

The vast majority of test changes are SIL tests where we had expected
to see the C/C++/Objective-C names in the tests for references to
foreign entities, and now we see Swift mangled names (ending in To).
The SIL declarations themselves will have a corresponding asmname.

Notably, the IRGen tests have *not* changed, because we generally the
same IR as before. It's only the modeling at the SIL lever that has
changed.

Another part of rdar://137014448.
2025-10-29 19:35:55 -07:00
Alexis Laferrière
94113f4a83 SE-496: Remove references to features CDecl and CImplementation 2025-10-29 17:31:20 -07:00
Alexis Laferrière
74ea47a159 Merge pull request #84489 from xymus/c-identifier-only
Parser: Reject `@c` attributes using the string format
2025-10-27 09:17:31 -07:00
Kuba (Brecka) Mracek
eb23d3bc0a Merge pull request #85074 from kubamracek/section
SE-0492: Stabilize @_section/@_used into @section/@used
2025-10-24 12:29:48 -07:00
Alexis Laferrière
659692e834 Tests: Update all uses of @c to use the identifier format 2025-10-24 11:55:34 -07:00
Kuba Mracek
adeb40f261 SE-0492: Stabilize @_section/@_used into @section/@used
Removes the underscored prefixes from the @_section and @_used attributes, making them public as @section and @used respectively. The SymbolLinkageMarkers experimental feature has been removed as these attributes are now part of the standard language. Implemented expression syntactic checking rules per SE-0492.

Major parts:
- Renamed @_section to @section and @_used to @used
- Removed the SymbolLinkageMarkers experimental feature
- Added parsing support for the old underscored names with deprecation warnings
- Updated all tests and examples to use the new attribute names
- Added syntactic validation for @section to align with SE-0492 (reusing the legality checker by @artemcm)
- Changed @DebugDescription macro to explicitly use a tuple type instead of type inferring it, to comply with the expression syntax rules
- Added a testcase for the various allowed and disallowed syntactic forms, `test/ConstValues/SectionSyntactic.swift`.
2025-10-22 16:05:39 -07:00
Arnold Schwaighofer
22c8ea6af6 Add experimental feature flag EmbeddedExistentials
This flag can be used to gradually add the functionility that will allow use of
protocol values in embedded mode.
2025-10-20 10:18:45 -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
39a36d9216 Merge pull request #84948 from DougGregor/extern-c-asmname
[SILGen] Map the @_extern(c) C function name over to the asmname of a SIL function
2025-10-16 18:27:00 -07:00
Alexis Laferrière
860f2db0f8 Merge pull request #84630 from xymus/non-neic-funcs-are-fragile
Sema: In embedded, consider functions as fragile unless `@_neverEmitIntoClient`
2025-10-16 13:31:48 -07:00
Michael Gottesman
81885a67d0 [silgen] Change two places we used Builtin.Executor to instead use Optional<any Actor> as an expected executor.
We want SILGen to have a simplified view of its executor and know that whenever
one sees an Actor, it is an actual actor instead of a Builtin.Executor. This
just simplifies code. Also, we should eventually have an invariant that
Builtin.Executor should only be allowed in LoweredSIL after LowerHopToExecutor
has run. But that is a change for another day.
2025-10-16 10:51:13 -07:00
Doug Gregor
1ca9235de6 [Serialization] Record the new SIL_EXTRA_STRING block
Also fix up a test
2025-10-16 08:00:40 -07:00
Doug Gregor
081b5cd1e8 [SIL] Serialize section name correctly and model it on global variables
Fixes rdar://162549960.
2025-10-15 20:44:11 -07:00
Alexis Laferrière
dd4221d2bd Embedded: Allow implementation-only-import-build to build wider 2025-10-14 14:14:55 -07:00
Alexis Laferrière
f758e210f1 Sema: Add requires to IOI + embedded code using the stdlib 2025-10-14 14:14:55 -07:00
Alexis Laferrière
495035d612 Sema: Add tests for embedded exportability checking with accessors 2025-10-14 14:14:55 -07:00
Alexis Laferrière
1b086e1bfc Sema: Embedded functions are fragile unless marked otherwise
Consider functions in embedded mode to be fragile unless marked with
`@_neverEmitIntoClient`. Basically treating them as if they were
`@_alwaysEmitIntoClient` by default. A fragile function cannot reference
restricted imports such as `@_implementationOnly`, `internal import`,
etc.

We consider them as fragile only for type-checking, at the SIL level
they remain treated as normal functions like before. This allows the
later optimization to work as expected. We may want to likely align both
ends of the compiler once this is properly supported in sema.

This is enabled only in embedded. Fragile functions in library-evolution
are marked with attributes and are already checked. We do not need this
is non-embedded non-library-evolution as CMO handles inlining and
already takes into consideration `@_implementationOnly` imports.

Note: We'll need a similar check for memory layouts exposed to clients.
We can still see compiler crashes and miscompiles in this scenario. This
will apply to both embedded and non-library-evolution modes. That will
likely need to extend the `@_neverEmitIntoClient` attribute and may
require a staged deployment.

rdar://161365361
2025-10-14 14:14:54 -07:00
Doug Gregor
7e8f782457 [IRGen] Use linkonce_odr hidden linkage for _swift_dead_method_stub
IRGen introduces the symbol _swift_dead_method_stub for dead vtable
entries to point to. This symbol was given internal linkage, which
would result in multiply-defined symbols when combined with Embedded
Swift's use of aggressive CMO. Switch to linkonce_odr hidden linkage
so multiple versions of this symbol can be coalesced by the linker
(and dropped if not needed).

Fixes rdar://162392119.
2025-10-13 08:47:16 -07:00
Erik Eckstein
0c8877ea33 CrossModuleOptimization: fix a crash with dynamic functions
Use the correct `FunctionRefBaseInst` API to get the callee.

Fixes a compiler crash when using `dynamic` functions in embedded swift.
rdar://162309631
2025-10-13 07:41:39 +02:00
Doug Gregor
d54f6018bc Merge pull request #84754 from DougGregor/suppress-warnings-embedded-restrictions
[Frontend] Don't enable EmbeddedRestrictions warnings under -suppress-warnings
2025-10-07 21:35:30 -07:00
Doug Gregor
70f3b2ef9c Merge pull request #84752 from DougGregor/nonunique-linkonce 2025-10-07 21:19:33 -07:00
Doug Gregor
81070ef371 Merge pull request #84719 from DougGregor/nonunique-protocol-conformance 2025-10-07 20:04:42 -07:00