Commit Graph

84 Commits

Author SHA1 Message Date
Andrew Trick
e8b0947897 [Exclusivity] Allow testing the -Onone pipeline with access markers.
Markers are always eliminated before -O passes.

At -Onone, markers can be enabled via command line for all -Onone passes.
2017-04-26 17:32:48 -07:00
practicalswift
7eb7d5b109 [gardening] Fix 100 typos. 2017-04-18 17:01:42 +02:00
Michael Gottesman
aa76c2a5e6 [silgen] (mark_uninitialized (project_box (alloc_box))) -> (project_box (mark_uninitialized (alloc_box)))
I put in a simple fixup pass (MarkUninitializedFixup) for staging purposes. I
don't expect it to be in tree long. I just did not feel comfortable fixing up in
1 commit all of the passes up to DI.

rdar://31521023
2017-04-17 17:45:54 -07:00
John McCall
b9676d2002 Add a SIL pass to select an access enforcement for allocated boxes.
Tests to come.
2017-04-11 03:10:51 -04:00
Andrew Trick
4355cad83e Add a pass to eliminate access markers. 2017-04-10 09:47:50 -07:00
Devin Coughlin
bee17f7838 [SILDiagnostics] Add static enforcement of Law of Exclusivity (#8560)
Add a diagnostic pass that emits errors when a violation of the "Law of
Exclusivity" is detected at compile time. The Law of Exclusivity requires
that the access duration of any access to an address not overlap
with an access to the same address unless both accesses are reads.

This pass relies on 'begin_access' and 'end_access' SIL instruction
markers inserted by SILGen to determine when an access to an address begins and
ends. It models the in-progress accesses with a map from storage locations to
the counts of read and write-like accesses in progress for that location.
2017-04-09 21:41:13 -07:00
Andrew Trick
be1881aa1f Remove redundant Transform.getName() definitions.
At some point, pass definitions were heavily macro-ized. Pass
descriptive names were added in two places. This is not only redundant
but a source of confusion. You could waste a lot of time grepping for
the wrong string. I removed all the getName() overrides which, at
around 90 passes, was a fairly significant amount of code bloat.

Any pass that we want to be able to invoke by name from a tool
(sil-opt) or pipeline plan *should* have unique type name, enum value,
commend-line string, and name string. I removed a comment about the
various inliner passes that contradicted that.

Side note: We should be consistent with the policy that a pass is
identified by its type. We have a couple passes, LICM and CSE, which
currently violate that convention.
2017-04-09 15:20:28 -07:00
Andrew Trick
3bcbf04d38 Rewrite unused SIL pass descriptions as proper, descriptive pass names.
I improved all of the pass names so they are minimal but fully descriptive.

The PASS macros contained wordy, unhelpful descriptions, which served
no functional purpose. Those descriptions are permanently preseved in
git history. If anyone thinks they contain useful information, then
they should be added to the *definition* of the pass (where passes are
actually described).

Long-winded pass comments have no place in Passes.def. The purpose of
those macros is to associate a unique type name, enum value,
command-line-option name, and pretty name with each pass for tooling
purposes. Any one of those entities needs to be sufficient for looking
up the others. That's not where anyone should look read a description
of the pass.
2017-04-09 15:12:53 -07:00
Michael Gottesman
5e278fdf67 [semantic-arc-opts] Create a new pass called semantic arc opts.
I am going to run it very early and use it to ensure that extra copies due to my
refactoring of SILGenPattern do not cause COW copies to be introduced.

For now, it does a very simple optimization, namely, it eliminates a copy_value,
with only a destroy_value user on a guaranteed parameter.

It is now disabled behind a flag.
2017-04-05 16:16:57 -07:00
Erik Eckstein
a0079ba5be SIL optimizations: Implement the new API for analysis invalidation.
There are now separate functions for function addition and deletion instead of InvalidationKind::Function.
Also, there is a new function for witness/vtable invalidations.

rdar://problem/29311657
2017-03-14 13:00:54 -07:00
Andrew Trick
855918c620 [Lowering] Add an AddressLowering pass. 2017-02-13 17:10:02 -08:00
Andrew Trick
2f4c84b41f SILPipelinePlan silently doesn't run some passes. Add an assert. 2017-02-13 17:10:02 -08:00
Andrew Trick
a6ebb4e914 Add an option -Xllvm -sil-print-on-error.
Print the SIL function body on an assert. Recovering the SIL code is the
critical path for pretty much any SIL development. The only alternative is
rebuilding the library with string matching or building a debug compiler and
hoping lldb works. The standard library takes a very long time to build with a
debug compiler.
2017-02-01 10:51:53 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Hugh Bellamy
ac3b56a554 Fix errors and warnings building swift/SILOptimizer on Windows using MSVC
-
https://connect.microsoft.com/VisualStudio/feedback/details/3116505/msvc-fails-to-compile-code-that-compiles-with-clang-reports-attempting-to-reference-a-deleted-function-error-from-destructor
-
https://connect.microsoft.com/VisualStudio/feedback/details/3116636/msvc-reports-ambiguous-symbol-error-for-friend-class-declaration-in-an-anonymous-namespace
2016-12-22 18:26:58 +00:00
Arnold Schwaighofer
f1c2dcf1fa Add an alloc_stack hoisting pass.
Hoist alloc_stack instructions of 'generic' or resilient type to the entry
block. At the same time also perform a very simple stack coloring analysis.
This does not use a true liveness-analysis yet but rather employs some simple
conservative checks to see whether the live ranges of two alloc_stacks might
interfere.

AllocStackHoisting is an IRGen SIL pass. This allows for using IRGen's type
lowering information. Furthermore, hoisting and merging the alloc_stack
instructions this late does not interfere with SIL optimizations because the
resulting SIL never gets serialized.
2016-12-20 07:51:55 -08:00
Arnold Schwaighofer
f38c912878 Add support of a IRGen lowering SIL pipeline.
This pipeline is run as part of IRGen and has access to the IRGenModule.

Passes that run as part of this pipeline can query for the IRGenModule.

We will use it for the AllocStackHoisting pass. It wants to know if a type is of
non-fixed size.

To break the cyclic dependency between IRGen -> SILOptimizer -> IRGen that would
arise from the SILPassManager having to know about the createIRGENPASS()
function IRGen passes instead of exposing this function dynamically have to add
themselves to the pass manager.
2016-12-20 07:51:47 -08:00
Michael Gottesman
be5ec6f990 [passmanager] Remove ExecutionKind::UntilFixPoint and all of ExecutionKind.
rdar://29650781
2016-12-19 13:38:46 -08:00
swift-ci
268c2b668d Merge pull request #6325 from practicalswift/gardening-20161216 2016-12-17 09:19:24 -08:00
Michael Gottesman
4bfaef8ae0 [semantic-sil] Add a new pass that dumps out the ownership of all SILValue in a function and performs some minor checks upon them.
rdar://29671437
2016-12-16 17:53:49 -08:00
practicalswift
92d4aa3b91 [gardening] Use consistent headers. 2016-12-16 21:42:09 +01:00
practicalswift
77a07467bf [gardening] Fix invalid Swift URLs in headers. 2016-12-16 21:42:08 +01:00
Michael Gottesman
1a47a25e90 [sil-bug-reducer] Add pass BugReducerTester to test sil-bug-reducer.
This pass works by blowing up if it finds an apply that calls a function
specified via the cl command line option 'bug-reducer-tester-target-func'. This
makes it easy to test sil-bug-reducer.
2016-12-12 23:30:51 -08:00
Michael Gottesman
483388c9a6 [sil-bug-reducer] Wire up SILPassManager to SILPassManagerPipeline.
We also either remove or make private the addPass* functions on SILPassManager,
so the only way to execute passes via SILPassManager is by creating a
SILPassPipelinePlan. This beyond adding uniformity ensures that we always
resetAndRemoveTransformations properly after a pipeline is run.
2016-12-12 14:42:46 -08:00
Michael Gottesman
3c61f783ea [sil-bug-reducer] Add SILPassPipeline{,Plan}. Add current pass pipelines to it.
This commit adds the functionality, but does not change SILPassManager to use
it. The reason why I am doing this is so I can implement sil-opt pass bisecting
functionality in python using a tool that dumps the current pass pipelines
out. This will ensure that even in the face of changes to the pass pipelines,
everything should just work.
2016-12-12 14:38:56 -08:00
Michael Gottesman
2177f770cc Merge pull request #6222 from gottesmm/pp-1
[sil-bug-reducer] Separate construction of pass pipelines into separate functions.
2016-12-12 10:37:22 -08:00
Michael Gottesman
9dd7074d96 [sil-func-extractor] Rather than manually creating a pass manager just to run DFE, just
add a high level API to perform the operation.
2016-12-12 09:16:04 -08:00
Michael Gottesman
a0cf40cf6b [gardening] Remove typedef that was used in only one place. 2016-12-12 09:13:32 -08:00
Roman Levenstein
354fd29f31 Add a new -assume-single-threaded option
This is a hidden option. It should be used like: -assume-single-threaded

When this function is provided, the compiler assumes that the code will be executed in the single threaded mode. It then performs certain optimizations that can benefit from it, e.g. it  marks as non-atomic all reference counting instructions in the user code being compiled.
2016-12-05 10:05:34 -08:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Michael Gottesman
6f4e2ab391 [semantic-arc] Add a new guaranteed ARC optimization pass.
Often times SILGen wants to hold onto values that have been copied. This causes
an issue, when due to Cleanups firing, SILBuilder inserts destroys and destroys
the copy that produced the value that SILGen held onto. This will then cause
SILGen to emit incorrect code.

There really is no reason to introduce such complexity into SILBuilder when a
small simple guaranteed pass can perform the same work. Thus the introduction of
this pass.

In a later commit, I am going to eliminate the SILBuilder entry points.

rdar://28685236
2016-11-02 11:24:05 -07:00
Michael Gottesman
e173438d6b [semantic-arc] Even when we are not running diagnostics, run the sil ownership eliminator.
This only happens when we are testing deserialization.
2016-10-29 20:11:08 -07:00
practicalswift
cc852042c9 [gardening] Fix accidental trailing whitespace. 2016-10-29 10:22:58 +02:00
Michael Gottesman
e48e7b7fca [semantic-arc] Add a new pass called the OwnershipModelEliminator that eliminates SILOwnership from the IR.
It currently just breaks up qualified loads/stores into their unqualified
constituant parts.

rdar://28685236
2016-10-15 21:35:34 -07:00
Joe Shajrawi
91bba4d425 Do not emit shadow copied for inout parameters (#5218)
radar rdar://problem/28434323

SILGen has no reason to insert shadow copies for inout parameters any more. They cannot be captured. We still emit these copies. Sometimes deshadowing removes them, but sometimes it does not.

In this PR we just avoid emitting the copies and remove the deshadowing pass.

This PR chery-picked some of @dduan work and built on top of it.
2016-10-13 10:10:59 -07:00
practicalswift
b19481f887 [gardening] Fix 67 recently introduced typos 2016-09-16 11:16:07 +02:00
Erik Eckstein
959e19d7bc Add an optimization to eliminate a partial_apply if all applied arguments are dead in the applied function.
This consists of 3 parts:
1) Extend CallerAnalysis to also provide information if a function is partially applied
2) A new DeadArgSignatureOpt pass, similar to FunctionSignatureOpts, which just specializes for dead arguments of partially applied functions.
3) Let CapturePropagation eliminate such partial_apply instructions and replace them with a thin_to_thick conversion of the specialized functions.

This optimzation improves benchmarks where static struct or class functions are passed as a closure (e.g. -20% for SortStrings).
Such functions have a additional metatype parameter. We used to create a partial_apply in this case, which allocates a context, etc.
But this is not necessary as the metatype parameter is not used in most cases.

rdar://problem/27513085
2016-08-23 07:32:41 -07:00
Erik Eckstein
a75a7f25f9 Re-instate: "SILPassManager: After a new function is pushed on the stack don't restart the pipeline on the old function."
This re-instates commit de9622654d

The problem of the infinite loop should be fixed by the previous fix in FunctionSignatureOpts.
In addition this new commit implements a safety check to void such cases, even if buggy optimizations try to keep pushing new functions onto the work list.
2016-08-19 14:04:49 -07:00
Greg Parker
19e0eb8537 Revert "SILPassManager: After a new function is pushed on the stack don't res…" 2016-08-18 14:48:12 -07:00
Erik Eckstein
de9622654d SILPassManager: After a new function is pushed on the stack don't restart the pipeline on the old function.
Instead the pipeline is continued on the old function. This happens when a pass pushes a new, e.g. specialized function, on the function stack.
There is no need to repeat passes which already did run on a function.
It saves a little of compile time and I didn't see any significant impact on code size or performance.
It also simplifies the pass manager.
2016-08-17 11:48:05 -07:00
Michael Gottesman
ac3024292b [simplify-cfg] When moving cond_fail to preds, do not bail early.
This commit fixes a bug where we were not checking that all predecessors had the
cond_fail block as its only successor. This occured since we were bailing early
when we saw a constant. So if we saw a predecessor with a constant before a
predecessor that had multiple successors, we would optimize even though we would
be introducing an extra cond_fail along a path.

I added a new utility pass to test this code since so much is going on in
SimplifyCFG that it is difficult to construct a test case running the full
pass.

Really this code should be in a different pass (properly SIL Code Motion TBH).
But for now, this commit just fixes the bug.

rdar://26904047
2016-07-17 23:31:53 -07:00
Xin Tong
eaaf825032 Implement an iterative data flow to find epilogue retains or releases.
We have a few places this analysis can be used. e.g. FSO, ASO, etc.
I will wire them up one by one later.

rdar://problem/26446587
2016-07-11 14:06:06 -07:00
Roman Levenstein
8f85b3d345 Print a pass number of the current SIL pass when a compiler crashes
It makes it easier to perform a bisection and finding out the reason of the crash.
2016-06-28 09:24:58 -07:00
Erik Eckstein
d6e86b7c4b Add a new SIL pass to move conditions closer to switch_enum to enable jump threading.
For details see the comment in ConditionForwarding.cpp.
This optimization pass helps to optimize loops iterating over closed ranges, e.g. for i in 0...n { }
2016-05-05 10:34:08 -07:00
Xin Tong
51b1c0bc68 Implement retain, release code motion.
Iterative data flow retain sinking and release hoisting.

This allows us to sink retains and hoist releases across harmless loops. which is
an improvement on the SILCodeMotion retain sinking and release hoisting.

It also separates the duty of moving retain and release with the duty of eliminating them
in ASO.

This should eventually replace RR code motion in SILcodemotion and insertion point
in ARCsequence opts (ASO).

This is the performance difference i get with retain sinking and release hoisting.
After disabling retain release code motion in ASO and SILCodeMotion. we can start to take
those code out once this lands.

I see that we go from 24.5% of time spent in SILOptimizations w.r.t. the whole stdlib compilation
to 25.1%.

Improvement is better (i.e. retain sinking and hoisting releases result in performance gain).

<details open>
  <summary>Regression (7)</summary>

TEST                                                    | OLD_MIN | NEW_MIN | DELTA (%) | SPEEDUP
---                                                     | ---     | ---     | ---       | ---
SetIsSubsetOf                                           | 441     | 510     | +15.7%    | **0.86x**
SetIntersect                                            | 1041    | 1197    | +15.0%    | **0.87x**
BenchLangCallingCFunction                               | 184     | 211     | +14.7%    | **0.87x**
Sim2DArray                                              | 326     | 372     | +14.1%    | **0.88x**
SetIsSubsetOf_OfObjects                                 | 498     | 567     | +13.9%    | **0.88x**
GeekbenchGEMM                                           | 945     | 1022    | +8.2%     | **0.92x**
COWTree                                                 | 3839    | 4181    | +8.9%     | **0.92x(?)**

</details>

<details >
  <summary>Improvement (31)</summary>

TEST                                                    | OLD_MIN | NEW_MIN | DELTA (%) | SPEEDUP
---                                                     | ---     | ---     | ---       | ---
ObjectiveCBridgeFromNSDictionaryAnyObjectToString       | 174526  | 165392  | -5.2%     | **1.06x**
RGBHistogram                                            | 3128    | 2957    | -5.5%     | **1.06x**
ObjectiveCBridgeToNSDictionary                          | 16510   | 15494   | -6.2%     | **1.07x**
LuhnAlgoLazy                                            | 2294    | 2120    | -7.6%     | **1.08x**
DictionarySwapOfObjects                                 | 6477    | 5994    | -7.5%     | **1.08x**
StringRemoveDupes                                       | 1610    | 1485    | -7.8%     | **1.08x**
ObjectiveCBridgeFromNSSetAnyObjectToString              | 159358  | 147824  | -7.2%     | **1.08x**
ObjectiveCBridgeToNSSet                                 | 16191   | 14924   | -7.8%     | **1.08x**
DictionaryHashableClass                                 | 1839    | 1704    | -7.3%     | **1.08x**
DictionaryLiteral                                       | 2906    | 2678    | -7.8%     | **1.09x(?)**
StringUtilsUnderscoreCase                               | 10031   | 9187    | -8.4%     | **1.09x**
LuhnAlgoEager                                           | 2320    | 2113    | -8.9%     | **1.10x**
ObjectiveCBridgeFromNSSetAnyObjectToStringForced        | 99553   | 90348   | -9.2%     | **1.10x**
RIPEMD                                                  | 3327    | 3009    | -9.6%     | **1.11x**
Combos                                                  | 595     | 538     | -9.6%     | **1.11x**
Roman                                                   | 10      | 9       | -10.0%    | **1.11x**
StringUtilsCamelCase                                    | 10783   | 9646    | -10.5%    | **1.12x**
SetIntersect_OfObjects                                  | 2511    | 2182    | -13.1%    | **1.15x**
SwiftStructuresTrie                                     | 28331   | 24339   | -14.1%    | **1.16x**
Dictionary2OfObjects                                    | 3748    | 3115    | -16.9%    | **1.20x**
DictionaryOfObjects                                     | 2473    | 2050    | -17.1%    | **1.21x**
Dictionary                                              | 894     | 737     | -17.6%    | **1.21x**
Dictionary2                                             | 2268    | 1859    | -18.0%    | **1.22x**
StringIteration                                         | 8027    | 6344    | -21.0%    | **1.27x**
Phonebook                                               | 8207    | 6436    | -21.6%    | **1.28x**
BenchLangArray                                          | 119     | 91      | -23.5%    | **1.31x**
LinkedList                                              | 8267    | 6297    | -23.8%    | **1.31x**
StrToInt                                                | 5585    | 4180    | -25.2%    | **1.34x**
Dictionary3OfObjects                                    | 1122    | 831     | -25.9%    | **1.35x**
Dictionary3                                             | 731     | 515     | -29.6%    | **1.42x**
SuperChars                                              | 513353  | 258735  | -49.6%    | **1.98x**
2016-04-18 15:39:17 -07:00
Arnold Schwaighofer
255779082e Add a peephole optimization for the builtin "unsafeGuaranteed"
We can remove the retain/release pair preceeding the builtins based on the
knowledge that the lifetime of the reference is guaranteed by someone hanging on
to the reference elsewhere.
2016-03-27 06:47:16 -07:00
Xin Tong
5907b8a3e2 Rename FunctionSignatureOptCloner to FunctionSignatureOpts
Eventually, we decided to do this

1. Have the function signature opts (used to be called the cloner to create
the optimized function.
2. Mark the thunk as always_inline
3. Rely on the inliner to inline the thunk to get the benefit of calling optimized
function directly.
2016-03-24 12:50:12 -07:00
Xin Tong
3f075dfe47 Remove function signature rewriter.
We decided to use the inliner to rewrite the caller's callsites.

And eventually I will turn FunctionSignatureAnalysis into a Utility.
As its data should only be used and kept in the cloner pass.
2016-03-24 10:50:47 -07:00
Andrew Trick
482b264afc Reapply "Merge pull request #1725 from atrick/specialize"
This was mistakenly reverted in an attempt to fix buildbots.
Unfortunately it's now smashed into one commit.

---
Introduce @_specialize(<type list>) internal attribute.

This attribute can be attached to generic functions. The attribute's
arguments must be a list of concrete types to be substituted in the
function's generic signature. Any number of specializations may be
associated with a generic function.

This attribute provides a hint to the compiler. At -O, the compiler
will generate the specified specializations and emit calls to the
specialized code in the original generic function guarded by type
checks.

The current attribute is designed to be an internal tool for
performance experimentation. It does not affect the language or
API. This work may be extended in the future to add user-visible
attributes that do provide API guarantees and/or direct dispatch to
specialized code.

This attribute works on any generic function: a freestanding function
with generic type parameters, a nongeneric method declared in a
generic class, a generic method in a nongeneric class or a generic
method in a generic class. A function's generic signature is a
concatenation of the generic context and the function's own generic
type parameters.

e.g.

struct S<T> {
var x: T
@_specialize(Int, Float)
mutating func exchangeSecond<U>(u: U, _ t: T) -> (U, T) {
x = t
return (u, x)
}
}
// Substitutes: <T, U> with <Int, Float> producing:
// S<Int>::exchangeSecond<Float>(u: Float, t: Int) -> (Float, Int)

---
[SILOptimizer] Introduce an eager-specializer pass.

This pass finds generic functions with @_specialized attributes and
generates specialized code for the attribute's concrete types. It
inserts type checks and guarded dispatch at the beginning of the
generic function for each specialization. Since we don't currently
expose this attribute as API and don't specialize vtables and witness
tables yet, the only way to reach the specialized code is by calling
the generic function which performs the guarded dispatch.

In the future, we can build on this work in several ways:
- cross module dispatch directly to specialized code
- dynamic dispatch directly to specialized code
- automated specialization based on less specific hints
- partial specialization
- and so on...

I reorganized and refactored the optimizer's generic utilities to
support direct function specialization as opposed to apply
specialization.
2016-03-21 12:43:05 -07:00
Xin Tong
53888e12b5 Remove FunctionSignatureOpts.cpp.
This optimization pass has been replaced by FunctionSigatureOptCloner.cpp
and FunctionSigatureOptRewriter.cpp in cff61d7fe7
2016-03-20 15:05:02 -07:00