Commit Graph

127 Commits

Author SHA1 Message Date
Max Moiseev
02006f20bc Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-09 16:05:03 -08:00
Joe Groff
77dd9b2992 Split exact-subclass and bindable-to-subclass queries.
In many places, we're interested in whether a type with archetypes *might be* a superclass of another type with the right bindings, particularly in the optimizer. Provide a separate Type::isBindableToSuperclassOf method that performs this check. Use it in the devirtualizer to fix rdar://problem/24993618. Using it might unblock other places where the optimizer is conservative, but we can fix those separately.
2016-03-09 11:14:45 -08:00
Arnold Schwaighofer
b5f018a4b1 Mark Array.withUnsafeMutableBuffer as not escaping the array storage.
This is safe because the closure is not allowed to capture the array according
to the documentation of 'withUnsafeMutableBuffer' and the current implementation
makes sure that any such capture would observe an empty array by swapping self
with an empty array.

Users will get "almost guaranteed" stack promotion for small arrays by writing
something like:

  func testStackAllocation(p: Proto) {
    var a = [p, p, p]
    a.withUnsafeMutableBufferPointer {
      let array = $0
      work(array)
    }
  }

It is "almost guaranteed" because we need to statically be able to tell the size
required for the array (no unspecialized generics) and the total buffer size
must not exceed 1K.
2016-03-08 19:37:47 -08:00
Arnold Schwaighofer
fc96c2e53d Teach escape analysis about the semantics of copy_addr instructions
If the copy_addr cannot release the destination then it behaves just like a load
followed by a store.

This allows us to stack promote protocol typed array literals.

  protocol Proto { func at() -> Int }

  func testStackAllocation(p: Proto) {
    var a = [p, p, p]

    for e in a {
      print(e.at())
    }
  }
2016-03-08 15:07:25 -08:00
Max Moiseev
1fae0d1325 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-08 12:48:48 -08:00
Xin Tong
56a09b67da Update some comments and delete abit dead code. NFC 2016-03-07 21:26:57 -05:00
Xin Tong
9d118f28c0 Improve on how final release is found.
Improve when to bail while finding the final releases.

The ConsumedArgToEpilogueReleaseMatcher finds the final releases
in the following way.

1. If an instruction, which is not releaseinst nor releasevalue, that
could decrement reference count is found. bail out. This may well be the
real epilogue release.

2. If a release is found and the release that can not be mapped to any
@owned argument. bail as this release may well be the final release of
an @owned argument, but somehow rc-identity fails to prove that.

3. A release that is mapped to an argument, but we already has a release
that (partially or completely) overlaps with this release. This release
for sure is not the final release.
2016-03-07 21:26:57 -05:00
Xin Tong
bfc258f628 Simplify LSValue::reduce for redundant load elimination
LSValue::reduce reduces a set of LSValues (mapped to a set of LSLocations) to
a single LSValue.

It can then be used as the forwarding value for the location.

Previously, we expand into intermediate nodes and leaf nodes and then go bottom
up, trying to create a single LSValue out of the given LSValues.

Instead, we now use a recursion to go top down. This simplifies the code. And this
is fine as we do not expect to run into type tree that are too deep.

Existing test cases ensure correctness.
2016-03-07 21:26:56 -05:00
Max Moiseev
cf4bafe9e3 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-03 13:22:03 -08:00
Xin Tong
5176173cc8 Rename ConsumedReturnValueToEpilogueRetainMatcher.
Rename to ConsumedResultToEpilogueRetainMatcher

Also remove some dead code in ConsumedResultToEpilogueRetainMatcher.

NFC.
2016-03-01 20:21:00 -08:00
Xin Tong
6c0186b61c Reinstate "Improve funciton signature @owned return result to "not owned" conversion"
This enables function signature handles a case of self-recursion.

With this change we convert 11 @owned return value to "not owned", while
we convert 179 @owned parameter to @guanrateed.

rdar://24022375
2016-03-01 17:25:16 -08:00
Xin Tong
d72ad28b08 Revert "Improve funciton signature @owned return result to "not owned" conversion"
This reverts commit c6de7c0123.

Broke OSS linux and OSX builds.
2016-03-01 11:17:06 -08:00
Xin Tong
c6de7c0123 Improve funciton signature @owned return result to "not owned" conversion
More specifically, this handles a case of self-recursion.

With this change we convert 11 @owned return value to "not owned", while
we convert 179 @owned parameter to @guanrateed.

rdar://24022375
2016-03-01 10:58:32 -08:00
Max Moiseev
a49dab6bf8 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-29 12:08:52 -08:00
Erik Eckstein
2e6926cdce Re-format condition in loop. NFC. 2016-02-26 14:05:48 -08:00
Erik Eckstein
3fe19515df Handle more complex conditions in ColdBlockInfo.
Now we can handle the case where the cond_br condition is a block argument.
This change mostly effects the inlining heuristic.

rdar://problem/24777440
2016-02-25 13:31:11 -08:00
Mark Lacey
6a5b25120d Fix 80-column violations in ColdBlockInfo.{h,cpp}. 2016-02-25 13:04:16 -08:00
Max Moiseev
bb3eaaf308 Merging in latest master 2016-02-24 15:10:25 -08:00
Michael Gottesman
a5be2fff01 [sil] Use FullApplySite instead of ApplyInst in SILInstruction::getMemoryBehavior().
We were giving special handling to ApplyInst when we were attempting to use
getMemoryBehavior(). This commit changes the special handling to work on all
full apply sites instead of just AI. Additionally, we look through partial
applies and thin to thick functions.

I also added a dumper called BasicInstructionPropertyDumper that just dumps the
results of SILInstruction::get{Memory,Releasing}Behavior() for all instructions
in order to verify this behavior.
2016-02-23 15:00:43 -08:00
Max Moiseev
0b759a409c Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-23 14:26:14 -08:00
Erik Eckstein
3b6a4d8231 Handle array.get_element semantics calls with direct results in SIL optimizations.
Currently the array.get_element calls return the element as indirect result.
The generic specializer will change so that the element can be returned as direct result.
2016-02-22 13:58:10 -08:00
Erik Eckstein
666e20381f Refactor some ArraySemanticsCall code. NFC. 2016-02-22 13:58:10 -08:00
Dmitri Gribenko
3d3d4540e1 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-20 14:37:49 -08:00
practicalswift
9a96e412f9 [gardening] Fix recently introduced typo: "transistive" → "transitive" 2016-02-20 07:27:59 +01:00
Xin Tong
fcb707b40c Improve epilogue retain matcher.
Instead of only checking the return block, we could potentially check
its predecessors and its predecessors's predecessors, etc.

Alos put in a threshold to throttle this to make sure its cheap.

We are still only being able to find of a small # of epilogue retains.
The bail on MayDecrement is blocking many of the opportunites.

This should bring us closer to being able to handle Walsh.

This is part of rdar://24022375.
2016-02-19 16:22:41 -08:00
Dmitri Gribenko
f39b443e24 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-19 01:16:19 -08:00
Michael Gottesman
13cc88f694 Revert "[arc] Put back in the RCIdentity cache."
This reverts commit 6c728daa61.

This is a speculative revert to try and fix the ASAN build.
2016-02-18 18:25:01 -08:00
Dmitri Gribenko
0f36bec31f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-18 16:41:35 -08:00
John McCall
e249fd680e Destructure result types in SIL function types.
Similarly to how we've always handled parameter types, we
now recursively expand tuples in result types and separately
determine a result convention for each result.

The most important code-generation change here is that
indirect results are now returned separately from each
other and from any direct results.  It is generally far
better, when receiving an indirect result, to receive it
as an independent result; the caller is much more likely
to be able to directly receive the result in the address
they want to initialize, rather than having to receive it
in temporary memory and then copy parts of it into the
target.

The most important conceptual change here that clients and
producers of SIL must be aware of is the new distinction
between a SILFunctionType's *parameters* and its *argument
list*.  The former is just the formal parameters, derived
purely from the parameter types of the original function;
indirect results are no longer in this list.  The latter
includes the indirect result arguments; as always, all
the indirect results strictly precede the parameters.
Apply instructions and entry block arguments follow the
argument list, not the parameter list.

A relatively minor change is that there can now be multiple
direct results, each with its own result convention.
This is a minor change because I've chosen to leave
return instructions as taking a single operand and
apply instructions as producing a single result; when
the type describes multiple results, they are implicitly
bound up in a tuple.  It might make sense to split these
up and allow e.g. return instructions to take a list
of operands; however, it's not clear what to do on the
caller side, and this would be a major change that can
be separated out from this already over-large patch.

Unsurprisingly, the most invasive changes here are in
SILGen; this requires substantial reworking of both call
emission and reabstraction.  It also proved important
to switch several SILGen operations over to work with
RValue instead of ManagedValue, since otherwise they
would be forced to spuriously "implode" buffers.
2016-02-18 01:26:28 -08:00
Dmitri Gribenko
65d840c0ae stdlib: lowercase cases in Optional and ImplicitlyUnwrappedOptional 2016-02-18 00:40:33 -08:00
Michael Gottesman
35abad24d1 Merge pull request #1343 from gottesmm/arc-rc-identity-cache
[arc] Put back in the RCIdentity cache. This shaves of ~0.5 seconds f…
2016-02-17 23:26:49 -08:00
Xin Tong
b69706734d Implement @owned to @unowned retain value conversion.
If a value is returned as @owned, we can move the epilogue retain
to the caller and convert the return value to @unowned. This gives
ARC optimizer more freedom to optimize the retain out on the caller's
side.

It appears that epilgue retains are harder to find than epilogue
releases. Most of the time they are not in the return block.

(1) Sometimes, they are in predecessors
(2) Sometimes they come from a call which returns an @owned return value.
This should be improved if we fix (1) and go bottom up.
(3) We do not handle exploded retain_value.

Currently, this catches a small number of opportunities.

We probably need to improve epilogue retain matcher if we are to handle
more cases.

This is part of rdar://24022375.

We also need some refactoring in the pass. e.g. break functions into smaller
functions. I will do with subsequent commit.
2016-02-17 21:59:55 -08:00
Michael Gottesman
6c728daa61 [arc] Put back in the RCIdentity cache.
This shaves of ~0.5 seconds from ARC when compiling the stdlib on my machine.

I wired up the cache to the delete notification trigger so we are still memory
safe.
2016-02-17 21:56:32 -08:00
Erik Eckstein
fd566d92bd EscapeAnalysis: fix a problem where a inconsistent connection graph can be generated.
rdar://problem/24686791
2016-02-17 13:27:31 -08:00
Xin Tong
a007d47dd4 Add a simple epilogue retain matcher.
This is similar and yet different from epilogue release matcher. Particularly
how retain is found and when to bail. Therefore this is put into a different
class than ConsumedArgToEpilogueReleaseMatcher

This is currently a NFC other than some basic testing using the epilogue dumper.
2016-02-16 22:27:40 -08:00
practicalswift
12012b1a15 [gardening] Fix recently introduced typo: "idenity" → "identity" 2016-02-16 22:20:48 +01:00
Xin Tong
81cc962c54 Create a fast path for release silargument.
When we have a single release that can be traced back to a SILArgument.
i.e. the released value is RC-identical to the SILArgument. we do not
need projection to check whether there are overlapping/uncovered releases
for the SILargument (which will result we exit the epilogue walking sequnece).

This brings the # of Total owned args -> guaranteed args from 118 and 149.

There are 23 owned args which we can not find epilogue releases for at this
point and many (if not most) is a result of partial_apply which rc-identity
nor projection can handle.

with this commit i see differences only in 2 benchmarks. baseline is without this
change. I am looking at the SuperChars regression.

StrToInt                              9297             8402      -895     -9.6%     1.11x
SuperChars                          676554           748375    +71821    +10.6%     0.90x (!)
2016-02-16 09:59:48 -08:00
Xin Tong
99ca08e4af Check whether epilogue releases cover all non-trivial fields.
When we have all the epilogue releases. Make sure they cover all the non-trivial
parts of the base. Otherwise, treat as if we've found no releases for the base.

Currently. this is a NFC other than epilogue dumper. I will wire it up with
function signature with next commit.

This is part of rdar://22380547
2016-02-15 16:00:02 -08:00
practicalswift
109eb8063f [gardening] Fix recently introduced typo: "thats" → "that is" 2016-02-15 20:46:44 +01:00
Xin Tong
4f66bc88b4 Move ProjectionTree::isRedundantRelease to ConsumedArgToEpilogueReleaseMatcher::isRedundantRelease.
NFC.
2016-02-15 10:22:47 -08:00
Xin Tong
40ff0895d6 Improve epilogue release matcher to handle exploded release_value.
So instead of only being able to match %1 and release %1 in (1). we
can also match %1 with (release %2, and release%3, i.e. exploded release_value)
in (2).

(1)
foo(%1)
  strong_release %1

(2)
foo(%1)
  %2 = struct_extract %1, field_a
  %3 = struct_extract %1, field_b
  strong_release %2
  strong_release %3

This will allow function signature to better move the release instructions to
the callers.

Currently, this is a NFC other than testing using the epilogue match dumper.
2016-02-12 15:22:13 -08:00
Xin Tong
84a6ff1d98 And lastly rename NewProjection to Projection. This is a NFC. rdar://24520269 2016-02-09 22:20:10 -08:00
saisi
08abcd92c3 fixed niggling typos 2016-01-29 22:22:12 -05:00
Xin Tong
546471ac4d Port dead store elimination and redundant load elimination to use the new projection.
This patch also implements some of the missing functions used by RLE and DSE in new projection
that exist in the old projection.

New projection provides better memory usage, eventually we will phase out the old projection code.

New projection is now copyable, i.e. we have a proper constructor for it.  This helps make the code
more readable.

We do see a bit increase in compilation time in compiling stdlib -O, this is a result of the way
we now get types of a projection path, but I expect this to go down (away) with further improvement
on how memory locations are constructed and cached with later patches.

=== With the OLD Projection. ===

Total amount of memory allocated.
--------------------------------
Bytes Used	Count		   Symbol Name
13032.01 MB      50.6%	2158819    swift::SILPassManager::runPassesOnFunction(llvm::ArrayRef<swift::SILFunctionTransform*>, swift::SILFunction*)
2879.70 MB      11.1%	3076018    (anonymous namespace)::ARCSequenceOpts::run()
2663.68 MB      10.3%	1375465	   (anonymous namespace)::RedundantLoadElimination::run()
1534.35 MB       5.9%	5067928	   (anonymous namespace)::SimplifyCFGPass::run()
1278.09 MB       4.9%	576714	   (anonymous namespace)::SILCombine::run()
1052.68 MB       4.0%	935809	   (anonymous namespace)::DeadStoreElimination::run()
 771.75 MB       2.9%	1677391	   (anonymous namespace)::SILCSE::run()
 715.07 MB       2.7%	4198193	   (anonymous namespace)::GenericSpecializer::run()
 434.87 MB       1.6%	652701	   (anonymous namespace)::SILSROA::run()
 402.99 MB       1.5%	658563	   (anonymous namespace)::SILCodeMotion::run()
 341.13 MB       1.3%	962459	   (anonymous namespace)::DCE::run()
 279.48 MB       1.0%	415031	   (anonymous namespace)::StackPromotion::run()

Compilation time breakdown.
--------------------------
Running Time	Self (ms)	    Symbol Name
25716.0ms   35.8%	0.0	    swift::runSILOptimizationPasses(swift::SILModule&)
25513.0ms   35.5%	0.0	    swift::SILPassManager::runOneIteration()
20666.0ms   28.8%	24.0	    swift::SILPassManager::runFunctionPasses(llvm::ArrayRef<swift::SILFunctionTransform*>)
19664.0ms   27.4%	77.0	    swift::SILPassManager::runPassesOnFunction(llvm::ArrayRef<swift::SILFunctionTransform*>, swift::SILFunction*)
3272.0ms    4.5%	12.0	    (anonymous namespace)::SimplifyCFGPass::run()
3266.0ms    4.5%	7.0	    (anonymous namespace)::ARCSequenceOpts::run()
2608.0ms    3.6%	5.0	    (anonymous namespace)::SILCombine::run()
2089.0ms    2.9%	104.0	    (anonymous namespace)::SILCSE::run()
1929.0ms    2.7%	47.0	    (anonymous namespace)::RedundantLoadElimination::run()
1280.0ms    1.7%	14.0	    (anonymous namespace)::GenericSpecializer::run()
1010.0ms    1.4%	45.0	    (anonymous namespace)::DeadStoreElimination::run()
966.0ms    1.3%	191.0	 	    (anonymous namespace)::DCE::run()
496.0ms    0.6%	6.0	 	    (anonymous namespace)::SILCodeMotion::run()

=== With the NEW Projection. ===

Total amount of memory allocated.
--------------------------------
Bytes Used	Count		    Symbol Name
11876.64 MB      48.4%	22112349    swift::SILPassManager::runPassesOnFunction(llvm::ArrayRef<swift::SILFunctionTransform*>, swift::SILFunction*)
2887.22 MB      11.8%	3079485	    (anonymous namespace)::ARCSequenceOpts::run()
1820.89 MB       7.4%	1877674	    (anonymous namespace)::RedundantLoadElimination::run()
1533.16 MB       6.2%	5073310	    (anonymous namespace)::SimplifyCFGPass::run()
1282.86 MB       5.2%	577024	    (anonymous namespace)::SILCombine::run()
 772.21 MB       3.1%	1679154	    (anonymous namespace)::SILCSE::run()
 721.69 MB       2.9%	936958	    (anonymous namespace)::DeadStoreElimination::run()
 715.08 MB       2.9%	4196263	    (anonymous namespace)::GenericSpecializer::run()

Compilation time breakdown.
--------------------------
Running Time	Self (ms)	    Symbol Name
25137.0ms   37.3%	0.0	    swift::runSILOptimizationPasses(swift::SILModule&)
24939.0ms   37.0%	0.0	    swift::SILPassManager::runOneIteration()
20226.0ms   30.0%	29.0	    swift::SILPassManager::runFunctionPasses(llvm::ArrayRef<swift::SILFunctionTransform*>)
19241.0ms   28.5%	83.0	    swift::SILPassManager::runPassesOnFunction(llvm::ArrayRef<swift::SILFunctionTransform*>, swift::SILFunction*)
3214.0ms    4.7%	10.0	    (anonymous namespace)::SimplifyCFGPass::run()
3005.0ms    4.4%	14.0	    (anonymous namespace)::ARCSequenceOpts::run()
2438.0ms    3.6%	7.0	    (anonymous namespace)::SILCombine::run()
2217.0ms    3.2%	54.0	    (anonymous namespace)::RedundantLoadElimination::run()
2212.0ms    3.2%	131.0	    (anonymous namespace)::SILCSE::run()
1195.0ms    1.7%	11.0	    (anonymous namespace)::GenericSpecializer::run()
1168.0ms    1.7%	39.0	    (anonymous namespace)::DeadStoreElimination::run()
853.0ms    1.2%	150.0	 	    (anonymous namespace)::DCE::run()
499.0ms    0.7%	7.0	 	    (anonymous namespace)::SILCodeMotion::run()
2016-01-25 20:08:29 -08:00
Erik Eckstein
74d44b74e7 SIL: remove SILValue::getDef and add a cast operator to ValueBase * as a repelacement. NFC. 2016-01-25 15:00:49 -08:00
Erik Eckstein
d3c975391f SIL: remove dump and print from SILValue
If you want to dump a SILValue from the debugger, use Value->dump()
2016-01-25 15:00:49 -08:00
Erik Eckstein
506ab9809f SIL: remove getTyp() from SILValue 2016-01-25 15:00:49 -08:00
Erik Eckstein
5a53b31f57 SIL: remove use-iteration functions from SILValue.
They are not needed anymore. NFC.
2016-01-25 15:00:49 -08:00
Erik Eckstein
b745691a38 SIL refactoring: Move some functions out of SILValue and Operand
SILValue.h/.cpp just defines the SIL base classes. Referring to specific instructions is a (small) kind of layering violation.
Also I want to keep SILValue small so that it is really just a type alias of ValueBase*.
NFC.
2016-01-25 10:37:03 -08:00
Arnold Schwaighofer
924cc48e84 Add side effects for fix_lifetime
A fix_lifetime acts as a read on memory. A retain can move past it but the
ultimate release cannot move before it
2016-01-25 08:49:17 -08:00