Commit Graph

138 Commits

Author SHA1 Message Date
Arnold Schwaighofer
3676671b7f Merge pull request #1587 from aschwaighofer/stack_promote_with_unsafe_mutable_buffer_pointer
Mark Array.withUnsafeMutableBuffer as not escaping the array storage.
2016-03-08 19:39:28 -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
Michael Gottesman
bb15808554 Convert some trivial std::count_if invocations on ranges to use the provided range adaptor. 2016-03-08 14:58:13 -08:00
Michael Gottesman
5f72810ad3 Add a range adaptor for std::count and update various trivial usages in the compiler to use this API instead. 2016-03-08 14:58:13 -08:00
practicalswift
37bf58399a [gardening] Fix formatting of two recently introduced file headers 2016-03-08 11:23:45 +01:00
Xin Tong
2fe08d9cda Move LSBase.cpp to lib/SILOptimzer/Utils/ 2016-03-07 22:04:15 -05:00
Xin Tong
56a09b67da Update some comments and delete abit dead code. NFC 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
naoyashiga
803bb1ba54 fix typo 2016-03-04 22:14:38 +09:00
practicalswift
9b1d73fcac [gardening] Fix recently introduced typo: "exit exit" → "exit" 2016-03-02 09:34:21 +01: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
Erik Eckstein
115c50a5c7 DeadObjectElimination: Fix a problem with dead array elimination.
With the new ValueLifetimeAnalysis DOE did not handle case where a dead array is not released on a path at all (because it leads to an unreachable).
2016-03-01 12:30:52 -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
Erik Eckstein
68f0d5c202 Reinstate "GenericSpecializer: When specializing a generic function, convert indirect parameters/result to direct parameters/result.""
This reinstates commit 4187959e66.

It was reverted because of a bug in ValueLifetimeAnalysis which is now fixed.
2016-02-29 07:42:59 -08:00
Erik Eckstein
1730b90ae4 Reinstate "Rewrite the ValueLifetimeAnalysis."
Reinstates commit 0c2ca94ef7

With two bug fixes:
*) use after free asan crash
*) wrong check in ValueLifetimeAnalysis::isWithinLifetime
And some refactoring
2016-02-29 07:42:59 -08:00
Xin Tong
d0dc008fc1 Revert GenericSpecializer code.
This reverts commit

ed8126d050
ac0e7fd183
a11042eb05
b2d6e8ce6e
3a83cee006
0c2ca94ef7

First 4 commits are @practicalswift typo fixes which are implicated. Last 2 are
the culprits.

This causes an asan build crash.
2016-02-28 11:13:44 -08:00
practicalswift
b2d6e8ce6e [gardening] Fix recently introduced typo: "corrseponding" → "corresponding" 2016-02-27 12:16:52 +01:00
Erik Eckstein
3a83cee006 Reinstate "GenericSpecializer: When specializing a generic function, convert indirect parameters/result to direct parameters/result.""
This reinstates commit 4187959e66.

The exposed crash in the ClosureSpecializer is fixed.
2016-02-26 14:05:48 -08:00
Erik Eckstein
0c2ca94ef7 Rewrite the ValueLifetimeAnalysis.
It fixes a problem with lifetime regions having "exit-edges". This crashed the ClosureSpecializer.
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
Erik Eckstein
f70b53b015 Revert "Reinstate "GenericSpecializer: When specializing a generic function, convert indirect parameters/result to direct parameters/result."""
This reverts commit c556d5cd39.

Hitting a new assert.
2016-02-25 09:50:11 -08:00
Erik Eckstein
c556d5cd39 Reinstate "GenericSpecializer: When specializing a generic function, convert indirect parameters/result to direct parameters/result.""
This reinstates commit 4187959e66.

After Xin's recent fix in ARC (6a9a430f68) the crash on i386 should be resolved.
2016-02-25 08:48:15 -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
Erik Eckstein
5b4c73ed3b Revert "GenericSpecializer: When specializing a generic function, convert indirect parameters/result to direct parameters/result."
This reverts commit 4187959e66.

There is a crash in StdlibUnittests on i386 (Release-Assert build)
2016-02-23 08:29:41 -08:00
practicalswift
86e3986303 [gardening] Fix recently introduced typo: "calle" → "callee" 2016-02-22 23:29:19 +01:00
practicalswift
107057f529 [gardening] Fix recently introduced typo: "paramters" → "parameters" 2016-02-22 23:27:57 +01:00
practicalswift
dad1325c24 [gardening] Fix recently introduced typo: "the the" → "the" 2016-02-22 23:27:25 +01:00
Erik Eckstein
4187959e66 GenericSpecializer: When specializing a generic function, convert indirect parameters/result to direct parameters/result.
With this re-abstraction a specialized function has the same calling convention as if it would have been written with the specialized types in the first place.
In general this results in less alloc_stacks and load/stores.
It also can eliminate some re-abstraction thunks, e.g. if a generic closure is used in a non-generic context.
It some (hopefully rare) cases it may require to add re-abstraction thunks.

In case a function has multiple indirect results, only the first is converted to a direct result. This is an open TODO.
2016-02-22 13:58:10 -08:00
Erik Eckstein
682c1cb5a1 refactoring: move static function isPartialApplyOfReabstractionThunk from SILCombiner to Local.
Because it will be needed by the generic specializer, too.
NFC
2016-02-22 13:58:10 -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
Xin Tong
a48584ccbc Create a fast path for not-final release instruction.
For a release on a guaranteed function paramater, we know right away
that its not the final release and therefore does not call deinit.

Therefore we know it does not read or write memory other than the reference
count.

This reduces the compilation time of dead store and redundant load elim. As
we need to go over alias analysis to make sure tracked locations do not alias
with it.
2016-02-20 22:00:36 -08: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
Mark Lacey
945065f37d Change where in the pass manager we validate that analyses are unlocked.
Verify just prior to running passes, and after running each pass, that
no analyses are locked from being invalidated.
2016-02-19 13:32:40 -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
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
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
4cdb142b57 [gardening] Fix recently introduced typo: "sematics" → "semantics" 2016-02-16 09:53:36 +01: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
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
d1c75976c9 Add a epilogue release dumper. This pass tests/prints what epilogue releases for
function arguments are found and dump them.

This is important as we are trying to improve EpilogueReleaseMatcher.
2016-02-12 10:40:55 -08:00
practicalswift
b6adb264bd [gardening] Fix recently introduced typo: "for for" → "for" 2016-02-11 15:19:20 +01:00