[ConstraintGraph] Collect and print changes in current Active Scope.

This commit is contained in:
Amritpan Kaur
2022-08-12 15:29:53 -07:00
parent bd2968e2c1
commit 01ea11b34a
3 changed files with 125 additions and 0 deletions

View File

@@ -387,6 +387,7 @@ public:
/// Print the graph. /// Print the graph.
void print(ArrayRef<TypeVariableType *> typeVars, llvm::raw_ostream &out); void print(ArrayRef<TypeVariableType *> typeVars, llvm::raw_ostream &out);
void dump(llvm::raw_ostream &out); void dump(llvm::raw_ostream &out);
void dumpActiveScopeChanges(llvm::raw_ostream &out, unsigned indent = 0);
// FIXME: Potentially side-effectful. // FIXME: Potentially side-effectful.
SWIFT_DEBUG_HELPER(void dump()); SWIFT_DEBUG_HELPER(void dump());

View File

@@ -545,6 +545,13 @@ public:
auto scope = std::make_unique<Scope>(CS); auto scope = std::make_unique<Scope>(CS);
if (attempt(*choice)) { if (attempt(*choice)) {
ActiveChoice.emplace(std::move(scope), *choice); ActiveChoice.emplace(std::move(scope), *choice);
if (CS.isDebugMode()) {
auto &log = llvm::errs();
auto &CG = CS.getConstraintGraph();
CG.dumpActiveScopeChanges(log, CS.solverState->getCurrentIndent());
}
return suspend(std::make_unique<SplitterStep>(CS, Solutions)); return suspend(std::make_unique<SplitterStep>(CS, Solutions));
} }
} }

View File

@@ -1558,6 +1558,123 @@ void ConstraintGraph::dump(llvm::raw_ostream &out) {
print(CS.getTypeVariables(), out); print(CS.getTypeVariables(), out);
} }
void ConstraintGraph::dumpActiveScopeChanges(llvm::raw_ostream &out,
unsigned indent) {
if (Changes.empty())
return;
// Collect Changes for printing.
std::map<TypeVariableType *, TypeBase *> tvWithboundTypes;
std::vector<TypeVariableType *> addedTypeVars;
std::vector<TypeVariableType *> equivTypeVars;
std::set<Constraint *> addedConstraints;
std::set<Constraint *> removedConstraints;
for (unsigned int i = ActiveScope->getStartIdx(); i < Changes.size(); i++) {
auto change = Changes[i];
switch (change.Kind) {
case ChangeKind::BoundTypeVariable:
tvWithboundTypes.insert(std::pair<TypeVariableType *, TypeBase *>(
change.Binding.TypeVar, change.Binding.FixedType));
break;
case ChangeKind::AddedTypeVariable:
addedTypeVars.push_back(change.TypeVar);
break;
case ChangeKind::ExtendedEquivalenceClass:
equivTypeVars.push_back(change.EquivClass.TypeVar);
break;
case ChangeKind::AddedConstraint:
addedConstraints.insert(change.TheConstraint);
break;
case ChangeKind::RemovedConstraint:
removedConstraints.insert(change.TheConstraint);
break;
}
}
// If there are any constraints that were both added and removed in this set
// of Changes, remove them from both.
std::set<Constraint *> intersects;
set_intersection(addedConstraints.begin(), addedConstraints.end(),
removedConstraints.begin(), removedConstraints.end(),
std::inserter(intersects, intersects.begin()));
llvm::set_subtract(addedConstraints, intersects);
llvm::set_subtract(removedConstraints, intersects);
// Print out Changes.
PrintOptions PO;
PO.PrintTypesForDebugging = true;
out.indent(indent);
out << "(Changes:\n";
if (!tvWithboundTypes.empty()) {
out.indent(indent + 2);
out << "(Newly Bound: \n";
for (const auto &tvWithType : tvWithboundTypes) {
out.indent(indent + 4);
out << "> $T" << tvWithType.first->getImpl().getID() << " := ";
tvWithType.second->print(out, PO);
out << '\n';
}
out.indent(indent + 2);
out << ")\n";
}
if (!addedTypeVars.empty()) {
out.indent(indent + 2);
auto heading = (addedTypeVars.size() > 1) ? "(New Type Variables: \n"
: "(New Type Variable: \n";
out << heading;
for (const auto &typeVar : addedTypeVars) {
out.indent(indent + 4);
out << "> $T" << typeVar->getImpl().getID();
out << '\n';
}
out.indent(indent + 2);
out << ")\n";
}
if (!equivTypeVars.empty()) {
out.indent(indent + 2);
auto heading = (equivTypeVars.size() > 1) ? "(New Equivalences: \n"
: "(New Equivalence: \n";
out << heading;
for (const auto &typeVar : equivTypeVars) {
out.indent(indent + 4);
out << "> $T" << typeVar->getImpl().getID();
out << '\n';
}
out.indent(indent + 2);
out << ")\n";
}
if (!addedConstraints.empty()) {
out.indent(indent + 2);
auto heading = (addedConstraints.size() > 1) ? "(Added Constraints: \n"
: "(Added Constraint: \n";
out << heading;
for (const auto &constraint : addedConstraints) {
out.indent(indent + 4);
out << "> ";
constraint->print(out, &CS.getASTContext().SourceMgr);
out << '\n';
}
out.indent(indent + 2);
out << ")\n";
}
if (!removedConstraints.empty()) {
out.indent(indent + 2);
auto heading = (removedConstraints.size() > 1) ? "(Removed Constraints: \n"
: "(Removed Constraint: \n";
out << heading;
for (const auto &constraint : removedConstraints) {
out.indent(indent + 4);
out << "> ";
constraint->print(out, &CS.getASTContext().SourceMgr);
out << '\n';
}
out.indent(indent + 2);
out << ")\n";
}
out.indent(indent);
out << ")\n";
}
void ConstraintGraph::printConnectedComponents( void ConstraintGraph::printConnectedComponents(
ArrayRef<TypeVariableType *> typeVars, ArrayRef<TypeVariableType *> typeVars,
llvm::raw_ostream &out) { llvm::raw_ostream &out) {