Commit Graph

4277 Commits

Author SHA1 Message Date
Michael Gottesman
3e47e23aea [semantic-arc] Small cleanups + some comments. NFC. 2019-12-18 16:25:10 -08:00
Michael Gottesman
68960005f6 [gardening] Remove unused variable. 2019-12-18 13:54:11 -08:00
Michael Gottesman
df6d25fa8a [gardening] Be more defensive around a potentially uninitialized Store by initializing it to nullptr and asserting that it is not nullptr (i.e. was assigned) before using it. 2019-12-18 13:54:11 -08:00
Ravi Kandhadai
935686460c [SIL Optimization] Create a new utility InstructionDeleter to delete instructions
and eliminate dead code. This is meant to be a replacement for the utility:
recursivelyDeleteTriviallyDeadInstructions. The new utility performs more aggresive
dead-code elimination for ownership SIL.

This patch also migrates most non-force-delete uses of
recursivelyDeleteTriviallyDeadInstructions to the new utility.
and migrates one force-delete use of recursivelyDeleteTriviallyDeadInstructions
(in IRGenPrepare) to use the new utility.
2019-12-18 13:17:17 -08:00
eeckstein
bb067f4d68 Revert "EscapeAnalysis: add node flags and change the meaning of "escaping"" 2019-12-18 16:17:12 +01:00
Andrew Trick
3fdf6c8630 Merge pull request #28800 from atrick/escapeflags
EscapeAnalysis: add node flags and change the meaning of "escaping"
2019-12-17 19:49:11 -08:00
zoecarver
37fae30efc Merge branch 'master' into optimize/dead-global-vars 2019-12-17 15:30:16 -08:00
Andrew Trick
0ea3bb1ff5 EscapeAnalysis: rewrite canEscapeToUsePoint.
Correctness: do not make any unenforced assumptions about how the
connection graph is built (I don't think the previous assumption about
the structure of the graph node mapped to a reference-type value would
always hold if content nodes can be arbitrarily merged). Only make one
assumption about the client code: the access being checked must be to
some address within the provided value, not another object indirectly
reachable from that value.

Optimization: Allow escape analysis to prove that an addressable
object does not escape even when one of its reference-type fields
escapes.
2019-12-17 11:05:28 -08:00
Andrew Trick
cec925c549 EscapeAnalysis: Do not create defer edges for block arguemnts.
That appears to have been a partial workaround for the real
problem that usepoints need to be propagated across the entire
defer web. This is now solved by considering use points on the
reference node's content, not the reference node itself.
2019-12-16 16:43:06 -08:00
Andrew Trick
5a27e5d802 EscapeAnalysis: Make EscapeState and UsePoints a property of the content node only.
For alias analysis query to be generally correct, we need to
effectively merge the escape state and use points for everything in a
defer web.

It was unclear from the current design whether the "escaping" property
applied to the pointer value or its content. The implementation is
inconsistent in how it was treated. It appears that some bugs have
been worked around by propagating forward through defer edges, some
have been worked around by querying the content instead of the
pointer, and others have been worked around be creating fake use
points at block arguments.

If we always simply query the content for escape state and use points,
then we never need to propagate along defer edges. The current code
that propagates escape state along defer edges in one direction is
simply incorrect from the perspective of alias analysis.

One very attractive solution is to merge nodes eagerly without
creating any defer edges, but that would be a much more radical change
even than what I've done here. It would also pose some new issues: how
to resolve the current "node types" when merging and how to deal with
missing content nodes.

This solution of applying escape state to content nodes solves all
these problems without too radical of a change at the expense of
eagerly creating content nodes. (The potential graph memory usage is
not really an issue because it's possible to drastically shrink the
size of the graph anyway in a future commit--I've been able to fit a
node within one cache line). This solution nicely preserves graph
structure which makes it easy to debug and relate to the IR.

Eagerly creating content nodes also solves the missing content node
problem. For example, when querying canEscapeTo, we need to know
whether to look at the escape state for just the pointer value itself,
or also for its content. It may be possible the its content node is
actually part of the same object at the IR level. If the content node
is missing, then we don't know if the object's interior address is not
recognizable/representable or whether we simply never saw an access to
the interior address. We can't simply look at whether the current IR
value happens to be a reference, because that doesn't tell us whether
the graph node may have been merged with a non-reference node or even
with it's own content node. To be correct in general, this query would
need to be extremely conservative. However, if content nodes are
always created for references, then we only need to query the escape
state of a pointer's content node. The content node's flag tells us if
it's an interior node, in which case it will always point to another
content node which also needs to be queried.
2019-12-16 16:43:06 -08:00
Andrew Trick
f7472e2c45 EscapeAnalysis: Add PointerKind and interior/reference flags
Categorize three kinds of pointers:

NoPointer (don't create a node)

ReferenceOnly (safe to make normal assumptions)

AnyPointer (may have addresses, rawpointers, or any mix of thoses with references)

Flag ConnectionGraph nodes as
- hasReferenceOnly
- isInterior

An interior node always has an additional content node.

All sorts of arbitrary node merging is supported. Nodes with totally
different properties can be safely merged. Interior nodes can safely
be merged with their field content (which does happen surprisingly
often).

Alias analysis will use these flags to safely make assumptions about
properties of the connection graph.
2019-12-16 16:43:06 -08:00
Arnold Schwaighofer
a78bb3d49f Instead of just fixing the ClosureLifetimeFixup change releasePartialApplyCapturedArg to handle undef captures 2019-12-16 12:14:29 -08:00
Arnold Schwaighofer
7d09aee9a1 ClosureLifetimeFixup: Handle undef partial_apply arguments gracefully
We have to handle undef partial_apply arguments to handle the following
source gracefully during the diagnosis pipeline.

```
class TestUndefined {
  private var stringList: [String]!

  func dontCrash(strings: [String]) {
    assert(stringList.allSatisfy({ $0 == stringList.first!}))
    let stringList = strings.filter({ $0 == "a" })
  }
}
```

rdar://57893008
2019-12-16 09:28:56 -08:00
zoecarver
ea0698fcda Add comments and fix checking dead instructions 2019-12-14 10:31:56 -08:00
zoecarver
7304d5a257 Fix logic around checking dead instructions 2019-12-14 10:11:54 -08:00
zoecarver
af84de80b5 Fix typo 2019-12-13 18:39:18 -08:00
zoecarver
2434235216 Fix issues with worklist 2019-12-13 18:36:40 -08:00
zoecarver
b81e47b00b Remove double-collect 2019-12-13 17:56:26 -08:00
Michael Gottesman
f30b2c9b9f [pa-combiner] Change worklist iteration to use a more canonical worklist form.
We generally do not use a for loop for worklist iteration since one runs into
weird issues around always needing to recompute the worklist size since it may
change per iteration. In contrast, using the while loop approach that just pops
off the back avoids such implicit weirdness.
2019-12-13 13:31:15 -08:00
Michael Gottesman
89ff110c0d [pa-combiner] Use llvm::any_of instead of a for loop. 2019-12-13 13:31:15 -08:00
Michael Gottesman
5acb59aea4 [pa-combiner] Simplify some cleanup code by using FullApplySite::insertAfterFullInvocation. 2019-12-13 12:51:04 -08:00
zoecarver
6a3d32b6d7 Fix use of worklist 2019-12-13 09:33:25 -08:00
zoecarver
808c9faa50 * use worklist instead of vector
* replace dead global collection with global vector
* fix test
2019-12-13 09:28:24 -08:00
zoecarver
578b15e94d Add assertion in collectUsesOfInstructionForDeletion 2019-12-13 08:55:31 -08:00
zoecarver
b65b3174c2 Check global can be used before removing alloc 2019-12-13 08:53:12 -08:00
Erik Eckstein
4017570de7 Generic Specializer: Use getResilienceExpansion() throughout ReabstractionInfo
It must be consistent, otherwise the specialized function types may not match for calls in functions with different resilience expansions.

Fixes an assertion crash in the generic specializer.

rdar://problem/57844964
2019-12-13 11:15:38 +01:00
Michael Gottesman
9efb49ac9a [applysite] Add new methods that ease insertion of code after FullApplySites.
Specifically:

1. I renamed the method insertAfter -> insertAfterInvocation and added an
ehaustive switch to ensure that we properly update this code if we add new apply
sites.

2. I added a new method insertAfterFullEvaluation that is like
insertAfterInvocation except that the callback is called with insertion points
after the end/abort apply instead of after the initial invocation of the
begin_apply.
2019-12-12 16:25:10 -08:00
Arnold Schwaighofer
b5985e0953 Merge pull request #28744 from aschwaighofer/outliner_fix_57849479
Outliner: Check that we have a self argument before we ask for it
2019-12-12 12:30:37 -08:00
swift-ci
ee43a79dd0 Merge pull request #28742 from gottesmm/pr-78cb5286a0f0c99dfe6c50c2b7ee2b624f6a53f2 2019-12-12 11:42:01 -08:00
Arnold Schwaighofer
22a0445296 Merge pull request #28711 from aschwaighofer/outliner_comment
Outliner: Add comment explaining that getBridgeTo/FromObjectiveC returns an invalid SILDeclRef to signal that the type does not conform to the bridgeable protocol
2019-12-12 10:17:27 -08:00
Arnold Schwaighofer
448a6cef1e Outliner: Check that we have a self argument before we ask for it
rdar://57849479
2019-12-12 10:14:37 -08:00
Michael Gottesman
e4e48e81f1 [pa-combiner] Modernize some appending code to use a range instead of iterators. NFC. 2019-12-12 09:52:28 -08:00
Michael Gottesman
0d88ff1432 [pa-combiner] Some further updates for supporting ossa.
Specifically:

1. I converted a bunch of cases where we were emitting releases/unqualified
loads to use instead the *Operation commands that work in both modes.
2. I renamed some methods/variables that referred to releases to instead refer
to destroys.
2019-12-12 09:52:28 -08:00
eeckstein
87aac598c8 Merge pull request #28707 from eeckstein/cmo-fixes
Two fixes for cross module optimization
2019-12-12 09:22:13 +01:00
Michael Gottesman
8e50c97c2b Merge pull request #28725 from gottesmm/pr-1b10b0856adef409da4bed3d4af33137abf625cf
[ownership] Change BorrowScopeIntroducerValue::areInstructionsWithinScope to take SILInstructions instead of BranchPropagatedUser.
2019-12-11 19:21:55 -08:00
Michael Gottesman
0d08f8c802 Merge pull request #28724 from gottesmm/pr-4e2a94eb74331d3c9eadc35c9c0df4c978adc200
[sil] Move partial apply combiner code from SILCombiner into InstOptUtils.h/SILCombinerApplyVisitor.cpp.
2019-12-11 18:28:40 -08:00
Michael Gottesman
d815c603c2 [ownership] Change BorrowScopeIntroducerValue::areInstructionsWithinScope to take SILInstructions instead of BranchPropagatedUser.
All non cond_br SILInstructions are layout compatible with BranchPropagatedUser
since BPU just stores a PointerIntPair that leaves its low bits as zero unless
one adds a cond_br. Since in these cases, the SILInstructions are all not
cond_br, we can use this alternative API.

I also added some asserts to validate that the assumption that all such
SILInstructions are not cond_br is respected.
2019-12-11 16:55:13 -08:00
Michael Gottesman
bec1279414 Merge pull request #28719 from gottesmm/pr-39c568e8fa7a8e0f2ad1323b55401d557a150ad9
[diagnose-unreachable] Fix a small part of diagnose unreachable that did not handle ossa correctly.
2019-12-11 16:45:11 -08:00
Michael Gottesman
113c22a680 [sil] Move partial apply combiner code from SILCombiner into InstOptUtils.h/SILCombinerApplyVisitor.cpp.
This is in preparation for moving this into the mandatory combiner.
2019-12-11 14:48:19 -08:00
Michael Gottesman
b461eb0623 [diagnose-unreachable] Fix a small part of diagnose unreachable that did not handle ossa correctly.
Specifically if we had:

```
%1 = enum $Enum, %0
switch_enum %1
```

We would propagate %0 without eliminating the enum in certain cases. Instead, we
insert unchecked_enum_data right before the branch to ensure that:

1. The types line up.
2. The enum is only consumed along the path through the switch_enum instead of
   dealing with the lifetime of the enum along other paths.
2019-12-11 14:44:34 -08:00
Arnold Schwaighofer
c4256e7992 Outliner: Add comment explaining that getBridgeTo/FromObjectiveC returns an invalid SILDeclRef to signal that the type does not conform to the bridgeable protocol 2019-12-11 10:35:06 -08:00
Ravi Kandhadai
af3aa03e0d Merge pull request #28697 from ravikandhadai/oslog-generic-operator-bug-fix
[Constant Evaluator] Fix a bug in the composition of substitution maps
2019-12-11 09:29:31 -08:00
Erik Eckstein
71df54cc80 CrossModuleSerializationSetup: don't make shared functions usableFromInline
To avoid duplicate-symbol linker errors. Instead make them alwaysEmitIntoClient.
But only do that for thunks to limit the code size impact. Anyway, it's only important for thunks because thunks are generated at SILGen, i.e. before CrossModuleSerializationSetup.
Other shared functions, e.g. specializations are created after CrossModuleSerializationSetup, so we don't have to deal with them.
2019-12-11 18:14:41 +01: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
Ravi Kandhadai
7744720df3 [Constant Evaluator] Fix a bug in the composition of substitution
maps in the handling of partial applies. In particular, when using
substMap.subst(otherSubstMap), ensure that otherSubstMap is not empty.

Also, store the partial-apply instruction in the symbolic closure,
which makes it easier to debug errors in the folding of partial applies.
2019-12-10 20:13:49 -08:00
Arnold Schwaighofer
95475aaf15 Outliner: Verify that getBridgeFromObjectiveC and getBridgeToObjectiveC return a valid SILDeclRef
rdar://57757241
2019-12-10 11:39:26 -08:00
Michael Gottesman
f236e0f37a [semantic-arc-opts] Convert isDeadLiveRange into a method on a general LiveRange class.
In a forthcoming commit, I am going to need access to the "escaping" uses of a
dead live range. Rather than add /another/ argument to isDeadLiveRange, this
commit refactors the code to be a constructor on a general LiveRange class that
internally contains the lists of destroys, forwarding instructions we can
convert to be guaranteed, and consuming instructions that we do not understand.

Should be NFC.
2019-12-09 16:16:31 -08:00
Michael Gottesman
53b14a258d Merge pull request #28653 from gottesmm/pr-48051636c430a08d11def5e474d526a5e8e1eed1
[semantic-arc-opts] Convert load [copy] -> load_borrow given single init alloc_stack.
2019-12-09 16:07:39 -08:00
Arnold Schwaighofer
97d69ab57b Outliner: Make sure that the argument convention is guaranteed or owned
rdar://57759220
2019-12-09 12:39:17 -08:00
Michael Gottesman
61e5653000 [semantic-arc-opts] Convert load [copy] -> load_borrow given single init alloc_stack. 2019-12-09 11:33:50 -08:00