Commit Graph

11224 Commits

Author SHA1 Message Date
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
Andrew Trick
762cdc4c94 Fix PredictableDeadAllocationElimination ownership for empty structs
Preserve ownership for empty non-trivial structs. This currently applies to
~Escapable structs. People often use empty structs to investigate language
behavior. They should behave just like a struct that wraps a
pointer.

Previously, this would crash later during OSSA lifetime completion:

Assertion failed: (isa<UnreachableInst>(block->getTerminator())),
function computeRegion, file OSSALifetimeCompletion.cpp.
2025-01-13 16:36:40 -08:00
Erik Eckstein
9d0fc8b614 embedded: In IRGen, don't force generate code for functions which are imported from other modules.
Only generate code lazily for such functions, i.e. only if such a function is referenced from already generated code.
This is achieved by converting the SILLinkage for `public_external` functions to `internal` instead of `public in IRGenPrepare.
2025-01-13 14:08:35 +01:00
Meghana Gupta
15733446ed Merge pull request #78581 from meg-gupta/outlinerfix
Outliner: Do not outline if the BridgedArg value is not available at the BridgedCall
2025-01-13 04:15:20 -08:00
Nate Chandler
afe95135a0 [NFC] SILCombine: Ennamespace canonicalize class. 2025-01-12 15:32:54 -08:00
eeckstein
999669c860 Merge pull request #78441 from stzn/improve-use-before-initialized-diagnostic
[Diagnostics] A self argument implicitly passed as an inout parameter is diagnosed as used before initialized
2025-01-12 16:44:48 +01:00
Meghana Gupta
abc37ce218 Remove copy added by outliner for guaranteed bridged value
This copy may remain unoptimized, avoid outlining this case.
2025-01-11 08:15:24 -08:00
Meghana Gupta
c56573e424 Outliner: Do not outline if the BridgedArg value is not available at the BridgedCall
Avoid outlining this scenario instead of creating a copy to make the value available
2025-01-11 07:40:35 -08:00
nate-chandler
50c4861a04 Merge pull request #78552 from nate-chandler/rdar142520491
[OSSACanonicalizeGuaranteed] Don't rewrite consuming uses of move-only values.
2025-01-10 17:21:32 -08:00
Meghana Gupta
2c256180ce Add new utility swift::areUsesWithinValueLifetime 2025-01-10 16:15:50 -08:00
Nate Chandler
6dd45a70f9 [OSSACanonicalizeGuaranteed] Don't copy MO values.
When visiting a consuming use of a move-only value (which can be
produced by a forwarding operation), the inner rewriter must bail out.
Otherwise, it would produce a copy of that move-only value.

rdar://142520491
2025-01-10 10:14:21 -08:00
Nate Chandler
3b4803028d [NFC] OSSACanonicalizeGuaranteed: Args can bail.
Allow rewriting of arguments to bail out.  This is necessary because not
all forwarding instructions allow rewriting of forward(copy) as
copy(forward) (e.g. when forward produces a move-only value).
2025-01-10 10:14:21 -08:00
Nate Chandler
8803b612ac [NFC] OSSACanonicalizeGuaranteed: Flip condition.
Replace large nested condition with early exit.
2025-01-10 10:14:20 -08:00
Nate Chandler
0f6283c5dd [Gardening] OSSACanGuaranteed: Tweak comment. 2025-01-10 10:14:20 -08:00
eeckstein
b0bb19c001 Merge pull request #78524 from eeckstein/fix-codemotion
Fix two bugs which cause SIL verification errors
2025-01-10 10:48:04 +01:00
nate-chandler
d92ce1f436 Merge pull request #78507 from nate-chandler/rdar142570727
[SILCombine] Fix apply(convert_function(@guaranteed)) with an @owned operand.
2025-01-09 11:13:34 -08:00
Erik Eckstein
b80a08ee6c CodeMotion: check the ownership of arguments when sinking arguments
So far we only checked the ownership of incoming values.
But even if the incoming instruction has no ownership, the argument may have.
This can happen with enums which are constructed with a non-payload case:

   %1 = enum $Optional<C>, #Optional.none!enumelt
   br bb3(%1)
 bb1(%3 : @owned $Optional<C>):

Fixes an ownership verification error:
rdar://142506300
2025-01-09 19:56:25 +01:00
Erik Eckstein
db96d63e9d SimplifyCFG: correctly handle borrowed-from values when removing a redundant phi argument
Fixes a verifier crash
2025-01-09 19:56:25 +01:00
eeckstein
6b8c341b64 Merge pull request #78464 from eeckstein/sil-linker-error
SILLinker: convert an assert to a compiler error message
2025-01-09 07:44:47 +01:00
Nate Chandler
482e18b90f [SILCombine] Fix apply(convert_func(@guaranteed)).
The operands to the original apply are cast via an ownership forwarding
instruction to the appropriate type for the rewritten apply.

```
%converted = convert_function %original to $(NewTy) -> ()
apply %converted(%operand)
```
->
```
%cast = cast %operand to $OriginalTy
apply %original(%cast)
```

Previously, when an original operand is owned but the new apply does not
consume that operand, the newly added cast would consume the original
operand (an owned value)--something the original code being replaced did
not do.

```
%converted = convert_function %original to $(NewTy) -> ()
apply %converted(%operand : @guaranteed)
// %operand remains available
```
->
```
%cast = cast %operand to $OriginalTy // consumes %operand!
apply %original(%cast : @guaranteed)
// %operand is not available!
```

This is incorrect for the complementary reasons that the result of the
cast is leaked and any uses of the original operand subsequent to the
new apply are uses-after-consume.

Here, this is fixed by borrowing the original operand before casting in
this case.

rdar://142570727
2025-01-08 20:33:49 -08:00