Commit Graph

806 Commits

Author SHA1 Message Date
Erik Eckstein
1a009f5b0d ClosureSpecializer: don't specialize captures of stack-allocated Objective-C blocks
Bail if the closure captures an ObjectiveC block which might _not_ be copied onto the heap, i.e optimized by SimplifyCopyBlock.
We can't do this because the optimization inserts retains+releases for captured arguments.
That's not possible for stack-allocated blocks.

Fixes a mis-compile
rdar://154241245
2025-06-26 19:17:37 +02:00
Andrew Trick
a1fc2bc4d4 Merge pull request #80577 from atrick/addressable-escape
Fix GenericSpecializer for addressable parameters.
2025-04-08 12:44:28 -07:00
Slava Pestov
be5d03faa3 SILOptimizer: Remove witness table marking from DeadFunctionElimination
This logic was unconditionally disabled and incorrect.
2025-04-07 22:13:05 -04:00
Slava Pestov
b94a0448f2 SIL: Remove SILWitnessTable::ConditionalConformance 2025-04-07 21:36:05 -04:00
Andrew Trick
6898e33edd [NFC] fix a dropUnusedArguments parameter name 2025-04-07 14:42:10 -07:00
Nate Chandler
4951e9d091 [DFE] Default overrides are alive.
They are used externally and can't be eliminated as dead when there are
no uses from SIL (of which in fact there never are any).
2025-03-25 07:22:43 -07:00
Erik Eckstein
25873407ad CapturePropagation: handle existential keypaths
Handle open_existential_ref instructions which cast keypath instructions before they are passed to a partial_apply.
In Swift language mode 6 keypaths are existentials (e.g. `any WritableKeyPath<Str, Int> & Sendable`) and we need to deal with that in CapturePropagation.

rdar://141370412
2025-03-06 16:48:01 +01:00
Dario Rexin
bde7daa2f2 [SILOptimizer] Don't apply CMO to key paths that reference inaccessible properties
rdar://145095088
2025-02-24 23:07:18 -08:00
elsh
415ede4f4a Assert that makeDeclUsableFromInline is not called in Package CMO mode.
Removes a workaround previously added to handle types that were
overlooked during a serializability detection pass. Now that the
pass has been refactored with instruction visitor, we can replace
it with assert.

Resolves rdar://137711038
2025-02-07 12:52:57 -08:00
elsh
cb0720b77e Package CMO: Prevent serializing types from SDK/system modules imported as @_implementationOnly.
Currently, types from @_implementationOnly modules can be serialized into client modules if their
defining modules are SDK or system modules.

However, @_implementationOnly is intended to hide types from external clients, and may cause
the type’s metadata (e.g., field offsets) to be stripped. If such types are serialized and later
accessed by a client module, it can lead to linker errors due to the missing metadata.

This PR prevents all types imported with @_implementationOnly from being serialized.

Resolves rdar://144181455
2025-02-05 11:00:37 -08:00
Ellie Shin
2de333c9e0 Merge pull request #79035 from swiftlang/elsh/pcmo-imports
Package CMO: Enable serializing decls imported with `@_spiOnly` or `package import`.
2025-01-30 13:15:33 -08:00
elsh
67594f1d8b Package CMO: Enable serializing decls imported with @_spiOnly or package import.
Starting in Swift 6.0, `package` access level and `@_spiOnly` attribute have been increasingly used in import statements.
However, existing import filtering prevented serialization of package APIs that included such decls, leading to a
significant drop in overall serialization. This PR removes these restrictive filters, and allows decls from SDK or system
modules to be included in serialization.

rdar://130788606
2025-01-29 23:22:17 -08:00
Kuba (Brecka) Mracek
7c685ba36b Merge pull request #78915 from kubamracek/embedded-string-switch-with-cache
[embedded] Support _findStringSwitchCaseWithCache in Embedded Swift
2025-01-28 15:56:22 -08:00
Kuba Mracek
500f8ad893 [embedded] Only treat findStringSwitchCaseWithCache as anchor in early DFE, let late DFE collect it if unused 2025-01-28 10:07:54 -08:00
Kuba Mracek
e1d22b5076 [embedded] Avoid changing lookupStdlibFunction, load findStringSwitchCaseWithCache in MPO instead 2025-01-28 10:03:14 -08:00
Erik Eckstein
3e4a4d62ed embedded: make sure to serialize vtable methods with the right linkage
In embedded mode CrossModuleOptimization must visit all vtable methods to make sure that no private/internal methods are serialized.

Fixes a compiler crash
rdar://143153941
2025-01-28 11:19:35 +01:00
elsh
83f1ff6ce4 PackageCMO: fix serializability check for keypath
When a keypath instruction was checked for serializability, its referenced function was
sometimes incorrectly deemed serializable when its referenced method had package or
public access level. This resulted in incorrectly serializing a function that dynamically
accesses a property on a generic type using key path. This PR fixes the issue by skipping
the access level check if the referenced function is determined to be un-serializable.

Resolves rdar://142950306
2025-01-19 23:52:36 -08:00
Erik Eckstein
750d6ec81b CapturePropagation: handle keypaths which are upcast.
Deal with upcast instructions which cast keypath instructions before they are passed to a partial_apply.

rdar://141370412
2024-12-13 07:51:36 +01:00
Erik Eckstein
d71f36be19 GlobalPropertyOpt: handle load_borrow and destroy_addr 2024-12-11 12:32:32 +01:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
elsh
60f0eac73a Fix canSerialize logic 2024-10-29 12:28:39 -07:00
elsh
6676288005 Revert "Revert "Merge pull request #76832 from swiftlang/elsh/pcmo-refactor""
This reverts commit 056d4474ad.
2024-10-28 17:24:24 -07:00
elsh
056d4474ad Revert "Merge pull request #76832 from swiftlang/elsh/pcmo-refactor"
This reverts commit 6b5df5225c, reversing
changes made to b5e3f42cad.
2024-10-18 09:57:43 -07:00
elsh
e0945e9d0d [Package CMO] Move shouldSerialize into canSerializeFunction
Remove redundant checks and consolidate serializability logic into one.
2024-10-16 12:05:04 -07:00
elsh
7b358c25f4 Override getMappedValue with operand type checks. 2024-10-15 15:29:52 -07:00
elsh
f87c5ba69a [Package CMO] Use InstructionVisitor to check if inst can be serialized.
Currently, InstructionVisitor is only used during the serialization pass,
that comes after a pass that checks if instructions are serializable. This
causes inconsistencies as some types bypass the first pass but are processed
in the second, leading to assert fails.

This PR uses InstructionVisitor in the first pass to be exhaustive and to
be consistent with the coverage in the second pass. The downside is the visitor
is SILCloner, which introduces overhead of cloning and discarding insts per pass
-- a potential area for future refactoring.

Resolves rdar://130788545.
2024-10-14 17:33:55 -07:00
Erik Eckstein
5502373018 SIL: rename AssociatedTypeProtocolWitness -> AssociatedConformanceWitness in SILWitnessTable
To be consistent with the naming convention in the AST
2024-10-02 07:10:30 +02:00
Erik Eckstein
2950e4521e SIL: representation for specialized witness tables
The main change here is to associate a witness table with a `ProtocolConformance` instead of a `RootProtocolConformance`.
A `ProtocolConformance` is the base class and can be a `RootProtocolConformance` or a `SpecializedProtocolConformance`.
2024-09-25 19:32:08 +02:00
Kavon Farvardin
ba0aac3f39 Merge pull request #76093 from kavon/coldsplit-2
ColdBlockInfo: overhaul analysis pass
2024-09-12 16:06:07 -07:00
elsh
66b504a253 [Package CMO] Diagnose missing library-evolution flag.
This PR ensures library-evolution is enabled for Package CMO; without it,
it previously fell back to regular CMO, which caused mismatching serialization
attributes if importing another module that had Package CMO enbaled, causing
an assert fail for loadable types.

Resolves rdar://135308288
2024-09-05 05:55:12 -07:00
Kavon Farvardin
7203a4fa73 ColdBlockInfo: overhaul analysis pass
The old analysis pass doesn't take into account profile data, nor does
it consider post-dominance. It primarily dealt with _fastPath/_slowPath.

A block that is dominated by a cold block is itself cold. That's true
whether it's forwards or backwards dominance.

We can also consider a call to any `Never` returning function as a
cold-exit, though the block(s) leading up to that call may be executed
frequently because of concurrency. For now, I'm ignoring the concurrency
case and assuming it's cold. To make use of this "no return" prediction,
use the `-enable-noreturn-prediction` flag, which is currently off by
default.
2024-09-03 15:41:10 -07:00
Ryan Mansfield
908d97dde5 SIL: Fix cl::desc typos. [NFC] 2024-08-29 16:26:56 -04:00
Slava Pestov
da4d076f02 AST: Introduce SubstFlags::SubstitutePrimaryArchetypes 2024-08-22 18:41:14 -04:00
Slava Pestov
39b4bda1dc AST: Introduce SubstFlags::SubstituteLocalArchetypes 2024-08-21 14:23:37 -04:00
Slava Pestov
ae77d6f0c1 AST: Replace one-off predicates with SubstitutionMap::getRecursiveProperties() 2024-08-21 13:19:10 -04:00
Erik Eckstein
44d85c1f72 CrossModuleOptimization: serialized witness tables in embedded mode
This is needed so that client modules can de-virtualize witness method calls.

Fixes a false "cannot use a value of protocol type in embedded Swift" error.
rdar://133993657
2024-08-20 20:32:32 +02:00
Ellie Shin
116abbb1dc Merge pull request #74641 from swiftlang/elsh/cmo-ufi 2024-06-26 09:06:39 -07:00
Ellie Shin
1e5266e7b6 [PackageCMO] Don't allow modifying AST.
Currently not all types are visited in canSerialize* calls, sometimes
 resulting in an internal type getting @usableFromInline, which is
 incorrect.

 For example, for `let q = P() as? Q`, where Q is an internal class
 inherting a public class P, Q is not visited in the canSerialize*
 checks, thus resulting in `@usableFromInline class Q`; this is not
 the intended behavior in the conservative mode used by PackageCMO
 as it modifies AST.

 To properly fix, instruction visitor needs to be refactored to do
 both the "canSerialize" check (that visits all types) and serialize
 or update visibility (modify AST in non-conservative modes).

 This PR provides a short-term fix that prevents modifying AST, and
 also ensures that the generated interfaces with PackageCMO flags
 are not affected by the optimization or contain modified AST.

rdar://130292190
2024-06-25 18:15:29 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Akira Hatanaka
d92f181ace Create two versions (for caller and callee) of the functions that answer questions about parameter convention (#74124)
Create two versions of the following functions:

isConsumedParameter
isGuaranteedParameter
SILParameterInfo::isConsumed
SILParameterInfo::isGuaranteed
SILArgumentConvention::isOwnedConvention
SILArgumentConvention::isGuaranteedConvention

These changes will be needed when we add a new convention for
non-trivial C++ types as the functions will return different answers
depending on whether they are called for the caller or the callee. This
commit doesn't change any functionality.
2024-06-18 09:06:09 -07:00
Ellie Shin
534c35b08b [PackageCMO] Optimize witness thunks.
- Keep witness thunk linkage private for a package protocol member in SILGen.
- Optimize private/hidden functions during Package CMO; if they don't contain
references that have private/hidden symbols, serialize them and set the linkage
to shared. For unserialized witness thunks, set the linkage to package, so the
witness table itself can be serialized.
- Update witness table and vtable serialization.

Resolves rdar://129976582
2024-06-17 13:06:46 -07:00
Nate Chandler
ac5ae3cc2a [ClosureSpecializer] Bail on noncopyable argument.
The optimization currently always creates a retain of the argument.
This isn't legal for noncopyable values.

rdar://129622373
2024-06-11 14:57:23 -07:00
Nate Chandler
55eb29ad3a [Gardening] Detypo'd. 2024-06-11 14:46:03 -07:00
Nate Chandler
38db63c05e [NFC] ClosureSpecializer: Use llvm::enumerate.
Rather than manually tracking the index.
2024-06-11 14:46:03 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Ellie Shin
af9e5e6378 Merge branch 'main' into elsh/pcmo-fixes 2024-06-04 11:41:35 -07:00
Ellie Shin
5a0c73c45f Add isPackageCMOEnabled check for MethodInst and KeyPathInst.
Update lambda in table serialization.
Add more tests.
2024-06-03 23:39:04 -07:00
Ellie Shin
fd07578b29 Package CMO fixes.
* Do not use [serialized_for_package] for witness method before Package CMO.
* Update v-table and witness-table and their entry serialization.
  -- Allow non-serialized but visible entries in a serialized table so they
    can be optimized by other SIL opt passes.
* Only serialize MethodInst if it has the right visibility.

Resolves rdar://129089105&129088935
2024-05-31 17:05:00 -07:00
Ellie Shin
4ecfc96578 [SIL][PackageCMO] Allow optimizing [serialized_for_pkg] functions during SIL
inlining, generic/closure specialization, and devirtualization optimization passes.

SILFunction::canBeInlinedIntoCaller now exlicitly requires a caller's SerializedKind_t arg.
isAnySerialized() is added as a convenience function that checks if [serialized] or [serialized_for_pkg].

Resolves rdar://128704752
2024-05-27 23:05:56 -07:00
Ellie Shin
5ccc4cd394 SIL function can be serialized with different kinds: [serialized] or
[serialized_for_package] if Package CMO is enabled. The latter kind
allows a function to be serialized even if it contains loadable types,
if Package CMO is enabled. Renamed IsSerialized_t as SerializedKind_t.

The tri-state serialization kind requires validating inlinability
depending on the serialization kinds of callee vs caller; e.g. if the
callee is [serialized_for_package], the caller must be _not_ [serialized].
Renamed `hasValidLinkageForFragileInline` as `canBeInlinedIntoCaller`
that takes in its caller's SerializedKind as an argument. Another argument
`assumeFragileCaller` is also added to ensure that the calle sites of
this function know the caller is serialized unless it's called for SIL
inlining optimization passes.

The [serialized_for_package] attribute is allowed for SIL function, global var,
v-table, and witness-table.

Resolves rdar://128406520
2024-05-23 15:53:02 -07:00