Commit Graph

193 Commits

Author SHA1 Message Date
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
Andrew Trick
5ae231eaab Rename getFieldNo() to getFieldIndex().
Do I really need to justify this?
2020-09-24 22:44:13 -07:00
Erik Eckstein
b9612b2edb SILCombine: fix an assertion crash in SILCombine when casting AnyClass to Any
This caused a problem when propagating the concrete type of an existential: if the concrete type is itself an opened existential, it was not added to the OpenedArchetypeTracker.

https://bugs.swift.org/browse/SR-13444
rdar://problem/68077098
2020-09-01 14:53:48 +02:00
Erik Eckstein
662f03ec4c SILCombine: optimize casts of existential boxes.
Optimize the unconditional_checked_cast_addr in this pattern:

   %box = alloc_existential_box $Error, $ConcreteError
   %a = project_existential_box $ConcreteError in %b : $Error
   store %value to %a : $*ConcreteError
   %err = alloc_stack $Error
   store %box to %err : $*Error
   %dest = alloc_stack $ConcreteError
   unconditional_checked_cast_addr Error in %err : $*Error to ConcreteError in %dest : $*ConcreteError

to:
   ...
   retain_value %value : $ConcreteError
   destroy_addr %err : $*Error
   store %value to %dest $*ConcreteError

This lets the alloc_existential_box become dead and it can be removed in following optimizations.
The same optimization is also done for conditional_checked_cast_addr.

There is also an implication for debugging:
Each "throw" in the code calls the runtime function swift_willThrow. The function is used by the debugger to set a breakpoint and also add hooks.
This optimization can completely eliminate a "throw", including the runtime call.
So, with optimized code, the user might not see the program to break at a throw, whereas in the source code it is actually throwing.
On the other hand, eliminating the existential box is a significant performance win and we don't guarantee any debugging behavior for optimized code anyway. So I think this is a reasonable trade-off.
I added an option "-Xllvm -keep-will-throw-call" to keep the runtime call which can be used if someone want's to reliably break on "throw" in optimized builds.

rdar://problem/66055678
2020-07-29 21:57:51 +02:00
Erik Eckstein
fc3e68a9d5 SILCombine: optimize a load from global static let
Propagate a value from a static "let" global variable.
This optimization is also done by GlobalOpt, but not with de-serialized globals, which can occur with cross-module optimization.
2020-06-22 16:49:26 +02:00
Erik Eckstein
6a9d08793a SILCombine: a peephole optimization to optimize alloc_stack of enums.
Replaces an alloc_stack of an enum by an alloc_stack of the payload if only one enum case (with payload) is stored to that location.

For example:

  %loc = alloc_stack $Optional<T>
  %payload = init_enum_data_addr %loc
  store %value to %payload
  ...
  %take_addr = unchecked_take_enum_data_addr %loc
  %l = load %take_addr

is transformed to

  %loc = alloc_stack $T
  store %value to %loc
  ...
  %l = load %loc

https://bugs.swift.org/browse/SR-12710
2020-06-10 09:23:37 +02:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Erik Eckstein
33c8e16ce0 SIL optimizer: Support begin_cow_mutation and end_cow_mutation in some optimizations.
Mostly this is about "looking through" a begin_cow_mutation or end_cow_mutation.
2020-05-26 18:01:17 +02:00
Saleem Abdulrasool
fa46f7131c sprinkle some llvm_unreachable for MSVC (NFC)
MSVC does not realize that the switch is exhaustive and requires that
the path is explicitly marked as unreachable.  This silences the C4715
warning ("not all control paths return a value").
2020-04-24 18:59:07 -07:00
David Zarzycki
e2c4aa13ed [SILOptimizer] Do not hardcode unowned for loadable ref storage types 2020-04-17 10:40:59 -04:00
swift-ci
22d7267899 Merge pull request #30625 from zoecarver/fix/sil-combine-empty-visitors 2020-03-25 00:04:09 -07:00
swift-ci
0eeefaea05 Merge pull request #30559 from zoecarver/ossa/sil-combine 2020-03-24 22:02:43 -07:00
zoecarver
56d5c8aa4a [nfc] remove empty visitors in silcombine
Remove visitors in SILCombine that only return nullptr.
2020-03-24 20:03:59 -07:00
zoecarver
d3a01131d7 Update all relevant visitors in SILCombine to skip ownership functions.
Instead of bailing on ownership functions in SILCombine::run, we will
bail in individual visitors. This way, as SILCombine is updated we can
paritially support ownership across the pass.
2020-03-24 19:55:08 -07:00