Skip to content

Commit

Permalink
FlowChecker: Use StackExecutor for cloning generic nodes
Browse files Browse the repository at this point in the history
Summary: This mitigates the problem of cloning deep in the stack.

Reviewed By: tmikov

Differential Revision: D66383991

fbshipit-source-id: 821724baa8722b8c4283df94645efe1eff11c4b7
  • Loading branch information
avp authored and facebook-github-bot committed Nov 26, 2024
1 parent f6779f9 commit ab480d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/Sema/FlowChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1949,19 +1949,21 @@ sema::Decl *FlowChecker::specializeGenericWithParsedTypes(
};

// Create specialization if it doesn't exist.
// TODO: Determine if the actual clone can be deferred to avoid potential
// stack overflow by cloning from deep in the AST.
if (doClone) {
LLVM_DEBUG(
llvh::dbgs() << "Creating specialization for: " << oldDecl->name.str()
<< "\n");
specialization = cloneNode(
astContext_,
semContext_,
declCollectorMap_,
generic.originalNode,
scope->parentFunction,
scope);
// Avoid stack overflow in the clone by executing in a separate stack.
executeInStack(
*stackExecutor_, [this, &generic, scope, &specialization]() -> void {
specialization = cloneNode(
astContext_,
semContext_,
declCollectorMap_,
generic.originalNode,
scope->parentFunction,
scope);
});
if (!specialization) {
sm_.error(
typeArgsNode->getSourceRange(),
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/FlowChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "hermes/Sema/FlowContext.h"
#include "hermes/Sema/Keywords.h"
#include "hermes/Sema/SemContext.h"
#include "hermes/Support/StackExecutor.h"

namespace hermes {
namespace flow {
Expand Down Expand Up @@ -73,6 +74,13 @@ class FlowChecker : public ESTree::RecursionDepthTracker<FlowChecker> {
/// Keywords we will be checking for.
sema::Keywords &kw_;

/// Use an 8MB stack, which is the default size on mac and linux.
static constexpr size_t kExecutorStackSize = 1 << 23;

/// The executor used for cloning generics.
std::shared_ptr<StackExecutor> stackExecutor_ =
newStackExecutor(kExecutorStackSize);

struct TypeDecl {
/// nullptr is used to indicate a generic declaration.
Type *type;
Expand Down

0 comments on commit ab480d1

Please sign in to comment.