Commit Graph

2610 Commits

Author SHA1 Message Date
Erik Eckstein
2eda3b3bcb TempRValueOpt: don't move end_access instructions after a terminator instruction.
When extending access scopes, prevent that we end up with an `end_access` after the block's terminator.

Fixes a verifier crash.

rdar://85020372
2022-06-27 19:55:37 +02:00
eeckstein
51bc273541 Merge pull request #59591 from eeckstein/fix-dead-obj-elim
DeadObjectElimination: fix a bug which caused wrong inserted release instructions.
2022-06-22 15:42:11 +02:00
Meghana Gupta
f99eb1df26 Fix possible iterator invalidation in SILMem2Reg 2022-06-21 14:22:26 -07:00
Meghana Gupta
344ef14796 Fix load_borrow replacement in SILMem2Reg 2022-06-21 14:22:20 -07:00
Erik Eckstein
8fd17764e4 DeadObjectElimination: fix a bug which caused wrong inserted release instructions.
This bug triggered a "instruction isn't dominated by its operand" verifier crash.
Or - if the verifier doesn't run - a crash later in IRGen.

rdar://94376582
2022-06-20 20:24:32 +02:00
Meghana Gupta
a0a0ebb558 Enable Mem2Reg for allocs with load_borrow (#59350) 2022-06-15 14:37:10 -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
24979d48e0 [SSADestroyHoisting] Adopt new utilities.
Instead of doing one or two non-iterative BackwardReachability runs,
do a single run of IterativeBackwardReachability.  During that, pause
after discovery/local dataflow and use VisitBarrierAccessScopes to
determine which end_access instructions in the discovered region are
barriers.  Add those instructions as kills to the dataflow.  Finally run
the global dataflow.

Enables SSADestroyHoisting to hoist destroys over loops.

Addresses a correctness issue where access scopes which were open at
barrier blocks were not promoted to barriers, resulting in destroy_addrs
getting hoisted into unrelated access scopes.
2022-05-21 12:56:51 -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
Arnold Schwaighofer
bcf24e4715 Eager specializer: Fix pre-specialization of imported code
We must no pre-specialize imported code (except if this was explicitly
called for by the importing module).

Therefore, don't pre-specialize `shared` definitions based on their
pre-specialization attributes.

Rather, only pre-specialize if the pre-specialization is called for
using a `target: "theFunctionToSpecialize"` parameter.

Run OnonePrespecializations before serialization so that module native functions
are not yet marked `shared` and can be identified as native.

rdar://92337361
2022-05-04 08:32:41 -07:00
Anton Korobeynikov
df6e2ebbd5 [NFC] Fix use-after-free issues in debug printing inside SIL Mem2Reg (#42183)
Turned printing of addresses of instructions into instructions while there.
2022-05-02 11:35:09 -07:00
Joe Groff
d2fad71328 Merge pull request #42589 from jckarter/begin-partial-apply-simplification-pass
[SIL] Initial work on PartialApplySimplification pass
2022-05-02 10:20:39 -07:00
Erik Eckstein
cad646b283 re-implement the StackPromotion pass in swift
It uses the new EscapeInfo.
2022-05-02 14:22:27 +02:00
Erik Eckstein
653b6ecc33 fix a crash/hang in SimplifyCFG.
Jump threading in an unreachable CFG region can lead to a crash (in an assert compiler) or hang (in an no-assert compiler) in `ValueBase::replaceAllUsesWith`.

Unfortunately I couldn't come up with an isolated SIL test case.

rdar://92267349
2022-04-25 16:48:59 +02:00
Erik Eckstein
b47afec018 RedundantLoadElimination: support bisecting individual optimized loads when debugging this pass. 2022-04-25 09:19:29 +02:00
Joe Groff
1f30a54b48 Sketch out a SILType::isSingleSwiftRefcounted function. 2022-04-21 14:53:39 -07:00
Joe Groff
56c7547fa3 Treat types obviously word-sized in SIL as simple nonescaping partial_apply contexts 2022-04-21 14:53:39 -07:00
Joe Groff
a416f3d2ac If a partial_apply would be simple by simply changing the convention of the callee, change the convention 2022-04-21 12:51:49 -07:00
Joe Groff
95eca55d72 PartialApplySimplification: Transform nonescaping invocation functions that are also fully applied 2022-04-21 12:51:49 -07:00
Joe Groff
5d7861ff1e Support non-generic nonescaping partial_applys using an on-stack tuple 2022-04-21 12:51:48 -07:00
Joe Groff
8b08a74053 PartialApplySimplification: Use a tuple in a box.
IRGen only implements box lowering for single-field boxes at the moment.
We can represent closure contexts that don't capture type info as just
capturing a tuple of the values, so let's do that for now to allow
for initial end-to-end testing of the pass.
2022-04-21 12:51:48 -07:00
Joe Groff
1e2bda9ea0 Extensions to SILBoxType to represent captures.
- Add a `[reflection]` bit to `alloc_box` instructions, to indicate that a box
  should be allocated with reflection metadata attached.
- Add a `@captures_generics` attribute to SILLayouts, to indicate a type layout
  that captures the generic arguments it's substituted with, meaning it can
  recreate the generic environment without additional ABI-level arguments, like
  a generic partial application can.
2022-04-21 12:51:46 -07:00
Joe Groff
f2e785e66d (wip) reflection flag for alloc_box 2022-04-21 12:47:45 -07:00
Joe Groff
85760d5348 SIL: Add a PartialApplySimplification pass.
This will turn `partial_apply` instructions into explicit box construction and
extraction code sequences. To begin with, recognize when a private function
is only used in partial applications and directly modify the function to be
usable as a closure invocation function. This simplifies the lowering in IRGen
and avoids generating a "partial application forwarder" thunk.
2022-04-21 12:47:44 -07:00
Erik Eckstein
ecfb431a21 COWOpts: handle struct_extract and struct instructions.
Sometimes the def-use chain between `end_cow_mutation` and `begin_cow_mutation` has a phi-term which is wrapped in a struct (e.g. `Array`).
The PhiExpansionPass is supposed to clean that up, but this only works if there are no "unknown" uses of the phi term.
With this change, COWOpts can handle such patterns without relying on the PhiExpansionPass.

rdar://91964659
2022-04-20 15:53:32 +02:00
Erik Eckstein
8522a84403 fix a crash in AccessEnforementWMO 2022-04-14 19:39:56 +02: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
Nate Chandler
9492619319 [CopyPropagation] Delete dead copy_values.
When canonicalizing an owned value's lifetime, also check whether the
value is dead.  If it is, track it for deletion.  In particular, this
eliminates dead copy_values.
2022-03-23 17:02:59 -07:00
Andrew Trick
2fd4de411e [SIL-opaque] Removed [Unconditional]CheckedCastValue 2022-03-22 17:04:13 -07:00
Andrew Trick
5e36a2a689 Fix an AccessedStorage assert for SIL global variables.
Allow round-tripping access to global variables. Previously,
AccessedStorage asserted that global variables were always associated
with a VarDecl. This was to ensure that AccessEnforcmentWMO always
recognized the global. Failing to recognize access to a global will
cause a miscompile.

SILGlobalVariable now has all the information needed by
SIL. Particularly, the 'isLet' flag. Simply replace VarDecl with
SILGlobalVariable in AccessEnforcmentWMO to eliminate the need for the
assert.
2022-03-21 08:51:23 -07:00
Nate Chandler
672ec76d54 [NFC] Removed spurious namespacing. 2022-03-16 20:45:40 -07:00
Nate Chandler
87d0c9fd6e [SSADestroyHoisting] Bitcasts obstruct folding.
Make loads and copy_addrs of casts of the underlying storage barriers to
folding.  Destroying the target address may not be equivalent to
destroying the source address: for example, if the target address is a
generic and the source address is AnyObject, specialization may turn the
generic into a trivial type; the destruction of that trivial type fails
to destroy the original stored AnyObject, resulting in a leak.
2022-03-16 11:33:51 -07:00
Nate Chandler
266668d010 [SSADestroyHoisting] Fold into sequences.
Previously, destroy_addrs were folded into copy_addrs and load [copy]s
to produce copy_addr [take]s and load [take]s respectively, but only if
the source of the load/copy was exactly the address being destroyed.

Generalize that to a single-block sequence of copy_addrs and load
[copy]s of projections of the address being destroyed.
2022-03-16 11:33:51 -07:00
Nate Chandler
febdf30e5b [SSADestroyHoisting] Tie barriers to lifetimes.
Only respect deinit barriers when lexical lifetimes are enabled.  If
they aren't, hoist destroy_addrs of all addresses aggressively
regardless of whether doing so involves hoisting over deinit barriers.
2022-03-16 11:33:51 -07:00
Nate Chandler
5b5ed561c5 Added printing during inlining.
Enable caller and callee to be printed as inlining runs.  The printing
is filtered based on -sil-print-function/-sil-print-functions and
includes finer-grained info than those do already.  The caller before
and after each callee is inlined can be printed as well as the callee
on its own as it exists when inlining occurs.
2022-03-15 13:11:47 -07:00
nate-chandler
4343d940ba Merge pull request #41751 from nate-chandler/mem2reg/bail-on-load-take-complex-projections
[Mem2Reg] Skip load [take] of cast projections.
2022-03-09 17:14:34 -08:00
nate-chandler
009ed74b09 Merge pull request #41753 from nate-chandler/lexical_lifetimes/lexical_destroy_addr_hoisting/pointer-handling
[SSADestroyHoisting] Pointer handling.
2022-03-09 17:12:00 -08:00
Nate Chandler
4ffde96a31 [SSADestroyHoisting] Fixed pointer handling.
Previously, SSADestroyHoisting was attempting to check whether an
unknown use of a variable was an address_to_pointer.
UniqueStorageUseVisitor, however, doesn't call back with that
instruction.  Instead, it adds its uses to the stack of uses to visit.

Instead, we need to check whether the use was produced by an
address_to_pointer or more generally whether it's a
BuiltinRawPointerType.
2022-03-09 10:08:16 -08:00
Nate Chandler
3508d41459 [Mem2Reg] Skip load [take] of cast projections.
Already, load [take]s of struct_element_addr|tuple_element_addr
projections resulted in Mem2Reg bailing.  Expand that to include load
[take]s involving unchecked_addr_cast.

To handle load [take]s of (struct|tuple)_element_addr projections, it
would be necessary to replace the running value with a value obtained
from the original product by recursive destructuring, replacing the
value at the load [take]n address with undef, and then restructuring.

To handle load [take]s of cast projections, it would be necessary to use
unchecked_value_cast instead of unchecked_bitwise_cast.  But we would
need to still use unchecked_bitwise_cast in the case of load [copy]
because otherwise we would lose the original value--unchecked_value_cast
forwards ownership, and not all casts can be reversed (because they may
narrow).

For now, just bail out in the face of these complex load [take]s.
2022-03-09 08:39:33 -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
Erik Eckstein
a62a5caaf4 SILMem2Reg: fix a problem with leaking enum values
When optimizing an enum `store` to an `alloc_stack`, require that all uses are in the same block.
Otherwise it could be a `switch_enum` of an optional where the none-case does not have a destroy of the enum value.
After transforming such an `alloc_stack`, the value would leak in the none-case block.

It fixes the same OSSA verification error as done for TempRValueOpt in a previous commit.
2022-03-09 09:47:48 +01:00
Erik Eckstein
90695d9cdf TempRValueElimination: fix a problem with leaking enum values
When optimizing an enum `store` to an `alloc_stack`, require that all uses are in the same block.
Otherwise it could be a `switch_enum` of an optional where the none-case does not have a destroy of the enum value.
After transforming such an `alloc_stack`, the value would leak in the none-case block.

Fixes a OSSA verification error.
2022-03-08 15:47:14 +01:00
Nate Chandler
2de9171749 [NFC] Used convenience method. 2022-02-23 12:37:24 -08:00
Nate Chandler
e0262ebc0e [SSADestroyHoisting] Fixed barrier check.
Previously, FindBarrierAccessScopes::checkReachablePhiBarrier was not
looking at the terminator of predecessors but rather looking at the
terminator of block itself.  Previously, in cases where the current
block's terminator was in fact a barrier, that resulted in failing to
hoist any live-in access scopes.

Now that we aren't running the data flow twice, the result was worse: in
cases where the current block's terminator was a barrier but there was
no access scope in play, no barrier would be added at all.
2022-02-23 12:35:49 -08:00
Nate Chandler
26853f4be6 [SSADestroyHoisting] Avoid second per-var dataflow.
In order to determine which end_access instructions are barriers to
hoisting, a data flow which looks for access scopes containing barriers
is run.  Those scopes that do contain barriers are added to a set.  When
the second pass runs, the end_access instructions corresponding to
scopes in that set (i.e. the ends of scopes which contain barriers) are
treated as barriers.

In the common case where there are no barrier access scopes, though,
running two dataflows per variable is wasteful.  Avoid that by just
checking whether we found any scopes that are barriers.  If we didn't,
then we already visited all the barrier instructions and were told by
BackwardReachability which blocks had reachable ends and begins.

Tweaked the first data flow to record the barriers and the blocks in
DeinitBarriers.  In DeinitBarriers::compute, if no access scopes that
are barriers were found, stop working.  If any were found, clear what
had been recorded so far and run the second data flow.

In order to be able to clear everything, switched from using
BasicBlockSet and BasicBlockSetVector to SmallPtrSet<SILBasicBlock *>
and SmallPtrSetVector<SILBasicBlock *>.
2022-02-22 16:25:10 -08:00
Nate Chandler
40e57e185a [SSADestroyHoisting] Split destroy from copy.
As was done with store [init], transform instructions like

    copy_addr %n to %m

into the sequence

    destroy_addr %m
    copy_addr %n to [initialization] %m

in order to create more opportunities for hoisting destroys.

After hoisting, if these opportunities for hoisting don't result in
hoisting actually occurring, recombine the two instructions.
2022-02-21 09:21:26 -08:00
Nate Chandler
cea8d75604 [NFC] Replaced block with continue. 2022-02-21 09:21:26 -08:00
Andrew Trick
aa9d14773a Merge pull request #41472 from nate-chandler/lexical_lifetimes/lexical_destroy_addr_hoisting/fixes/20220218/1
[SSADestroyHoisting] Aliasable addr args respect deinit barriers.
2022-02-19 17:26:51 -08:00
nate-chandler
fdc40046d5 Merge pull request #41377 from nate-chandler/lexical_lifetimes/destroy_hoisting/remove-from-copy_forwarding
[CopyForwarding] Removed destroy hoisting.
2022-02-19 08:02:20 -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