-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Unify ConstraintGraph change tracking with SavedTypeVariableBindings #76759
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4cc27c9
Sema: Factor out SolverTrail from ConstraintGraph
slavapestov 8ee4eee
Sema: Migrate SavedTypeVariableBindings to SolverTrail
slavapestov 105b6b3
Sema: Remove ConstraintGraphScope
slavapestov 0c53543
Sema: More general dumpActiveScopeChanges()
slavapestov cab6982
Sema: Fancier assertions in ConstraintGraph
slavapestov 4ef30a4
Sema: Recycle ConstraintGraphNode
slavapestov c0afe3f
Sema: Don't create new ConstraintGraphNode during active undo
slavapestov 1480339
Sema: Split off introduceToInference() into its own Change
slavapestov e70d0bc
Sema: Add missing newline in debug output
slavapestov 4548880
Sema: Add LLVM_DEBUGs to CSTrail.cpp
slavapestov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
//===--- CSTrail.h - Constraint Solver Trail --------------------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines the \c SolverTrail class, which records the decisions taken | ||
// while attempting to find a solution. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef SWIFT_SEMA_CSTRAIL_H | ||
#define SWIFT_SEMA_CSTRAIL_H | ||
|
||
#include <vector> | ||
|
||
namespace llvm { | ||
class raw_ostream; | ||
} | ||
|
||
namespace swift { | ||
|
||
class TypeBase; | ||
class TypeVariableType; | ||
|
||
namespace constraints { | ||
|
||
class Constraint; | ||
|
||
class SolverTrail { | ||
public: | ||
|
||
/// The kind of change made to the graph. | ||
enum class ChangeKind { | ||
/// Added a type variable to the constraint graph. | ||
AddedTypeVariable, | ||
/// Added a new constraint to the constraint graph. | ||
AddedConstraint, | ||
/// Removed an existing constraint from the constraint graph. | ||
RemovedConstraint, | ||
/// Extended the equivalence class of a type variable in the constraint graph. | ||
ExtendedEquivalenceClass, | ||
/// Added a fixed binding for a type variable in the constraint graph. | ||
BoundTypeVariable, | ||
/// Introduced a type variable's fixed type to inference. | ||
IntroducedToInference, | ||
/// Set the fixed type or parent and flags for a type variable. | ||
UpdatedTypeVariable, | ||
}; | ||
|
||
/// A change made to the constraint system. | ||
/// | ||
/// Each change can be undone (once, and in reverse order) by calling the | ||
/// undo() method. | ||
class Change { | ||
public: | ||
/// The kind of change. | ||
ChangeKind Kind; | ||
|
||
union { | ||
TypeVariableType *TypeVar; | ||
Constraint *TheConstraint; | ||
|
||
struct { | ||
/// The type variable whose equivalence class was extended. | ||
TypeVariableType *TypeVar; | ||
|
||
/// The previous size of the equivalence class. | ||
unsigned PrevSize; | ||
} EquivClass; | ||
|
||
struct { | ||
/// The type variable being bound to a fixed type. | ||
TypeVariableType *TypeVar; | ||
|
||
/// The fixed type to which the type variable was bound. | ||
TypeBase *FixedType; | ||
} Binding; | ||
|
||
struct { | ||
/// The type variable being updated. | ||
TypeVariableType *TypeVar; | ||
|
||
/// The representative of the equivalence class, or the fixed type. | ||
llvm::PointerUnion<TypeVariableType *, TypeBase *> ParentOrFixed; | ||
|
||
/// The saved value of TypeVariableType::Implementation::getRawOptions(). | ||
unsigned Options; | ||
} Update; | ||
}; | ||
|
||
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { } | ||
|
||
/// Create a change that added a type variable. | ||
static Change addedTypeVariable(TypeVariableType *typeVar); | ||
|
||
/// Create a change that added a constraint. | ||
static Change addedConstraint(Constraint *constraint); | ||
|
||
/// Create a change that removed a constraint. | ||
static Change removedConstraint(Constraint *constraint); | ||
|
||
/// Create a change that extended an equivalence class. | ||
static Change extendedEquivalenceClass(TypeVariableType *typeVar, | ||
unsigned prevSize); | ||
|
||
/// Create a change that bound a type variable to a fixed type. | ||
static Change boundTypeVariable(TypeVariableType *typeVar, Type fixed); | ||
|
||
/// Create a change that introduced a type variable to inference. | ||
static Change introducedToInference(TypeVariableType *typeVar, Type fixed); | ||
|
||
/// Create a change that updated a type variable. | ||
static Change updatedTypeVariable( | ||
TypeVariableType *typeVar, | ||
llvm::PointerUnion<TypeVariableType *, TypeBase *> parentOrFixed, | ||
unsigned options); | ||
|
||
/// Undo this change, reverting the constraint graph to the state it | ||
/// had prior to this change. | ||
/// | ||
/// Changes must be undone in stack order. | ||
void undo(ConstraintSystem &cs) const; | ||
|
||
void dump(llvm::raw_ostream &out, ConstraintSystem &cs, | ||
unsigned indent = 0) const; | ||
}; | ||
|
||
SolverTrail(ConstraintSystem &cs) : CS(cs) {} | ||
|
||
~SolverTrail(); | ||
|
||
SolverTrail(const SolverTrail &) = delete; | ||
SolverTrail &operator=(const SolverTrail &) = delete; | ||
|
||
bool isUndoActive() const { return UndoActive; } | ||
|
||
void recordChange(Change change); | ||
|
||
void dumpActiveScopeChanges(llvm::raw_ostream &out, | ||
unsigned fromIndex, | ||
unsigned indent = 0) const; | ||
|
||
unsigned size() const { | ||
return Changes.size(); | ||
} | ||
|
||
void undo(unsigned toIndex); | ||
|
||
private: | ||
ConstraintSystem &CS; | ||
|
||
/// The list of changes made to this constraint system. | ||
std::vector<Change> Changes; | ||
|
||
bool UndoActive = false; | ||
unsigned Total = 0; | ||
unsigned Max = 0; | ||
}; | ||
|
||
} // namespace constraints | ||
} // namespace swift | ||
|
||
#endif // SWIFT_SEMA_CSTRAIL_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's needed because eg this calls the default constructor and it won't synthesize one for us because of the unions:
If there's another way to initialize this without the default constructor I can refactor it later, because I'll be adding lots more Change kinds soon. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a similar pattern in
SyntacticElementTarget
, we just need a fee constructors. No need to change anything in this PR if you are going to follow up anyway!