Skip to content

Commit e51b72b

Browse files
committed
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 6e53336 commit e51b72b

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
@@ -229,7 +229,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
229229
}
230230

231231
// Register the defaulted type variables.
232-
DefaultedConstraints.append(solution.DefaultedConstraints.begin(),
232+
DefaultedConstraints.insert(DefaultedConstraints.end(),
233+
solution.DefaultedConstraints.begin(),
233234
solution.DefaultedConstraints.end());
234235

235236
// Register the conformances checked along the way to arrive to solution.
@@ -310,6 +311,12 @@ bool ConstraintSystem::simplify(bool ContinueAfterFailures) {
310311

311312
namespace {
312313

314+
template<typename T>
315+
void truncate(std::vector<T> &vec, unsigned newSize) {
316+
assert(newSize <= vec.size() && "Not a truncation!");
317+
vec.erase(vec.begin() + newSize, vec.end());
318+
}
319+
313320
/// Truncate the given small vector to the given new size.
314321
template<typename T>
315322
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
@@ -2254,7 +2254,7 @@ size_t Solution::getTotalMemory() const {
22542254
llvm::capacity_in_bytes(Fixes) + DisjunctionChoices.getMemorySize() +
22552255
OpenedTypes.getMemorySize() + OpenedExistentialTypes.getMemorySize() +
22562256
(DefaultedConstraints.size() * sizeof(void *)) +
2257-
llvm::capacity_in_bytes(Conformances);
2257+
Conformances.size() * sizeof(std::pair<ConstraintLocator *, ProtocolConformanceRef>);
22582258
}
22592259

22602260
DeclName OverloadChoice::getName() const {

lib/Sema/ConstraintSystem.h

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

611611
/// The set of type bindings.
612-
llvm::SmallDenseMap<TypeVariableType *, Type> typeBindings;
612+
llvm::DenseMap<TypeVariableType *, Type> typeBindings;
613613

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

617617
/// The set of constraint restrictions used to arrive at this restriction,
618618
/// which informs constraint application.
619-
llvm::SmallDenseMap<std::pair<CanType, CanType>, ConversionRestrictionKind>
619+
llvm::DenseMap<std::pair<CanType, CanType>, ConversionRestrictionKind>
620620
ConstraintRestrictions;
621621

622622
/// The list of fixes that need to be applied to the initial expression
@@ -628,19 +628,19 @@ class Solution {
628628

629629
/// The set of disjunction choices used to arrive at this solution,
630630
/// which informs constraint application.
631-
llvm::SmallDenseMap<ConstraintLocator *, unsigned> DisjunctionChoices;
631+
llvm::DenseMap<ConstraintLocator *, unsigned> DisjunctionChoices;
632632

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

636636
/// The opened existential type for a given locator.
637-
llvm::SmallDenseMap<ConstraintLocator *, OpenedArchetypeType *>
637+
llvm::DenseMap<ConstraintLocator *, OpenedArchetypeType *>
638638
OpenedExistentialTypes;
639639

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

643-
llvm::SmallVector<std::pair<ConstraintLocator *, ProtocolConformanceRef>, 8>
643+
std::vector<std::pair<ConstraintLocator *, ProtocolConformanceRef>>
644644
Conformances;
645645

646646
/// Simplify the given type by substituting all occurrences of
@@ -1055,7 +1055,7 @@ class ConstraintSystem {
10551055
/// solution it represents.
10561056
Score CurrentScore;
10571057

1058-
SmallVector<TypeVariableType *, 16> TypeVariables;
1058+
std::vector<TypeVariableType *> TypeVariables;
10591059

10601060
/// Maps expressions to types for choosing a favored overload
10611061
/// type in a disjunction constraint.
@@ -1090,7 +1090,7 @@ class ConstraintSystem {
10901090
/// there are multiple ways in which one type could convert to another, e.g.,
10911091
/// given class types A and B, the solver might choose either a superclass
10921092
/// conversion or a user-defined conversion.
1093-
SmallVector<std::tuple<Type, Type, ConversionRestrictionKind>, 32>
1093+
std::vector<std::tuple<Type, Type, ConversionRestrictionKind>>
10941094
ConstraintRestrictions;
10951095

10961096
/// The set of fixes applied to make the solution work.
@@ -1100,7 +1100,7 @@ class ConstraintSystem {
11001100

11011101
/// The set of remembered disjunction choices used to reach
11021102
/// the current constraint system.
1103-
SmallVector<std::pair<ConstraintLocator*, unsigned>, 32>
1103+
std::vector<std::pair<ConstraintLocator*, unsigned>>
11041104
DisjunctionChoices;
11051105

11061106
/// The worklist of "active" constraints that should be revisited
@@ -1124,12 +1124,12 @@ class ConstraintSystem {
11241124
SmallVector<std::pair<ConstraintLocator *, OpenedArchetypeType *>, 4>
11251125
OpenedExistentialTypes;
11261126

1127-
SmallVector<std::pair<ConstraintLocator *, ProtocolConformanceRef>, 8>
1127+
std::vector<std::pair<ConstraintLocator *, ProtocolConformanceRef>>
11281128
CheckedConformances;
11291129

11301130
public:
11311131
/// The locators of \c Defaultable constraints whose defaults were used.
1132-
SmallVector<ConstraintLocator *, 8> DefaultedConstraints;
1132+
std::vector<ConstraintLocator *> DefaultedConstraints;
11331133

11341134
/// A cache that stores the @dynamicCallable required methods implemented by
11351135
/// types.

0 commit comments

Comments
 (0)