Commit Graph

71 Commits

Author SHA1 Message Date
Doug Gregor
5a4af23a63 [Constraint graph] Maintain constraint order when splitting components.
Maintain the order of constraints when splitting the system into
connected components, to match the behavior prior to the refactoring
into a separate connected-components algorithm.
2019-08-11 21:25:24 -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
805b02da37 [Constraint graph] Associate all constraints with components.
The connected components computation was not forming components when all of
the type variables in a component were already bound. Any remaining
constraints involving those type variables would then arbitrarily end up
in component 0, due to the the default-construction behavior of a map’s
operator[] with missing keys, artificially increasing the size of that
component with (typically) additional disjunctions.

Ensure that all constraints get into a component, creating one to hold
the a constraint even when all of the type variables are already bound.
Then, assert the invariant that every constraint is associated with a
component.

In time, we should probably eliminate this notion of disjunctions that
remain even when the type variable has been bound. For now, strengthen the
invariant to at least ensure that they get solved in isolation.
2019-08-06 23:15:58 -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
14ca9d1461 [Constraint graph] Make connected components never touch all type variables.
Simplify the connected-components computation slightly and make sure
that it never performs work outside of the subgraph described by the
input set of type variables.
2019-07-27 01:18:20 -04:00
Doug Gregor
6be6401581 [Constraint solver] Fix incorrect bailout in connected-components.
Address a silly mistake that meant we were miscomputing connected
components.
2019-07-27 01:18:00 -04:00
Doug Gregor
62502efffb [Constraint graph] Restrict connected components to requested type variables.
The API of the connected-components algorithm asks clients to
provide the set of type variables of interest. However, the connected
components algorithm itself was operating across the entire set of
type variables, then narrowing the result down to the type variables
of interest. Instead, only perform connected components on those type
variables of interest, so that we are only doing work proportional to
the subgraph we're working in.
2019-07-25 23:41:14 -04: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
54bdd7b840 [Constraint solver] Migrate ConstraintGraph::gatherConstraints() off adjacencies list.
Use the adjacencies implied by the constraints of a node rather than looking
at the "adjacency" list, and try to simplify this code path a bit. The
diagnostic change is because we are now uniformly recording the
members of the equivalence class.
2019-07-25 01:54:06 -04:00
Doug Gregor
6737cf9fb8 [Constraint graph] Generalize the implementation of depth-first search. 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
Slava Pestov
33871548b3 Clean up TypeVariableType::Implementation::mustBeMaterializable(), etc 2019-01-12 14:10:11 -05: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
Pavel Yaskevich
e021691bae [ConstraintGraph] Fix contractEdges to gather constraints only once
Currently we have this non-optimal behavior in `contractEdges` where
for every type variable it gathers constraints for its whole equivalence
class before checking if any are "contractable", instead constraints
could be gathered/filtered once which removes a lot of useless work.
2018-05-11 16:08:53 -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
Pavel Yaskevich
3b7e555c7e [CSSolver] Fix performance regression related to contraction of closure parameters
Improve situation around closure parameter/argument contractions
by allowing such action if it can be proved that none of the bindings
would result in solver attempting to bind parameter to `inout` type.

Resolves: rdar://problem/36838495
2018-01-31 14:21:14 -08:00
Pavel Yaskevich
80e4a2226b [ConstraintGraph] Don't try to contract edge of parameter bindings with inout attribute
Currently edge related to the parameter bindings is contracted
without properly checking if newly created equivalence class has
the same inout & l-value requirements. This patch improves the
situation by disallowing contraction of the edges related to parameter
binding constraint where left-hand side has `inout` attribute set.

Such guarantees that parameter can get `inout` type assigned when
argument gets `l-value` type.

Resolves: rdar://problem/33429010
2017-09-22 17:23:12 -07:00
Mark Lacey
8563911ac6 [Constraint system] Use llvm::any_of instead of std::any_of. 2017-09-13 15:29:15 -07:00
Mark Lacey
c960926b06 [Constraint system] Turn explicit loop into std::any_of. 2017-09-13 14:31:18 -07:00
Pavel Yaskevich
0a178fd263 [ConstraintGraph] Disallow subtype constraint contractions
Presently subtype constraint is considered a candidate for contraction
via `shouldContractEdge` when left-hand side of the subtype constraint
is an inout type with type variable inside. Although it's intended to
be used only in combination with BindParam constraint assigned to the
same variable, that is actually never checked and contraction of subtype
constraint like that is invalid.

Resolves: rdar://problem/34333874
2017-09-11 15:13:13 -07:00
Joe Groff
6e9f4dcd62 Sema: Avoid asking for the SecondType of Constraints that don't have them.
Assertions tripped while trying to reproduce SR-5513.
2017-07-20 16:04:30 -07:00
Mark Lacey
687624e317 [Constraint solver] Gather constraints from adjacencies of equiv class.
When gathering constraints for a type variable, we were gathering all of
the constraints for the members of the equivalence class, and then for
the adjacencies of the representative of the equivalence class, but not
from the adjacencies of other members of the equivalence class.

For example for:
  #3 = $T3
  #4 = $T4 equivalent to $T3
  #5 = $T5 as $T4.Element

after binding $T3 we would collect the constraints related to $T3 and
$T4, but not $T5. The end result can be that we finish examining all
disjunctions and type bindings but still have inactive constraints in
the constraint graph, which is treated as a failure in the solver.

Fixes SR-5120 / rdar://problem/32618740.
2017-06-23 09:43:06 -07: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
Hugh Bellamy
f001b7562b Use relatively new LLVM_FALLLTHROUGH instead of our own SWIFT_FALLTHROUGH 2017-02-12 10:47:03 +07:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +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
Pavel Yaskevich
8208c94dcc [TypeChecker] Add getter/setter methods for generatedConstraints of SolverState 2016-12-11 21:46:03 -08:00
Michael Gottesman
59c6a64f5a [gardening] 0 => nullptr. Fixed with clang-tidy. 2016-12-06 23:14:13 -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
Doug Gregor
a65812c558 [Constraint solver] Add ConstraintKind::BindToPointerType.
This matches up with TypeMatchKind::BindToPointerType.
2016-10-24 21:24:49 -07:00
Doug Gregor
2e800f4acc [Constraint system] Centralize the handling of "unsolved" returns in matchTypes. 2016-10-14 10:28:20 -07:00
Doug Gregor
a36993add0 [Constraint graph] Make dumping print type variables. NFC 2016-10-11 17:09:13 -07:00
practicalswift
abfecfde17 [gardening] if ([space]…[space]) → if (…), for(…) → for (…), while(…) → while (…), [[space]x, y[space]] → [x, y] 2016-04-04 16:22:11 +02:00
Joe Pamer
50749dfcd0 Re-enable contractions for parameter binding constraints between identical type variables.
In these cases, we should just go ahead and remove the redundant constraints:
- They don't help us derive a solution for the system
- Not doing so might leave "dangling" constraints that will make the system unsolvable
2016-01-22 17:06:22 -08:00
Joe Pamer
32121ecdc6 Pending further work, do not contract related parameter binding constraints that refer to the same type variable. (E.g., "$0 == $0") 2016-01-22 15:29:56 -08:00
Joe Pamer
bbf0c1e8b9 Introduce limited support for subtype constraint contractions, with special support for solving over implicit closure parameters with inferred inout types. 2016-01-22 12:34:34 -08:00
Joe Pamer
762fc2a0ab - Do not update the work list whilst contracting edges.
- Temporarily disable contraction of conversion constraints.
2016-01-22 12:34:33 -08:00
Joe Pamer
33bbe2c787 Account for generated constraints when removing edges from the constraint graph. 2016-01-22 12:34:33 -08:00
Joe Pamer
fb03b7974a Pave the way for contracting edges between closure parameter bindings. 2016-01-22 12:34:33 -08:00
Joe Pamer
c8ac294ea4 - Consider parameter bindings
- Slightly improve logging for contractions
2016-01-22 12:34:32 -08: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
Ben Langmuir
e9e1666ab0 Update for upstream LLVM changes
* removal of StringMap's GetOrCreateValue
* SmallSet::insert now returns a pair like std::set

Swift SVN r23435
2014-11-19 16:49:30 +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