Commit Graph

1597 Commits

Author SHA1 Message Date
Doug Gregor da170e7e8e [Embedded] Only emit global variables from another module if they are @used 2026-05-20 08:35:25 -07:00
Doug Gregor b393f0bfa8 [Embedded] Eliminate special-case handling of declarations used externally
Declarations that are potentially used externally, including those
exposed to foreign languages (e.g., via `@c`), placed in a specific
section (`@section`), or explicitly marked used (`@used`) are
generally eagerly emitted in IR. Embedded Swift was overriding this in
a partial manner, applying specific linkage rules to them to force
them to have unique definitions (regardless of the code generation
model) and then overriding the behavior later on when it came to lazy
emission.

Remove the special cases for Embedded. These declarations will now
follow the same linkage rules as all other declarations, greatly
simplifying the "is non-unique definition" check, and will be
considered as being emitted non-lazily.
2026-05-20 08:35:18 -07:00
Doug Gregor 14bc0baecf Introduce the notion of an "effective" code generation model
The code generation model for a particular declaration or conformance
can be defined explicitly with `@export(interface)`,
`@export(implementation)`, or `@inlinable` (for declarations),
indicating where the definition will occur.

Embedded Swift also has some limitations on what can be emitted into
IR. For example, a generic function cannot be `@export(interface)`
because Embedded Swift does not support unspecialized generics.

Compute the effective code generation model based on what was
explicitly specified, the limitations of the model, and the default
code generation model for the given module, which defaults to
"inlinable" but can be made "implementation" by the DeferredCodeGen
feature. Use the effective code generation model for IR- and SIL-level
determinations of linkage and where to emit symbols.
[WIP] Start computing and using the "effective" code generation model

FIXUP linkage of the alias symbol
2026-05-20 08:35:11 -07:00
Doug Gregor 09dd9db3e1 [Embedded] Extend @export(interface) to non-generic types
@export(interface) makes it so that Embedded Swift will emit a strong
definition of a symbol in its defining module, and use that symbol in
other modules. Extend this notion to non-generic types, where we will
emit a strong definition for the full type metadata, and let it be
referenced from other modules rather than the lazy, shared emission.

Implements rdar://176392354.
2026-05-08 14:23:21 -07:00
Doug Gregor 2aabf1109d [Embedded Swift] Lazily emit metadata for metatypes and existentials
Following the emission of metadata for tuple and function types, also
lazily emit (sparse) metadata for metatype and existential types. This
is needed when forming existential values.
2026-04-27 14:02:04 -07:00
Kavon Farvardin ad0ea4a1b7 Merge pull request #88495 from kavon/relative-indexing-reparentable
Reparenting: use resilient indexing to access base witness table
2026-04-22 02:57:04 -07:00
Kavon Farvardin 84643dbb03 IRGen: fix use-after-free with extension descriptors
To summarize the theory based on
`validation-test/Evolution/test_protocol_add_reparented.swift`:

1. Two extensions on MultiParent share the mangled name
   $s23protocol_add_reparented11MultiParentPAAEMXE for their context descriptor
2. The first extension to be emitted creates a GlobalVariable with type
   TypeContextDescriptorTy (forward decl)
3. The second extension finds it in getAddrOfSharedContextDescriptor via
   Module.getGlobalVariable() and inserts the same pointer in GlobalVars
4. When the first extension's descriptor gets defined, getAddrOfLLVMVariable
   creates a new GlobalVariable and erases the old one.
5. The second extension's GlobalVars entry is now a dangling pointer, as the
   act of defining it for the first extension definition didn't update the map.

This patch does seem to fix the dangling pointer, though there is probably
a better way to fix the underlying bad ordering of events.
2026-04-21 13:05:48 -07:00
Doug Gregor aad51cab01 [Embedded] Remove the ability to disable existentials in Embedded Swift
Support for existentials in Embedded Swift has been available for a
little while now and appears to be solid. Remove the ability to disable
them (via `-disable-experimental-feature EmbeddedExistentials`), both
because it simplifies the code and because it's an ABI break to
disable the feature.
2026-04-17 17:38:01 -07:00
Erik Eckstein 68b55ff3ce IRGen: generate conformance entries in vtables for fast existential casts 2026-04-03 07:49:35 +02:00
Doug Gregor 454237a083 Allow the OSLog module to specify the log string section name
When building the `OSLog` module, look for a variable named
`osLogStringSectionName`. It must have a string literal as its
initializer, which provides the section name where the log strings
should be emitted. The `OSLog` module should contain something like
this:

    let osLogStringSectionName = "__TEXT,__logit"

When not present, the compiler will default to
`__TEXT,__oslogstring,cstring_literals`, which was previously the
hardcoded section name. Now, OSLog can customize the name.

Implements rdar://171571056
2026-03-09 10:27:54 -07:00
Konrad `ktoso` Malawski 4c1e2c55cc [Distributed] distributed accessor fixes against optimization (#87453)
WIP, still not enough

resolves rdar://168881945
2026-03-02 13:46:11 +09:00
Arnold Schwaighofer 3dd5286d48 [embedded] [IRGen] Avoid creating "aliases" for type metadata address points in client modules
We don't need to export the type metadata address point alias in clients that
lazily emit other module's type metadata. There will be an exported
metadata symbol in the originating module for that purpose.

Instead, satisfy any local uses of the metadata address point uses by its
underlying address computation.

This is to workaround a bug where LLVM generates the wrong assembly for
weak aliases that point to an offset of another symbol.

```
@"$e1C7MyClassCySiGN" = weak_odr hidden alias %swift.type, getelementptr
inbounds (<{ ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }>,
ptr @"$e1C7MyClassCySiGMf", i32 0, i32 1)
```

Generates:

```
.weak_reference _$e1C7MyClassCySiGN
.private_extern _$e1C7MyClassCySiGN
.alt_entry  _$e1C7MyClassCySiGN
  _$e1C7MyClassCySiGN = _$e1C7MyClassCySiGMf+8
```

Instead of

```
.weak_definition _$e1C7MyClassCySiGN
.private_extern _$e1C7MyClassCySiGN
.alt_entry  _$e1C7MyClassCySiGN
  _$e1C7MyClassCySiGN = _$e1C7MyClassCySiGMf+8
```

Leading for "weak"ness to be ignored and duplicate type medata symbol linkage
errors.

rdar://169573918
2026-02-06 12:24:08 -08:00
Dario Rexin 1f8e88d659 Merge pull request #85908 from drexin/wip-163631865
[IRGen] Use proper linkage for async function pointers to partial app…
2025-12-11 09:52:51 -08:00
Arnold Schwaighofer 4d879967a7 [embedded] Feature::EmbeddedExistentials requires Feature::Embedded 2025-12-09 10:21:51 -08:00
Arnold Schwaighofer d06929ccd2 [embedded] Add support for some foreign metadata 2025-12-08 15:35:43 -08:00
Dario Rexin 0fdca11e46 [IRGen] Use proper linkage for async function pointers to partial apply forwarders
rdar://163631865

Under certain circumstances the same symbol can be used for different implementations of the forwarders.
The forwarders themselves are already emitted with "internal" LLVM linkage, but some particularities in
the mapping from SILLinkage to LLVM linkage caused async function pointers to partial apply forwarders
to be emitted with linkonce_odr instead, which caused the wrong forwarder to be called at runtime, causing
unexpected behavior and crashes.
2025-12-08 11:30:22 -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
Arnold Schwaighofer b9f6454386 [embedded] Add support for storing/casting function types 2025-11-21 14:55:32 -08:00
Slava Pestov bfb6c826c4 Merge pull request #85560 from slavapestov/fix-ridiculous-irgen-thing
IRGen: Remove unsafe usage of static variable
2025-11-20 02:54:38 -05:00
Slava Pestov ca0f310242 IRGen: Remove unsafe usage of static variable
We cannot use 'static' linkage for something that points into the
ASTContext, because there might be more than one ASTContext in a
single process.

Also, fix the spelling mistake in a related function name.
2025-11-19 10:19:18 -05:00
Arnold Schwaighofer 17447a3378 Merge pull request #85551 from aschwaighofer/wip_embedded_exit
Preliminary support for existential in embedded Swift
2025-11-18 15:10:40 -05:00
Arnold Schwaighofer a725de5ba6 Address review comments 2025-11-17 15:52:00 -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
Anthony Latsis dd5ac838e5 AST: Properly disallow isa/cast/dyn_cast on Type
We currently disallow these by deleting them in the `swift` namespace.
This approach has several loopholes, all of which ultimately work
because we happen to define specializations of `simplify_type` for
`swift::Type`:
* `llvm::isa/cast/dyn_cast`. The deleted partial specializations will
  not be selected because they are not defined in the `llvm` namespace.
* The argument is a non-const `Type`. The deleted function templates
  will not be selected because they all accept a `const Type &`, and
  there is a better `Y &Val` partial specialization in LLVM.
* Other casting function templates such as `isa_and_nonull` and
  `cast_if_present` are not deleted.

Eliminate these loopholes by instead triggering a static assertion
failure with a helpful message upon instantiation of `CastInfo` for
`swift::Type`.
2025-11-14 19:02:41 +00:00
Slava Pestov 819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05: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
Yuta Saito 3e834ccf9f Merge pull request #85106 from kateinoigakukun/yt/fix-dead-stub-comdat-wasm
[wasm][IRGen] Stop attaching COMDATs to dead stub methods
2025-10-29 15:29:48 +09:00
Yuta Saito 50298bd8e5 [wasm][IRGen] Stop attaching COMDATs to dead stub methods
When generating dead stub methods for vtable entries, we should not
attach COMDATs to their definitions. This is because such stubs may be
referenced only through aliases, and the presence of a COMDAT on the
stub definition would cause the aliased symbol, we want to keep, to be
discarded from the symbol table when other object files also have a
dead stub method.

Given the following two object files generated:

```mermaid
graph TD
    subgraph O0[A.swift.o]
        subgraph C0[COMDAT Group swift_dead_method_stub]
            D0_0["[Def] swift_dead_method_stub"]
        end
        S0_0["[Symbol] swift_dead_method_stub"] --> D0_0
        S1["[Symbol] C1.dead"] --alias--> D0_0
    end

    subgraph O1[B.swift.o]
        subgraph C1[COMDAT Group swift_dead_method_stub]
            D0_1["[Def] swift_dead_method_stub"]
        end
        S0_1["[Symbol] swift_dead_method_stub"] --> D0_1
        S2["[Symbol] C2.beef"] --alias--> D0_1
    end
```

When linking these two object files, the linker will pick one of the
COMDAT groups of `swift_dead_method_stub`. The other COMDAT group will be
discarded, along with all symbols aliased to the discarded definition,
even though those aliased symbols (`C1.dead` and `C2.beef`) are the
ones we want to keep to construct vtables.

This change stops attaching COMDATs to dead stub method definitions.
This effectively only affects WebAssembly targets because MachO's
`supportsCOMDAT` returns false, and COFF targets don't use
`linkonce_odr` for dead stub methods.

The COMDAT was added in 7e8f782457
2025-10-29 01:17:38 +00: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
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
Doug Gregor bccfe3351e [IRGen] Leave _swift_dead_method_stub Internal for COFF
COFF linkers don't merge linkonce_odr symbols, leading to multiple
definition errors.
2025-10-13 08:47:18 -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
Doug Gregor 70f3b2ef9c Merge pull request #84752 from DougGregor/nonunique-linkonce 2025-10-07 21:19:33 -07:00
Doug Gregor 4952a2c9ef [Embedded] Prefer linkonce_odr to weak_odr for nonunique definitions
This allows the implementation to drop definitions that it does need.
2025-10-07 14:57:31 -07:00
Ellis Hoag 4f254950c1 Merge pull request #84661 from ellishg/fix-objc-cstr-sections
[ObjC][Gen] Emit strs into __cstring if willBeRelativelyAddressed
2025-10-06 23:08:47 -07:00
Mishal Shah 03a599c5be Merge pull request #84606 from swiftlang/rebranch
Merge clang 21.x rebranch into main
2025-10-02 20:17:05 -07:00
Arnold Schwaighofer 7853ba0a7f Merge pull request #84178 from aschwaighofer/inline_always
Add experimental feature `@inline(always)`
2025-10-01 07:23:24 -07:00
swift-ci c00e3621eb Merge remote-tracking branch 'origin/main' into rebranch 2025-09-30 08:56:26 -07:00
Ellis Hoag 2c919e138e [ObjC][Gen] Emit ObjC strings to respective sections (#84300)
Clang emits ObjC strings into special sections

https://github.com/llvm/llvm-project/blob/d5e7c27d53887e6ae490d8e26193a54987728458/clang/lib/CodeGen/CGObjCMac.cpp#L4056-L4072

As far as I can tell from `CGObjCMac.cpp`:
* types go into `__objc_methtype`
* class, category, and protocol names go into `__objc_classname`
* method names, property names, and property types go into
`__objc_methname`

See also https://github.com/swiftlang/swift/pull/84236.
2025-09-30 08:42:11 -07:00
Arnold Schwaighofer 25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
swift-ci 4bbfc9db04 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-13 17:40:42 -07:00
Ellis Hoag f5b70998ab [ObjC][GenDecl] Emit ObjC properties in __objc_methname section 2025-09-11 12:22:24 -07:00
swift-ci 7f8473a16c Merge remote-tracking branch 'origin/main' into rebranch 2025-09-10 02:51:07 -07:00
Anton Korobeynikov 7668666ad2 Support differentiation of wrapped value modify accessors (#78794)
Some fixes for coroutines with normal results and `partial_apply` of coroutines were required.

Fixes #55084
2025-09-10 02:30:26 -07:00
Yuta Saito 401b705295 Merge pull request #81348 from kateinoigakukun/yt/dyn-replace-asan
IRGen: Disable ASan for the auto dynamic replacement array
2025-09-10 17:46:04 +09:00
swift-ci e84e776fc3 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-09 07:15:59 -07:00
Doug Gregor 66a730b638 [Embedded] Remove -mergeable-symbols
This option is no longer necessary, because we emit weak definitions
for any imported modules. Fixes rdar://158364032.
2025-09-08 17:44:51 -07:00
swift-ci 2fc9025c9a Merge remote-tracking branch 'origin/main' into rebranch 2025-09-05 13:49:08 -07:00
Doug Gregor dbc30dc5c9 [IRGen] Cache the layout of the LLVM struct type for statically-initialized objects
Lazy emission of SIL global variables caused us to go through the
creation of two different LLVM struct types for the same
initialization, tripping an assertion in LLVM. Cache it along with
other information about the static-initialized object rather than
rebuilding it.

Also fix the lazy_globals test to account for the laziness,
generalizing it to also run on arm64 so I won't miss it locally.
2025-09-04 15:58:32 -07:00
Doug Gregor 4009814783 [IRGen] Lazily emit SIL global variables
Delay the emission of SIL global variables that aren't externally
visible until they are actually used. This is the same lazy emission
approach that we use for a number of other entities, such as SIL
functions.

Part of rdar://158363967.
2025-09-04 13:03:15 -07:00