Skip to content

Commit 3c9cb97

Browse files
committed
[ConstraintSystem] Add an option to disable the constraint solver perf hacks.
This is helpful in experimenting with constraint solver changes that might help us remove some of these unsound options. It's not ever mean to be enabled, but if we're able to remove the things guarded by the option we can eventually remove the option.
1 parent b8cb40b commit 3c9cb97

File tree

6 files changed

+27
-3
lines changed

6 files changed

+27
-3
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ namespace swift {
122122
/// completions.
123123
bool CodeCompleteCallPatternHeuristics = false;
124124

125+
/// Disable constraint system performance hacks.
126+
bool DisableConstraintSolverPerformanceHacks = false;
127+
125128
///
126129
/// Flags for use by tests
127130
///

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ def debug_time_function_bodies : Flag<["-"], "debug-time-function-bodies">,
183183
def debug_time_expression_type_checking : Flag<["-"], "debug-time-expression-type-checking">,
184184
HelpText<"Dumps the time it takes to type-check each expression">;
185185

186+
def disable_constraint_solver_performance_hacks : Flag<["-"], "disable-constraint-solver-performance-hacks">,
187+
HelpText<"Disable all the hacks in the constraint solver">;
188+
186189
def debug_assert_immediately : Flag<["-"], "debug-assert-immediately">,
187190
DebugCrashOpt, HelpText<"Force an assertion failure immediately">;
188191
def debug_assert_after_parse : Flag<["-"], "debug-assert-after-parse">,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
352352
Opts.UseDarwinPreStableABIBit = true;
353353
#endif
354354

355+
Opts.DisableConstraintSolverPerformanceHacks |=
356+
Args.hasArg(OPT_disable_constraint_solver_performance_hacks);
357+
355358
// Must be processed after any other language options that could affect
356359
// platform conditions.
357360
bool UnsupportedOS, UnsupportedArch;

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -3539,6 +3539,8 @@ Type ConstraintSystem::generateConstraints(Pattern *pattern,
35393539
}
35403540

35413541
void ConstraintSystem::optimizeConstraints(Expr *e) {
3542+
if (TC.getLangOpts().DisableConstraintSolverPerformanceHacks)
3543+
return;
35423544

35433545
SmallVector<Expr *, 16> linkedExprs;
35443546

lib/Sema/CSRanking.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
8686
}
8787

8888
bool ConstraintSystem::worseThanBestSolution() const {
89+
if (TC.getLangOpts().DisableConstraintSolverPerformanceHacks)
90+
return false;
91+
8992
if (retainAllSolutions())
9093
return false;
9194

lib/Sema/CSSolver.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ Solution ConstraintSystem::finalize(
129129

130130
// Update the best score we've seen so far.
131131
if (solverState && !retainAllSolutions()) {
132-
assert(!solverState->BestScore || CurrentScore <= *solverState->BestScore);
133-
solverState->BestScore = CurrentScore;
132+
assert(TC.getLangOpts().DisableConstraintSolverPerformanceHacks ||
133+
!solverState->BestScore || CurrentScore <= *solverState->BestScore);
134+
135+
if (!solverState->BestScore || CurrentScore <= *solverState->BestScore) {
136+
solverState->BestScore = CurrentScore;
137+
}
134138
}
135139

136140
for (auto tv : TypeVariables) {
@@ -1763,6 +1767,9 @@ static bool shortCircuitDisjunctionAt(Constraint *constraint,
17631767
Constraint *successfulConstraint,
17641768
ASTContext &ctx) {
17651769

1770+
if (ctx.LangOpts.DisableConstraintSolverPerformanceHacks)
1771+
return false;
1772+
17661773
// If the successfully applied constraint is favored, we'll consider that to
17671774
// be the "best".
17681775
if (successfulConstraint->isFavored() && !constraint->isFavored()) {
@@ -1832,6 +1839,9 @@ static bool shouldSkipDisjunctionChoice(ConstraintSystem &cs,
18321839
if (!cs.shouldAttemptFixes() && choice.isUnavailable())
18331840
return true;
18341841

1842+
if (cs.TC.getLangOpts().DisableConstraintSolverPerformanceHacks)
1843+
return false;
1844+
18351845
// Don't attempt to solve for generic operators if we already have
18361846
// a non-generic solution.
18371847

0 commit comments

Comments
 (0)