Commit Graph

12796 Commits

Author SHA1 Message Date
John McCall
75f228040c Fix IRGen to pass complete metadata to various concurrency builtins
Fixes rdar://146155888
2025-09-11 20:06:41 -04:00
Dario Rexin
8dc35c846c [IRGen] Fix computation of spare bits for fixed arrays
rdar://159143492

Previously all bits after the spare bits of the first element were marked as spare bits. This caused enum tags to be stored in bits used by the payload.
2025-09-03 10:26:04 -07:00
Augusto Noronha
7b46d41823 [DebugInfo] Fix infinite recursion when opaque return type is defined
inside function returning it

A stack overflow would happen when the compiler tried emitting debug
info for a function whose opaque return type was declared inside the
function itself. This fixes the issue by emitting a forward declaration
for the function before emitting it.

rdar://150313956

(cherry picked from commit c03831f70d)
2025-08-05 17:36:28 -07:00
Shubham Sandeep Rastogi
87925d5c37 Revert "Set the Compilation directory when generating the Skeleton CU"
This reverts commit b286b1c3a5.

Reverted because fix for rdar://131726681 broke rdar://154247270

Reverting and will try to fix properly later.

We will not land this in 6.2

(cherry picked from commit 5b4fdd6e62)
2025-07-24 11:02:31 -07:00
Dario Rexin
4a8ada3fb0 [IRGen] Set generic context before getting call emission in visitFullApplySite
rdar://149007227

Without the generic context, the result type can't be mapped into the current context, causing the compiler to crash.
2025-07-15 13:36:16 -07:00
Doug Gregor
d0d1a42b7c [IRGen] Mangling of nonisolated(nonsending) function types was introduced in Swift 6.2
We can't use mangled named when back-deploying earlier.
2025-07-11 14:26:32 -07:00
Doug Gregor
81dc98b0c8 [IRGen] Don't use mangled names for metadata including Span et al when back-deploying
If back-deploying prior to the introduction of name mangling and
runtime support for invertible constraints (~Copyable, ~Escapable),
don't use mangled names to access metadata. The code already existed
for this, but had a carve-out that still used mangled names for
standard library types that have always existed but got generalized to
support non-copyable & non-escapable types.

Tweak that carve-out to not apply to types like Span that come from a
back-deployment library. Fixes crashes when using metadata for Span et
al on older platforms.

Fixes rdar://155639204.
2025-07-11 14:26:32 -07:00
Zachary 'Clack' Cole
305125d8a0 Merge pull request #82694 from clackary/cherrypick/autodiff-fix-link-entity-diff-witness
[6.2 🍒][AutoDiff] Use `LinkEntity::SecondaryPointer` for diff witness
2025-07-02 11:30:48 -06:00
Doug Gregor
dc27da053c [SE-0371] Back-deploy support for main-actor-isolated deinit
When targeting a platform that predates the introduction of isolated
deinit, make a narrow exception that allows main-actor-isolated deinit
to work through a special, inlineable entrypoint that is
back-deployed. This implementation

  1. Calls into the real implementation when available, otherwise
  2. Checks if we're on the main thread, destroying immediately when
we are, otherwise
  3. Creates a new task on the main actor to handle destruction.

This implementation is less efficient than the implementation in the
runtime, but allows us to back-deploy this functionality as far back
as concurrency goes.

Fixes rdar://151029118.
2025-06-30 17:40:00 -07:00
Konrad `ktoso` Malawski
9c9c8b6408 Merge pull request #82612 from ktoso/pick-wiup-cleanup-mergeconflict-potential 2025-07-01 07:25:14 +09:00
Daniil Kovalev
a86e717991 [AutoDiff] Use LinkEntity::SecondaryPointer for diff witness
If `LinkEntity::isTypeKind()` is true, `IRGenModule::getAddrOfLLVMVariable`
assumes that we can safely call `LinkEntity::getType()`, which does
`reinterpret_cast` of `LinkEntity::Pointer` to `TypeBase *`. However, for SIL
differentiability witness, the pointer has `SILDifferentiabilityWitness *`
type, which is not derived from `TypeBase`. So, such a cast is not allowed.

Just as with `ProtocolWitnessTableLazyAccessFunction` and
`ProtocolWitnessTableLazyCacheVariable` link entity kinds (which are
also type kinds), we should use `SecondaryPointer` instead of `Pointer` for
storing payload here, while setting `Pointer` to `nullptr`.

(cherry picked from commit 77a3873448)
2025-06-30 15:18:55 -06:00
Joe Groff
f299c6a856 Merge pull request #82344 from jckarter/addressable-for-dependencies-vw-flag-6.2
[6.2] Add an "addressable for dependencies" flag to value witness flags.
2025-06-30 12:54:25 -07:00
Konrad 'ktoso' Malawski
6989f05abe Revert revert & fix distributed protocol and TBD handling
This reverts a revert that was done internally here https://stashweb.sd.apple.com/projects/DEVTOOLS/repos/swift/pull-requests/8697/overview
and corrects some issues with the reverted code while at it.

This fix is crucial to land for correctness and necessary to fix the
rdar://144568615 which had a fix submitted however that assumed that
THIS change also was present, so the fix submitted for 144568615 is
incomplete without this change as well.

This enables, and adds more TBD testing and was explicitly checked
against the GameKit reproducer project from rdar://144568615.

I will also upstream the necessary parts missing from OSS repo there so
we have alignment between all branches.
2025-06-30 11:47:29 +09:00
Dave Lee
2d40d0e73d IRGen: Emit objc type encoding for ivars
ObjC ivar metadata has up to now emitted an empty string for the ivar's objc type encoding. Conversely, ObjC property metadata is emitted with an objc type encoding. This changes the compiler to also emit an objc type encoding for ivars.

The motivation for this change is for lldb to print ivars of classes declared in Objective-C but implemented in Swift, as defined in [SE-0436](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0436-objc-implementation.md). Without the presence of type encoding for ivars, lldb is unable to present to the user the ivars/properties of instances of such classes.

rdar://138569299
(cherry picked from commit ce7a3b39a4)
2025-06-27 16:24:49 -07:00
Dario Rexin
96d6eb87ee Merge pull request #82352 from drexin/wip-153681688-6.2
[6.2][IRGen] Fix placeholder logic for emission of conditionally inverted protocols
2025-06-23 18:22:26 -07:00
Egor Zhdan
bf20dab48c Merge pull request #82185 from swiftlang/susmonteiro/6.2-class-metadata-private-fields
🍒 [cxx-interop] Support for printing C++ foreign references
2025-06-23 17:52:00 +01:00
Dario Rexin
dc71b7e4c4 Merge pull request #82241 from drexin/wip-149882902-6.2
[6.2][IRGen] Emit null check before swift_conformsToProtocol for nullable metatypes
2025-06-18 15:56:50 -07:00
Dario Rexin
ae8c455170 [IRGen] Fix placeholder logic for emission of conditionally inverted protocols
rdar://153681688

Instead fo counting the actual conformances, the logic took the size of the bit field, i.e. used the highest set bit, so when a type had a conditional conformance only on ~Escapable, but not on ~Copyable, it would still add 2 placeholders, but only fill one.
2025-06-18 14:47:40 -07:00
Joe Groff
009ce4b82a Add an "addressable for dependencies" flag to value witness flags.
This may be useful for type layout of borrow fields in the future, should we
decide that addressable-for-dependencies borrows should always be represented
by a pointer. rdar://153650278
2025-06-18 12:47:36 -07:00
Nate Chandler
2cf7ef5fdc [IRGen] Fix FixedArray of fixedSize 1 elt addring.
When a `FixedArray`'s fixed size is 1, it looks like `[1 x %Ty]`. Given
an array's address, performing an operation on each element's address
entail's indexing into the array to each element's index to produce an
element's address for each index.  That is true even when the array
consists of a single element.  In that case, produce an address for that
single element by indexing to index 0 into each passed-in array.

rdar://151726387
2025-06-16 20:34:22 -07:00
Dario Rexin
533e215f6f [IRGen] Emit null check before swift_conformsToProtocol for nullable metatypes
rdar://149882902

swift_conformsToProtocol does not properly handle nullptr values, which can currently be passed if the source type is an optional metatype. This change adds emission of a null check before calling the runtime function in these cases.
2025-06-13 17:00:49 -07:00
Alastair Houghton
20df87c940 [IRGen] Don't use GOTPCREL relocations for x86 ELF.
Unforunately, x86 ELF linkers like to optimize GOTPCREL relocations by
replacing `mov` instructions that go via the GOT with `lea` instructions
that do not.

That would be fine, but they aren't very selective and will happily
perform this transformation in non-code sections if they think that
the bytes before a relocation look like a `mov` instruction.

This corrupts our metadata.

rdar://148168098
2025-06-13 10:25:56 +01:00
susmonteiro
862efcf3a0 [cxx-interop] Support for printing C++ foreign references
Adds support for printing a C++ foreign reference in Swift.

Also skips metadata of private fields in C++ records imported as Swift classes, following up on #81035

(cherry-picked from 848fad0021)
2025-06-11 17:13:01 +01:00
Konrad `ktoso` Malawski
2e368ce31c Merge pull request #82010 from ktoso/pick-wip-fix-computed-variables-arm64-protocol-dist
[6.2][Distributed] Fix computed properties in protocols on arm64e
2025-06-10 09:08:59 +09:00
Allan Shortlidge
c9d5e4b26c Merge pull request #82038 from tshortli/language-modes-and-features-in-module-trace-6.2
[6.2] Frontend: Add language mode and enabled features to `.SWIFT_TRACE`
2025-06-05 23:35:32 -07:00
Arnold Schwaighofer
cf81104407 Merge pull request #80989 from aschwaighofer/loadable_by_address_fix_yields_pattern_subst_6.2
[6.2] LoadableByAddress: Fix shouldTransformYields to use (properly) substituted types
2025-06-05 17:16:31 -07:00
Erik Eckstein
7435a0366a IRGen: Fix an unused static function warning.
Move a static inline function into the cpp file.

NFC.
2025-06-05 15:39:33 -07:00
Pavel Yaskevich
608a37ad04 Merge pull request #81997 from xedin/using-for-default-isolation-in-file-context-6.2
[6.2][AST/Sema] SE-0478: Implement using declaration under an experimental flag
2025-06-05 00:44:21 -07:00
Konrad 'ktoso' Malawski
362783c0f8 [Distributed] Don't drop dist get accessors from witness tables.
This actually manifested as an pointer auth crash, but the real reason
being is that we messed up the order of elements in the witness table.
If we'd skip the accessor like this, the types we sign/auth with would
no longer align and manifest in a crash.

There is no real reason to skip this entry so we just bring it back, and
avoid making this special in any way.

This unlocks a few tests as well as corrects any distributed+protocol
use where a requirement distributed var was _followed by_ other
requirements.

resolves rdar://125628060
2025-06-05 12:16:20 +09:00
Pavel Yaskevich
d057429e9a [AST] Add new declaration - using
Initially this declaration is going to be used to determine
per-file default actor isolation i.e. `using @MainActor` and
`using nonisolated` but it could be extended to support other
file-global settings in the future.

(cherry picked from commit aabfebec03)
2025-06-04 13:16:55 -07:00
Konrad 'ktoso' Malawski
7c21ea8b7d [Distributed] More IRGenMangler fixes for distributed thunks
We also need to mangle the dispatch thunks to distributed thunks
uniquely.
2025-06-04 21:07:56 +09:00
Konrad 'ktoso' Malawski
84a8adbd4c [Distributed] IRGen mangler must mangle distributed thunk 2025-06-04 21:07:06 +09:00
Susana Monteiro
71b7de4f61 Merge pull request #81867 from swiftlang/susmonteiro/6.2-metadata-private-fields
🍒 [cxx-interop] Ensure field offset vector matches the structs/fields metadata
2025-06-02 16:46:00 +01:00
Egor Zhdan
ee5f7ad262 [cxx-interop] Do not crash when emitting layout of std::string
If a `[[no_unique_address]]` field has zero size according to Clang, and field has a type that isn't representable in Swift, Swift would previously try to add an opaque field of size 1 for it.

This is wrong and was causing crashes for `std::string` while emitting a padding field:
```
_LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);
```

rdar://151941799
(cherry picked from commit a7c174431f)
2025-05-30 17:33:31 +01:00
susmonteiro
2e66b84e9f [cxx-interop] Ensure field offset vector matches the structs/fields metadata
The field offset vector should be consistent with the type metadata emitted for structs and fields. If we don't emit metadata for private C++ fields, then the field offset vector shouldn't include these fields.

This is a follow-up to https://github.com/swiftlang/swift/pull/81838/https://github.com/swiftlang/swift/pull/81740
(cherry picked from c3077bfedb and 7686f05f28)
2025-05-30 13:12:47 +01:00
Shubham Sandeep Rastogi
bc7e9531f2 Merge pull request #81753 from rastogishubham/CompDir6.2
[6.2] Set the Compilation directory when generating the Skeleton CU
2025-05-28 10:06:33 -07:00
Shubham Sandeep Rastogi
de425be15b Set the Compilation directory when generating the Skeleton CU
When creating a skeleton Compile Unit, the DIFile passed
in, it always uses the include path. This leads to the DW_AT_comp_dir
being wrong, if the -file-compilation-dir option is passed, we need to
use the remapped compilation dir passed in to the DIFile instead.

This patch fixes that problem.

(cherry picked from commit b286b1c3a5)
2025-05-23 14:43:57 -07:00
susmonteiro
d81d6547ba [cxx-interop] Fix metadata mismatch regarding fields of structs
In https://github.com/swiftlang/swift/pull/78467 and https://github.com/swiftlang/swift/pull/78961, we stopped emitting metadata for private C++ fields. However, this created a mismatch between the fields emitted and the number of fields + their offsets in the StructDescriptor.

rdar://147263490
(cherry picked from commit 72b13b3b48)
2025-05-23 11:16:09 +01:00
Stephen Canon
85b304220f [6.2] Implement Builtin.select binding llvm select instruction (#81665)
Not used (yet), but needed to implement SIMD.replacing(with:where:)
idiomatically, and probably useful otherwise.

**Explanation:** Makes select available in Swift's builtin module, which
allows implementing concrete SIMD operations more efficiently.
**Risk:** Low. New builtin protected by a feature flag, currently
unused.
**Testing:** New tests added.
**Reviewers:** @eeckstein, @Azoy 
**Main branch PR:** https://github.com/swiftlang/swift/pull/81598
2025-05-21 19:14:33 -04:00
Slava Pestov
7a334bc094 Merge pull request #81565 from slavapestov/fix-issue-78191-6.2
[6.2] IRGen: Fix miscompile when a generic parameter is fixed to a tuple containing a pack
2025-05-20 15:35:23 -04:00
Erik Eckstein
1f3bff3f77 IRGen: fix failing unconditional class casts
When unconditionally casting from a base to a final derived class, e.g. `base as! Derived`, the program did not abort with a trap.
Instead the resulting null-pointer caused a crash later in the program.
This fix inserts a trap condition for the failing case of such a cast.

rdar://151462303
2025-05-20 07:39:16 +02:00
Erik Eckstein
442db1bdc0 IRGen: extract generating a cond_fail into a utility function emitConditionalTrap
NFC
2025-05-20 07:39:16 +02:00
Erik Eckstein
31cd6f9ffd IRGen: move FailBBs from IRGenSILFunction to its base class IRGenFunction
NFC
2025-05-20 07:39:16 +02:00
Slava Pestov
85bdf45b44 IRGen: Fix miscompile when a generic parameter is fixed to a tuple containing a pack
If you had something like:

    struct G<T> {
      func f<each U>(_: repeat each U) where T == (repeat each U) {}
    }

We would fulfill 'each U' from the metadata for 'G<(repeat each U)>',
by taking apart the tuple metadata for `(repeat each U)` and forming
a pack.

However this code path was only intended to kick in for a tuple
conformance witness thunk. In the general case, this optimization
is not correct, because if 'each U' is substituted with a
one-element pack, the generic argument of `G<(repeat each U)>` is
just that one element's metadata, and not a tuple. In fact, we
cannot distinguish the one-element tuple case, because the wrapped
element may itself be a tuple.

The fix is to just split off FulfillmentMap::searchTupleTypeMetadata()
from searchTypeMetadata(), and only call the former when we're in
the specific situation that requires it.

- Fixes https://github.com/swiftlang/swift/issues/78191.
- Fixes rdar://problem/135325886.
2025-05-16 17:35:30 -04:00
Dario Rexin
8299d7be0b Merge pull request #81531 from drexin/wip-151176697-6.2
[6.2][IRGen] Don't set HasLayoutString flag on non-copyable generic types
2025-05-15 20:27:24 -07:00
Dario Rexin
75c581d3a0 [IRGen] Don't set HasLayoutString flag on non-copyable generic types
rdar://151176697

While generic types generally have layout strings (when enabled), non-copyable types don't, so
we have to make sure the flag does not get set.
2025-05-15 10:25:33 -07:00
Slava Pestov
bf8d836074 IRGen: Fix out-of-order task_dealloc with parameter pack metadata
We deallocate an instruction's packs at points where no further
control flow path uses the value. In the case of an alloc_stack,
this will be right after the dealloc_stack. Thus, if alloc_stack
allocates some packs to build type metadata for a tuple type
that contains a pack, and then proceeds to allocate a value
large enough to hold the tuple, we will free the second allocation
first, before we free the pack, as expected.

However, after stack allocating the value, alloc_stack does
some further work to emit debug info. This could result in
emission of additional metadata packs.

Split up the debug info emission into two parts; the first we do
before we perform the stack allocation, the rest we do after.

- Fixes https://github.com/swiftlang/swift/issues/67702.
- Fixes rdar://problem/141363236.
2025-05-15 12:24:14 -04:00
Shubham Sandeep Rastogi
cf204f2e05 Set debug location to Coroutine call expression
If debug info generation is enabled, set debug location to the
coroutine call instruction to make sure there are no issues with invalid
debug information in LTO.

This happens because in LTO, if a call to a function doesn't contain a
debug location, we see the warning:

inlinable function call in a function with debug info must have a
!dbg location

ld: warning: Invalid debug info found, debug info will be stripped

Which then strips the debug info from the entire .o file linked into the
dylib.

(cherry picked from commit 17e756ba81)
2025-05-13 17:34:05 -07:00
Dario Rexin
51489ac69b Merge pull request #81381 from drexin/wip-wip-150722907-6.2
[6.2][IRGen] Emit llvm fneg operation for Builtin.fneg_*
2025-05-09 09:27:16 -07:00
Konrad `ktoso` Malawski
d3248fc0c4 Merge pull request #81394 from ktoso/pick-wip-sign-protocol-ptr
[6.2][Distributed] pointer auth protocol pointers as we use conformsToProtocol
2025-05-09 19:26:46 +09:00