From 51cd64a03e2aa520f3428ff6820e41fb688f6c01 Mon Sep 17 00:00:00 2001 From: Tzvetan Mikov Date: Fri, 22 Nov 2024 12:19:49 -0800 Subject: [PATCH] use the StackExecutor API for lazy compilation and debug eval Summary: Use the new API. For now this provides no benefit, but allows us to add a more efficient implementation later. Note that `eval()` still does not use any of these APIs and is thus subject to stack overflow. The main challenge is the sharing of the executor. Reviewed By: avp Differential Revision: D66343421 fbshipit-source-id: b473529cead70a60f2ad3813be112c297889cdae --- include/hermes/BCGen/HBC/BCProviderFromSrc.h | 9 +++--- lib/BCGen/HBC/HBC.cpp | 30 ++++---------------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/include/hermes/BCGen/HBC/BCProviderFromSrc.h b/include/hermes/BCGen/HBC/BCProviderFromSrc.h index 0b01758f9a2..1736370380d 100644 --- a/include/hermes/BCGen/HBC/BCProviderFromSrc.h +++ b/include/hermes/BCGen/HBC/BCProviderFromSrc.h @@ -10,7 +10,7 @@ #include "hermes/BCGen/HBC/BCProvider.h" #include "hermes/BCGen/HBC/Bytecode.h" -#include "hermes/Support/SerialExecutor.h" +#include "hermes/Support/StackExecutor.h" #include "llvh/ADT/Optional.h" @@ -109,7 +109,8 @@ class BCProviderFromSrc final : public BCProviderBase { std::chrono::milliseconds(1000); /// The executor used to run the compiler. - SerialExecutor serialExecutor_{kExecutorStackSize, kExecutorTimeout}; + std::shared_ptr stackExecutor_ = + newStackExecutor(kExecutorStackSize, kExecutorTimeout); /// The BytecodeModule that provides the bytecode data. /// Placed below CompilationData to ensure its destruction before the @@ -256,8 +257,8 @@ class BCProviderFromSrc final : public BCProviderBase { sourceHash_ = hash; }; - SerialExecutor &getSerialExecutor() { - return serialExecutor_; + StackExecutor &getStackExecutor() { + return *stackExecutor_; } static bool classof(const BCProviderBase *provider) { diff --git a/lib/BCGen/HBC/HBC.cpp b/lib/BCGen/HBC/HBC.cpp index 65a20709078..6e03f330406 100644 --- a/lib/BCGen/HBC/HBC.cpp +++ b/lib/BCGen/HBC/HBC.cpp @@ -17,8 +17,6 @@ #include "llvh/Support/raw_ostream.h" -#include - #define DEBUG_TYPE "hbc-backend" namespace hermes { @@ -67,21 +65,6 @@ std::unique_ptr generateBytecodeModuleForEval( namespace { -/// Execute a function in a SerialExecutor, blocking until the function -/// completes. -/// \param executor the executor to use. -/// \param f the function to execute. -/// \param data the data to pass to the function. -static void executeBlockingInSerialExecutor( - SerialExecutor &executor, - void (*f)(void *), - void *data) { - std::packaged_task task([f, data]() { f(data); }); - auto future = task.get_future(); - executor.add([&task]() { task(); }); - future.wait(); -} - /// Data for the compileLazyFunctionWorker. class LazyCompilationThreadData { public: @@ -359,11 +342,10 @@ std::pair compileLazyFunction( return {false, *errMsgOpt}; } - // Run on a thread to prevent stack overflow if this is run from deep inside - // JS execution. + // Run in a new stack to prevent stack overflow when deep inside JS execution. LazyCompilationThreadData data{provider, funcID}; - executeBlockingInSerialExecutor( - provider->getSerialExecutor(), compileLazyFunctionWorker, &data); + executeInStack( + provider->getStackExecutor(), &data, compileLazyFunctionWorker); if (data.success) { return std::make_pair(true, llvh::StringRef{}); @@ -424,11 +406,9 @@ std::pair, std::string> compileEvalModule( hbc::BCProviderFromSrc *provider, uint32_t enclosingFuncID, const CompileFlags &compileFlags) { - // Run on a thread to prevent stack overflow if this is run from deep inside - // JS execution. + // Run in a new stack to prevent stack overflow when deep inside JS execution. EvalThreadData data{std::move(src), provider, enclosingFuncID, compileFlags}; - executeBlockingInSerialExecutor( - provider->getSerialExecutor(), compileEvalWorker, &data); + executeInStack(provider->getStackExecutor(), &data, compileEvalWorker); return data.success ? std::make_pair(std::move(data.result), "")