Skip to content

Commit 9b9aa23

Browse files
committed
[Constraint solver] Lazily populate the expression depth/index map.
1 parent 01f203a commit 9b9aa23

File tree

2 files changed

+64
-46
lines changed

2 files changed

+64
-46
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,6 @@ ExpressionTimer::~ExpressionTimer() {
7373
.highlight(E->getSourceRange());
7474
}
7575

76-
/// Extend the given depth map by adding depths for all of the subexpressions
77-
/// of the given expression.
78-
static void extendDepthMap(
79-
Expr *expr,
80-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap) {
81-
class RecordingTraversal : public ASTWalker {
82-
public:
83-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
84-
unsigned Depth = 0;
85-
86-
explicit RecordingTraversal(
87-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap)
88-
: DepthMap(depthMap) {}
89-
90-
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
91-
DepthMap[E] = {Depth, Parent.getAsExpr()};
92-
Depth++;
93-
return { true, E };
94-
}
95-
96-
Expr *walkToExprPost(Expr *E) override {
97-
Depth--;
98-
return E;
99-
}
100-
};
101-
102-
RecordingTraversal traversal(depthMap);
103-
expr->walk(traversal);
104-
}
10576

10677
ConstraintSystem::ConstraintSystem(DeclContext *dc,
10778
ConstraintSystemOptions options,
@@ -111,7 +82,7 @@ ConstraintSystem::ConstraintSystem(DeclContext *dc,
11182
CG(*new ConstraintGraph(*this))
11283
{
11384
if (expr) {
114-
extendDepthMap(expr, ExprWeights);
85+
InputExprs.insert(expr);
11586
}
11687

11788
assert(DC && "context required");
@@ -530,6 +501,54 @@ ConstraintSystem::getCalleeLocator(ConstraintLocator *locator,
530501
return getConstraintLocator(anchor);
531502
}
532503

504+
/// Extend the given depth map by adding depths for all of the subexpressions
505+
/// of the given expression.
506+
static void extendDepthMap(
507+
Expr *expr,
508+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap) {
509+
class RecordingTraversal : public ASTWalker {
510+
public:
511+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
512+
unsigned Depth = 0;
513+
514+
explicit RecordingTraversal(
515+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap)
516+
: DepthMap(depthMap) {}
517+
518+
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
519+
DepthMap[E] = {Depth, Parent.getAsExpr()};
520+
Depth++;
521+
return { true, E };
522+
}
523+
524+
Expr *walkToExprPost(Expr *E) override {
525+
Depth--;
526+
return E;
527+
}
528+
};
529+
530+
RecordingTraversal traversal(depthMap);
531+
expr->walk(traversal);
532+
}
533+
534+
Optional<std::pair<unsigned, Expr *>> ConstraintSystem::getExprDepthAndParent(
535+
Expr *expr) {
536+
// Bring the set of expression weights up to date.
537+
while (NumInputExprsInWeights < InputExprs.size()) {
538+
extendDepthMap(InputExprs[NumInputExprsInWeights], ExprWeights);
539+
++NumInputExprsInWeights;
540+
}
541+
542+
auto e = ExprWeights.find(expr);
543+
if (e != ExprWeights.end())
544+
return e->second;
545+
546+
if (baseCS && baseCS != this)
547+
return baseCS->getExprDepthAndParent(expr);
548+
549+
return None;
550+
}
551+
533552
Type ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
534553
ConstraintLocatorBuilder locator,
535554
OpenedTypeMap &replacements) {

lib/Sema/ConstraintSystem.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,12 @@ class ConstraintSystem {
10241024
unsigned CountDisjunctions = 0;
10251025

10261026
private:
1027+
/// The set of expressions for which we have generated constraints.
1028+
llvm::SetVector<Expr *> InputExprs;
1029+
1030+
/// The number of input expressions whose parents and depths have
1031+
/// been entered into \c ExprWeights.
1032+
unsigned NumInputExprsInWeights = 0;
10271033

10281034
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> ExprWeights;
10291035

@@ -1982,29 +1988,22 @@ class ConstraintSystem {
19821988
getConstraintLocator(const ConstraintLocatorBuilder &builder);
19831989

19841990
/// Lookup and return parent associated with given expression.
1985-
Expr *getParentExpr(Expr *expr) const {
1986-
auto e = ExprWeights.find(expr);
1987-
if (e != ExprWeights.end())
1988-
return e->second.second;
1989-
1990-
if (baseCS && baseCS != this)
1991-
return baseCS->getParentExpr(expr);
1992-
1991+
Expr *getParentExpr(Expr *expr) {
1992+
if (auto result = getExprDepthAndParent(expr))
1993+
return result->second;
19931994
return nullptr;
19941995
}
19951996

19961997
/// Retrieve the depth of the given expression.
1997-
Optional<unsigned> getExprDepth(Expr *expr) const {
1998-
auto e = ExprWeights.find(expr);
1999-
if (e != ExprWeights.end())
2000-
return e->second.first;
2001-
2002-
if (baseCS && baseCS != this)
2003-
return baseCS->getExprDepth(expr);
2004-
1998+
Optional<unsigned> getExprDepth(Expr *expr) {
1999+
if (auto result = getExprDepthAndParent(expr))
2000+
return result->first;
20052001
return None;
20062002
}
20072003

2004+
/// Retrieve the depth and parent expression of the given expression.
2005+
Optional<std::pair<unsigned, Expr *>> getExprDepthAndParent(Expr *expr);
2006+
20082007
/// Returns a locator describing the callee for the anchor of a given locator.
20092008
///
20102009
/// - For an unresolved dot/member anchor, this will be a locator describing

0 commit comments

Comments
 (0)