Skip to content

Sema: Optimize ConstraintGraph::computeConnectedComponents() #78278

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 3 commits into from
Dec 19, 2024
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: 0 additions & 11 deletions include/swift/Sema/ConstraintGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,6 @@ class ConstraintGraph {
llvm::function_ref<bool(Constraint *)> acceptConstraint =
[](Constraint *constraint) { return true; });

/// Retrieve the type variables that correspond to nodes in the graph.
///
/// The subscript operator can be used to retrieve the nodes that
/// correspond to these type variables.
ArrayRef<TypeVariableType *> getTypeVariables() const {
return TypeVariables;
}

/// Describes a single component, as produced by the connected components
/// algorithm.
struct Component {
Expand Down Expand Up @@ -462,9 +454,6 @@ class ConstraintGraph {
/// The constraint system.
ConstraintSystem &CS;

/// The type variables in this graph, in stable order.
std::vector<TypeVariableType *> TypeVariables;

/// Constraints that are "orphaned" because they contain no type variables.
SmallVector<Constraint *, 4> OrphanedConstraints;

Expand Down
69 changes: 46 additions & 23 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "swift/Sema/SolutionResult.h"
#include "swift/Sema/SyntacticElementTarget.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetOperations.h"
Expand Down Expand Up @@ -339,21 +340,17 @@ class TypeVariableType::Implementation {
/// The corresponding node in the constraint graph.
constraints::ConstraintGraphNode *GraphNode = nullptr;

/// Index into the list of type variables, as used by the
/// constraint graph.
unsigned GraphIndex;
/// Temporary state for ConstraintGraph::computeConnectedComponents(),
/// stored inline for performance.
llvm::PointerIntPair<TypeVariableType *, 1, unsigned> Component;

friend class constraints::SolverTrail;

public:
/// Retrieve the type variable associated with this implementation.
TypeVariableType *getTypeVariable() {
return reinterpret_cast<TypeVariableType *>(this) - 1;
}

/// Retrieve the type variable associated with this implementation.
const TypeVariableType *getTypeVariable() const {
return reinterpret_cast<const TypeVariableType *>(this) - 1;
TypeVariableType *getTypeVariable() const {
return reinterpret_cast<TypeVariableType *>(
const_cast<Implementation *>(this)) - 1;
}

explicit Implementation(constraints::ConstraintLocator *locator,
Expand Down Expand Up @@ -406,17 +403,6 @@ class TypeVariableType::Implementation {
void setGraphNode(constraints::ConstraintGraphNode *newNode) {
GraphNode = newNode;
}

/// Retrieve the index into the constraint graph's list of type variables.
unsigned getGraphIndex() const {
assert(GraphNode && "Graph node isn't set");
return GraphIndex;
}

/// Set the index into the constraint graph's list of type variables.
void setGraphIndex(unsigned newIndex) {
GraphIndex = newIndex;
}

/// Check whether this type variable either has a representative that
/// is not itself or has a fixed type binding.
Expand All @@ -430,6 +416,12 @@ class TypeVariableType::Implementation {
return ParentOrFixed.get<TypeVariableType *>() != getTypeVariable();
}

/// Low-level accessor; use getRepresentative() or getFixedType() instead.
llvm::PointerUnion<TypeVariableType *, TypeBase *>
getRepresentativeOrFixed() const {
return ParentOrFixed;
}

/// Record the current type-variable binding.
void recordBinding(constraints::SolverTrail &trail) {
trail.recordChange(constraints::SolverTrail::Change::UpdatedTypeVariable(
Expand Down Expand Up @@ -647,6 +639,37 @@ class TypeVariableType::Implementation {
impl.getTypeVariable()->Bits.TypeVariableType.Options |= TVO_CanBindToHole;
}

void setComponent(TypeVariableType *parent) {
Component.setPointerAndInt(parent, /*valid=*/false);
}

TypeVariableType *getComponent() const {
auto *rep = getTypeVariable();
while (rep != rep->getImpl().Component.getPointer())
rep = rep->getImpl().Component.getPointer();

// Path compression
if (rep != getTypeVariable()) {
const_cast<TypeVariableType::Implementation *>(this)
->Component.setPointer(rep);
}

return rep;
}

bool isValidComponent() const {
ASSERT(Component.getPointer() == getTypeVariable());
return Component.getInt();
}

bool markValidComponent() {
if (Component.getInt())
return false;
ASSERT(Component.getPointer() == getTypeVariable());
Component.setInt(1);
return true;
}

/// Print the type variable to the given output stream.
void print(llvm::raw_ostream &OS);

Expand Down Expand Up @@ -2339,7 +2362,7 @@ class ConstraintSystem {
ConstraintList InactiveConstraints;

/// The constraint graph.
ConstraintGraph &CG;
ConstraintGraph CG;

/// A mapping from constraint locators to the set of opened types associated
/// with that locator.
Expand Down Expand Up @@ -2707,7 +2730,7 @@ class ConstraintSystem {
~ConstraintSystem();

/// Retrieve the constraint graph associated with this constraint system.
ConstraintGraph &getConstraintGraph() const { return CG; }
ConstraintGraph &getConstraintGraph() { return CG; }

/// Retrieve the AST context.
ASTContext &getASTContext() const { return Context; }
Expand Down
Loading