Commit Graph

361 Commits

Author SHA1 Message Date
Meghana Gupta
5285bf7200 Turn off speculative devirtualization by default. (#29359)
Turn off speculative devirtualization by default. Add a flag to support enabling the pass.
Fixes rdar://58778959 and rdar://58429282
2020-01-24 10:23:06 -08:00
Michael Gottesman
28ffcf9a7a [ownership] Eliminate the need for mark_uninitialized fixup.
This commit eliminates the need for mark uninitialized fixup by updating the
compiler so that we now emit:

```
%0 = alloc_box
%1 = mark_uninitialized %0
%2 = project_box %1
...
destroy_value %1
```

Instead of:

```
%0 = alloc_box
%1 = project_box %0
%2 = mark_uninitialized %1
...
destroy_value %0
```

Now that the first type of code is generated, I can change project_box to only
take guaranteed arguments. This will ensure that the OSSA ARC optimizer can
eliminate copies of boxes without needing to understand the usage of the
project_box.
2020-01-02 09:54:18 -08:00
Erik Eckstein
f03956b30c Cross-module-optimization: Serialize immediately after CrossModuleSerializationSetup
Otherwise it can happen that e.g. specialization runs between CrossModuleSerializationSetup  and serialization, resulting that an inlinable function references a shared function (which doesn't have a public linkage).
The solution is to move serialization right after CrossModuleSerializationSetup. But only do that if cross-module-optimization is enabled (it would be a disruptive change to move serialization in general).
2019-12-11 18:14:41 +01:00
swift-ci
f0157b0f87 Merge pull request #28473 from atrick/arrayproperty-opt 2019-12-03 10:32:49 -08:00
Erik Eckstein
a5397b434c Cross module optimization
This is a first version of cross module optimization (CMO).

The basic idea for CMO is to use the existing library evolution compiler features, but in an automated way. A new SIL module pass "annotates" functions and types with @inlinable and @usableFromInline. This results in functions being serialized into the swiftmodule file and thus available for optimizations in client modules.
The annotation is done with a worklist-algorithm, starting from public functions and continuing with entities which are used from already selected functions. A heuristic performs a preselection on which functions to consider - currently just generic functions are selected.

The serializer then writes annotated functions (including function bodies) into the swiftmodule file of the compiled module. Client modules are able to de-serialize such functions from their imported modules and use them for optimiations, like generic specialization.

The optimization is gated by a new compiler option -cross-module-optimization (also available in the swift driver).
By default this option is off. Without turning the option on, this change is (almost) a NFC.

rdar://problem/22591518
2019-12-03 14:37:01 +01:00
Andrew Trick
4da33e15ad Cleanup: move ArrayPropertyOpt out of COWArrayOpt.cpp.
These are separate, mostly unrelated passes. Putting them in their own
files makes it easier to read the code, understand how to control the
passes, and makes it possible to independently trace, and debug them.
2019-11-25 11:53:49 -08:00
Michael Gottesman
47de65c17a [ownership] Add an additional run of the SemanticARCOpts at the beginning of the perf pipeline.
I left in the run before DestroyHoisting since I believe that DestroyHoisting
depends a bit on SemanticARCOpts running, but at the same time I don't want to
deal with any regressions that may come from moving DestroyHoisting.
2019-11-20 17:08:03 -08:00
Michael Gottesman
8de96f3959 [silopt] Add a new SerializeSIL utility pipeline.
This pipeline just runs the Serialize SIL pass. The reason I am adding this is
that currently if one passes -disable-sil-perf-optzns or mess with
-sil-opt-pass-count, one can cause serialization to not occur, making it
difficult to bisect/turn off-on opts.
2019-11-18 16:14:57 -08:00
Arnold Schwaighofer
8aaa7b4dc1 SILOptimizer: Pipe through TypeExpansionContext 2019-11-11 14:21:52 -08:00
Michael Gottesman
451252d42f [sil] Add a run of ownership model eliminator in the lowering pass pipeline to ensure we eliminate ownership even if a bisecting counter has disabled the normal ome run.
I also had to fix up one test and eliminated a test that did not really make
sense in the first place to test with ossa. (Specifically,
copy_value_destroy_value is a test I added when adding copy_value,
destroy_value, it really doesn't make sense since we will just lower it to
retain, release before we hit IRGen).
2019-10-24 18:26:15 -07:00
Andrew Trick
bddc69c8a6 Organize SILOptimizer/Utils headers. Remove Local.h.
The XXOptUtils.h convention is already established and parallels
the SIL/XXUtils convention.

New:
- InstOptUtils.h
- CFGOptUtils.h
- BasicBlockOptUtils.h
- ValueLifetime.h

Removed:
- Local.h
- Two conflicting CFG.h files

This reorganization is helpful before I introduce more
utilities for block cloning similar to SinkAddressProjections.

Move the control flow utilies out of Local.h, which was an
unreadable, unprincipled mess. Rename it to InstOptUtils.h, and
confine it to small APIs for working with individual instructions.
These are the optimizer's additions to /SIL/InstUtils.h.

Rename CFG.h to CFGOptUtils.h and remove the one in /Analysis. Now
there is only SIL/CFG.h, resolving the naming conflict within the
swift project (this has always been a problem for source tools). Limit
this header to low-level APIs for working with branches and CFG edges.

Add BasicBlockOptUtils.h for block level transforms (it makes me sad
that I can't use BBOptUtils.h, but SIL already has
BasicBlockUtils.h). These are larger APIs for cloning or removing
whole blocks.
2019-10-02 11:34:54 -07:00
Harlan Haskins
b904133c42 [Modules] Add flag to skip non-inlinable function bodies
This flag, currently staged in as `-experimental-skip-non-inlinable-function-bodies`, will cause the typechecker to skip typechecking bodies of functions that will not be serialized in the resulting `.swiftmodule`. This patch also includes a SIL verifier that ensures that we don’t accidentally include a body that we should have skipped.

There is still some work left to make sure the emitted .swiftmodule is exactly the same as what’s emitted without the flag, which is what’s causing the benchmark noise above. I’ll be committing follow-up patches to address those, but for now I’m going to land the implementation behind a flag.
2019-09-26 10:40:11 -07:00
Nate Chandler
b8062f2054 [SILOptimizer] Added MandatoryCombiner to passes.
The mandatory combiner is added both to the Onone pass and also to the
performance pass.
2019-09-07 09:07:14 -07:00
Erik Eckstein
0e08976600 SILOptimizer: a new optimization to hoist destroys of memory locations
DestroyHoisting moves destroys of memory locations up the control flow as far as possible.
Beside destroy_addr, also "store [assign]" is considered a destroy, because is is equivalent to an destroy_addr + a "store [init]".
The main purpose of this optimization is to minimize copy-on-write operations for arrays, etc. Especially if such COW containers  are used as enum payloads and modified in-place. E.g.

switch e {
  case .A(var arr):
    arr.append(x)
    self = .A(arr)
...

In such a case DestroyHoisting can move the destroy of the self-assignment up before the switch and thus let the array buffer only be single-referenced at the time of the append.

When we have ownership SIL throughout the pass pipeline this optimization will replace the current destroy hoisting optimization in CopyForwarding.
For now, this optimization only runs in the mandatory pipeline (but not for -Onone) where we already have ownership SIL.

SR-10605
rdar://problem/50463362
2019-08-28 15:40:05 +02:00
Ravi Kandhadai
7a3c6d58f5 [stdlib][oslog] Create an SPI _globalStringTablePointer that exposes
the builtin.globalStringTablePointer to the new OSLog overlay.

Modify the new OSLog implementation to use this SPI instead of
`withCString` to pass the (compiler-generated) format string to
the C os_log_impl ABI.

Move the OSLogOptimization pass before constant propagation in
the pass pipeline so that the SPI and the builtin it uses can be
folded to a string_literal instruction.

Update OSLogTests to work with the changes in the implementation.
2019-07-03 17:25:37 -07:00
Ravi Kandhadai
9be4fef53a [SIL Optimization] Add a mandatory optimization pass for optimizing
the new os log APIs based on string interpolation.
2019-05-14 18:08:59 -07:00
ravikandhadai
f563212f03 Revert "[SIL Optimization] Add a mandatory pass for optimizing the new os log APIs based on string interpolation." 2019-05-14 15:11:05 -07:00
Ravi Kandhadai
b7b46622aa [SIL Optimization] Add a mandatory optimization pass for optimizing
the new os log APIs based on string interpolation.
2019-05-13 19:40:39 -07:00
Andrew Trick
301ff8c9f0 Add a SILGenCleanup pass.
The SILGenCleanup pass runs before diagnostics to perform any
canonicalization required by diagnostics.
2019-05-06 09:36:08 -07:00
Arnold Schwaighofer
fbf09031e4 Add a pass to specialize opaque type archetypes.
Clones functions containing opaque type archetypes replacing the opaque
type by a concrete type.

rdar://46140751
2019-05-01 09:31:07 -07:00
Michael Gottesman
1defc94966 [ownership] Add a frontend option to strip ownership after serialization.
I am going to use this to ensure some end-to-end tests that do not inline from
the stdlib will work after flipping the switch and stripping ownership after
serialization.
2019-04-13 20:53:42 -07:00
Slava Pestov
9ac0dc3d6c SILOptimizer: Re-implement escaping capture diagnostics
The new pass is based on existing asserts in DiagnoseStaticExclusivity.
They were compiled out in release builds and only checked for captures of
inout parameters. This patch converts the assertions into diagnostics and
adds checks for captures of non-escaping function values.

Unlike the Sema-based checks that this replaces, the new code handles
transitive captures from recursive local functions, which means certain
invalid code that used to compile will now be rejected with an error.

The new analysis also looks at the ultimate usages of a local function
instead of just assuming all local functions are escaping, which fixes
issues where the compiler would reject valid code.

Fixes a bunch of related issues, including:

- <rdar://problem/29403178>
- <https://bugs.swift.org/browse/SR-8546> / <rdar://problem/43355341>
- <https://bugs.swift.org/browse/SR-9043> / <rdar://problem/45511834>
2019-04-09 15:02:14 -04:00
Michael Gottesman
3d7123e613 [semantic-arc-opts] Enable by default. 2019-03-27 14:15:35 -07:00
Michael Gottesman
381ae7946a [semantic-arc-opts] Move this past closure lifetime fixup. 2019-03-27 09:40:34 -07:00
Michael Gottesman
e6aec46ade [ownership] Serialize/strip ownership at the beginning of the Onone pass pipeline so we strip ownership before use-prespecialized.
There shouldn't be any performance difference from doing this and it allows me
to avoid updating parts of the generic specializer.
2019-03-12 11:43:31 -07:00
Ravi Kandhadai
a9b0ebe542 [SIL Diagnostics] Create a mandatory pass to check correct usage of
yields in generalized accessors: _read and _modify, which are
yield-once corountines. This pass is based on the existing SIL verifier
checks but diagnoses only those errors that can be introduced by programmers
when using yields.

<rdar://43578476>
2019-02-19 18:25:47 -08:00
Michael Gottesman
04ec2c86b2 [ownership] Move OME out of the diagnostics pipeline behind a flag.
Once the flag is flipped, ownership stripping will no longer be done in the
diagnostics pipeline. Instead what will happen is that:

* Onone: At -Onone, we run the entire diagnostics pipeline with ownership
enabled and do not strip ownership until after we serialize in the Onone
"optimization" pass pipeline plan.

* -O: At -O, to temporarily work around serialization issues with transparent
functions, we strip ownership from all but transparent functions at the
beginning of the performance pipeline, serialize, and then strip ownership from
transparent functions. For this to work, I needed to make sure that the
performance pipeline passes that do not support ownership SIL, just skip such
functions. So the transparent functions will arrive (mostly) untouched in
serialized SIL and the rest of the pipeline will optimize non-transparent
functions as they should.

The key thing about the -O change is that it /should/ be performance neutral
since after we serialize we re-run the entire pipeline so we can optimize
semantic functions that we only can inline after we serialize.
2019-02-02 22:58:39 -08:00
Michael Gottesman
fccd75e5ce [sil-passmanager] Stash a SILOptions reference in SILPassPipeline.
This normalizes the creation of pass pipelines by ensuring that all pass
pipelines take a SILOption instead of only some. It also makes it so that we do
not need to propagate options through various pipeline creation helpers.
2019-02-02 22:57:05 -08:00
Michael Gottesman
ac1f956ac0 Split predictable mem opts into two different passes, one that runs before diagnostics and one that runs after diagnostics.
I discovered while updating PMO for ownership that for ~5 years there has been a
bug where we were treating copy_addr of trivial values like an "Assign" (in PMO
terminology) of a non-trivial value and thus stopping allocation
elimination. When I fixed this I discovered that this caused us to no longer
emit diagnostics in a predictable way. Specifically, consider the following
swift snippet:

  var _: UInt = (-1) >> 0

Today, we emit a diagnostic that -1 can not be put into a UInt. This occurs
since even though the underlying allocation is only stored into, the copy_addr
assign keeps it alive, causing the diagnostics pass to see the conversion. With
my fix though, we see that we are only storing into the allocation, causing the
allocation to be eliminated before the constant propagation diagnostic pass
runs, causing the diagnostic to no longer be emitted.

We should truly not be performing this type of DCE before we emit such
diagnostics. So in this commit, I split the pass into two parts:

1. A load promotion pass that performs the SSA formation needed for SSA based
diagnostics to actually work.

2. An allocation elimination passes that run /after/ SSA based diagnostics.

This should be NFC since the constant propagation SSA based diagnostics do not
create memory operations so the output should be the same.
2019-01-17 14:48:03 -08:00
swift-ci
cd8f565adc Merge pull request #21822 from gottesmm/pr-8d2416681ea41244ae3c2bd3d3d3bb35fcc1eade 2019-01-14 12:46:16 -08:00
Michael Gottesman
4fd45930f8 [sil-optimizer] At -Onone serialize when running the Onone optimization pipeline instead of after running SIL passes.
NOTE: This is not in the mandatory passes (which run before this). This will
enable me to strip out ownership after we serialize without touching frontend
code. It also makes Onone and O use the same code paths for serialization
instead of one happening in the driver (Onone today) and the other in a SIL pass
(-O, -Osize).

The reason that I updated the sil-func-extractor test is that I found a bug in
how we emit sib files, namely if you try to emit a sib file to stdout, the
llvm-bcanalyzer flags it as malformed. If I output the .sib into a file rather
than trying to use stdout, everything works.
2019-01-14 10:33:58 -08:00
Michael Gottesman
b2ffe1a4c1 Only run predictable mem opts in the guaranteed pipeline. 2019-01-11 13:55:16 -08:00
Erik Eckstein
130f1629bb SILOptimizer: add an earlier DeadStoreElimination pass to fix a phase ordering problem
GlobalOpt was blocked by a not eliminated dead store, introduced in an integer expression in the initializer.
2019-01-04 14:47:36 -08:00
Michael Gottesman
7d9ec8d742 [semantic-arc-opts] Make sure to only run this pass when optimization is enabled even though it is running during the mandatory passes (for now).
The idea is that eventually down the line we will split this pass into a
mandatory/performance parts once ownership SSA is eliminated after the
guaranteed passes run. For now though, just run this when optimization is
enabled.

This pass only runs on the stdlib/overlays so far so there shouldn't have been
any slow downs in non-stdlib -Onone builds.
2018-12-16 12:39:35 -08:00
Harlan Haskins
2e50a431b1 Merge pull request #20986 from harlanhaskins/clogged-pipes
[SILOptimizer] Stop optimization after serialization if only emitting a module
2018-12-06 15:49:11 -08:00
Harlan Haskins
b137928ff5 [SILOptimizer] Add flag to stop optimization after serialization 2018-12-04 16:44:01 -08:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Michael Gottesman
82c73b3793 [semantic-arc] Move semantic arc opts past definite initialization.
To be clear this only runs on the stdlibs/overlays since it is gated behind a
flag that is set in our cmake.

I can not move it past closure lifetime fixup since the transformation as
written does not express all of the necessary ownership constraints explicitly
in the IR by using addresses.
2018-11-30 11:54:21 -08:00
Joe Shajrawi
32da4f9f45 [Exclusivity] Sink may release release instructions out of access scopes
General case:
begin_access A
...
strong_release / release_value / destroy
end_access

The release instruction can be sunk below the end_access instruction,
This extends the lifetime of the released value, but, might allow us to
Mark the access scope as no nested conflict.
2018-10-31 15:48:08 -07:00
Joe Shajrawi
63b50f65a4 [Exclusivity] Remove dominated access checks with no nested conflict.
General case:

—
begin_access A (may or may not have no_nested_conflict)
load/store
end_access

apply // may have a scoped access that conflicts with A

begin_access A [no_nested_conflict]
load/store
end_access A
—

The second access scope does not need to be emitted.

NOTE: KeyPath access must be identified at the top-level, non-inlinable stdlib entry point.
As such, The sodlib entry pointed is annotated by a new @_semantics that is equivalent to inline(never)
2018-10-25 15:21:06 -07:00
Erik Eckstein
7b16f7f196 SILOptimizer: fix a phase ordering problem, which prevented array optimizations to work in some cases
Introduce an "early redundant load elimination", which does not optimize loads from arrays.
Later array optimizations, like ABCOpt, get confused if an array load in a loop is converted to a pattern with a phi argument.

This problem was introduced with accessors.

rdar://problem/44184763
2018-10-12 09:32:50 -07:00
Raj Barik
89d31be7a5 ExistentialSpecializer Pass (without SILCombine/ConcreteType Propagation) 2018-09-25 14:52:00 -07:00
Erik Eckstein
192431b830 SIL optimizer: remove the RemovePin optimization.
Not useful anymore after removing the pinning adressors
2018-08-23 12:47:56 -07:00
Joe Shajrawi
7281a76deb [AccessEnforcementOpts] Add mergeAccesses optimization 2018-08-09 16:15:25 -07:00
Jordan Rose
cefb0b62ba Replace old DEBUG macro with new LLVM_DEBUG
...using a sed command provided by Vedant:

$ find . -name \*.cpp -print -exec sed -i "" -E "s/ DEBUG\(/ LLVM_DEBUG(/g" {} \;
2018-07-20 14:37:26 -07:00
Andrew Trick
4f13dedc93 Add and enable an AccessEnforcementWMO pass.
Remove dynamic access checks for global variables and and class properties that
have been proven by earlier analysis never to conflict with nested access.

This only applies "-O -enforce-exclusivity=checked" builds. By default, it is
currently NFC.

These are the most important improvements:

RecursiveOwnedParameter                           -93.7%   **15.75x**
ReversedArray                                     -89.1%   **9.17x**
ReversedDictionary                                -81.0%   **5.28x**
ArrayInClass                                      -74.3%   **3.89x**
Ary3                                              -71.7%   **3.54x**
Random                                            -66.2%   **2.96x**
StringWalk                                        -63.5%   **2.74x**
LevenshteinDistance                               -55.4%   **2.24x**
Voronoi                                           -50.0%   **2.00x**
HashTest                                          -47.7%   **1.91x**
Richards                                          -46.7%   **1.88x**
NopDeinit                                         -44.8%   **1.81x**
Rectangles                                        -41.3%   **1.70x**
MultiFileTogether                                 -33.1%   **1.50x**
MultiFileSeparate                                 -32.8%   **1.49x**
SetIntersect_OfObjects                            -26.5%   **1.36x**
Ary2                                              -22.7%   **1.29x**
Prims                                             -21.9%   **1.28x**
PrimsSplit                                        -21.8%   **1.28x**
SetExclusiveOr_OfObjects                          -19.4%   **1.24x**
ObjectAllocation                                  -18.6%   **1.23x**
DropFirstAnySeqCRangeIterLazy                     -17.2%   **1.21x**
DropFirstAnySeqCRangeIter                         -17.2%   **1.21x**
Dictionary4OfObjects                              -16.5%   **1.20x**
SetUnion_OfObjects                                -15.3%   **1.18x**
DropWhileCountableRangeLazy                       -15.3%   **1.18x**
CharIndexing_[*]_Backwards                        -14.6%   **1.17x**
(all 17 variants of CharIndexing are -14%, 1.17x)
CharIteration_[*]_Backwards                       -14.3%   **1.17x**
(all 17 variants of CharIteration take 14%, 1.17x)
RGBHistogramOfObjects                             -14.2%   **1.17x**
DeltaBlue                                         -13.5%   **1.16x**
CharacterPropertiesPrecomputed                    -12.4%   **1.14x**
DictionarySwapOfObjects                           -9.9%    **1.11x**
ClassArrayGetter                                  -9.8%    **1.11x**
DictionaryGroupOfObjects                          -7.9%    **1.09x**
DictionaryRemoveOfObjects                         -7.2%    **1.08x**
Dictionary4OfObjectsLegacy                        -6.8%    **1.07x**
Havlak                                            -6.4%    **1.07x**
COWTree                                           -6.2%    **1.07x**
Radix2CooleyTukeyf                                -5.6%    **1.06x**
2018-06-29 17:56:56 -07:00
Joe Shajrawi
bc59eaad70 [LICM] Refactoring + Improvements + Exclusivity Support
Major refactoring + tuning of LICM. Includes:
Support for hosting more array semantic calls
Remove restrictions for sinking instructions
Add support for hoisting and sinking instruction pairs (begin and end accesses)

Testing with Exclusivity enabled on a couple of benchmarks shows:
ReversedArray 7x improvement
StringWalk 2.6x improvement
2018-06-26 13:26:37 -07:00
Erik Eckstein
bb5fe18300 Add/fix comments as a follow-up to the string constants optimization changes. 2018-06-07 16:22:04 -07:00
Erik Eckstein
ca04efbb44 SIL optimizer: add a few cleanup passes between loop unrolling and GlobalOpt.
Those are needed to fully optimize static String constants.
2018-06-07 13:43:34 -07:00
Michael Gottesman
2872389826 [definite-init] Split raw SIL instruction lowering out of DI into its own pass run after DI.
I am doing this so I can start writing DI tests without this lowering occuring.
There never was a real reason for this code to be in DI beyond convenience. Now
it just makes writing tests more difficult. To prevent any test delta, I changed
all current DI tests to run this pass after DI.
2018-05-22 18:18:22 -07:00