Skip to content

[SourceKit] Support cancellation of requests while an AST is being built #38782

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 2 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,12 @@ namespace swift {
/// than this many seconds.
unsigned ExpressionTimeoutThreshold = 600;

/// If the shared pointer is not a \c nullptr and the pointee is \c true,
/// typechecking should be aborted at the next possible opportunity.
/// This is used by SourceKit to cancel requests for which the result is no
/// longer of interest.
std::shared_ptr<std::atomic<bool>> CancellationFlag = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xedin or @hborla any concerns about this mutable state living in TypeCheckerOptions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have concerns but I've also never touched this thing 🙂 @CodaFi do you have any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s a fine place to put it to start. I just hope we move off a cooperative cancellation model and push this into the request evaluator at some point.


/// If non-zero, abort the switch statement exhaustiveness checker if
/// the Space::minus function is called more than this many times.
///
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -5155,6 +5155,10 @@ class ConstraintSystem {
if (isExpressionAlreadyTooComplex)
return true;

auto CancellationFlag = getASTContext().TypeCheckerOpts.CancellationFlag;
if (CancellationFlag && CancellationFlag->load(std::memory_order_relaxed))
return true;

auto used = getASTContext().getSolverMemory() + solutionMemory;
MaxMemory = std::max(used, MaxMemory);
auto threshold = getASTContext().TypeCheckerOpts.SolverMemoryThreshold;
Expand Down
15 changes: 13 additions & 2 deletions test/SourceKit/Misc/stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func foo() {}
// SEMA_3: 1 {{.*}} source.statistic.num-ast-builds
// SEMA_3: 1 {{.*}} source.statistic.num-asts-in-memory
// SEMA_3: 1 {{.*}} source.statistic.max-asts-in-memory
// SEMA_3: 0 {{.*}} source.statistic.num-ast-cache-hits
// SEMA_3: 1 {{.*}} source.statistic.num-ast-snaphost-uses
// SEMA_3: 1 {{.*}} source.statistic.num-ast-cache-hits
// SEMA_3: 0 {{.*}} source.statistic.num-ast-snaphost-uses

// RUN: %sourcekitd-test -req=sema %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == -req=related-idents -pos=1:6 %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == -req=stats | %FileCheck %s -check-prefix=SEMA_4

Expand All @@ -58,3 +58,14 @@ func foo() {}
// SEMA_4: 1 {{.*}} source.statistic.max-asts-in-memory
// SEMA_4: 1 {{.*}} source.statistic.num-ast-cache-hits
// SEMA_4: 0 {{.*}} source.statistic.num-ast-snaphost-uses

// Test that we can have two files open and don't need to rebuild an AST when doing the cursor info request on '%s' after opening '10bytes.swift'
// RUN: %sourcekitd-test \
// RUN: -req=sema %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == \
// RUN: -req=sema %S/Inputs/10bytes.swift -- -Xfrontend -disable-implicit-concurrency-module-import %S/Inputs/10bytes.swift == \
// RUN: -req=cursor -pos=1:6 %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == \
// RUN: -req=stats | %FileCheck %s -check-prefix=OPEN_TWO_FILES

// OPEN_TWO_FILES: 2 {{.*}} source.statistic.num-ast-builds
// OPEN_TWO_FILES: 2 {{.*}} source.statistic.num-asts-in-memory
// OPEN_TWO_FILES: 2 {{.*}} source.statistic.max-asts-in-memory
Loading