Skip to content

Commit a36d1f8

Browse files
gmittertDougGregor
authored andcommitted
Reduce the Stack Size of ConstraintSystem
The ConstraintSystem class is on the order of 1000s of bytes in size on the stacka nd is causing issues with dispatch's 64k stack limit. This changes most Small data types which store data on the stack to non small heap based data types.
1 parent 751eabe commit a36d1f8

File tree

7 files changed

+34
-27
lines changed

7 files changed

+34
-27
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
245245
}
246246

247247
// Register the defaulted type variables.
248-
DefaultedConstraints.append(solution.DefaultedConstraints.begin(),
248+
DefaultedConstraints.insert(DefaultedConstraints.end(),
249+
solution.DefaultedConstraints.begin(),
249250
solution.DefaultedConstraints.end());
250251

251252
// Add the node types back.
@@ -339,6 +340,12 @@ bool ConstraintSystem::simplify(bool ContinueAfterFailures) {
339340

340341
namespace {
341342

343+
template<typename T>
344+
void truncate(std::vector<T> &vec, unsigned newSize) {
345+
assert(newSize <= vec.size() && "Not a truncation!");
346+
vec.erase(vec.begin() + newSize, vec.end());
347+
}
348+
342349
/// Truncate the given small vector to the given new size.
343350
template<typename T>
344351
void truncate(SmallVectorImpl<T> &vec, unsigned newSize) {

lib/Sema/CSStep.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ void SplitterStep::computeFollowupSteps(
9999
// FIXME: We're seeding typeVars with TypeVariables so that the
100100
// connected-components algorithm only considers those type variables within
101101
// our component. There are clearly better ways to do this.
102-
SmallVector<TypeVariableType *, 16> typeVars(CS.TypeVariables);
103-
SmallVector<unsigned, 16> components;
102+
std::vector<TypeVariableType *> typeVars(CS.TypeVariables);
103+
std::vector<unsigned> components;
104104
unsigned numComponents = CG.computeConnectedComponents(typeVars, components);
105105
if (numComponents < 2) {
106106
componentSteps.push_back(llvm::make_unique<ComponentStep>(

lib/Sema/CSStep.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class ComponentStep final : public SolverStep {
291291
ConstraintSystem &CS;
292292
ConstraintSystem::SolverScope *SolverScope;
293293

294-
SmallVector<TypeVariableType *, 16> TypeVars;
294+
std::vector<TypeVariableType *> TypeVars;
295295
ConstraintSystem::SolverScope *PrevPartialScope = nullptr;
296296

297297
// The component this scope is associated with.
@@ -336,7 +336,7 @@ class ComponentStep final : public SolverStep {
336336
std::unique_ptr<Scope> ComponentScope = nullptr;
337337

338338
/// Type variables and constraints "in scope" of this step.
339-
SmallVector<TypeVariableType *, 16> TypeVars;
339+
std::vector<TypeVariableType *> TypeVars;
340340
/// Constraints "in scope" of this step.
341341
ConstraintList *Constraints;
342342

lib/Sema/ConstraintGraph.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ void ConstraintGraph::gatherConstraints(
528528
static void connectedComponentsDFS(ConstraintGraph &cg,
529529
ConstraintGraphNode &node,
530530
unsigned component,
531-
SmallVectorImpl<unsigned> &components) {
531+
std::vector<unsigned> &components) {
532532
// Local function that recurses on the given set of type variables.
533533
auto visitAdjacencies = [&](ArrayRef<TypeVariableType *> typeVars) {
534534
for (auto adj : typeVars) {
@@ -562,8 +562,8 @@ static void connectedComponentsDFS(ConstraintGraph &cg,
562562
}
563563

564564
unsigned ConstraintGraph::computeConnectedComponents(
565-
SmallVectorImpl<TypeVariableType *> &typeVars,
566-
SmallVectorImpl<unsigned> &components) {
565+
std::vector<TypeVariableType *> &typeVars,
566+
std::vector<unsigned> &components) {
567567
// Track those type variables that the caller cares about.
568568
llvm::SmallPtrSet<TypeVariableType *, 4> typeVarSubset(typeVars.begin(),
569569
typeVars.end());
@@ -871,9 +871,9 @@ void ConstraintGraph::dump() {
871871
}
872872

873873
void ConstraintGraph::printConnectedComponents(llvm::raw_ostream &out) {
874-
SmallVector<TypeVariableType *, 16> typeVars;
875-
typeVars.append(TypeVariables.begin(), TypeVariables.end());
876-
SmallVector<unsigned, 16> components;
874+
std::vector<TypeVariableType *> typeVars;
875+
typeVars.insert(typeVars.end(), TypeVariables.begin(), TypeVariables.end());
876+
std::vector<unsigned> components;
877877
unsigned numComponents = computeConnectedComponents(typeVars, components);
878878
for (unsigned component = 0; component != numComponents; ++component) {
879879
out.indent(2);

lib/Sema/ConstraintGraph.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ class ConstraintGraph {
249249
/// one component for each of the constraints produced by
250250
/// \c getOrphanedConstraints().
251251
unsigned computeConnectedComponents(
252-
SmallVectorImpl<TypeVariableType *> &typeVars,
253-
SmallVectorImpl<unsigned> &components);
252+
std::vector<TypeVariableType *> &typeVars,
253+
std::vector<unsigned> &components);
254254

255255
/// Retrieve the set of "orphaned" constraints, which are known to the
256256
/// constraint graph but have no type variables to anchor them.

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,7 @@ size_t Solution::getTotalMemory() const {
23292329
llvm::capacity_in_bytes(Fixes) + DisjunctionChoices.getMemorySize() +
23302330
OpenedTypes.getMemorySize() + OpenedExistentialTypes.getMemorySize() +
23312331
(DefaultedConstraints.size() * sizeof(void *)) +
2332-
llvm::capacity_in_bytes(Conformances);
2332+
Conformances.size() * sizeof(std::pair<ConstraintLocator *, ProtocolConformanceRef>);
23332333
}
23342334

23352335
DeclName OverloadChoice::getName() const {

lib/Sema/ConstraintSystem.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,14 @@ class Solution {
591591
ConstraintSystem &getConstraintSystem() const { return *constraintSystem; }
592592

593593
/// The set of type bindings.
594-
llvm::SmallDenseMap<TypeVariableType *, Type> typeBindings;
594+
llvm::DenseMap<TypeVariableType *, Type> typeBindings;
595595

596596
/// The set of overload choices along with their types.
597-
llvm::SmallDenseMap<ConstraintLocator *, SelectedOverload> overloadChoices;
597+
llvm::DenseMap<ConstraintLocator *, SelectedOverload> overloadChoices;
598598

599599
/// The set of constraint restrictions used to arrive at this restriction,
600600
/// which informs constraint application.
601-
llvm::SmallDenseMap<std::pair<CanType, CanType>, ConversionRestrictionKind>
601+
llvm::DenseMap<std::pair<CanType, CanType>, ConversionRestrictionKind>
602602
ConstraintRestrictions;
603603

604604
/// The list of fixes that need to be applied to the initial expression
@@ -610,22 +610,22 @@ class Solution {
610610

611611
/// The set of disjunction choices used to arrive at this solution,
612612
/// which informs constraint application.
613-
llvm::SmallDenseMap<ConstraintLocator *, unsigned> DisjunctionChoices;
613+
llvm::DenseMap<ConstraintLocator *, unsigned> DisjunctionChoices;
614614

615615
/// The set of opened types for a given locator.
616-
llvm::SmallDenseMap<ConstraintLocator *, ArrayRef<OpenedType>> OpenedTypes;
616+
llvm::DenseMap<ConstraintLocator *, ArrayRef<OpenedType>> OpenedTypes;
617617

618618
/// The opened existential type for a given locator.
619-
llvm::SmallDenseMap<ConstraintLocator *, OpenedArchetypeType *>
619+
llvm::DenseMap<ConstraintLocator *, OpenedArchetypeType *>
620620
OpenedExistentialTypes;
621621

622622
/// The locators of \c Defaultable constraints whose defaults were used.
623-
llvm::SmallPtrSet<ConstraintLocator *, 8> DefaultedConstraints;
623+
llvm::SmallPtrSet<ConstraintLocator *, 2> DefaultedConstraints;
624624

625625
/// The node -> type mappings introduced by this solution.
626626
llvm::SmallVector<std::pair<TypedNode, Type>, 8> addedNodeTypes;
627627

628-
llvm::SmallVector<std::pair<ConstraintLocator *, ProtocolConformanceRef>, 8>
628+
std::vector<std::pair<ConstraintLocator *, ProtocolConformanceRef>>
629629
Conformances;
630630

631631
/// The set of closures that have been transformed by a function builder.
@@ -1042,7 +1042,7 @@ class ConstraintSystem {
10421042
/// solution it represents.
10431043
Score CurrentScore;
10441044

1045-
SmallVector<TypeVariableType *, 16> TypeVariables;
1045+
std::vector<TypeVariableType *> TypeVariables;
10461046

10471047
/// Maps expressions to types for choosing a favored overload
10481048
/// type in a disjunction constraint.
@@ -1075,7 +1075,7 @@ class ConstraintSystem {
10751075
/// there are multiple ways in which one type could convert to another, e.g.,
10761076
/// given class types A and B, the solver might choose either a superclass
10771077
/// conversion or a user-defined conversion.
1078-
SmallVector<std::tuple<Type, Type, ConversionRestrictionKind>, 32>
1078+
std::vector<std::tuple<Type, Type, ConversionRestrictionKind>>
10791079
ConstraintRestrictions;
10801080

10811081
/// The set of fixes applied to make the solution work.
@@ -1085,7 +1085,7 @@ class ConstraintSystem {
10851085

10861086
/// The set of remembered disjunction choices used to reach
10871087
/// the current constraint system.
1088-
SmallVector<std::pair<ConstraintLocator*, unsigned>, 32>
1088+
std::vector<std::pair<ConstraintLocator*, unsigned>>
10891089
DisjunctionChoices;
10901090

10911091
/// The worklist of "active" constraints that should be revisited
@@ -1112,7 +1112,7 @@ class ConstraintSystem {
11121112
/// The node -> type mappings introduced by generating constraints.
11131113
llvm::SmallVector<std::pair<TypedNode, Type>, 8> addedNodeTypes;
11141114

1115-
SmallVector<std::pair<ConstraintLocator *, ProtocolConformanceRef>, 8>
1115+
std::vector<std::pair<ConstraintLocator *, ProtocolConformanceRef>>
11161116
CheckedConformances;
11171117

11181118
/// The set of closures that have been transformed by a function builder.
@@ -1121,7 +1121,7 @@ class ConstraintSystem {
11211121

11221122
public:
11231123
/// The locators of \c Defaultable constraints whose defaults were used.
1124-
SmallVector<ConstraintLocator *, 8> DefaultedConstraints;
1124+
std::vector<ConstraintLocator *> DefaultedConstraints;
11251125

11261126
/// A cache that stores the @dynamicCallable required methods implemented by
11271127
/// types.

0 commit comments

Comments
 (0)