Skip to content

Commit 3b00177

Browse files
authored
Merge pull request #40964 from rintaro/sourcekit-compile-cancellation
[SourceKit] Add cancellation points for 'compile' requests
2 parents f5e5a07 + d91ec5a commit 3b00177

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ class CompilerInstance {
676676
/// If \p fn returns true, exits early and returns true.
677677
bool forEachFileToTypeCheck(llvm::function_ref<bool(SourceFile &)> fn);
678678

679+
/// Whether the cancellation of the current operation has been requested.
680+
bool isCancellationRequested() const;
681+
679682
private:
680683
/// Compute the parsing options for a source file in the main module.
681684
SourceFile::ParsingOptions getSourceFileParsingOptions(bool forPrimary) const;

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,10 @@ bool CompilerInstance::performSILProcessing(SILModule *silModule) {
13681368
return false;
13691369
}
13701370

1371+
bool CompilerInstance::isCancellationRequested() const {
1372+
auto flag = getASTContext().CancellationFlag;
1373+
return flag && flag->load(std::memory_order_relaxed);
1374+
}
13711375

13721376
const PrimarySpecificPaths &
13731377
CompilerInstance::getPrimarySpecificPathsForWholeModuleOptimizationMode()

lib/FrontendTool/FrontendTool.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,10 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
15561556
if (observer)
15571557
observer->performedSILGeneration(*SM);
15581558

1559+
// Cancellation check after SILGen.
1560+
if (Instance.isCancellationRequested())
1561+
return true;
1562+
15591563
auto *Stats = Instance.getASTContext().Stats;
15601564
if (Stats)
15611565
countStatsPostSILGen(*Stats, *SM);
@@ -1629,6 +1633,10 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
16291633
if (observer)
16301634
observer->performedSILProcessing(*SM);
16311635

1636+
// Cancellation check after SILOptimzation.
1637+
if (Instance.isCancellationRequested())
1638+
return true;
1639+
16321640
if (PSPs.haveModuleSummaryOutputPath()) {
16331641
if (serializeModuleSummary(SM.get(), PSPs, Context)) {
16341642
return true;
@@ -1663,6 +1671,10 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
16631671

16641672
runSILLoweringPasses(*SM);
16651673

1674+
// Cancellation check after SILLowering.
1675+
if (Instance.isCancellationRequested())
1676+
return true;
1677+
16661678
// TODO: at this point we need to flush any the _tracing and profiling_
16671679
// in the UnifiedStatsReporter, because the those subsystems of the USR
16681680
// retain _pointers into_ the SILModule, and the SILModule's lifecycle is
@@ -1690,6 +1702,10 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
16901702
IRGenOpts, Invocation.getTBDGenOptions(), std::move(SM), PSPs,
16911703
OutputFilename, MSF, HashGlobal, ParallelOutputFilenames);
16921704

1705+
// Cancellation check after IRGen.
1706+
if (Instance.isCancellationRequested())
1707+
return true;
1708+
16931709
// If no IRModule is available, bail. This can either happen if IR generation
16941710
// fails, or if parallelIRGen happened correctly (in which case it would have
16951711
// already performed LLVM).

lib/IDE/CompileInstance.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,18 @@ bool CompileInstance::performCompile(
329329
DiagnosticConsumer *DiagC,
330330
std::shared_ptr<std::atomic<bool>> CancellationFlag) {
331331

332+
// Cancellation check. This gives a chance to cancel queued up requests before
333+
// processing anything.
334+
if (CancellationFlag && CancellationFlag->load(std::memory_order_relaxed))
335+
return true;
336+
332337
performSema(Args, fileSystem, DiagC, CancellationFlag);
333338
if (CI->getDiags().hadAnyError())
334339
return true;
335340

336-
// TODO: Cancellation check.
341+
// Cancellation check after Sema.
342+
if (CI->isCancellationRequested())
343+
return true;
337344

338345
int ReturnValue = 0;
339346
return performCompileStepsPostSema(*CI, ReturnValue, /*observer=*/nullptr);

0 commit comments

Comments
 (0)