Commit Graph

12816 Commits

Author SHA1 Message Date
nate-chandler
b5fed1546e Merge pull request #84763 from nate-chandler/cherrypick/release/6.2/rdar161606892_2
6.2: [VariadicGenerics] Fix memeffect of metadata accessor.
2025-10-08 15:36:36 -07:00
Nate Chandler
dab39bbf91 [VariadicGenerics] Fix memeffect of metadata accessor call.
Follow up to https://github.com/swiftlang/swift/pull/84635/.

The metadata accessor for a variadic generic type takes as arguments
packs of metadata records and witness tables, and each such pack is
passed in a buffer. So the call to any such accessor is not correctly
annotated memory(none).

rdar://161606892
2025-10-08 06:50:04 -07:00
Nate Chandler
9ab224212e [VariadicGenerics] Fix memeffect of metadata accessor.
The metadata accessor for a variadic generic type takes as arguments
packs of metadata records and witness tables, and each such pack is
passed in a buffer.  So any such accessor is not correctly annotated
`memory(none)`.

rdar://161606892
2025-10-08 06:50:04 -07:00
Alejandro Alonso
fbf07e7d54 Merge pull request #84655 from Azoy/negative-nancy
[IRGen] Correctly sext instead of zext for negative integer types
2025-10-07 14:27:08 -07:00
Gábor Horváth
295b5c60d7 Merge pull request #84430 from Xazax-hun/unbalanced-refcount-in-pods-on-6.2
[6.2][cxx-interop] Fix over-releasing reference members of trivial C++ types
2025-09-25 16:12:02 +01:00
Egor Zhdan
c5c33d6c5a [cxx-interop] Allow retain/release operations to be methods
Some foreign reference types such as IUnknown define retain/release operations as methods of the type.

Previously Swift only supported retain/release operations as standalone functions.

The syntax for member functions would be `SWIFT_SHARED_REFERENCE(.doRetain, .doRelease)`.

rdar://160696723
(cherry picked from commit e78ce6165f)
2025-09-23 14:45:41 +01:00
Gábor Horváth
65bf53ddf4 [6.2][cxx-interop] Fix over-releasing reference members of trival C++ types
Explanation: Large trivial types were copied via memcpy instead of doing a
field-wise copy. This is incorrect for types with reference fields where we also
need to bump the corresponding refcounts. This PR makes sure that trival
C++ types with reference members are not trivial in Swift.
Issues: rdar://160315343
Original PRs: #84321
Risk: There is a low chance of breaking source compatibility as some
types that used to be BitwiseCopyable are no longer considered as such.
However, code relying on that probably has latent memory safety bugs.
Testing: Added a compiler test.
Reviewers: @egorzhdan, @j-hui
2025-09-22 16:21:58 +01:00
Gábor Horváth
859f60d4d4 Merge pull request #84120 from Xazax-hun/no-vwt-for-anon-types-on-6.2
[6.2][cxx-interop] Basic support for anonymous structs with non-copyable fields
2025-09-17 15:38:33 +02:00
John McCall
81b2dae5e9 Fix IRGen to pass complete metadata to various concurrency builtins
Fixes rdar://146155888
2025-09-11 18:42:33 -04:00
Gábor Horváth
95ff51c739 Merge pull request #84199 from Xazax-hun/no-reflection-anon-types
[cxx-interop] Restrict the uses of anonymous types
2025-09-11 09:56:35 +01:00
Gábor Horváth
22f5eb79aa Merge pull request #84152 from Xazax-hun/add-nullptr-to-metadata
[cxx-interop] Make the size of anonymous types metadata is unchanged
2025-09-09 10:44:07 +01:00
Egor Zhdan
0059c4250a Merge pull request #84090 from egorzhdan/egorzhdan/6.2-frt-indirection
🍒[cxx-interop] Pass foreign reference types with correct level of indirection
2025-09-05 21:47:16 +01:00
Gábor Horváth
e58347e8d2 [6.2][cxx-interop] Basic support for anonymous structs with non-copyable fields
Explanation: Anonymous structs cannot be copied or moved, these operations only
can happen to their enclosing non-anonymous types. Stop trying to emit special
member functions and value witness tables for these structs. This fix is
required to unblock a high priority libc++ change that fixes an
unintended ABI break.
Issues: rdar://159928354
Original PRs: #84105
Risk: Medium. I believe this is the right change but hard to anticipate
if something depends on the presence of these operations (which is
likely to be a bug). But this is required to unblock an important libc++
fix.
Testing: Added a compiler test.
Reviewers: @rjmccall
2025-09-05 10:53:46 +01:00
Dario Rexin
5bc14ff7e6 Merge pull request #84007 from drexin/wip-159143492-6.2
[6.2][IRGen] Fix computation of spare bits for fixed arrays
2025-09-04 13:25:39 -07:00
Egor Zhdan
8178aa8b65 [cxx-interop] Pass foreign reference types with correct level of indirection
When calling a C++ function that takes a reference to a pointer to a foreign reference type, Swift would previously pass a pointer to the foreign reference type as an argument (instead of a reference to a pointer), which resulted in invalid memory accesses.

This was observed when using `std::vector<ImmortalRef*>::push_back`.

rdar://150791778
(cherry picked from commit 0a766e59ce)
2025-09-03 20:42:06 +01:00
Dario Rexin
1a10f85782 [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:27:10 -07:00
Alejandro Alonso
f15d600460 Merge pull request #83953 from Azoy/62-extended-existentials
[6.2] [AST & Runtime] Correctly mangle extended existentials with inverse requirements
2025-08-30 11:10:21 -07:00
nate-chandler
c89bc61167 Merge pull request #83711 from nate-chandler/cherrypick/release/6.2/rdar158083136
6.2: [InlineArray] Fix outlining metadata collection.
2025-08-27 12:57:54 -07:00
Alejandro Alonso
0877b04efe Merge pull request #81365 from Azoy/my-existentials
[AST & Runtime] Correctly mangle extended existentials with inverse requirements
2025-08-27 10:00:59 -07:00
Egor Zhdan
5c7d5f6414 [cxx-interop] Enable reference-counted types on Windows
This enables the use of reference-counted foreign reference types on Windows. As it turns out, the assertion that was failing on Windows previously was unnecessary. This also enabled many of the tests on Windows.

rdar://154694125 / resolves https://github.com/swiftlang/swift/issues/82643

(cherry picked from commit b440c4ff70)
2025-08-21 11:20:14 +01:00
Nate Chandler
9178277dbf [InlineArray] Fix outlining metadata collection.
The metadata of the element type is required for outlining.

rdar://158083136
2025-08-13 16:52:26 -07:00
Doug Gregor
8b10bdc189 [Clang importer] Allow noncopyable C structs to define "destroy" operation
A C struct can be imported as noncopyable, but C doesn't have
destructors, so there is no way to provide user-defined logic to
perform the destruction. Introduce a new swift_attr that applies to
imported noncopyable types and which provides such a "destroy"
operation. It can be used like this:

    typedef struct __attribute__((swift_attr("~Copyable")))
                   __attribute__((swift_attr("destroy:wgpuAdapterInfoFreeMembers")))
      WGPUAdapterInfo { /*...*/ } WGPUAdapterInfo;

    void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo adapterInfo);

This will bring the WGPUAdapterInfo struct in as a noncopyable type
that will be cleaned up by calling wgpuAdapterInfoFreeMembers once it
is no longer in use.

Implements rdar://156889370.

(cherry picked from commit 6ba560fb4b)
2025-08-12 11:27:54 -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