Skip to content

Commit e60afb3

Browse files
committed
[ConstraintSystem] Augment ExpressionTimer to carry the threshold
Instead of asking callers of `isExpired` to provide the threshold, let's ask for that upfront. This change also allows us to check how much time remains in the timer and build timers with different thresholds without having to safe that information somewhere else.
1 parent 2a1d539 commit e60afb3

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,18 @@ class ExpressionTimer {
171171
ASTContext &Context;
172172
llvm::TimeRecord StartTime;
173173

174+
/// The number of milliseconds from creation until
175+
/// this timer is considered expired.
176+
unsigned ThresholdInMillis;
177+
174178
bool PrintDebugTiming;
175179
bool PrintWarning;
176180

177181
public:
182+
/// This constructor sets a default threshold defined for all expressions
183+
/// via compiler flag `solver-expression-time-threshold`.
178184
ExpressionTimer(Expr *E, ConstraintSystem &CS);
185+
ExpressionTimer(Expr *E, ConstraintSystem &CS, unsigned thresholdInMillis);
179186

180187
~ExpressionTimer();
181188

@@ -196,9 +203,9 @@ class ExpressionTimer {
196203
// than the warning threshold.
197204
void disableWarning() { PrintWarning = false; }
198205

199-
bool isExpired(unsigned thresholdInMillis) const {
206+
bool isExpired() const {
200207
auto elapsed = getElapsedProcessTimeInFractionalSeconds();
201-
return unsigned(elapsed) > thresholdInMillis;
208+
return unsigned(elapsed) > ThresholdInMillis;
202209
}
203210
};
204211

@@ -5226,9 +5233,7 @@ class ConstraintSystem {
52265233
return isExpressionAlreadyTooComplex= true;
52275234
}
52285235

5229-
const auto timeoutThresholdInMillis =
5230-
getASTContext().TypeCheckerOpts.ExpressionTimeoutThreshold;
5231-
if (Timer && Timer->isExpired(timeoutThresholdInMillis)) {
5236+
if (Timer && Timer->isExpired()) {
52325237
// Disable warnings about expressions that go over the warning
52335238
// threshold since we're arbitrarily ending evaluation and
52345239
// emitting an error.

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,17 @@ using namespace inference;
4444
#define DEBUG_TYPE "ConstraintSystem"
4545

4646
ExpressionTimer::ExpressionTimer(Expr *E, ConstraintSystem &CS)
47-
: E(E),
48-
Context(CS.getASTContext()),
47+
: ExpressionTimer(
48+
E, CS,
49+
CS.getASTContext().TypeCheckerOpts.ExpressionTimeoutThreshold) {}
50+
51+
ExpressionTimer::ExpressionTimer(Expr *E, ConstraintSystem &CS,
52+
unsigned thresholdInMillis)
53+
: E(E), Context(CS.getASTContext()),
4954
StartTime(llvm::TimeRecord::getCurrentTime()),
55+
ThresholdInMillis(thresholdInMillis),
5056
PrintDebugTiming(CS.getASTContext().TypeCheckerOpts.DebugTimeExpressions),
51-
PrintWarning(true) {
52-
}
57+
PrintWarning(true) {}
5358

5459
ExpressionTimer::~ExpressionTimer() {
5560
auto elapsed = getElapsedProcessTimeInFractionalSeconds();

0 commit comments

Comments
 (0)