Commit Graph

206 Commits

Author SHA1 Message Date
Meghana Gupta
5e20112e0c Revert "Implement several peephole optimizations to unblock further optimizations of autodiff code (#60520)"
This reverts commit 2f5492f572.
2022-10-25 13:35:14 -07:00
Erik Eckstein
ed54253d29 SIL Optimizer: remove legacy C++ passes
They were used as a backup during the transition to Swift passes. Now they are not needed anymore.
2022-10-20 18:31:06 +02:00
Anton Korobeynikov
2f5492f572 Implement several peephole optimizations to unblock further optimizations of autodiff code (#60520)
* Implement several peephole optimizations to unblock further optimizations of autodiff code

1. Simplify differentiable_function_extract of differentiable_function.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = differentiable_function_extract [original] %x
After:
%y = %orig

2. Push conversion instructions inside of differentiable_function.
This unblocks inlining and specialization.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = convert_escape_to_noescape %x
After:
%orig' = convert_escape_to_noescape %orig
%jvp' = convert_escape_to_noescape %jvp
%vjp' = convert_escape_to_noescape %vjp
%y = differentiable_function(%orig', %jvp', %vjp')

3. Another peephole is needed for reordering function conversion instructions to enable full inlining:
(convert_escape_to_noescape (convert_function (thin_to_thick_function x)))
=>
(convert_escape_to_noescape (thin_to_thick_function (convert_function x)))

* Remove broken disabled folding transform:
  - It is disabled
  - It is broken
  - It is certainly not something mandatory

Co-authored-by: Dan Zheng <danielzheng@google.com>
2022-09-26 09:44:04 -07:00
Erik Eckstein
97b2354be6 SIL: add needsStackProtection flags for address_to_pointer and index_addr instructions.
Also add new "unprotected" variants of the `addressof` builtins:
* `Builtin.unprotectedAddressOf`
* `Builtin.unprotectedAddressOfBorrow`
2022-09-08 08:42:22 +02:00
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
Erik Eckstein
777b42180d SILOptimizer: use the new set data structures in various optimizations 2022-07-13 14:27:50 +02:00
Allan Shortlidge
f0360a2984 Fix trivial warnings emitted during a full build. 2022-04-05 17:16:19 -07:00
Arnold Schwaighofer
0c9a54a789 Also optimize calls to getContiguousArrayStorageType<SomeClass> to ContiguousArrayStorage<AnyObject> under the right deployment setting 2022-02-23 07:19:19 -08: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
Erik Eckstein
3cb63c0646 SILCombine: fix a crash when optimizing stack allocations of existentials
The deinit_existential_addr instruction needs to be handled.

rdar://86069731
2021-12-06 12:31:55 +01:00
Erik Eckstein
b89f58de6d SILCombine: replace Array.capacity with 0 if it's the empty array singleton
We already do that for Array.count. Now add Array.capacity.
2021-11-29 09:41:05 +01:00
Andrew Trick
a5b1b5c3f8 SILOptimizer OSSA support for switch_enum & checked_cast_br
To create OSSA terminator results, use:
- OwnershipForwardingTermInst::createResult(SILType ValueOwnershipKind)
- SwitchEnumInst::createDefaultResult()

Add support for passing trivial values to nontrivial forwarding
ownership. This effectively converts None to Guaranteed ownership.

This is essential for handling ".none" enums as trivial values while
extracting a nontrivial payload with switch_enum. This converts None
to Guaranteed ownership. Generates a copy if needed to convert back to
Owned ownership.
2021-09-07 22:50:46 -07:00
Min-Yih Hsu
343d842394 [SIL][DebugInfo] PATCH 3/3: Deprecate debug_value_addr SIL instruciton
This patch removes all references to DebugValueAddrInst class and
debug_value_addr instruction in textual SIL files.
2021-08-31 12:01:04 -07:00
Min-Yih Hsu
e1023bc323 [DebugInfo] PATCH 2/3: Duplicate logics regarding debug_value_addr
This patch replace all in-memory objects of DebugValueAddrInst with
DebugValueInst + op_deref, and duplicates logics that handles
DebugValueAddrInst with the latter. All related check in the tests
have been updated as well.

Note that this patch neither remove the DebugValueAddrInst class nor
remove `debug_value_addr` syntax in the test inputs.
2021-08-31 11:57:56 -07:00
Erik Eckstein
8be0ca07c2 SIL optimizer: remove unbalanced retains/releases from immortal objects
ARC operations don't have an effect on immortal objects, like the empty array singleton or statically allocated arrays.
Therefore we can freely remove and retain/release instructions on such objects, even if there is no paired balanced ARC operation.

This optimization can only be done with a minimum deployment target of Swift 5.1, because in that version we added immortal ref count bits.

The optimization is implemented in libswift. Additionally, the remaining logic of simplifying strong_retain and strong_release is also ported to libswift.

rdar://81482156
2021-08-23 10:23:59 +02:00
Erik Eckstein
e8d408d036 libswift: implement SILCombine's global_value optimization as instruction pass in libswift.
But keeping the old visit function as legacy
2021-06-09 11:33:41 +02:00
Andrew Trick
0407a4e34a Add UpdatingInstructionIterator.
Track in-use iterators and update them both when instructions are
deleted and when they are added.

Safe iteration in the presence of arbitrary changes now looks like
this:

    for (SILInstruction *inst : deleter.updatingRange(&bb)) {
      modify(inst);
    }
2021-06-02 07:38:27 -07:00
Andrew Trick
0f88e0f3cc Rewrite instruction deletion logic in many passes
Fix innumerable latent bugs with iterator invalidation and callback invocation.

Removes dead code earlier and chips away at all the redundant copies the compiler generates.
2021-06-02 07:38:27 -07:00
Erik Eckstein
ea8f43a8ba SILCombine: remove unneeded reference counting instructions from global_value
Delete all reference count instructions on a global_value if the only other uses are projections (ref_element_addr and ref_tail_addr).
2021-03-15 11:54:23 +01:00
Erik Eckstein
78a7887244 SILCombine: optimize mark_dependence of a string literal.
This pattern can occur after StringOptimization when a utf8CString of a literal is replace by the string_literal itself.
2021-03-08 08:34:53 +01:00
Erik Eckstein
fe10f98cf0 SIL: rename the SILBitfield.h header file to BasicBlockBits.h
NFC
2021-02-12 11:15:55 +01:00
Erik Eckstein
65b03f9815 SILOptimizer: prevent illegal load forwarding of function references in global variables.
We cannot replace a load from a global let-variable with a function_ref, if the referenced function would violate the resilience rules.
That means if a non-public function_ref would be inlined into a function which is serialized.
2021-02-09 19:56:43 +01:00
Michael Gottesman
d1e462cdd1 [sil-combine] Leave the new load at the old load site when promoting unchecked_take_enum_data_addr + load -> load + unchecked_enum_data.
We must make sure the new load is inserted where the old load was instead of
inserting it at the unchecked_take_enum_data_addr, since the
unchecked_take_enum_data_addr may be in a different block from old load /and/
old load may not post-dominate that point. This was just a thinko.
2021-02-05 17:56:15 -08:00
Michael Gottesman
6475178189 [sil-combine] When promoting an unchecked_take_enum_data_addr + load -> load + unchecked_enum_data, make sure to handle the non-trivialness of the operand and the trivialness of the result correctly. 2021-02-05 12:31:16 -08:00
Erik Eckstein
5dd322ffaf SILCombine: remove release_value of a trivial enum
Even if the enum type is not trivial (because it has not trivial payloads for some cases), a release of such an enum can be removed if the enum is constructed with a trivial case.
2021-02-05 08:53:03 +01:00
Erik Eckstein
34ae1d911a SILCombine: remove release_value of a value_to_bridge_object
Needed to remove unneeded reference counting for String literals.
2021-02-04 07:53:32 +01:00
Erik Eckstein
d33ea9f350 SIL: remove the JointPostDominanceSetComputer helper struct.
Instead make `findJointPostDominatingSet` a stand-alone function.
There is no need to keep the temporary SmallVector alive across multiple calls of findJointPostDominatingSet for the purpose of re-using malloc'ed memory. The worklist usually contains way less elements than its small size.
2021-02-02 10:20:35 +01:00
Michael Gottesman
c32df33393 [sil-combine] Enable alloc_ref_dynamic -> alloc_ref canonicalization
This involves folding a metatype into the alloc_ref_dynamic and we always
replace the alloc_ref_dynamic with an alloc_ref so this is always safe from an
ownership perspective.
2021-01-28 12:10:16 -08:00
Michael Gottesman
ddba98d51e [sil-combine] Enable mark_dependence simplifications. 2021-01-28 12:10:16 -08:00
Michael Gottesman
7d5c60b87d [sil-combine] Implement simple alloc_existential_box removal. 2021-01-28 12:10:16 -08:00
Michael Gottesman
35f941f8bb [sil-combine] Update unchecked_take_enum_data_addr -> unchecked_enum_data promotion for ownership. 2021-01-28 12:10:15 -08:00
Michael Gottesman
ac2ef83951 [sil-combine] Enable cond_br canonicalizations in ossa.
The key thing is that all of these while they do modify the branches of the CFG
do not invalidate block level CFG analyses like dominance and dead end
blocks.
2021-01-28 12:10:15 -08:00
Erik Eckstein
f48191966c SILOptimizer: use BasicBlockSet instead of SmallPtrSet in various transformations.
It reduces compile time.
2021-01-27 10:31:17 +01:00
Michael Gottesman
04ba6c6d96 [sil-combine] Enable select_enum canonicalization in OSSA. 2021-01-25 20:30:39 -08:00
Michael Gottesman
bf5ac43a66 [sil-combine] Enable unreachable pruning for ownership.
This doesn't mess with ownership, so we can just enable it.
2021-01-25 18:46:45 -08:00
Michael Gottesman
d6ffddc63f [sil-combine] Add ownership support for switch_enum_addr transforms.
I implemented specifically the promotion from switch_enum_addr -> switch_enum
for loadable types.

NOTE: I did not add support for the inject_enum_addr based conversion to br since that
optimization deletes an edge from the CFG, potentially invalidating CFG
properties. Since OSSA needs to use DeadEndBlocks, we want to avoid changing
reachability in any way with our SILCombine transforms.
2021-01-25 16:00:32 -08:00
Michael Gottesman
94c643cb27 [sil-combine] Enable ownership on all of the apply visitors.
All of the non-SILCombiner specific helpers have already been updated for OSSA,
so this was not too bad.

NOTE: I also added two small combines that delete copy_value, destroy_value with
.none arguments. The reason why I added this is that this is a pretty small
addition and many of the tests of this code rely on SILCombine being able to
eliminate such operations on thin_to_thick_function.

NOTE: I also disabled TypePropagation in OSSA, we are going to redo that code
when we bring up opaque values.
2021-01-19 22:47:18 -08:00
Michael Gottesman
a3f172884e [sil-combine] Enable classify_bridge_object folding when ownership is enabled.
From an ownership perspective, this is just eliminating a non-lifetime ending
use. The new instructions we create are not connecting to the underlying object
anymore.
2021-01-18 14:00:48 -08:00
Michael Gottesman
364cee87aa [sil-combine] Enable elimination of simple tuple results from overflow functions when ownership is enabled.
This doesn't actually touch non-trivial values, so from an ownership perspective
it is inert. It is tested by the pattern in the next commit.
2021-01-18 13:55:56 -08:00
Michael Gottesman
1e7990c9af [sil-combine] Enable cond_fail elimination for ownership. 2021-01-18 13:47:24 -08:00
Michael Gottesman
5572d04010 [sil-combine] Enable optimized nested index_addr optimization in ownership.
This just works with transitive addresses meaning that there aren't any
ownership issues we need to think about.
2021-01-18 10:48:38 -08:00
Michael Gottesman
1ab36330a6 Revert "[sil-combine] Update switch_enum_addr -> switch_enum for loadable types for ownership."
This reverts commit 0a0f8cffc1.

Currently the memory lifetime verifier (I believe due to the switch_enum) is not
caring that we are not emitting destroy_addr for an enum address along paths
where we know the underlying enum case is trivial.

This transform eliminated that switch_enum_addr causing the memory lifetime
verifier to then trigger.

Disabling to unblock the bots!
2021-01-05 13:08:53 -08:00
Michael Gottesman
560483114d [sil-combine] Enable switch_value to be optimized in OSSA.
This routine never touches values with non-trivial ownership, so I just removed
the early exit and updated the style.
2021-01-04 15:44:58 -08:00
Michael Gottesman
2413f5fe7c [sil-combine] Update select_enum_addr canonicalization to use OSSA. 2021-01-04 15:44:58 -08:00
Michael Gottesman
0a0f8cffc1 [sil-combine] Update switch_enum_addr -> switch_enum for loadable types for ownership. 2021-01-04 15:44:58 -08:00
Michael Gottesman
a5e7c3f271 [sil-combine] Update LoadInst opts for OSSA and expand opts to also support LoadBorrow.
NOTE: The stdlib count/capacity propagation code is tested in an end<->end
fashion in a separate Swift test. Once I flip the switch, that test will run.
The code is pretty simple, so I feel relatively confident with it.
2021-01-04 15:44:58 -08:00
Michael Gottesman
04ffcafaa5 [sil-combine] Enable dead alloc_ref elim for ossa.
It was just looking for an alloc_ref that only has dealloc_ref,
set_deallocating, fix_lifetime users.
2020-12-21 13:07:00 -08:00
Michael Gottesman
061632edef [sil-combine] Update fix_lifetime opts for ownership
The one opt we perform here is that we promote fix_lifetime on loadable
alloc_stack addresses to fix_lifetimes on objects by loading the underlying
value and putting the fix lifetime upon it.
2020-12-20 21:22:19 -08:00
Michael Gottesman
9f6688e185 [sil-combine] Use high level ARC APIs instead of low level ARC APIs in a few cases to make code work in ossa and non-ossa. 2020-12-10 16:50:26 -08:00
Erik Eckstein
28220ff259 SILCombine: make the alloc_stack-enum optimization a bit more tolerant.
When eliminating an enum from an alloc_stack, accept "dead" inject_enum_addr instructions, which inject different enum cases.
2020-09-30 16:44:58 +02:00