diff --git a/llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp b/llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp --- a/llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp +++ b/llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp @@ -112,12 +112,9 @@ MainJD.addGenerator(std::move(ProcessSymbolsGenerator)); this->CODLayer.setImplMap(&Imps); this->ES->setDispatchMaterialization( - - [this](JITDylib &JD, std::unique_ptr MU) { - // FIXME: Switch to move capture once we have C++14. - auto SharedMU = std::shared_ptr(std::move(MU)); - auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); }; - CompileThreads.async(std::move(Work)); + [this](JITDylib &JD, std::shared_ptr MU) { + CompileThreads.async( + [MU = std::move(MU), &JD]() { MU->doMaterialize(JD); }); }); ExitOnErr(S.addSpeculationRuntime(MainJD, Mangle)); LocalCXXRuntimeOverrides CXXRuntimeoverrides; diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Core.h b/llvm/include/llvm/ExecutionEngine/Orc/Core.h --- a/llvm/include/llvm/ExecutionEngine/Orc/Core.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Core.h @@ -1060,7 +1060,7 @@ /// For dispatching MaterializationUnit::materialize calls. using DispatchMaterializationFunction = std::function MU)>; + JITDylib &JD, std::shared_ptr MU)>; /// Construct an ExecutionSession. /// @@ -1112,7 +1112,8 @@ /// Unhandled errors can be sent here to log them. void reportError(Error Err) { ReportError(std::move(Err)); } - /// Set the materialization dispatch function. + /// Set a materialization dispatch function. It can be used to delegate + /// compilation to different threads. ExecutionSession &setDispatchMaterialization( DispatchMaterializationFunction DispatchMaterialization) { this->DispatchMaterialization = std::move(DispatchMaterialization); @@ -1195,7 +1196,11 @@ dbgs() << "Dispatching " << *MU << " for " << JD.getName() << "\n"; }); }); - DispatchMaterialization(JD, std::move(MU)); + if (DispatchMaterialization) { + DispatchMaterialization(JD, std::move(MU)); + } else { + MU->doMaterialize(JD); + } } /// Dump the state of all the JITDylibs in this session. @@ -1206,20 +1211,13 @@ logAllUnhandledErrors(std::move(Err), errs(), "JIT session error: "); } - static void - materializeOnCurrentThread(JITDylib &JD, - std::unique_ptr MU) { - MU->doMaterialize(JD); - } - void runOutstandingMUs(); mutable std::recursive_mutex SessionMutex; std::shared_ptr SSP; VModuleKey LastKey = 0; ErrorReporter ReportError = logErrorsToStdErr; - DispatchMaterializationFunction DispatchMaterialization = - materializeOnCurrentThread; + DispatchMaterializationFunction DispatchMaterialization = nullptr; std::vector> JDs; diff --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp --- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp +++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp @@ -155,11 +155,9 @@ CompileLayer->setCloneToNewContextOnEmit(true); CompileThreads = std::make_unique(S.NumCompileThreads); ES->setDispatchMaterialization( - [this](JITDylib &JD, std::unique_ptr MU) { - // FIXME: Switch to move capture once we have c++14. - auto SharedMU = std::shared_ptr(std::move(MU)); - auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); }; - CompileThreads->async(std::move(Work)); + [this](JITDylib &JD, std::shared_ptr MU) { + CompileThreads->async( + [MU = std::move(MU), &JD]() { MU->doMaterialize(JD); }); }); } } diff --git a/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp b/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp --- a/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp @@ -948,7 +948,7 @@ TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) { bool ExpectNoMoreMaterialization = false; ES.setDispatchMaterialization( - [&](JITDylib &JD, std::unique_ptr MU) { + [&](JITDylib &JD, std::shared_ptr MU) { if (ExpectNoMoreMaterialization) ADD_FAILURE() << "Unexpected materialization"; MU->doMaterialize(JD); @@ -1126,7 +1126,7 @@ std::thread MaterializationThread; ES.setDispatchMaterialization( - [&](JITDylib &JD, std::unique_ptr MU) { + [&](JITDylib &JD, std::shared_ptr MU) { MaterializationThread = std::thread([MU = std::move(MU), &JD] { MU->doMaterialize(JD); }); });