Commit Graph

2768 Commits

Author SHA1 Message Date
Michael Gottesman
1e6187c4f4 [sil] Update all usages of old API SILValue::getOwnershipKind() in favor of new ValueBase::getOwnershipKind().
Andy some time ago already created the new API but didn't go through and update
the old occurences. I did that in this PR and then deprecated the old API. The
tree is clean, so I could just remove it, but I decided to be nicer to
downstream people by deprecating it first.
2022-07-26 11:46:23 -07:00
Nate Chandler
f1108e57cb [CanonicalizeOSSALifetime] Handle later reborrows.
Properly handle the different sorts of reborrows when they are visited:
- non-branch reborrows (tuples, etc) are non-lifetime-ending uses; such
  reborrows are relevant defs for liveness
- branch reborrows which also consume the owned phi are lifetime-ending
  uses
- branch reborrows which DO NOT consume the owned phi are
  non-lifetime-ending uses, and the corresponding phis are relevant defs
  for liveness

rdar://97399065
2022-07-21 15:30:16 -07:00
Nate Chandler
2bbdeac2fe [CanonicalizeOSSALifetime] Handle adj reborrows.
When canonicalizing lifetime of an owned phi, add adjacent phis which
are reborrows of its reaching values to the worklist.  Update liveness
to consider uses whose operand ownership is specific to guaranteed
values (i.e. uses which are derived from these adjacent reborrow phis)
as standard, non-lifetime-ending uses.

rdar://94346482
2022-07-21 15:03:44 -07:00
Erik Eckstein
c3de7c1317 MandatoryGenericSpecializer: drop metatype arguments in specialized functions
And replace them with explicit `metatype` instruction in the entry block.
This allows such metatype instructions to be deleted if they are dead.

rdar://94388453
2022-07-07 08:34:54 +02:00
Meghana Gupta
768a82f2cf Handle undefs in a few utilities used in RLE (#59448) 2022-06-15 14:37:46 -07:00
Slava Pestov
795751c540 Merge pull request #59434 from slavapestov/devirtualize-class-witness-method-fix
SILOptimizer: Fix invariant violation in getWitnessMethodSubstitutions() with class witness methods
2022-06-14 17:57:55 -04:00
Slava Pestov
ed4e8d65f0 SILOptimizer: Fix invariant violation in getWitnessMethodSubstitutions() with class witness methods
Witness thunks where the conforming type is a class and the witness is in a
protocol extension have an extra generic parameter constrained to the class
type that is passed as the 'Self' parameter for the protocol extension
method.

This means the substitution map for the devirtualized call must be
assembled from three sources:

- The 'Self' substitution
- The generic parameters of the concrete conforming type, if any
- The generic parameters of the protocol requirement, if any

This was previously done by making two calls to combineSubstitutionMaps(),
the first call combined the first two maps and the second call combined the
result of the first call with the third map.

Unfortunately, both calls were made with the generic signature of the
witness thunk, and the result of combining the first two substitution maps
does not provide sufficient replacements for all generic parameters and
conformance requirements in the witness thunk's signature.

This was apparently fine with the GenericSignatureBuilder, but the
Requirement Machine flags the missing generic parameters in assert builds.

Fixes https://github.com/apple/swift/issues/59193.
2022-06-14 13:57:20 -04:00
Nate Chandler
39ba0bc19d [NFC] Silenced unused private field warning.
Removed the field.
2022-06-13 10:27:58 -07:00
Michael Gottesman
e1006c62f9 [move-only] Add copyable_to_moveonlywrapper and moveonlywrapper_to_copyable instructions.
These instructions have the following attributes:

1. copyably_to_moveonlywrapper takes in a 'T' and maps it to a '@moveOnly
T'. This is semantically used when initializing a new moveOnly binding from a
copyable value. It semantically destroys its input @owned value and returns a
brand new independent @owned @moveOnly value. It also is used to convert a
trivial copyable value with type 'Trivial' into an owned non-trivial value of
type '@moveOnly Trivial'. If one thinks of '@moveOnly' as a monad, this is how
one injects a copyable value into the move only space.

2. moveonlywrapper_to_copyable takes in a '@moveOnly T' and produces a new 'T'
value. This is a 'forwarding' instruction where at parse time, we only allow for
one to choose it to be [owned] or [guaranteed].

* moveonlywrapper_to_copyable [owned] is used to signal the end of lifetime of
the '@moveOnly' wrapper. SILGen inserts these when ever a move only value has
its ownership passed to a situation where a copyable value is needed. Since it
is consuming, we know that the no implicit copy checker will ensure that if we
need a copy for it, the program will emit a diagnostic.

* moveonlywrapper_to_copyable [guaranteed] is used to pass a @moveOnly T value
as a copyable guaranteed parameter with type 'T' to a function. In the case of
using no-implicit-copy checking this is always fine since no-implicit-copy is a
local pattern. This would be an error when performing no escape
checking. Importantly, this instruction also is where in the case of an
@moveOnly trivial type, we convert from the non-trivial representation to the
trivial representation.

Some important notes:

1. In a forthcoming commit, I am going to rebase the no implicit copy checker on
top of these instructions. By using '@moveOnly' in the type system, we can
ensure that later in the SIL pipeline, we can have optimizations easily ignore
the code.

2. Be aware of is that due to SILGen only emitting '@moveOnly T' along immediate
accesses to the variable and always converts to a copyable representation when
calling other code, we can simply eliminate from the IR all moveonly-ness from
the IR using a lowering pass (that I am going to upstream). In the evil scheme
we are accomplishing here, we perform lowering of trivial values right after
ownership lowering and before diagnostics to simplify the pipeline.

On another note, I also fixed a few things in SILParsing around getASTType() vs
getRawASTType().
2022-06-09 19:47:31 -07:00
Meghana Gupta
defafa7d52 ClosureLifetimeFixup : Fix bug in lifetime extension of non escaping closure 2022-05-31 11:31:50 -07:00
Nate Chandler
8775148f7b [SILOpt] Used SetVector for fast contains check.
IterableBackwardReachability just requires an iterable list of gens.
ShrinkBorrowScope, LexicalDestroyHoisting, and SSADestroyHoisting all
need to be able to check whether a given instruction is scope ending
quickly.  Use a SmallSetVector rather than a SmallVector for the gens in
all three.
2022-05-24 14:15:27 -07:00
Nate Chandler
e68c12f251 [LexicalDestroyHoisting] Adopt VisitBarrierAccessScopes.
Avoids hoisting destroy_values into unrelated access scopes which could
introduce exclusivity violations.
2022-05-24 10:29:30 -07:00
Nate Chandler
fdc35a1138 [LexicalDestroyHoisting] Adopt iterative dataflow.
Adopt IterativeBackwardReachability.  Enables hoisting destroy_values
over loops.

rdar://93369506
2022-05-24 10:29:30 -07:00
Nate Chandler
158db440fc [Gardening] Recapitalized type. 2022-05-24 10:29:29 -07:00
Nate Chandler
3c7ad6013a [ShrinkBorrowScope] Adopt VisitBarrierAccessScopes.
Avoids hoisting borrow scopes into unrelated access scopes which could
introduce exclusivity violations.

rdar://93060369
2022-05-24 08:05:13 -07:00
Nate Chandler
180095ae3a [ShrinkBorrowScope] Adopt iterative dataflow.
Adopt IterativeBackwardReachability.  Enables hoisting end_borrow
instructions over loops.  That in turn enables hoisting destroy_values
over those same loops.

rdar://93186505
2022-05-24 08:05:13 -07:00
Nate Chandler
bdc1f404e6 [Gardening] Recapitalized type. 2022-05-24 08:05:13 -07:00
Slava Pestov
ac74b845e5 Merge pull request #58513 from AnthonyLatsis/se-309-sil-opt
SE-0309: SILOptimizer fixes & reenable executable tests
2022-05-20 16:38:05 -07:00
Anton Korobeynikov
4f05096594 Propagate location from destination alloca in salvageDebugInfo(). (#58763)
`salvageDebugInfo` is called during SIL Mem2Reg and could produce misleading debug info in the following case:

```
%a = alloc_stack $MyModel.TangentVector, var, name "self", argno 1, implicit, loc "debug2.swift":37:17 ...
...
store %b to %a : $*MyModel.TangentVector
```

Such SIL could be created as a result of inlining (where store comes from the inlined function).
Before this patch we'd end with `debug_value` instruction with variable information, but without or incorrect location.

This caused LLVM IR debug info verifier assertions when there might be another instruction with complete debug info (including location) for the same argument.

After this patch we always reuse it from destination alloca

Fixes #58660
2022-05-11 13:28:23 -07:00
Arnold Schwaighofer
104fd6662f GenericSpecialization: Move once initialization of pre-specializations earlier before the first query for pre-specialziations 2022-05-06 13:13:19 -07:00
Anthony Latsis
63d0130350 Devirtualize: Extend SE-0309 bail-out logic to variadic function parameters and Self-rooted type parameters 2022-05-02 13:54:09 +03:00
Erik Eckstein
e41c237686 GenericCloner: copy effects from the original function to the generic specialization 2022-05-02 12:00:06 +02:00
Anthony Latsis
b2a9092ea4 [NFC] SILOptimizer: OpenedArchetypeInfo should use OpenedArchetypeType 2022-04-29 02:42:19 +03:00
Andrew Trick
1a9c2ad8a3 Add doesCastPreserveOwnershipForType() to SIL/Utils/DynamicCasts.cpp
Added an AST helper in Types.h:

- isPotentiallyAnyObject()

This formalizes logic for when cast operations forward
ownership. Various OSSA optimization rely on this for
correctness. This fixes latent bugs throughout the optimizer.

I was compelled to fix this now because we want to make OSSA
optimizations across dynamic casts more aggressive. For example, we
want to optimize retain/release across enum formation.
2022-04-05 20:28:19 -07:00
Erik Eckstein
6b7ae416e6 SIL: remove unused instructions thin_function_to_pointer and pointer_to_thin_function 2022-03-25 15:44:59 +01:00
Konrad `ktoso` Malawski
ef10cd016a [Distributed] Fix generic returning distributed funcs 2022-03-25 16:19:08 +09:00
Kavon Farvardin
af683dc271 basic implementation of performing an init of the id after an assign of the actorSystem.
The resignID call within the initializer moved into DI, because an assignment to
the actorSystem, and thus initialization of the id, is no longer guaranteed to happen.
Thus, while we previously could model the resignation as a clean-up emitted on all
unwind paths in an initializer, now it's conditional, based on whether the id was
initialized. This is exactly what DI is designed to do, so we inject the resignation
call just before we call the identity's deinit.
2022-03-24 16:20:01 -07:00
Kavon Farvardin
6c0cfac69a remove dependence on system parameter in SILGen/DefiniteInit 2022-03-24 16:18:49 -07:00
Andrew Trick
2fd4de411e [SIL-opaque] Removed [Unconditional]CheckedCastValue 2022-03-22 17:04:13 -07:00
Andrew Trick
406aa86d2e Merge pull request #41557 from atrick/addrlower-update
Update and reimplement AddressLowering pass (for SIL opaque values).
2022-03-21 22:15:06 -07:00
Konrad `ktoso` Malawski
5ab8e0834d [Distributed] Reimplement distributed call thunks completely in AST (#41616)
* [Distributed] dist actor always has default executor (currently)

* [Distributed] extra test for missing makeEncoder

* [DistributedDecl] Add DistributedActorSystem to known SDK types

* [DistributedActor] ok progress on getting the system via witness

* [Distributed] allow hop-to `let any: any X` where X is DistActor

* [Distributed] AST: Add an accessor to determine whether type is distributed actor

- Classes have specialized method on their declarations
- Archetypes and existentials check their conformances for
  presence of `DistributedActor` protocol.

* [Distributed] AST: Account for distributed members declared in class extensions

`getConcreteReplacementForProtocolActorSystemType` should use `getSelfClassDecl`
otherwise it wouldn't be able to find actor if the member is declared in an extension.

* [Distributed] fix ad-hoc requirement checks for 'mutating'

[PreChecker] LookupDC might be null, so account for that

* [Distributed] Completed AST synthesis for dist thunk

* [Distributed][ASTDumper] print pretty distributed in right color in AST dumps

* wip on making the local/remote calls

* using the _local to mark the localCall as known local

* [Distributed] fix passing Never when not throwing

* fix lifetime of mangled string

* [Distributed] Implement recordGenericSubstitution

* [Distributed] Dont add .

* [Distributed] dont emit thunk when func broken

* [Distributed] fix tests; cleanups

* [Distributed] cleanup, move is... funcs to DistributedDecl

* [Distributed] Remove SILGen for distributed thunks, it is in Sema now!

* [Distributed]  no need to check stored props in protocols

* remote not used flag

* fix mangling test

* [Distributed] Synthesis: Don't re-use AST nodes for `decodeArgument` references

* [Distributed] Synthesis: Make sure that each thunk parameter has an internal name

* [Distributed/Synthesis] NFC: Add a comment regarding empty internal parameter names

* [Distributed] NFC: Adjust distributed thunk manglings in the accessor section test-cases

* cleanup

* [Distributed] NFC: Adjust distributed thunk manglings in the accessor thunk test-cases

* review follow ups

* xfail some linux tests for now so we can land the AST thunk

* Update distributed_actor_remote_functions.swift

Co-authored-by: Pavel Yaskevich <xedin@apache.org>
2022-03-10 23:58:23 +09:00
Andrew Trick
b187ba0dde Add support for indirect tuple-type results.
This could happen as a result of specialization or concrete
address-only values.

For now, it's just tested by SIL unit tests.
2022-03-09 17:18:15 -08:00
Erik Eckstein
6a020f8f15 Stabilize and simplify SIL linkage and serialization
The main point of this change is to make sure that a shared function always has a body: both, in the optimizer pipeline and in the swiftmodule file.
This is important because the compiler always needs to emit code for a shared function. Shared functions cannot be referenced from outside the module.
In several corner cases we missed to maintain this invariant which resulted in unresolved-symbol linker errors.

As side-effect of this change we can drop the shared_external SIL linkage and the IsSerializable flag, which simplifies the serialization and linkage concept.
2022-03-09 15:28:05 +01:00
Robert Widmann
ab44a07045 Convert DeclContext Parameters to their Associated Generic Signatures Instead 2022-03-07 22:54:23 -08:00
Robert Widmann
d6186c9cfb Add a DeclContext Parameter to Opened Archetype Construction
This ensures that opened archetypes always inherit any outer generic parameters from the context in which they reside. This matters because class bounds may bind generic parameters from these outer contexts, and without the outer context you can wind up with ill-formed generic environments like

<τ_0_0, where τ_0_0 : C<T>, τ_0_0 : P>

Where T is otherwise unbound because there is no entry for it among the generic parameters of the environment's associated generic signature.
2022-03-07 22:54:22 -08:00
Andrew Trick
7e66bfb622 Add asserts to CanonicalizeBorrowScope::visitBorrowScopeUses
As suggested in code review.
2022-02-24 22:47:57 -08:00
Andrew Trick
372b2be9e3 Merge pull request #41471 from atrick/remove-temp-deleter
Remove temporary InstructionDeleter in ConstantFolding.
2022-02-21 11:37:47 -08:00
swift-ci
e45bb6fd63 Merge pull request #41460 from atrick/fix-canonical-func-arg
Fix an assert in canonicalizeFunctionArgument.
2022-02-19 07:52:14 -08:00
Andrew Trick
d3e945e27f Remove temporary InstructionDeleter in ConstantFolding.
I believe the original issue was actually fixed by:

  Add isReadOnlyConstantEvaluableCall to handle stdlib asserts #40254
  https://github.com/apple/swift/pull/40254

Fixes rdar://85849838 (Remove InstructionDeleter workaround in
ConstantPropagation)
2022-02-18 17:53:25 -08:00
Andrew Trick
4a30118ce1 Fix an assert in canonicalizeFunctionArgument.
Assertion failed: (succeed && "should be filtered by
FindBorrowScopeUses"), function canonicalizeFunctionArgument, file
CanonicalizeBorrowScope.cpp, line 798

Canonicalization for guaranteed function arguments is triggered by
SILCombine without any up-front analysis. Because the canonicalization
rewrites the function argument's copies in place, it must always
succeed.

Fix the visitBorrowScopeUses utility to be aware that it is being
invoked on a function argument and avoid bailing out.
2022-02-18 16:52:30 -08:00
Nate Chandler
96d90d0e6b [Gardening] Removed extraneous import. 2022-02-18 07:47:40 -08:00
nate-chandler
a3709b0ab4 Merge pull request #41450 from atrick/rauw-fix-rebase
Temporary fix for OSSA RAUW utilities.
2022-02-18 07:23:48 -08:00
Andrew Trick
9dba1c1fb5 Temporary fix for OSSA RAUW utilities.
This fix unblocks unrelated optimizer commits. A unit test will be
introduced with those commits.

The RAUW utility does not correctly handle reborrows. It is in the
middle of being rewritten. For now, simply bail out since this isn't
an important case to optimize.
2022-02-17 23:55:32 -08:00
nate-chandler
0011b075fa Merge pull request #41387 from nate-chandler/lexical_lifetimes/destroy_hoisting/add
Hoist destroys of owned lexical values.
2022-02-17 11:36:50 -08:00
Arnold Schwaighofer
62ec31a462 Merge pull request #41338 from aschwaighofer/reuse_contiguous_array_storage_metadata
Reuse `_ContiguousArrayStorage<AnyObject>` metadata for any class or objc generic type
2022-02-17 12:47:23 -05:00
Nate Chandler
1e820068a4 [ShrinkBorrowScope] Don't hoist over begin_borrows.
While it is sometimes valid to hoist over begin_borrows of copies of the
borrowee, it is not always valid, as the test case committed here
illustrates.  As a future optimization, we can reenable this hoisting
with the appropriate condition.
2022-02-16 22:25:55 -08:00
Andrew Trick
1c8f142a73 OwnershipOptUtils - computeGuaranteedBoundary 2022-02-16 12:23:01 -08:00
Andrew Trick
522dc9d32e InstructionDeleter - delete dead alloc_stack 2022-02-16 12:23:01 -08:00
Nate Chandler
dd168d837c [LexicalDestroyHoisting] Added.
The new utility, to be run as part of copy propagation, hoists
destroy_values of owned lexical values up to deinit barriers.  It is
heavily based on the rewritten ShrinkBorrowScope.
2022-02-16 10:31:24 -08:00
Nate Chandler
2c4f14b1d7 [ShrinkBorrowScope] Replaced check with assertion.
Previously, it was checked whether a barrier terminator was actually a
control flow terminator before adding end_borrows at the beginnings of
its successors.  But it can't actually happen that a terminator is
classified as a barrier without the beginnings of all of its
predecessors having been reached because the BackwardReachability data
flow is pessimistic and only visits the end of a block (i.e. its
terminator) if it reached the beginnings of all of that block's
successors (see BackwardReachability::meetOverSuccessors and where the
data flow calls it).
2022-02-16 10:31:24 -08:00