Commit Graph

43 Commits

Author SHA1 Message Date
Robert Widmann
d890b8ad41 Remove some save-and-restores
An awful pattern we use throughout the compiler is to save and restore global flags just for little things.  In this case, it was just to turn on some extra options in AST printing for type variables. The kicker is that the ASTDumper doesn't even respect this flag. Add this as a PrintOption and remove the offending save-and-restores.

This doesn't quite get them all: we appear to have productized this pattern in the REPL.
2019-11-13 07:37:12 -08:00
Brent Royal-Gordon
ffbe53e290 [NFC] Eliminate const_casts in constraint system dumpers 2019-10-31 18:41:11 -07:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00
Doug Gregor
cf1732cce2 [Constraint graph] Reinstate the adjacencies of constraint graph nodes.
Reinstate the list of adjacencies in each constraint graph node,
effectively reverting
dfdd352d3d. Exclude one-way constraints
from this computation; we'll handle them separately.
2019-08-22 09:38:21 -07:00
Doug Gregor
da267bf7ca [Constraint system] Switch TypeVariables to a SetVector.
There were a few places where we wanted fast testing to see whether a
particular type variable is currently of interest. Instead of building
local hash tables in those places, keep type variables in a SetVector
for efficient testing.
2019-08-16 14:13:15 -07:00
Doug Gregor
3c69f6a305 [Constraint solver] Introduce one-way constraints.
Introduce the notion of "one-way" binding constraints of the form

  $T0 one-way bind to $T1

which treats the type variables $T0 and $T1 as independent up until
the point where $T1 simplifies down to a concrete type, at which point
$T0 will be bound to that concrete type. $T0 won't be bound in any
other way, so type information ends up being propagated right-to-left,
only. This allows a constraint system to be broken up in more
components that are solved independently. Specifically, the connected
components algorithm now proceeds as follows:

1. Compute connected components, excluding one-way constraints from
consideration.
2. Compute a directed graph amongst the components using only the
one-way constraints, where an edge A -> B indicates that the type
variables in component A need to be solved before those in component
B.
3. Using the directed graph, compute the set of components that need
to be solved before a given component.

To utilize this, implement a new kind of solver step that handles the
propagation of partial solutions across one-way constraints. This
introduces a new kind of "split" within a connected component, where
we collect each combination of partial solutions for the input
components and (separately) try to solve the constraints in this
component. Any correct solution from any of these attempts will then
be recorded as a (partial) solution for this component.

For example, consider:

  let _: Int8 = b ? Builtin.one_way(int8Or16(17)) :
  Builtin.one_way(int8Or16(42\
))

where int8Or16 is overloaded with types `(Int8) -> Int8` and
`(Int16) -> Int16`. There are two one-way components (`int8Or16(17)`)
and (`int8Or16(42)`), each of which can produce a value of type `Int8`
or `Int16`. Those two components will be solved independently, and the
partial solutions for each will be fed into the component that
evaluates the ternary operator. There are four ways to attempt that
evaluation:

```
  [Int8, Int8]
  [Int8, Int16]
  [Int16, Int8]
  [Int16, Int16]

To test this, introduce a new expression builtin `Builtin.one_way(x)` that
introduces a one-way expression constraint binding the result of the
expression 'x'. The builtin is meant to be used for testing purposes,
and the one-way constraint expression itself can be synthesized by the
type checker to introduce one-way constraints later on.

Of these two, there are only two (partial) solutions that can work at
all, because the types in the ternary operator need a common
supertype:

  [Int8, Int8]
  [Int16, Int16]

Therefore, these are the partial solutions that will be considered the
results of the component containing the ternary expression. Note that
only one of them meets the final constraint (convertibility to
`Int8`), so the expression is well-formed.

Part of rdar://problem/50150793.
2019-08-13 11:48:42 -07:00
Doug Gregor
dec149ce62 [Constraint graph] Move component sorting into connected components. 2019-08-11 21:49:08 -07:00
Doug Gregor
ab38be128d [Constraint graph] Handle orphaned constraints within connected components
Move the logic for creating connected components of orphaned
constraints into the connected-components algorithm code, rather than
making it a special part of SplitterStep.
2019-08-11 21:28:34 -07:00
Doug Gregor
4c04ced939 [Constraint graph] Make connected components more self-contained.
Have the constraint graph's connected-component implementation be more
self-contained, producing a vector containing each of the actual
components (where each is defined by a list of type variables and a list
of constraints). This simplifies the contract with the client
(SplitterStep) and eliminates a bunch of separate mapping steps to
interpret the results.

It also lets us enrich the Component data structure in the future.
2019-08-07 08:32:34 -07:00
Doug Gregor
c2a9286d1d [Constraint graph] Print only those type variables that are of interest now 2019-08-05 22:21:21 -07:00
Doug Gregor
8355f3d270 [Constraint graph] Move constraint uniquing into gatherConstraints().
Simplify the interface to gatherConstraints() by performing the
uniquing within the function itself and returning only the resulting
(uniqued) vector of constraints.
2019-07-25 02:26:49 -04:00
Doug Gregor
dfdd352d3d [Constraint graph] Eliminate adjacency information in the graph nodes.
Each constraint graph node maintained adjacency information that was
fairly expensive---a vector of type variables and a dense map of extra
adjacency information---and that was continuously maintained. Remove
this adjacency information, instead recomputing it by walking the
constraints (to get their type variables) and having a separate
(smaller) list of type variables that are adjacent due to fixed
bindings. This simplifies the constraint graph code and reduces
per-node memory overhead.
2019-07-25 01:54:06 -04:00
Doug Gregor
6a970a8736 [Constraint solver] Reimplement connected components using constraints.
The constraint graph maintains a fairly heavyweight list of
adjacencies that is only used in two operations: connected components
and gathering related constraints. Switch connected components over to
primarily use the set of constraints (which are necessary for many
reasons), reducing the need for the adjacencies list.
2019-07-25 01:54:05 -04:00
Gwen Mittertreiner
e51b72b3e0 Reduce the Stack Size of ConstraintSystem
The ConstraintSystem class is on the order of 1000s of bytes in size on
the stacka nd is causing issues with dispatch's 64k stack limit.

This changes most Small data types which store data on the stack to non
small heap based data types.
2019-05-13 11:40:43 -07:00
Pavel Yaskevich
624c183fe0 [ConstraintGraph] Change gatherConstraints to take SetVector
For stable iteration order, let's switch from `SmallPtrSet`
to `SetVector` which ensures insertion order iteration.
2018-07-27 15:34:15 -07:00
Pavel Yaskevich
48dd1e837b [ConstraintGraph] Add filtering to gatherConstraints per type variable
Most of the use-cases of `gatherConstraints` require filtering
at least based on the constraint kind that caller is interested in,
so instead of returning unrelated results and asking caller to
filter separately, let's add that functionality directly to
`gatherConstraints`.
2018-07-26 22:41:15 -07:00
Pavel Yaskevich
dd798accd8 [ConstraintGraph] Use set to gather constraints for type variables
Since it's possible to find the same constraint through two different
but equivalent type variables, let's use a set to store constraints
instead of a vector to avoid processing the same constraint multiple
times.
2018-07-26 22:41:07 -07:00
Pavel Yaskevich
3e254678a2 [Sema] Add counter to track number of constraints considered by each edge contraction attempt 2018-05-12 02:37:52 -07:00
Huon Wilson
d4fbca1183 [Sema/CS] std::function -> llvm::function_ref for some non-escaping params. 2018-05-01 08:29:08 +10:00
Doug Gregor
40b6764e80 [Constraint solver] Handle disjunctions as separate connected components.
The constraint graph models type variables (as the nodes) and
constraints (as the multi-edges connecting nodes). The connected
components within this (multi-)graph are independent subproblems that
are solved separately; the results from each subproblem are then
combined. The approach helps curtail exponential behavior, because
(e.g.) the disjunctions/type variables in one component won't ever be
explored while solving for another component

This approach assumes that all of the constraints that cannot be
immediately solved are associated with one or more type
variables. This is almost entirely true---constraints that don't
involve type variables are immediately simplified.

Except for disjunctions. A disjunction involving no type variables
would not appear *at all* in the constraint graph. Worse, it's
independence from other constraints could not be established, so the
constraint solver would go exponential for every one of these
constraints. This has always been an issue, but it got worse with the
separation of type checking of "as" into the "coercion" case and the
"bridging" case, which introduced more of these disjunctions. This led
to counterintuitive behavior where adding "as Foo" would cause the
type checking to take *more* time than leaving it off, if both sides
of the "as" were known to be concrete. rdar://problem/30545483
captures a case (now in the new test case) where we saw such
exponential blow-ups.

Teach the constraint graph to keep track of "orphaned" constraints
that don't reference any type variables, and treat each "orphaned"
constraint as a separate connected component. That way, they're solved
independently.

Fixes rdar://problem/30545483 and will likely curtain other
exponential behavior we're seeing in the solver.
2017-02-20 17:18:18 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
9d0b2abfc2 [gardening] Normalize end-of-namespace comments 2016-12-17 22:29:07 +01:00
Doug Gregor
f68f87a56a [Constraint solver] After binding a type variable, activate affected constraints
Once we've bound a type variable, we find those inactive constraints
that mention the type variable and make them active, so they'll be
simplified again. However, we weren't finding *all* constraints that
could be affected---in particular, we weren't searching everything
related to the type variables in the equivalence class, which meant
that some constraints would not get visited... and we would to
type-check simply because we didn't look at a constraint again when we
should have.

Fixes rdar://problem/29633747.
2016-12-14 20:18:04 -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
Doug Gregor
f168e7270c [Type checker] Use DependentMemberType instead of type variables for nested types.
In the constraint solver, we've traditionally modeled nested type via
a "type member" constraint of the form

  $T1 = $T0.NameOfTypeMember

and treated $T1 as a type variable. While the solver did generally try
to avoid attempting bindings for $T1 (it would wait until $T0 was
bound, which solves the constraint), on occasion we would get weird
behavior because the solver did try to bind the type
variable.

With this commit, model nested types via DependentMemberType, the same
way we handle (e.g.) the nested type of a generic type parameter. This
solution maintains more information (e.g., we know specifically which
associated type we're referring to), fits in better with the type
system (we know how to deal with dependent members throughout the type
checker, AST, and so on), and is easier to reason able.

This change is a performance optimization for the type checker for a
few reasons. First, it reduces the number of type variables we need to
deal with significantly (we create half as many type variables while
type checking the standard library), and the solver scales poorly with
the number of type variables because it visits all of the
as-yet-unbound type variables at each solving step. Second, it
eliminates a number of redundant by-name lookups in cases where we
already know which associated type we want.

Overall, this change provides a 25% speedup when type-checking the
standard library.
2016-11-05 23:20:28 -07:00
Joe Pamer
37032b2a58 Take first steps towards simplifying the constraint graph by performing edge contraction. 2016-01-22 12:34:32 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Doug Gregor
c71644c418 Eliminate some newly-introduced nondeterminism in the constraint graph.
When merging two type variables, the member types of those type
variables were merged in DenseMap order.

Swift SVN r14382
2014-02-26 06:26:03 +00:00
Doug Gregor
be9c6f2d26 Track the member types of type variables explicitly within the constraint graph.
This eliminates the duplication of type variables that represent the member types of existing type variables. I'm unable to trigger this with a test case at the moment, but it becomes important when we begin to substitute type variables through protocol conformances.

Swift SVN r12971
2014-01-26 22:38:33 +00:00
Doug Gregor
117940958b Replace the type variable -> graph node dense map with an embedded pointer.
Provides a 4% speedup type-checking the standard library. This
optimization brings the global constraint graph within 2% of the prior
solution, and prevents us from creating multiple constraint graphs per
constraint system, so flip the switch to always use the global
constraint graph.


Swift SVN r11003
2013-12-09 04:53:33 +00:00
Doug Gregor
2dbc5018ae s/ConstraintGraph::Node/ConstraintGraphNode
Swift SVN r11002
2013-12-09 03:48:12 +00:00
Doug Gregor
30b6302b7c Introduce a basic worklist into the constraint solver.
Whenever we bind a type variable or merge two type variables, add
those constraints that could be affected to a worklist. Simplification
continues processing constraints in the worklist until the worklist is
empty.

This change reduces the number of constraints that we visit but can't
simplify by ~13x in the standard library, but this doesn't translate
into a performance win. More investigation is needed here.

Note that the worklist is only in use when we have a global constraint
graph, which isn't enabled by default yet.


Swift SVN r10936
2013-12-06 21:31:05 +00:00
Doug Gregor
cd595227f0 Zap an unused method
Swift SVN r10930
2013-12-06 19:37:42 +00:00
Doug Gregor
680a5ba5e7 Introduce a per-constraint system constraint graph (optionally).
Make the constraint graph into a scoped data structure that tracks the
changes that occur within a given scope, undoing those changes when
the scope is popped off the scope stack. The constraint graph's scopes
align with the constraint solver's scopes. Synchronize the solver's
constraint addition/removal operations with the constraint graph, and
improve our verification to make sure these stay in sync.

This is still a work in progress. Type checking the standard library
is about 5% slower with the per-constraint-system constraint graph
rather than building a new constraint graph each iteration *after*
simplification, and there are two intruiging test failures that appear
to be the constraint solver breaking its own invariants. Therefore,
this feature is off by default. See the ConstraintSystem constructor
for the one-line change to turn it back on.



Swift SVN r10927
2013-12-06 19:07:46 +00:00
Doug Gregor
61eb9b1f11 (Optionally) Evolve the constraint graph during simplification.
Rather than building the constraint graph after simplification and
then never modifying it, build the constraint graph before
simplification and update it as simplification adds/removes
constraints. This is a step toward two separable performance goals:
(1) having a single constraint graph that evolves over the lifetime of
the constraint system, rather than building a new constraint graph at
each solver step, and (2) using the constraint graph to implement a
worklist traversal during simplification, so we only re-simplify those
constraints that might be affected by a choice the solver makes.

This behavior is currently optional with an ugly in-source flag
because while it works, it's a rather painful performance regression
that I don't want to subject everyone to. I'll turn it on for everyone
once it's a win, which should be after either (1) or (2) above is
implemented.



Swift SVN r10924
2013-12-06 16:01:25 +00:00
Doug Gregor
5f928ef40b Constraint graph operations: merging nodes, removing nodes, binding type variables
Swift SVN r10912
2013-12-06 05:02:56 +00:00
Doug Gregor
5b333cd213 Track fixed bindings within the normal adjacency info.
This is a micro-optimization that eliminates the overhead of Yet
Another SmallVector in the Node class.


Swift SVN r10908
2013-12-06 03:48:59 +00:00
Doug Gregor
0bdb3a3487 Eliminate unused parameters for constraint graph add/remove adjacency methods.
Swift SVN r10906
2013-12-06 03:18:58 +00:00
Doug Gregor
651f858dbb Represent all type variables within the constraint graph without simplification.
Previously, the constraint graph only represented type variables that
were both unbound and were the representatives within their respective
equivalence classes. To achieve this, each constraint was fully
simplified when it was added to the graph, which is a fairly expensive
process. This representation made certain operations---merging two type
variables, replacing a type variable with a fixed type, etc---both
hard to implement and hard to reverse, forcing us to rebuild the
constraint graph each time.

Now, add all type variables to the graph (including those with fixed
type bindings and non-representatives) and add constraints without
simplification. Separately track the equivalence classes of each type
variable (in the representative's node) and adjacencies due to type
variables showing up in the fixed type bindings of other type
variables. Although not yet implemented, the merging and type variable
replacement operations are far easier to implement (and more
efficient) with this representation, and are also easier to undo,
making this a step toward creating and updating a single consistent,
global constraint graph rather than re-creating a constraint graph
during each solver step.

Performance-wise, this is a 4% regression when type-checking the
standard library. I expect to make that up easily once we switch to a
single constraint graph.




Swift SVN r10897
2013-12-06 01:23:39 +00:00
Doug Gregor
d52887c9bb Constraint graph WIP: collapse two nodes into a single node.
This currently-untestable code allows updates to the constraint graph
when a same-type constraint causes two type variables to be
unified. Additionally, it handles the removal of a constraint from the
constraint system, e.g., if it is solved.


Swift SVN r10716
2013-12-01 16:42:33 +00:00
Doug Gregor
e28c425a64 Compute connected components for the constraint graph.
This implements an offline algorithm for connected components. We
could use an online algorithm, which would be slightly more efficient
in the case where we always require the connected components, but such
algorithms don't cope with edge removals very well.

Still just a debugging tool!



Swift SVN r10663
2013-11-22 19:21:43 +00:00
Doug Gregor
f4a8b5e1ee More documentation for the constraint graph
Swift SVN r10662
2013-11-22 18:31:49 +00:00
Doug Gregor
ccf01377cc Introduce a constraint graph to track relationships among type variables.
We're not using this for anything other than debugging at the moment.


Swift SVN r10661
2013-11-22 18:25:14 +00:00