diff --git a/src/jitlayers.cpp b/src/jitlayers.cpp index b0a1338ab81dd..9a8b0639ab5c4 100644 --- a/src/jitlayers.cpp +++ b/src/jitlayers.cpp @@ -689,8 +689,17 @@ static void jl_compile_codeinst_now(jl_code_instance_t *codeinst) if (!decls.specFunctionObject.empty()) NewDefs.push_back(decls.specFunctionObject); } - auto Addrs = jl_ExecutionEngine->findSymbols(NewDefs); - + // Split batches to avoid stack overflow in the JIT linker. + // FIXME: Patch ORCJITs InPlaceTaskDispatcher to not recurse on task dispatches but + // push the tasks to a queue to be drained later. This avoids the stackoverflow caused by recursion + // in the linker when compiling a large number of functions at once. + SmallVector Addrs; + for (size_t i = 0; i < NewDefs.size(); i += 1000) { + auto end = std::min(i + 1000, NewDefs.size()); + SmallVector batch(NewDefs.begin() + i, NewDefs.begin() + end); + auto AddrsBatch = jl_ExecutionEngine->findSymbols(batch); + Addrs.append(AddrsBatch); + } size_t nextaddr = 0; for (auto &this_code : linkready) { auto it = invokenames.find(this_code); diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index 12b13c1984a5e..cf31f458ab942 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -1268,3 +1268,6 @@ end end end end + +# https://github.com/JuliaLang/julia/issues/58229 Recursion in jitlinking with inline=no +@test success(`$(Base.julia_cmd()) --inline=no -e 'Base.compilecache(Base.identify_package("Pkg"))'`)