Skip to content

[ConstraintSystem] NFC: Refactor solver state depth to its own function fo… #60495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3053,9 +3053,9 @@ class ConstraintSystem {
ConstraintSystem &CS;

FreeTypeVariableBinding AllowFreeTypeVariables;

/// Depth of the solution stack.
unsigned depth = 0;
/// Return current depth of solution stack for debug printing.
unsigned int getCurrentIndent() const { return depth * 2; }

/// Maximum depth reached so far in exploring solutions.
unsigned maxDepth = 0;
Expand Down Expand Up @@ -3238,6 +3238,9 @@ class ConstraintSystem {

SmallVector<Constraint *, 4> disabledConstraints;
SmallVector<Constraint *, 4> favoredConstraints;

/// Depth of the solution stack.
unsigned depth = 0;
};

class CacheExprTypes : public ASTWalker {
Expand Down Expand Up @@ -4341,7 +4344,7 @@ class ConstraintSystem {

if (isDebugMode()) {
auto &log = llvm::errs();
log.indent(solverState ? solverState->depth * 2 : 0)
log.indent(solverState ? solverState->getCurrentIndent() : 0)
<< "(failed constraint ";
constraint->print(log, &getASTContext().SourceMgr);
log << ")\n";
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,

if (isDebugMode()) {
auto &log = llvm::errs();
auto indent = solverState ? solverState->depth * 2 : 0;
auto indent = solverState ? solverState->getCurrentIndent() : 0;
log.indent(indent) << "------- Transfomed Body -------\n";
transformedBody->second->dump(log);
log << '\n';
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ Optional<BindingSet> ConstraintSystem::determineBestBindings() {
continue;

if (isDebugMode()) {
bindings.dump(typeVar, llvm::errs(), solverState->depth * 2);
bindings.dump(typeVar, llvm::errs(), solverState->getCurrentIndent());
}

// If these are the first bindings, or they are better than what
Expand Down
12 changes: 6 additions & 6 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {

if (isDebugMode() && value > 0) {
if (solverState)
llvm::errs().indent(solverState->depth * 2);
llvm::errs().indent(solverState->getCurrentIndent());
llvm::errs() << "(increasing '" << Score::getNameFor(kind) << "' score by " << value
<< ")\n";
}
Expand All @@ -72,7 +72,7 @@ bool ConstraintSystem::worseThanBestSolution() const {
return false;

if (isDebugMode()) {
llvm::errs().indent(solverState->depth * 2)
llvm::errs().indent(solverState->getCurrentIndent())
<< "(solution is worse than the best solution)\n";
}

Expand Down Expand Up @@ -795,7 +795,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
ConstraintSystem &cs, ArrayRef<Solution> solutions,
const SolutionDiff &diff, unsigned idx1, unsigned idx2) {
if (cs.isDebugMode()) {
llvm::errs().indent(cs.solverState->depth * 2)
llvm::errs().indent(cs.solverState->getCurrentIndent())
<< "comparing solutions " << idx1 << " and " << idx2 <<"\n";
}

Expand Down Expand Up @@ -1352,13 +1352,13 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
return 0;

if (isDebugMode()) {
llvm::errs().indent(solverState->depth * 2)
llvm::errs().indent(solverState->getCurrentIndent())
<< "Comparing " << viable.size() << " viable solutions\n";

for (unsigned i = 0, n = viable.size(); i != n; ++i) {
llvm::errs().indent(solverState->depth * 2)
llvm::errs().indent(solverState->getCurrentIndent())
<< "\n--- Solution #" << i << " ---\n";
viable[i].dump(llvm::errs().indent(solverState->depth * 2));
viable[i].dump(llvm::errs().indent(solverState->getCurrentIndent()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11187,7 +11187,7 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
if (isDebugMode()) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
llvm::errs().indent(solverState ? solverState->depth * 2 : 0)
llvm::errs().indent(solverState ? solverState->getCurrentIndent() : 0)
<< "(common result type for $T" << fnTypeVar->getID() << " is "
<< commonResultType.getString(PO)
<< ")\n";
Expand Down Expand Up @@ -12693,7 +12693,7 @@ static bool isAugmentingFix(ConstraintFix *fix) {
bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
if (isDebugMode()) {
auto &log = llvm::errs();
log.indent(solverState ? solverState->depth * 2 : 0)
log.indent(solverState ? solverState->getCurrentIndent() : 0)
<< "(attempting fix ";
fix->print(log);
log << ")\n";
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ ConstraintSystem::filterDisjunction(
}

if (isDebugMode()) {
llvm::errs().indent(solverState ? solverState->depth * 2 : 0)
llvm::errs().indent(solverState ? solverState->getCurrentIndent() : 0)
<< "(disabled disjunction term ";
constraint->print(llvm::errs(), &ctx.SourceMgr);
llvm::errs() << ")\n";
Expand Down Expand Up @@ -1678,7 +1678,7 @@ ConstraintSystem::filterDisjunction(
}

if (isDebugMode()) {
llvm::errs().indent(solverState ? solverState->depth * 2 : 0)
llvm::errs().indent(solverState ? solverState->getCurrentIndent(): 0)
<< "(introducing single enabled disjunction term ";
choice->print(llvm::errs(), &ctx.SourceMgr);
llvm::errs() << ")\n";
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class SolverStep {

llvm::raw_ostream &getDebugLogger(bool indent = true) const {
auto &log = llvm::errs();
return indent ? log.indent(CS.solverState->depth * 2) : log;
return indent ? log.indent(CS.solverState->getCurrentIndent()) : log;
}
};

Expand Down Expand Up @@ -519,7 +519,7 @@ template <typename P> class BindingStep : public SolverStep {
if (CS.isDebugMode()) {
auto &log = getDebugLogger();
log << "(attempting ";
choice->print(log, &CS.getASTContext().SourceMgr, CS.solverState->depth * 2 + 2);
choice->print(log, &CS.getASTContext().SourceMgr, CS.solverState->getCurrentIndent() + 2);
log << '\n';
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/ConstraintGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ namespace {
if (cs.isDebugMode()) {
auto &log = llvm::errs();
if (cs.solverState)
log.indent(cs.solverState->depth * 2);
log.indent(cs.solverState->getCurrentIndent());

log << "Collapsing one-way components for $T"
<< edge.first->getID() << " and $T" << edge.second->getID()
Expand Down Expand Up @@ -1440,7 +1440,7 @@ bool ConstraintGraph::contractEdges() {
if (CS.isDebugMode()) {
auto &log = llvm::errs();
if (CS.solverState)
log.indent(CS.solverState->depth * 2);
log.indent(CS.solverState->getCurrentIndent());

log << "Contracting constraint ";
constraint->print(log, &CS.getASTContext().SourceMgr);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3528,7 +3528,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
if (isDebugMode()) {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
llvm::errs().indent(solverState ? solverState->depth * 2 : 2)
llvm::errs().indent(solverState ? solverState->getCurrentIndent() : 2)
<< "(overload set choice binding "
<< boundType->getString(PO) << " := "
<< adjustedRefType->getString(PO) << ")\n";
Expand Down