Commit Graph

188 Commits

Author SHA1 Message Date
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
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
62010f3bfb When retrieving constraints that mention a type variable, also look through fixed bindings.
Swift SVN r12283
2014-01-14 15:17:35 +00:00
Doug Gregor
3755e6d556 Replace worklist deque with Active/Inactive constraint lists.
Swift SVN r11077
2013-12-10 16:36:36 +00:00
Doug Gregor
08893199b5 Pre-calculate and store the type variables referenced by a constraint.
This is a micro-optimization that doesn't seem to matter much at the
moment.


Swift SVN r11012
2013-12-09 13:39:16 +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
c63529483d Fix verification of the constraint graph
Swift SVN r10910
2013-12-06 04:31:17 +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
3d4da69b08 In "x as T", type-check the subexpression "x" using "T" as the context type.
Using "T" as the contextual type, either for an implicit conversion
(in the coercion case) or as a downcast (for the checked-cast case),
opens up more type-inference opportunities. Most importantly, it
allows coercions such as "1 as UInt32" (important for
<rdar://problem/15283100>). Additionally, it allows one to omit
generic arguments within the type we're casting to.

Some additional cleanup to follow.


Swift SVN r10799
2013-12-04 22:32:28 +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
8cd63c9b8d Use the constraint graph to isolate and solve the smallest connected component.
At each solution phase, construct a constraint graph and identify the
smallest connected component. Only solve for the variables in that
connected component, and restrict simplification to the constraints
within that connected component. This reduces the number of
visited-but-not-simplified constraints by ~2x when type-checking the
standard library.

Performance-wise, this is actually a regression (0.25s/8% when parsing
the standard library), because the time spent building the constraint
graph exceeds the time saved by the optimization above.

The hackaround in the standard library is due to
<rdar://problem/15168483>. Essentially, this commit changes the order
in which we visit type variables, causing the type checker to make
some very poor deduction choices.

The point of actually committing this is that it validates the
constraint graph construction and sets the stage for an actual
optimization based on isolating the solving work for the different
components.



Swift SVN r10672
2013-11-23 03:44:40 +00:00
Doug Gregor
5d9b955369 Overload-binding constraints can have base types; add those adjacencies.
Swift SVN r10665
2013-11-22 22:11:52 +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
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