Commit Graph

2592 Commits

Author SHA1 Message Date
Arnold Schwaighofer
9f2b6a4ebb Reuse _ContiguousArrayStorage<AnyObject> metadata for any class or objc generic type
Reduces the number of _ContiguousArrayStorage metadata.

In order to support constant time bridging we do need to set the correct
metadata when we bridge to Objective-C. This is so that the type check
succeeds when bridging back from Objective-C to reuse the storage
instance rather than bridging the elements.

To support dynamically setting the `_ContiguousArrayStorage` element
type i needed to add support for optimizing `alloc_ref_dynamic`
throughout the optimizer.

Possible future improvements:
* Use different metadata such that we can disambiguate native Swift
  classes during destruction -- allowing native release rather then unknown
  release usage.
* Optimize the newly added semantic function
  getContiguousArrayStorageType

rdar://86171143
2022-02-16 07:55:34 -08:00
Andrew Trick
83b01d8ebe Improve the SILPhiArgument API
This subclass of SILArgument should be eliminated--it's not always a
phi, and whether it is a "phi argument" has nothing whatsoever to do
with the opcode. That is a property of a value's uses, not a property of the
value.

Until then, provide a logical and useful API within the type. This
often avoids the need to explicitly cast to a SILPhiArgument type and
avoids a lot of boilerplate in code that deals with phis.

Note: PhiOperand and PhiValue are improved abstractions on top of this
API. But the SILArgument-level API is still an important bridge
between SILArgument and other phi abstractions.
2022-02-15 13:28:46 -08:00
Nate Chandler
41fdbc1285 [ShrinkBorrowScope] Adopt BackwardReachability.
Replaced ShrinkBorrowScope's own data flow with the general
BackwardReachability.

Took this opportunity to refactor and document the utility.

Taken together these changes make ShrinkBorrowScope serve as a template
for a future LexicalDestroyHoisting which will operate on owned lexical
values (rather than guaranteed as here) and hoist destroy_values (rather
than end_borrows as here) but should otherwise be quite similar.
2022-02-14 18:38:31 -08:00
Nate Chandler
94ef0af9ea [SILOpt] Reachability handles phis.
Previously, Reachability assumed that phis were not barriers.  Here,
handling for barrier phis is added.  To that end, a new delegate
callback `checkReachableBarrier(PhiValue)` is added.  Before marking the
beginning of a block as reached (or any of its predecessors), check
whether each argument that is a phi is a barrier.  If any is, then
reachability is done.

Implemented the new method in SSADestroyHoisting by splitting apart the
classification of an instruction and the work to do in response to
visiting an instruction.  Then, when visiting a PhiValue, just check
whether any of the predecessors terminators are classified as barriers.
That way, seeing that they're classified that way doesn't result in
noting down that those terminators had been reached (which indeed they
will not have been if any of the terminators from which the values are
flowing into the phi are barriers).
2022-02-14 17:12:47 -08:00
Nate Chandler
4e2667b4e7 [CopyForwarding] Removed destroy hoisting.
It has been replaced by the destroy hoisting in SSADestroyHoisting.
2022-02-14 15:56:03 -08:00
Nate Chandler
53c728efb6 [SSADestroyHoisting] Don't fold trivial loads.
For trivial values, the pattern

  %val = load [trivial] %addr
  destroy_addr %addr

arises.  Don't fold these two into

  %val = load [take] %addr

because that isn't valid SIL for trivial types in OSSA.
2022-02-10 20:18:17 -08:00
Nate Chandler
553f49d090 [Gardening] Fixed typo. 2022-02-10 20:18:04 -08:00
Nate Chandler
137a1607eb [SSADestroyHoisting] Deleted copy/assign ctors.
For the DeinitBarriers helper class.
2022-02-10 20:17:48 -08:00
Michael Gottesman
0e3bca99f5 [debug-var] When comparing address debug_value against value debug_value remove the diexpr from the address SILDebugVariable.
Otherwise they will never compare the same. Also restore the test that was
updated for the previous commit to its old state. I did this to ensure that each
commit would compile successfully and so that I could show the test associated
with this commit.
2022-02-09 14:06:23 -08:00
Erik Eckstein
8fe36eedd8 SILPassManager: improve bisecting for debugging the optimizer
* Add the possibility to bisect the individual transforms of SILCombine and SimplifyCFG.
   To do so, the `-sil-opt-pass-count` option now accepts the format `<n>.<m>`, where `m` is the sub-pass number.
   The sub-pass number limits the number of individual transforms in SILCombine or SimplifyCFG.

* Add an option `-sil-print-last` to print the SIL of the currently optimized function before and after the last pass, which is specified with `-sil-opt-pass-count`.
2022-02-09 13:25:30 +01:00
nate-chandler
fe74a7dc79 Merge pull request #41113 from nate-chandler/lexical_lifetimes/destroy_folding/add
[CopyPropagation] Added lexical destroy folding.
2022-02-04 22:23:21 -08:00
Nate Chandler
9e5e935b20 [CopyPropagation] Run LexicalDestroyFolding. 2022-02-04 14:41:37 -08:00
Anthony Latsis
60c85b0108 SILGen: Handle nested opened archetypes and add tests 2022-02-02 02:10:05 +03:00
Nate Chandler
e3fbadf7b7 [SILOptimizer] Let visitTransitiveEndBorrows take SILValues.
Previously, visitTransitiveEndBorrows took BorrowedValues.  However,
there is at least one kind of borrow--namely,
unchecked_ownership_conversion insts--that is not currently permitted by
the BorrowedValue API.  The long term fix is to make BorrowedValue
handle such instructions.  For now, change visitTransitiveEndBorrows to
take SILValues so that unchecked_ownership_conversion can be passed to
the API.

rdar://87985420
2022-01-25 15:55:08 -08:00
Max Desiatov
b964dba177 libswift: implement ReleaseDevirtualizer in Swift 2022-01-19 18:51:19 +00:00
Max Desiatov
0b34baa80f libswift: bridge RCIdentityAnalysis code 2022-01-19 18:51:17 +00:00
Erik Eckstein
736470a5c5 MandatoryGenericSpecializer: fix two crashes
* don't run dead alloc on non-ossa functions
* try to de-serialize transparent functions and don't inline if that doesn't succeed

rdar://87622503
2022-01-17 13:59:14 +01:00
nate-chandler
5fcac0811b Merge pull request #40793 from nate-chandler/silgen/emit-borrow-scopes-for-alloc_boxes
[SIL] Added borrow scopes to alloc_boxes.
2022-01-14 07:32:55 -08:00
Nate Chandler
ceea4cf57a [AllocBoxToStack] See through borrows.
Now that alloc_boxes whose lifetimes are lexical are emitted with
begin_borrow [lexical]/end_borrow, AllocBoxToStack needs to be able to
see through those new borrow scopes in order to continue stack promoting
the same boxes that it was able to before those lexical scopes were
emitted.
2022-01-13 13:33:21 -08:00
Andrew Trick
08b445fb7e Remove APPLYSITE obfuscation.
These macros make ApplySite.h and SILNodes.def unreadable. Getting rid
of them will save me (and I'm sure others) a lot of time whenever I
work with ApplySite.

Best practice dictates that the ApplySite abstraction be modeled in
one place. The macros serve no purpose other than obfuscation.
2022-01-12 10:03:16 -08:00
Max Desiatov
5ef19cf0d9 libswift: add back legacy pass to fix Windows test 2022-01-10 13:10:32 +00:00
Max Desiatov
42e6fac1ea libswift: reimplement AssumeSingleThreaded pass 2022-01-10 08:47:43 +00:00
Erik Eckstein
802d11a429 StackPromotion: support OSSA 2022-01-07 16:20:27 +01:00
Erik Eckstein
383c52aa35 SIL: rename dealloc_ref [stack] -> dealloc_stack_ref
Introduce a new instruction `dealloc_stack_ref ` and remove the `stack` flag from `dealloc_ref`.

The `dealloc_ref [stack]` was confusing, because all it does is to mark the deallocation of the stack space for a stack promoted object.
2022-01-07 16:20:27 +01:00
Michael Gottesman
3e9254c2be [allocbox-to-stack] Loosen an assert.
We should be able to accept mark_uninitialized in this position. The assert was
just being careful so that the codegen that we accept here is constricted
explicitly.

rdar://86535218
2022-01-04 11:56:38 -08:00
Andrew Trick
c8a2130554 Add a SSADestroyHoisting utility and pass
Extract and rewrite the destroy hoisting algorithm originally from
CopyForwarding (in 2014).

This is now a light-weight utility for hoisting destroy_addr
instructions. Shrinking an object's memory lifetime can allow removal
of copy_addr and other optimization.

This is extremely low-overhead and can run at any optimization level
without dependency on any analysis.

This algorithm is:
- Incremental
- SSA-based
- Canonical
- Free from alias analysis

See file-level comments.

The immediate purpose is to specify and test the constraints
introduced by adding lexical variable lifetimes to SIL semantics. It
can be used as a template for end_borrow hoisting.

Ultimately, this utility can be invoked within any pass that needs to
optimize a particular uniquely identified address. It will be used to
remove much of the complexity from CopyForwarding.
2021-12-22 11:32:57 -08:00
Nate Chandler
1c106eb6e8 [ShrinkBorrowScope] Return modified copy_values.
So that CopyPropagation and other clients can react accordingly, pass
back a list of copy_value instructions that were rewritten by
ShrinkBorrowScope.  In CopyPropagation, add each modified copy to the
copy worklist.
2021-12-19 08:14:45 -08:00
Nate Chandler
214e890e0f [CopyPropagation] Cleanup after borrow shrinking. 2021-12-19 08:14:45 -08:00
Pavel Yaskevich
4860f90fd7 [SIL] Add new flag to SILFunction - IsDistributed
Determines whether given SILFunction represents a distributed
method or its thunk.
2021-12-17 10:52:52 -08:00
Andrew Trick
a50108ff46 Fix a debug location in DCE.
Fixes SR-15300: Compiler crash when using Builtin.unreachable in
initializers

Otherwise, this triggers an debug info verification assert that
invalid locations must only be on unreachable instructions.

This is why generating lifetime cleanup code should never inherit its
location from its insertion point.
2021-12-13 21:35:14 -08:00
Nate Chandler
1464c1b1ff [NFC] Renamed LexicalLifetimesOption cases.
The cases' new names more accurately reflect what behavior occurs when
SILOptions::LexicalLifetimes is assigned that case.
2021-12-10 18:36:28 -08:00
Nate Chandler
646dc136fc [CopyPropagation] Note ShrinkBorrowScope changes.
Use the result returned from shrinkgBorrowScope to update the changed
flag.
2021-12-07 17:04:49 -08:00
Nate Chandler
cde250a3e3 [CopyPropagation] Add ShrinkBorrowScope.
During copy propagation (for which -enable-copy-propagation must still
be passed), also try to shrink borrow scopes by hoisting end_borrows
using the newly added ShrinkBorrowScope utility.

Allow end_borrow instructions to be hoisted over instructions that are
not deinit barriers for the value which is borrowed.  Deinit barriers
include uses of the value, loads of memory, loads of weak references
that may be zeroed during deinit, and "synchronization points".

rdar://79149830
2021-12-07 09:43:57 -08:00
Andrew Trick
056ebe3514 Add Builtin.assumeAlignment(RawPointer, Int64) 2021-11-30 11:53:58 -08:00
Erik Eckstein
f97876c9e7 RLE: better handling of ref_element/tail_addr [immutable]
Rerun RLE with cutting off the base address of loads at `ref_element/tail_addr [immutable]`. This increases the chance of catching loads of immutable COW class properties or elements.
2021-11-29 09:41:05 +01:00
Erik Eckstein
5090603df0 libswift: add an instruction pass to simplify begin_cow_mutation instructions
* Replace the uniqueness result of a begin_cow_mutation of an empty Array/Set/Dictionary singleton with zero.
* Remove empty begin_cow_mutation - end_cow_mutation pairs
* Remove empty end_cow_mutation - begin_cow_mutation pairs
2021-11-29 09:41:05 +01:00
Devin Coughlin
3abd7b0e1f Add a targetOSVersionAtLeast builtin.
This allows the compiler to parse the Swift swiftinterface file of a recent SDK.
2021-11-22 11:59:56 +01:00
Andrew Trick
f9d31ca116 InstructionDeleter ctor moves callbacks to fix iterator invalidation
The InstructionDeleter needs to move the callbacks during construction
to prevent the client code from using the old callbacks.

Fixes iterator invalidation bugs.
2021-11-18 11:38:08 -08:00
Andrew Trick
c86d112891 Update #include for InstructionDeleter.h 2021-11-18 11:38:08 -08:00
Meghana Gupta
39f74bb980 Merge pull request #40002 from meg-gupta/newfixoutliner
Fix OSSA Outliner for scoped guaranteed values
2021-11-17 15:39:36 -08:00
Michael Gottesman
72eb5e2eec [move-operator] Specify if LexicalLifetimes is enabled using an enum instead of a bool.
The reason why I am doing this is that we are going to be enabling lexical
lifetimes early in the pipeline so that I can use it for the move operator's
diagnostics.

To make it easy for passes to know whether or not they should support lexical
lifetimes, I included a query on SILOptions called
supportsLexicalLifetimes. This will return true if the pass (given the passed in
option) should insert the lexical lifetime flag. This ensures that passes that
run in both pipelines (e.x.: AllocBoxToStack) know whether or not to set the
lexical lifetime flag without having to locally reason about it.

This is just chopping off layers of a larger patch I am upstreaming.

NOTE: This is technically NFC since it leaves the default alone of not inserting
lexical lifetimes at all.
2021-11-15 13:47:22 -08:00
Nate Chandler
ee9d309116 [NFC] Added TODO about phi pruning of lifetimes. 2021-11-02 11:51:04 -07:00
Nate Chandler
09dde09a8d [Mem2Reg] Replaced loop with getSingleSuccessor.
Thanks to the lack of critical edges in SIL, if a block B dominated by P
has a successor S which is not dominated by P, then B must have only a
single successor.  Used this fact to replace a loop over successors to a
call to getSinglePredecessor.

Also added an assertion that, in the notation above, B is dominated by
P.
2021-11-02 11:50:28 -07:00
Meghana Gupta
2b0e101a78 Fix OSSA Outliner when guaranteed values are scoped and end before their new uses in the outlined call 2021-11-01 14:21:36 -07:00
Meghana Gupta
2585a5b05c Update DeadEndBlocks analysis in Outliner 2021-11-01 14:21:31 -07:00
Meghana Gupta
0683b3f998 Disable NRVO for alloc_stack [dynamic_lifetime] (#39972)
NRVO for alloc_stack [lifetime] will invalidate OSSA invariants
2021-11-01 13:37:59 -07:00
Michael Gottesman
f9122a79b7 [moveOnly] Implement a new _copy function that performs an explicit copy value.
The key thing is that the move checker will not consider the explicit copy value
to be a copy_value that can be rewritten, ensuring that any uses of the result
of the explicit copy_value (consuming or other wise) are not checked.

Similar to the _move operator I recently introduced, this is a transparent
function so we can perform one level of specialization and thus at least be
generic over all concrete types.
2021-10-29 15:37:46 -07:00
eeckstein
d5ae51130d Merge pull request #39902 from eeckstein/performance-annotations
First prototype of Performance Annotations
2021-10-29 07:39:41 +02:00
nate-chandler
03361671be Merge pull request #39939 from nate-chandler/dce/keep-outer-borrow-scope-alive-when-reborrowing
[DCE] Keep outer borrows alive when reborrowing.
2021-10-28 15:31:29 -07:00
nate-chandler
8e48fd6d79 Merge pull request #39930 from nate-chandler/lexical_lifetimes/mem2reg-end-single-block-lifetimes-with-valid-memory
[Mem2Reg] Handled unreachables for single-block allocations.
2021-10-28 15:20:06 -07:00