Commit Graph

2592 Commits

Author SHA1 Message Date
swift_jenkins
37ac3a7aba Merge remote-tracking branch 'origin/master' into master-next 2020-04-01 10:20:19 -07:00
marcrasi
77fd034a0b Merge pull request #30711 from rxwei/differential-operators
[AutoDiff upstream] Add differential operators and some utilities.
2020-04-01 10:11:35 -07:00
swift_jenkins
ec1911f0a8 Merge remote-tracking branch 'origin/master' into master-next 2020-04-01 00:00:20 -07:00
Erik Eckstein
6d533e8913 DestroyHoisting: correctly detect if debug_value_addr instructions need to be deleted.
https://bugs.swift.org/browse/SR-12355
rdar://problem/60832805
2020-03-31 17:34:56 +02:00
Richard Wei
57d228b39e [AutoDiff upstream] Add differential operators and some utilities.
* Add all [differential operators](https://github.com/apple/swift/blob/master/docs/DifferentiableProgramming.md#list-of-differential-operators).
* Add `withoutDerivative(at:)`, used for efficiently stopping the derivative propagation at a value and causing the derivative at the value to be zero.
* Add utility `differentiableFunction(from:)`, used for creating a `@differentiable` function from an original function and a derivative function.

Mostly work done by @marcrasi and @dan-zheng.
Partially resolves TF-843.

TODO:
* Add `AnyDerivative`.
* Add `Array.differentiableMap(_:)` and `differentiableReduce(_:_:)`.
2020-03-30 14:15:35 -07:00
swift_jenkins
f26e77133c Merge remote-tracking branch 'origin/master' into master-next 2020-03-27 03:57:36 -07:00
Erik Eckstein
93a0dfc578 SILOptimizer: a new small optimization pass to remove redundant basic block arguments.
RedundantPhiElimination eliminates block phi-arguments which have the same value as other arguments of the same block.
This also works with cycles, like two equivalent loop induction variables. Such patterns are generated e.g. when using stdlib's enumerated() on Array.

   preheader:
     br bb1(%initval, %initval)
   header(%phi1, %phi2):
     %next1 = builtin "add" (%phi1, %one)
     %next2 = builtin "add" (%phi2, %one)
     cond_br %loopcond, header(%next1, %next2), exit
   exit:

is replaced with

   preheader:
     br bb1(%initval)
   header(%phi1):
     %next1 = builtin "add" (%phi1, %one)
     %next2 = builtin "add" (%phi1, %one) // dead: will be cleaned-up later
     cond_br %loopcond, header(%next1), exit
   exit:

Any remaining dead or "trivially" equivalent instructions will then be cleaned-up by DCE and CSE, respectively.

rdar://problem/33438123
2020-03-26 19:30:01 +01:00
swift_jenkins
89af3ff684 Merge remote-tracking branch 'origin/master' into master-next 2020-03-24 09:37:37 -07:00
eeckstein
2ca8b15b51 Merge pull request #30610 from eeckstein/fix-destroy-hoisting
DestroyHoisting: fix a bug which creates invalid SIL
2020-03-24 17:20:53 +01:00
swift_jenkins
63f5052019 Merge remote-tracking branch 'origin/master' into master-next 2020-03-24 07:20:09 -07:00
Erik Eckstein
15f519f544 DestroyHoisting: fix a bug which creates invalid SIL
A dominance check was missing.
2020-03-24 15:07:37 +01:00
Erik Eckstein
71eebaa720 CSE: remove redundant global initializer calls 2020-03-23 15:53:23 +01:00
swift_jenkins
0db7f80752 Merge remote-tracking branch 'origin/master' into master-next 2020-03-21 08:21:22 -07:00
pi1024e
647db46581 Remove redundant boolean checks 2020-03-20 19:44:57 -04:00
swift_jenkins
433225bafb Merge remote-tracking branch 'origin/master' into master-next 2020-03-19 12:00:14 -07:00
Slava Pestov
9ec80df97e SIL: Remove curried SILDeclRefs 2020-03-19 02:20:21 -04:00
swift_jenkins
60e3c66874 Merge remote-tracking branch 'origin/master' into master-next 2020-03-18 21:58:04 -07:00
Michael Gottesman
dbe0cfa561 Merge pull request #30494 from gottesmm/pr-71e6912f769c828f0358528f1e46433cb7032362
[ownership] Rename BorrowScopeIntroducingValue -> BorrowedValue and BorrowScopeOperand -> BorrowingOperand.
2020-03-18 21:41:33 -07:00
Michael Gottesman
47e74473f1 [ownership] Rename BorrowScopeIntroducingValue -> BorrowedValue and BorrowScopeOperand -> BorrowingOperand.
Andy and I for some time have been discussing the right name for these two
"ownership concepts". What we realized is that the "ing" on
BorrowScopeIntroducingValue is very unfortunate since this value is the result
of a new borrow scope being introduced. So the name should be really:
BorrowScopeIntroducedValue. Given that is sort of unsatisfying, we settled on
the name BorrowedValue.

Once we found the name BorrowedValue, we naturally realized that
BorrowScopeOperand -> BorrowingOperand followed. This is because the operand is
the operand of the instruction that is creating the new borrow scope. So in a
sense the Operand is the "Use" that causes the original value to become
borrowed. So a BorrowingOperand is where the action is and is "active".
2020-03-18 19:43:30 -07:00
swift_jenkins
7330f9b6a8 Merge remote-tracking branch 'origin/master' into master-next 2020-03-18 13:38:11 -07:00
Andrew Trick
976a1d7c9b Merge pull request #30474 from atrick/add-inlining-diagnostics
Emit inlining diagnostics as debug messages.
2020-03-18 13:23:07 -07:00
Andrew Trick
ab14c432c3 Emit inlining diagnostics as debug messages.
So -debug-only=sil-inliner is useful.
2020-03-18 11:24:11 -07:00
Andrew Trick
70678ab856 SILOptimizer: Fix analysis invalidation after devirtualization.
Devirtualizing try_apply modified the CFG, but passes that run
devirtualization were not invalidating any CFG analyses, such as the
domtree.

This could hypothetically have caused miscompilation, but will be
caught by running with -sil-verify-all.
2020-03-18 10:21:35 -07:00
swift_jenkins
4e7ac348e6 Merge remote-tracking branch 'origin/master' into master-next 2020-03-16 00:44:08 -07:00
eeckstein
b5c0b6de9d Merge pull request #30381 from eeckstein/lazy_properties
Improve optimization of lazy properties.
2020-03-16 08:32:19 +01:00
swift_jenkins
b87e55c4f6 Merge remote-tracking branch 'origin/master' into master-next 2020-03-13 19:43:36 -07:00
Meghana Gupta
8e800e49bf Recommit #29812 with fixes (#30342) 2020-03-13 19:34:16 -07:00
Erik Eckstein
b51284fae3 CSE: optimize calls of lazy property getters
If such a call is dominated by another call to the same getter, it is replaced by a direct load of the property - assuming that it is already computed.

rdar://problem/34715412
2020-03-13 11:01:59 +01:00
swift_jenkins
7b39b9f24f Merge remote-tracking branch 'origin/master' into master-next 2020-03-12 00:47:37 -07:00
Michael Gottesman
faf4519123 [semantic-arc-opts] Instead of tracking owned value introducers, track incoming values that are seen from owned value introducers.
TLDR: This will allow me to in a forthcoming commit to eliminate the extra run
of the peephole optimizer to seed the phi arg analysis since this eliminates
potential data invalidation issues. That being said, I am formalizing the
language/model in the pass a little in this commit, so I wanted to give a long
description below.

----

In this commit, I am formalizing some of the language around this optimization
away phi nodes specifically and more towards a notion that Andy and I have
discussed called "Joined Live Ranges". The idea is a "Live Range" in OSSA is an
owned value together with its set of forwarding uses. The owned value is in a
certain sense the equivalence class representative of all of the values that are
derived from the owned value via forwarding instructions.

This works well as long as all of the "forwarding" instructions only derive
their ownership from a single operand. Some instructions without that property
are branch, tuple, struct. Since such a "forwarding" instruction derives its
ownership by merging the ownership of multiple operands, if multiple of its
operands are non-trivial owned operands, we are in a sense joining together the
live ranges of those non-trivial owned operands. The main implication of this is
that if we want to convert the joined live range from owned to guaranteed, we
can only do it if we can also do it for all of its incoming values at the same
time.

This creates a conundrum though since this pass has been written like a peephole
pass which is ill-suited to be converted into this form. So instead, this pass
seeds an analysis from peephole runs that /could/ have been converted from owned
to guaranteed except for a specific joined live range. Then after we reach a
fixed point, we use that information from the failed peepholes to try to
eliminate the joined live ranges.

This approach is a good approach, but if implemented in certain ways exposes the
risk of problems around touching invalidated data. Specifically, before this
patch, this was implemented with the concern that each branch
instruction (currently the only supported type of joined live range instruction)
could be paired with multiple owned value introducers. This is because
initially, we were allowing for tuple, struct with multiple non-trivial operands
to be used as owned value introducers. This implied that we could not rely on
just storing a bool per joined live range operand since we would need to know
that all introducers associated with the operand were optimizable.

After some thought/discussion, I realized that this sort of optimization can
also apply to tuple/struct/similar insts. This let to the shift in
thinking/realization that we could talk instead about general joined live ranges
and handle tuple/struct/similar instructions just like they were branch/phi! As
such, if we assume that all such instructions are treated as separate
LiveRanges, then we know that our branch can only have a single owned value
introducer since we do not look through /any/ joined live ranges when computing
LiveRanges.

This realization then allows us to store a single bool value marking the operand
on the joined live range as being one that we /may/ be able to optimize. This
then allows us to simplify the state we handle and not have to worry about
storing memory to instructions that may be eliminated. Since we have a joined
live range use, we know that any value that is used by the joined live range can
not be eliminated by the peephole optimizer.
2020-03-11 21:39:46 -07:00
swift_jenkins
f62acb4897 Merge remote-tracking branch 'origin/master' into master-next 2020-03-11 16:20:21 -07:00
Michael Gottesman
09cfcbf50e [semantic-arc-opts] Rename LiveRange::has{,Unknown}ConsumingUse.
We are checking the unknown consuming use vector, so the name makes this clearer
and makes it clear we are not talking about destroying consumers.

I also eliminated the phiToIncomingValueMultiMap being passed in favor of a bool
argument since that is how we are using it today. That was a vestige of an
earlier version of the patch that had to be sunk.
2020-03-11 12:06:12 -07:00
Michael Gottesman
4842e23ed5 [semantic-arc-opts] Rename LiveRange::{general,ownership}ForwardingUse.
This is a clearer name. It is also corresponds with the function
isOwnershipForwardingInst that classifies such operands.
2020-03-11 11:48:46 -07:00
swift_jenkins
ece4e488d6 Merge remote-tracking branch 'origin/master' into master-next 2020-03-11 01:39:30 -07:00
Michael Gottesman
fbdf729da6 Merge pull request #30298 from gottesmm/pr-4e1212f1e4e3640e5037f11164d07e20fcea7324
[inliner] Add a new Inliner that only inlines AlwaysInline functions (but do not put it in the pass pipeline).
2020-03-11 01:38:54 -07:00
swift_jenkins
6765d8bfdf Merge remote-tracking branch 'origin/master' into master-next 2020-03-11 00:59:59 -07:00
Michael Gottesman
1c1fae381d Merge pull request #30341 from gottesmm/pr-91997a0048cde708ab0a33c5e524a8c8f9422956
Recommit #30289 with ASAN fix.
2020-03-11 00:43:38 -07:00
swift_jenkins
d0d88b4a3b Merge remote-tracking branch 'origin/master' into master-next 2020-03-10 19:54:47 -07:00
Michael Gottesman
e3f2bb74d2 [inliner] Add a new Inliner that only inlines AlwaysInline functions (but do not put it in the pass pipeline).
We need this anyways for -Onone and I want to do some experiments with running
this very early so I can expose more of the stdlib (modulo inlining) to the new
ownership optimizing passes.

I also changed how the inliner handles inlining around OSSA by changing it to
check early that if the caller is in ossa, then we only inline if all of the
callees that the caller calls are in ossa. The intention is to hopefully avoid
weird swings in code-size/perf due to the inliner heuristic's calculation being
artificially manipulated due to some callees not being available to inline (due
to this difference) when others are already available.
2020-03-10 19:53:18 -07:00
Michael Gottesman
7323ea95ea [semantic-arc-opts] Eliminate unneeded use of bisection that caused ASAN to fire.
I was being too clever here and in the process of "foot-gunned"
myself. Specifically, I was hoping to use bisection to do some is contained in
list tests. But the list that I actually bisected upon was not the original list
and was just based on the original sorted list! So the pointer comparison for
the bisect no longer worked. Since the bisection was on pointer addresses, if we
were lucky and the new addresses in the new array were sorted the same as the
original array, we wouldn't hit anything.

rdar://60262326
2020-03-10 16:52:58 -07:00
Michael Gottesman
6b88599412 Revert "Merge pull request #30327 from rintaro/revert-30289"
This reverts commit 0a6ccd802f, reversing
changes made to 7c5d74e86b.
2020-03-10 16:52:58 -07:00
Rintaro Ishizaki
ccbc26d947 Revert "Use in_guaranteed for let captures (#29812)"
This reverts commit 13b9915c6f.
2020-03-10 16:08:08 -07:00
swift_jenkins
339565493a Merge remote-tracking branch 'origin/master' into master-next 2020-03-10 14:51:56 -07:00
Rintaro Ishizaki
a7cb72fe55 Merge remote-tracking branch 'origin/master' into master-next
Conflicts:
	stdlib/public/Reflection/TypeRefBuilder.cpp
2020-03-10 12:46:49 -07:00
Meghana Gupta
13b9915c6f Use in_guaranteed for let captures (#29812)
* Use in_guaranteed for let captures

With this all let values will be captured with in_guaranteed convention
by the closure. Following are the main changes :

SILGen changes:
- A new CaptureKind::Immutable is introduced, to capture let values as in_guaranteed.
- SILGen of in_guaranteed capture had to be fixed.
  in_guaranteed captures as per convention are consumed by the closure. And so SILGen should not generate a destroy_addr for an in_guaranteed capture.
  But LetValueInitialization can push Dealloc and Release states of the captured arg in the Cleanup stack, and there is no way to access the CleanupHandle and disable the emission of destroy_addr while emitting the captures in SILGenFunction::emitCaptures.
  So we now create, temporary allocation of the in_guaranteed capture iduring SILGenFunction::emitCaptures without emitting destroy_addr for it.

SILOptimizer changes:
- Handle in_guaranteed in CopyForwarding.
- Adjust dealloc_stack of in_guaranteed capture to occur after destroy_addr for on_stack closures in ClosureLifetimeFixup.

IRGen changes :
  - Since HeapLayout can be non-fixed now, make sure emitSize is used conditionally
  - Don't consider ClassPointerSource kind parameter type for fulfillments while generating code for partial apply forwarder.
    The TypeMetadata of ClassPointSource kind sources are not populated in HeapLayout's NecessaryBindings. If we have a generic parameter on the HeapLayout which can be fulfilled by a ClassPointerSource, its TypeMetaData will not be found while constructing the dtor function of the HeapLayout.
    So it is important to skip considering sources of ClassPointerSource kind, so that TypeMetadata of a dependent generic parameters gets populated in HeapLayout's NecessaryBindings.
2020-03-10 12:23:02 -07:00
Rintaro Ishizaki
fad1b431c2 Revert "[semantic-arc-opts] Implement @owned phi web elimination for phi webs with a single phi node that only have copy_value introducers."
This reverts commit 6fee59bd6a.
2020-03-10 08:43:46 -07:00
Rintaro Ishizaki
7f426d15a8 Revert "[semantic-arc-opts] Refactor out reinitializing the worklist into method drainVisitedSinceLastMutationIntoWorklist and add some types/comments."
This reverts commit d0d31f7d0d.
2020-03-10 08:43:20 -07:00
swift-ci
1a7e4397bd Merge pull request #30320 from gottesmm/pr-29b49f7445fdc16950dbb3bcdbb7d3a503a6b3a5 2020-03-10 00:13:12 -07:00
Michael Gottesman
ab9416b068 Merge pull request #30314 from gottesmm/pr-fd797f09f7fb277ea2c75a81fc46eb41b059bc38
[ownership] Now that cond_br only takes trivial operands in ossa, eliminate all special code for handling it.
2020-03-09 23:57:43 -07:00
Michael Gottesman
d0d31f7d0d [semantic-arc-opts] Refactor out reinitializing the worklist into method drainVisitedSinceLastMutationIntoWorklist and add some types/comments.
I was doing this in two places. It makes sense to refactor it out as such.
2020-03-09 22:30:28 -07:00