Commit Graph

11193 Commits

Author SHA1 Message Date
Michael Gottesman
8c19cd7de6 Merge pull request #79028 from gottesmm/more-closure-fixes
[rbi] Small tweaks to the closure patch #78837
2025-01-30 14:06:17 -08:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00: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
Erik Eckstein
5fb41a9c55 LowerHopToActor: insert borrow scope if an optional actor value is owned
The inserted switch_enum must not consume the actor value, therefore a borrow scope is needed.
Fixes an ownership error.
2025-01-30 15:09:51 +01: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
Michael Gottesman
8c96a8db1b [rbi] When finding closure uses in an immediately invoked closure, distinguish in between captures and parameters.
Otherwise, when one diagnoses code like the following:

```
Task {
  {
    use($0)
  }(x)
}
```

One gets that $0 was captured instead of x. Unfortunately, since function
parameters do not have locations associated with them, we do not mark x
itself... instead, we mark the closure... which is unfortunate.
2025-01-29 15:07:47 -08:00
Michael Gottesman
39e63b488f [sil] Make sure that we do not flag ignored_use as dead code when used after a try_apply or begin_apply. 2025-01-29 15:05:29 -08:00
Meghana Gupta
6a47193e0c Fix LICM for begin_access/end_access in OSSA
LICM in OSSA is enabled only for instructions involving trvial values.

begin_access/end_access is eligible for LICM in OSSA. However we need to
collect applies to check for conflicts.

rdar://143835241
2025-01-29 12:54:35 -08:00
Meghana Gupta
4fb0399dac Merge pull request #78982 from meg-gupta/fixmdi2
Fix AddressOwnership for unidentified access
2025-01-29 07:00:56 -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
eeckstein
5d23a4d1cb Merge pull request #78972 from eeckstein/fix-cmo
embedded: make sure to serialize vtable methods with the right linkage
2025-01-28 21:08:25 +01: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
Meghana Gupta
27fc324c8c Fix AddressOwnership for unidentified access
AccessBase can have Unindentified kind where the base value maybe
invalid.

Bailout early for such accesses in ownership rauw.
2025-01-28 09:08:38 -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
Erik Eckstein
631bee446a BasicCalleeAnalysis: don't crash if the bca argument to isDeinitBarrier is null
Some clients don't provide a callee analysis to this API.
Don't crash in this case.
2025-01-28 09:15:47 +01:00
nate-chandler
8c63134570 Merge pull request #78682 from nate-chandler/rdar142636711_2
[TempRValueOpt] Invalidate insts when completing.
2025-01-27 12:53:57 -08:00
eeckstein
95f1dfbf07 Merge pull request #78885 from eeckstein/fix-without-actually-escaping
Fix two problems with `withoutActuallyEscaping`
2025-01-27 07:59:02 +01:00
Andrew Trick
f0144ab7b1 Merge pull request #78830 from atrick/fix-lifedep-trivial-deadend
SILGenCleanup: extend to handle trivial local var scopes
2025-01-25 15:34:35 -08:00
Kuba Mracek
92b0c5f5d0 [embedded] Support _findStringSwitchCaseWithCache in Embedded Swift 2025-01-25 09:09:47 -08:00
eeckstein
1bd2a38231 Merge pull request #78886 from eeckstein/fix-devirtualize-begin-apply
Devirtualizer: fix de-virtualization of begin_apply
2025-01-25 08:20:52 +01:00
Meghana Gupta
d2936a15bc Merge pull request #78824 from meg-gupta/fixmdi
Fix operand ownership of mark_dependence [nonescaping] of address values
2025-01-24 14:59:36 -08:00
Erik Eckstein
98612171bb Devirtualizer: fix de-virtualization of begin_apply
Handle the special case of an guaranteed return value with no uses.
Fixes an assertion crash.

Unfortunately I don't have an isolated test case for this.
But this problem showed up in the `Interpreter/moveonly_read_modify_coroutines.swift` test with OSSA modules and will therefore be tested by this test once we enable OSSA modules.
2025-01-24 20:17:55 +01:00
Erik Eckstein
3ec5d7de24 SIL: replace the is_escaping_closure instruction with destroy_not_escaped_closure
The problem with `is_escaping_closure` was that it didn't consume its operand and therefore reference count checks were unreliable.
For example, copy-propagation could break it.
As this instruction was always used together with an immediately following `destroy_value` of the closure, it makes sense to combine both into a `destroy_not_escaped_closure`.
It
1. checks the reference count and returns true if it is 1
2. consumes and destroys the operand
2025-01-24 19:23:27 +01:00
eeckstein
694ee2d9c6 Merge pull request #78849 from eeckstein/autodiff-workarounds
Two workarounds for autodiff specific optimizations with OSSA
2025-01-24 15:53:32 +01:00
Meghana Gupta
79b649854b Fix operand ownership of mark_dependence [nonescaping] of address values 2025-01-24 00:49:32 -08:00
Andrew Trick
1b72c7bbf6 SILGenCleanup: extend to handle trivial local var scopes
Improves OSSALifetimeCompletion to handle trivial variables when running from
SILGenCleanup. This only affects lifetime dependence diagnostics.

For the purpose of lifetime diagnostics, trivial local variables are only valid
within their lexical scope. Sadly, SILGen only know how to insert cleanup code
on normal function exits. SILGenCleanup relies on lifetime completion to fix
lifetimes on dead end paths.

  %var = move_value [var_decl]
  try_apply %f() : $..., normal bb1, error error
error:
  extend_lifetime %var <=== insert this
  unreachable

This allows Span to depend on local unsafe pointers AND be used within
throwing closures:

    _ = a.withUnsafeBufferPointer {
      let buffer = $0
      let view = Span(_unsafeElements: buffer)
      return view.withUnsafeBufferPointer(\.count)
    }
2025-01-23 12:42:43 -08:00
Erik Eckstein
51628dde12 SILCombine: workaround a problem with differential_function in OSSA
Disable this peephole optimization until the underlying problem is fixed.
https://github.com/swiftlang/swift/issues/78848

Part of rdar://140229560
2025-01-23 16:10:32 +01:00
Michael Gottesman
082b824a8e [rbi] Change Region Based Isolation for closures to not use the AST and instead just use SIL.
The reason why I am doing this is that in certain cases the AST captures indices
will never actually line up with partial apply capture indices since we seem to
"smush" together closures and locally defined functions.

NOTE: The reason for the really small amount of test changes is that this change
does not change the actual output by design. The only cases I had to change were
a case where we began to emit a better diagnostic and also where I added code
coverage around _ and let _ since those require ignored_use to be implemented so
that they would be diagnosed (previously we just did not emit anything so we
couldn't emit the diagnostic at the SIL level).

rdar://142661388
2025-01-22 21:12:36 -08:00
Michael Gottesman
6058b1d9bd [silgen] Change SILGen to emit ignored_user for emitIgnoredExpr and black hole initialization. 2025-01-22 21:12:36 -08:00
Michael Gottesman
7ae56aab2e [sil] Add a new instruction ignored_use.
This is used for synthetic uses like _ = x that do not act as a true use but
instead only suppress unused variable warnings. This patch just adds the
instruction.

Eventually, we can use it to move the unused variable warning from Sema to SIL
slimmming the type checker down a little bit... but for now I am using it so
that other diagnostic passes can have a SIL instruction (with SIL location) so
that we can emit diagnostics on code like _ = x. Today we just do not emit
anything at all for that case so a diagnostic SIL pass would not see any
instruction that it could emit a diagnostic upon. In the next patch of this
series, I am going to add SILGen support to do that.
2025-01-22 21:12:36 -08:00
eeckstein
90cbaec78a Merge pull request #78781 from eeckstein/fix-loop-rotate
LoopRotate: don't rotate loops where the header block branches to a dead-end block.
2025-01-22 11:55:26 +01:00
Meghana Gupta
e2688425bf Merge pull request #78714 from meg-gupta/fixcf
Fix condition forwarding for switch_enum with default in ossa
2025-01-22 02:08:17 -08:00
Erik Eckstein
5dc80f62dc LoopRotate: don't rotate loops where the header block branches to a dead-end block.
Incomplete liveranges in the dead-end exit block can cause a missing adjacent phi-argument for a re-borrow if there is a borrow-scope is in the loop.
But even when we have complete lifetimes, it's probably not worth rotating a loop where the header block branches to a dead-end block.

Fixes a SIL verifier crash
2025-01-21 19:08:08 +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
Meghana Gupta
01d4f11cf5 Fix condition forwarding for switch_enum with default in ossa
In ossa, switch_enum's destination accepts an argument. Fixup
condition forwarding to correctly forward the enum in this case.

Fixes rdar://143042093
2025-01-17 11:01:12 -08:00
Saleem Abdulrasool
0e77477147 Merge pull request #78553 from compnerd/well-known-imports
IRGen: further generalise runtime function generation
2025-01-16 09:25:54 -08:00
Nate Chandler
8fb0fd2ba4 [TempRValueOpt] Invalidate insts when completing. 2025-01-16 08:18:30 -08:00
Nate Chandler
bf1e5dd308 [NFC] DestroyAddrHoisting: Add subpass bailouts. 2025-01-16 08:18:29 -08:00
Nate Chandler
dd5efc7c6b [NFC] DestroyAddrHoisting: Extract transformation.
Separate the hoisting of destroys from the setup and cleanup.
Facilitates bailing after completing N subpasses.
2025-01-16 08:18:29 -08:00
Nate Chandler
6e2f09f225 [NFC] CopyPropagation: Add subpass bailouts.
Before each transformation is done, check whether to continue with the
next subpass.
2025-01-16 08:18:29 -08:00
Nate Chandler
1d22288f24 [NFC] SIL: Subpass runs can take values.
Allow continueWithNextSubpassRun to take a SILValue.
2025-01-16 08:18:29 -08:00
Nate Chandler
8ace8b9ef9 [NFC] CopyPropagation: Extract transformation.
Separate the propagation of copies from the setup and cleanup.
Facilitates bailing after completing N subpasses.
2025-01-16 08:18:29 -08:00
nate-chandler
b59280c0f0 Merge pull request #78624 from nate-chandler/rdar142520491_2
[OSSACanonicalizeOwned] Fix liveness passed to completion.
2025-01-15 19:38:21 -08:00
Saleem Abdulrasool
ec70054c93 IRGen: further generalise runtime function generation
This adjusts the runtime function declaration handling to track the
owning module for the well known functions. This allows us to ensure
that we are able to properly identify if the symbol should be imported
or not when building the shared libraries. This will require a
subsequent tweak to allow for checking for static library linkage to
ensure that we do not mark the symbol as DLLImport when doing static
linking.
2025-01-15 13:54:34 -08:00
Nate Chandler
ebf602803e [OSSACanOwned] Fix liveness passed to completion.
To determine where a lifetime ends within dead-end blocks,
OSSACanonicalizeOwned uses OSSACompleteLifetime's
visitAvailabilityBoundary.  This API takes a liveness which it uses to
walk forward to the availability boundary.  Specifically, the liveness
passed from OSSACanonicalizeOwned to OSSACompleteLifetime is a variation
of OSSACanonicalizeOwned's own liveness (it has destroys added).

There is a mismatch in the characteristics of livenesses created by
OSSACanonicalizeOwned and OSSACompleteLifetime:  The former deals with
not only direct uses of a value but also uses of its copies; that
introduces the possibility for consuming uses in the middle of liveness.
The latter on the other hand deals only with uses of a single value
(nestedly, but at each level of nesting only a single value).  Passing a
liveness from the former to the latter without handling this mismatch
is incorrect: OSSACompleteLifetime understands consuming uses to always
end a lifetime, even when they are in the middle of a copy-extended
liveness.  The result is that OSSACompleteLifetime produces non-sensical
results when provided with such a liveness.

To address this, fixup the liveness passed from OSSACanonicalizeOwned to
OSSACompleteLifetime by demoting consuming uses that appear within
(that's to say _not_ on the boundary) of liveness to non-consuming uses.

rdar://142846936
2025-01-15 11:57:13 -08:00
eeckstein
ff7c331965 Merge pull request #78555 from eeckstein/witness-table-linkage
embedded: make sure to generate witness tables which are imported from other modules
2025-01-14 08:12:32 +01:00
Nate Chandler
af5cd31f3c [Test] Added test for SILCombiner::processInst. 2025-01-13 17:00:23 -08:00
Nate Chandler
6deba02792 [Test] Renamed test case. 2025-01-13 16:57:36 -08:00
Nate Chandler
5ce6c6cf14 [NFC] SILCombine: Extract processing function.
The pass is structured to drain an instruction worklist and perform a
sequence of operations on each popped instruction.  Extract that
sequence of operations into a new processInstruction function. Enables
testing the sequence on a single instruction.
2025-01-13 16:57:23 -08:00