From 0ced799878d1beb8f0fa1cc31fa6d2e4229c217c Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 4 Oct 2011 00:21:21 +0000 Subject: [PATCH] When build a module on demand, run the module-building job on a separate thread with the "suitably large" stack, so we don't blow the stack when building modules recursively. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141051 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/CompilerInstance.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp index 27fe3f15ff..c2cbeacc02 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -39,6 +39,7 @@ #include "llvm/Support/Program.h" #include "llvm/Support/Signals.h" #include "llvm/Support/system_error.h" +#include "llvm/Support/CrashRecoveryContext.h" #include "llvm/Config/config.h" using namespace clang; @@ -655,6 +656,20 @@ static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { return LangOpts.CPlusPlus? IK_CXX : IK_C; } +namespace { + struct CompileModuleData { + CompilerInstance &Instance; + GeneratePCHAction &CreateModuleAction; + }; +} + +/// \brief Helper function that executes the module-generating action under +/// a crash recovery context. +static void doCompileModule(void *UserData) { + CompileModuleData &Data = *reinterpret_cast(UserData); + Data.Instance.ExecuteAction(Data.CreateModuleAction); +} + /// \brief Compile a module file for the given module name with the given /// umbrella header, using the options provided by the importing compiler /// instance. @@ -703,9 +718,12 @@ static void compileModule(CompilerInstance &ImportingInstance, // Construct a module-generating action. GeneratePCHAction CreateModuleAction(true); - // Execute the action to actually build the module in-place. - // FIXME: Need to synchronize when multiple processes do this. - Instance.ExecuteAction(CreateModuleAction); + // Execute the action to actually build the module in-place. Use a separate + // thread so that we get a stack large enough. + const unsigned ThreadStackSize = 8 << 20; + llvm::CrashRecoveryContext CRC; + CompileModuleData Data = { Instance, CreateModuleAction }; + CRC.RunSafelyOnThread(&doCompileModule, &Data, ThreadStackSize); } ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, -- 2.40.0