From fa513db7edd3da714790c5509eb46a543cd8fb77 Mon Sep 17 00:00:00 2001 From: Gwen Mittertreiner Date: Mon, 3 Jun 2019 13:23:12 -0700 Subject: [PATCH] Run Sourcekitd-tests in a non dispatch thread Since Dispatch threads have a 64k stack size, tests were failing due to blowing the stack on Windows. This runs the tests in thread with a larger stack size. This fixes some 90ish sourcekit tests on Windows. --- .../lib/SwiftLang/SwiftASTManager.cpp | 11 ++++++- .../tools/sourcekitd-test/sourcekitd-test.cpp | 33 +++++++++++++------ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp b/tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp index 0c78d639e56c1..2537b837d7939 100644 --- a/tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp +++ b/tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp @@ -186,6 +186,15 @@ namespace SourceKit { void ASTUnit::Implementation::consumeAsync(SwiftASTConsumerRef ConsumerRef, ASTUnitRef ASTRef) { +#if defined(_WIN32) + // Windows uses more up for stack space (why?) than macOS/Linux which + // causes stack overflows in a dispatch thread with 64k stack. Passing + // useDeepStack=true means it's given a _beginthreadex thread with an 8MB + // stack. + bool useDeepStack = true; +#else + bool useDeepStack = false; +#endif Queue.dispatch([ASTRef, ConsumerRef]{ SwiftASTConsumer &ASTConsumer = *ConsumerRef; @@ -197,7 +206,7 @@ namespace SourceKit { LOG_WARN_FUNC("did not find primary SourceFile"); ConsumerRef->failed("did not find primary SourceFile"); } - }); + }, useDeepStack); } ASTUnit::ASTUnit(uint64_t Generation, std::shared_ptr Stats) diff --git a/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp b/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp index 4054778e4e46a..dd1a644691d5a 100644 --- a/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp +++ b/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp @@ -26,11 +26,12 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Regex.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/Threading.h" #include "llvm/Support/raw_ostream.h" #include #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) -#include #include +#include #elif defined(_WIN32) #define WIN32_LEAN_AND_MEAN #define NOMINMAX @@ -211,18 +212,26 @@ static void printBufferedNotifications(bool syncWithService = true) { }); } -static int skt_main(int argc, const char **argv); +struct skt_args { + int argc; + const char **argv; + int ret; +}; +static void skt_main(skt_args *args); int main(int argc, const char **argv) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ - int ret = skt_main(argc, argv); - exit(ret); + skt_args args = {argc, argv, 0}; + llvm::llvm_execute_on_thread((void (*)(void *))skt_main, &args); + exit(args.ret); }); dispatch_main(); } -static int skt_main(int argc, const char **argv) { +static void skt_main(skt_args *args) { + int argc = args->argc; + const char **argv = args->argv; llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); sourcekitd_initialize(); @@ -261,14 +270,16 @@ static int skt_main(int argc, const char **argv) { break; if (int ret = handleTestInvocation(Args.slice(0, i), InitOpts)) { sourcekitd_shutdown(); - return ret; + args->ret = ret; + return; } - Args = Args.slice(i+1); + Args = Args.slice(i + 1); } if (int ret = handleTestInvocation(Args, InitOpts)) { sourcekitd_shutdown(); - return ret; + args->ret = ret; + return; } for (auto &info : asyncResponses) { @@ -279,13 +290,15 @@ static int skt_main(int argc, const char **argv) { if (handleResponse(info.response, info.options, info.sourceFilename, std::move(info.sourceBuffer), nullptr)) { sourcekitd_shutdown(); - return 1; + args->ret = 1; + return; } } printBufferedNotifications(); sourcekitd_shutdown(); - return 0; + args->ret = 0; + return; } static inline const char *getInterfaceGenDocumentName() {