Commit Graph

2768 Commits

Author SHA1 Message Date
Erik Eckstein
9081edd4c2 Optimizer: fix an ownership violation when duplicating loops
If a guaranteed value is used in a dead-end exit block and the enclosing value is _not_ destroyed in this block, we end up missing the enclosing value as phi-argument after duplicating the loop.
TODO: once we have complete lifetimes we can remove this check again.

rdar://159125605
2025-08-29 17:28:57 +02:00
Michael Gottesman
348720b830 Revert "[rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe)."
This reverts commit 6ce93c8a37.
2025-07-16 09:58:09 -07:00
Michael Gottesman
f920aba2f9 [rbi] Use interned StringRefs for diagnostics instead of SmallString<64>.
This makes the code easier to write and also prevents any lifetime issues from a
diagnostic outliving the SmallString due to diagnostic transactions.

(cherry picked from commit 010fa39f31)
2025-07-09 12:25:06 -07:00
Michael Gottesman
85fafa5b1e [rbi] Begin tracking if a function argument is from a nonisolated(nonsending) parameter and adjust printing as appropriate.
Specifically in terms of printing, if NonisolatedNonsendingByDefault is enabled,
we print out things as nonisolated/task-isolated and @concurrent/@concurrent
task-isolated. If said feature is disabled, we print out things as
nonisolated(nonsending)/nonisolated(nonsending) task-isolated and
nonisolated/task-isolated. This ensures in the default case, diagnostics do not
change and we always print out things to match the expected meaning of
nonisolated depending on the mode.

I also updated the tests as appropriate/added some more tests/added to the
SendNonSendable education notes information about this.

(cherry picked from commit 14634b6847)
2025-07-09 12:25:03 -07:00
Michael Gottesman
ca8cdc1fd7 [rbi] Thread through a SILFunction into print routines so we can access LangOpts.Features so we can change how we print based off of NonisolatedNonsendingByDefault.
We do not actually use this information yet though... This is just to ease
review.

(cherry picked from commit 4433ab8d81)
2025-07-09 12:24:17 -07:00
Michael Gottesman
0ec2527bdc [rbi] Wrap use ActorIsolation::printForDiagnostics with our own SILIsolationInfo::printActorIsolationForDiagnostics.
I am doing this so that I can change how we emit the diagnostics just for
SendNonSendable depending on if NonisolatedNonsendingByDefault is enabled
without touching the rest of the compiler.

This does not actually change any of the actual output though.

(cherry picked from commit 4ce4fc4f95)
2025-07-09 12:24:17 -07:00
Michael Gottesman
f24ff98f9a [nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
We were effectively working around this previously at the SIL level. This caused
us not to obey the semantics of the actual evolution proposal. As an example of
this, in the following, x should not be considered main actor isolated:

```swift
nonisolated(nonsending) func useValue<T>(_ t: T) async {}
@MainActor func test() async {
  let x = NS()
  await useValue(x)
  print(x)
}
```

we should just consider this to be a merge and since useValue does not have any
MainActor isolated parameters, x should not be main actor isolated and we should
not emit an error here.

I also fixed a separate issue where we were allowing for parameters of
nonisolated(nonsending) functions to be passed to @concurrent functions. We
cannot allow for this to happen since the nonisolated(nonsending) parameters
/could/ be actor isolated. Of course, we have lost that static information at
this point so we cannot allow for it. Given that we have the actual dynamic
actor isolation information, we could dynamically allow for the parameters to be
passed... but that is something that is speculative and is definitely outside of
the scope of this patch.

rdar://154139237
(cherry picked from commit c12c99fb73)
2025-07-09 12:24:17 -07:00
Michael Gottesman
5361765212 Merge pull request #82428 from gottesmm/release/6.2-144111950
[6.2][rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe).
2025-07-09 11:06:52 -07:00
Doug Gregor
1d86ed5f15 [Region analysis] Simplify logic for dynamic casts by making them less special
Extend and use SILIsolationInfo::getConformanceIsolation() for the dynamic
cast instructions.
2025-07-08 17:33:33 -07:00
Doug Gregor
066fa0b828 [Region isolation] Re-simplify handling of init_existential* instructions
Centralize the logic for figuring out the conformances for the various
init_existential* instructions in a SILIsolationInfo static method, and
always go through that when handling "assign" semantics. This way, we
can use CONSTANT_TRANSLATION again for these instructions, or a simpler
decision process between Assign and LookThrough.

The actually undoes a small change made earlier when we stopped looking
through `init_existential_value` instructions. Now we do when there are
no isolated conformances.
2025-07-08 17:33:33 -07:00
Doug Gregor
22ca6b98e5 [Region isolation] Factor SILIsolationInfo creation into static methods
Better match the style of SILIsolationInfo by moving the code for determining
SILIsolationInfo from conformances or dynamic casts to existentials into
static `getXYZ` methods on SILIsolationInfo.

Other than adding an assertion regarding disconnected regions, no
intended functionality change.
2025-07-08 17:33:33 -07:00
Doug Gregor
6637960bdc [SE-0470] Track the potential introduction of isolated conformances in regions
When we introduce isolation due to a (potential) isolated conformance,
keep track of the protocol to which the conformance could be
introduced. Use this information for two reasons:

1. Downgrade the error to a warning in Swift < 7, because we are newly
diagnosing these
2. Add a note indicating where the isolated conformance could be introduced.

(cherry picked from commit 02c34bb830)
2025-07-08 17:32:06 -07:00
Michael Gottesman
6ce93c8a37 [rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe).
rdar://144111950
(cherry picked from commit 010443c854)
2025-07-08 13:35:52 -07:00
Erik Eckstein
849a5e9082 MandatoryPerformanceOptimizations: don't de-virtualize a generic class method call to specialized method
This results in wrong argument/return calling conventions.
First, the method call must be specialized. Only then the call can be de-virtualized.
Usually, it's done in this order anyway, because the `class_method` instruction is located before the `apply`.
But when inlining functions, the order (in the worklist) can be the other way round.

Fixes a compiler crash.
rdar://154631438
2025-07-03 13:21:27 +02:00
Jamie
11525cabf8 [SILOptimizer]: slow OSSA lifetime canonicalization mitigation
OSSA lifetime canonicalization can take a very long time in certain
cases in which there are large basic blocks. to mitigate this, add logic
to skip walking the liveness boundary for extending liveness to dead
ends when there aren't any dead ends in the function.

Updates `DeadEndBlocks` with a new `isEmpty` method and cache to
determine if there are any dead-end blocks in a given function.

(cherry picked from commit 1f3f830fc7)
2025-06-22 18:08:06 -05:00
Meghana Gupta
d5b82a3b7c [6.2] Fix an inliner crash when inlining begin_apply with scoped lifetime dependence 2025-06-13 14:18:29 -07:00
Allan Shortlidge
4c34f08b99 SILOptimizer: Fix an unused variable warning. 2025-06-05 15:40:30 -07:00
Michael Gottesman
7b434c2747 [rbi] Lookthrough an invocation of DistributedActor.asLocalActor when determining actor instances.
In this case, what is happening is that in SILGen, we insert implicit
DistributedActor.asLocalActor calls to convert a distributed actor to its local
any Actor typed form. The intention is that the actor parameter and result are
considered the same... but there is nothing at the SIL level to enforce that. In
this commit, I change ActorInstance (the utility that defines actor identity at
a value level) to look through such a call.

I implemented this by just recognizing the decl directly. We already do this in
parts of SILGen, so I don't really see a problem with doing this. It also
provides a nice benefit that we do not have to modify SILFunctionType to
represent this or put a @_semantic attribute on the getter.

NOTE: Generally, Sema prevents us from mixing together different actors. In this
case, Sema does not help us since this call is inserted implicitly by the
distributed actor implementation in SILGen. So this is not a problem in general.

rdar://152436817
(cherry picked from commit 331626e6fa)
2025-06-03 09:05:32 -07:00
Meghana Gupta
58300798ca Merge pull request #81521 from meg-gupta/disableverificationcp
[6.2] Add a new semantics attribute to disable SIL verification on a function
2025-05-19 18:01:34 -07:00
Meghana Gupta
06f681777d [6.2] Add a new semantics attribute to disable SIL verification on a function 2025-05-15 14:32:09 -07:00
Meghana Gupta
9f0f1da076 [6.2] Fix InstructionDeleter for drop_deinit instruction
Currently we delete dead drop_deinit instructions in InstructionDeleter. For address results, we may end up with ownership errors after being promoted to value forms. For value results, fixLifetimes mode of InstructionDeleter will insert an illegal destroy_value

rdar://151104993
2025-05-14 10:50:43 -07:00
Meghana Gupta
cc72edb119 Introduce end_cow_mutation_addr instruction 2025-04-30 14:38:48 -07:00
Erik Eckstein
ca75b6ec86 Optimize keypaths in language 6 mode
In language 6 mode keypath instructions are created as existentials and the optimizer needs to look through the `open_existential_ref` instructions to recognize a keypath.

rdar://150173106
2025-04-29 11:37:02 +02:00
Michael Gottesman
da57f9665c Merge pull request #80953 from gottesmm/release/6.2-135459885
[6.2][sil-isolation-info] When determining isolation of a function arg, use its VarDecl.
2025-04-24 10:14:41 -07:00
nate-chandler
05b997d9aa Merge pull request #81013 from nate-chandler/cherrypick/release/6.2/rdar147586673
6.2: [OSSACanOwned] Don't dead-end extend if consumed.
2025-04-23 15:40:54 -07:00
Nate Chandler
f2a00f0fcb [OSSACanOwned] Don't dead-end extend if consumed.
When the utility is used by the ConsumeOperatorCopyableValuesChecker,
the checker guarantees that the lifetime can end at the consumes, that
there are no uses after those consumes.  In that circumstance, the
utility maintains liveness to those consumes and as far as possible
without introducing a copy everywhere else.

The lack of complete lifetimes has forced the utility to extend liveness
of values to dead-ends.  That extension, however, is in tension with the
use that the checker is putting the utility to.  If there is a dead-end
after a consume, liveness must not be maintained to that dead-end.

rdar://147586673
2025-04-22 16:49:42 -07:00
Nate Chandler
19242dffde [NFC] OSSACanOwned: Extracted predicate. 2025-04-22 16:49:42 -07:00
Nate Chandler
6b977dc43b [NFC] OSSACanOwned: Renamed field. 2025-04-22 16:49:41 -07:00
Matt Seaman
ae1072a438 Fix assertion failure when performing constant-evaluation through basic blocks with non-constant arguments 2025-04-21 11:44:53 -07:00
Michael Gottesman
636aa31192 [sil-isolation-info] When determining isolation of a function arg, use its VarDecl.
Otherwise, we can be inconsistent with isolations returned by other parts of the
code. Previously we were just treating it always as self + nom decl, which is
clearly wrong if a type is not self (e.x.: if it is an isolated parameter).

rdar://135459885
(cherry picked from commit 0ece31e4f6)
2025-04-21 10:57:48 -07:00
Michael Gottesman
46926720a9 [rbi] Make it so that we correctly do not error on uses of Sendable values that are projected from non-Sendable bases.
Specifically, we only do this if the base is a let or if it is a var but not
captured by reference.

rdar://149019222
(cherry picked from commit 23b6937cbc)
2025-04-15 14:58:04 -07:00
Michael Gottesman
a93e994593 [rbi] Add the ability to add flags to PartitionOp.
I am doing this so I can mark requires as being on a mutable non-Sendable base
from a Sendable value.

I also took this as an opportunity to compress the size of PartitionOp to be 24
bytes instead of 40 bytes.

(cherry picked from commit a045c9880a)
2025-04-15 14:57:57 -07:00
Andrew Trick
4608c06f78 Fix GenericSpecializer for addressable parameters.
Addressable parameters must remain indirect.

Incidentally also fixes an obvious latent bug in which all specialization was
disabled if any metatypes could not be specialized.

Fixes rdar://145687827 (Crash of inline-stored Span properties with optimizations)

(cherry picked from commit 935b5e7ea2)
2025-04-08 14:02:19 -07:00
Andrew Trick
da2894ba2c [NFC] fix a dropUnusedArguments parameter name
(cherry picked from commit 6898e33edd)
2025-04-08 14:02:18 -07:00
Erik Eckstein
575388e188 CastOptimizer: handle isolated conformances
Check the isolation of conformances to avoid wrong folding of dynamic casts
rdar://147417762
2025-04-07 10:58:02 +02:00
Erik Eckstein
8f2c17a149 CastOptimizer: pass the SIL function to the cast-classify APIs, instead of the module decl
NFC
2025-04-07 10:58:02 +02:00
Erik Eckstein
4d1df28de0 ConstantFolding: add constant folding for some floating point intrinsics
* `sitofp` signed integer to floating point
* `rint` round floating point to integral
* `bitcast` between integer and floating point

Constant folding `bitcast`s also made it necessary to rewrite constant folding for Nan and inf values, because the old code explicitly checked for `bitcast` intrinsics.
Relying on constant folded `bitcast`s makes the new version much simpler.

It is important to constant fold these intrinsics already in SIL because it enables other optimizations.
2025-04-01 18:13:01 +02:00
Erik Eckstein
e1f4057ffc ConstantFolding: only print a warning for infinity floating point literals if it has an AST node
This avoids false alarms for floating point literals which are imported from the `var infinity` getter of floating point types.
2025-04-01 18:12:45 +02:00
Artem Chikin
66334f8f1b Merge pull request #79290 from artemcm/ConstProtoSIL
[Compile Time Values] Implement a mandatory SIL pass to verify '@const' values
2025-04-01 00:12:27 -07:00
Pavel Yaskevich
0897962d27 Merge pull request #80373 from xedin/rdar-146101172-narrow
[SILOptimizer] Prevent devirtualization of call to distributed witnes…
2025-03-31 09:29:37 -07:00
Pavel Yaskevich
585b687103 [SILOptimizer] Prevent devirtualization of call to distributed witness requirements
This is a narrow fix, we are going to work on fixing this properly
and allowing both devirtualization and specialization for distributed
requirement witnesses.

Anything that uses an ad-hoc serialization requirement scheme cannot
be devirtualized because that would result in loss of ad-hoc conformance
in new substitution map.

Resolves: https://github.com/swiftlang/swift/issues/79318
Resolves: rdar://146101172
2025-03-29 19:27:46 -07:00
Artem Chikin
d484ec7c1f [Compile Time Values] Implement a mandatory SIL pass to verify '@const' values 2025-03-27 14:33:35 -07:00
Doug Gregor
e0b52cd20e [SIL] Extend checked-cast instructions with "prohibit isolated conformances" flag
When performing a dynamic cast to an existential type that satisfies
(Metatype)Sendable, it is unsafe to allow isolated conformances of any
kind to satisfy protocol requirements for the existential. Identify
these cases and mark the corresponding cast instructions with a new flag,
`[prohibit_isolated_conformances]` that will be used to indicate to the
runtime that isolated conformances need to be rejected.
2025-03-26 22:31:47 -07:00
Andrew Trick
e7000e4668 SIL: Add mark_dependence_addr 2025-03-25 23:02:42 -07:00
Pavel Yaskevich
5af7e0d09b Merge pull request #80236 from xedin/is-self-recursive-cache
[SILOptimizer] Turn "is self-recursive" check into analysis
2025-03-25 10:37:07 -07:00
Pavel Yaskevich
41c88f864a [SILOptimizer] Turn "is self-recursive" check into analysis
The body of a function has to be re-analyzed for every call
site of the function, which is very expensive and if the
body is not changed would produce the same result.

This takes about ~10% from swift-syntax overall build time
in release configuration.
2025-03-24 00:25:13 -07:00
Doug Gregor
731f58443c Address review feedback on AbstractConformance in ProtocolConformanceRef 2025-03-23 20:54:39 -07:00
eeckstein
29876fd491 Merge pull request #80137 from eeckstein/fix-cast-optimizer
CastOptimizer: fix optimization of casts from non-existentials to existentials
2025-03-20 07:26:18 +01:00
Amritpan Kaur
3c30d68d2e Merge pull request #78823 from amritpan/method-keypaths
[Sema/SILGen/IRGen] Implement method & initializer keypaths.
2025-03-19 18:59:17 -07:00
Erik Eckstein
21264019cd CastOptimizer: fix optimization of casts from non-existentials to existentials
We need to consider that archetypes (generic types) can be existentials if they conform to self-conforming protocols.

Fixes a miscompile
rdar://147269904
2025-03-19 20:30:48 +01:00