https://github.com/anutosh491 updated https://github.com/llvm/llvm-project/pull/230047
>From e3424c5c3a7f2cdd8dcffa74b246e4e883f953d1 Mon Sep 17 00:00:00 2001 From: anutosh491 <[email protected]> Date: Thu, 8 Oct 2026 15:18:59 +0530 Subject: [PATCH] [clang-repl] Split incremental executor builders by backend --- .../clang/Interpreter/IncrementalExecutor.h | 55 ++++---------- .../OrcIncrementalExecutorBuilder.h | 73 +++++++++++++++++++ clang/lib/Interpreter/CMakeLists.txt | 3 +- .../IncrementalExecutorBuilder.cpp | 27 +++++++ clang/lib/Interpreter/Interpreter.cpp | 2 +- ....cpp => OrcIncrementalExecutorBuilder.cpp} | 25 ++++--- clang/lib/Interpreter/Wasm.cpp | 26 ++----- clang/lib/Interpreter/Wasm.h | 7 ++ clang/tools/clang-repl/ClangRepl.cpp | 4 +- .../Interpreter/InterpreterExtensionsTest.cpp | 5 +- .../OutOfProcessInterpreterTests.cpp | 4 +- 11 files changed, 154 insertions(+), 77 deletions(-) create mode 100644 clang/include/clang/Interpreter/OrcIncrementalExecutorBuilder.h create mode 100644 clang/lib/Interpreter/IncrementalExecutorBuilder.cpp rename clang/lib/Interpreter/{IncrementalExecutor.cpp => OrcIncrementalExecutorBuilder.cpp} (95%) diff --git a/clang/include/clang/Interpreter/IncrementalExecutor.h b/clang/include/clang/Interpreter/IncrementalExecutor.h index 2e037d1f090e58..bb49e673cb529f 100644 --- a/clang/include/clang/Interpreter/IncrementalExecutor.h +++ b/clang/include/clang/Interpreter/IncrementalExecutor.h @@ -13,13 +13,15 @@ #ifndef LLVM_CLANG_LIB_INTERPRETER_INCREMENTALEXECUTOR_H #define LLVM_CLANG_LIB_INTERPRETER_INCREMENTALEXECUTOR_H -#include "llvm/Support/CodeGen.h" #include "llvm/Support/Error.h" +#include <memory> +#include <string> +#include <vector> + namespace llvm { namespace orc { class ExecutorAddr; -class LLJITBuilder; class ThreadSafeContext; } // namespace orc } // namespace llvm @@ -27,53 +29,28 @@ class ThreadSafeContext; namespace clang { class IncrementalExecutor; class TargetInfo; -namespace driver { -class Compilation; -} // namespace driver +/// Common configuration and interface for incremental executor builders. class IncrementalExecutorBuilder { public: - /// Indicates whether out-of-process JIT execution is enabled. - bool IsOutOfProcess = false; - /// Path to the out-of-process JIT executor. - std::string OOPExecutor = ""; - std::string OOPExecutorConnect = ""; - /// Indicates whether to use shared memory for communication. - bool UseSharedMemory = false; - /// Representing the slab allocation size for memory management in kb. - unsigned SlabAllocateSize = 0; - /// Path to the ORC runtime library. - std::string OrcRuntimePath = ""; - /// PID of the out-of-process JIT executor. - uint32_t ExecutorPID = 0; - /// Custom lambda to be executed inside child process/executor - std::function<void()> CustomizeFork = nullptr; - /// An optional code model to provide to the JITTargetMachineBuilder - std::optional<llvm::CodeModel::Model> CM = std::nullopt; - /// An optional external IncrementalExecutor + /// An optional external IncrementalExecutor. std::unique_ptr<IncrementalExecutor> IE; - /// mllvm args from the frontend; on wasm these are re-applied after each - /// lldMain call because lld resets all cl options for test-isolation - /// purposes. + /// Frontend -mllvm arguments for backends that need to restore LLVM options. std::vector<std::string> LLVMArgs; -#ifndef __EMSCRIPTEN__ - /// An optional external orc jit builder - std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder; -#endif - /// A default callback that can be used in the IncrementalCompilerBuilder to - /// retrieve the path to the orc runtime. - std::function<llvm::Error(const driver::Compilation &)> - UpdateOrcRuntimePathCB = [this](const driver::Compilation &C) { - return UpdateOrcRuntimePath(C); - }; - - ~IncrementalExecutorBuilder(); + virtual ~IncrementalExecutorBuilder(); + + /// Create the default builder for the platform clangInterpreter is built for. + static std::unique_ptr<IncrementalExecutorBuilder> createDefault(); + + /// Return the supplied executor, or create one using the selected backend. llvm::Expected<std::unique_ptr<IncrementalExecutor>> create(llvm::orc::ThreadSafeContext &TSC, const clang::TargetInfo &TI); private: - llvm::Error UpdateOrcRuntimePath(const driver::Compilation &C); + virtual llvm::Expected<std::unique_ptr<IncrementalExecutor>> + createExecutor(llvm::orc::ThreadSafeContext &TSC, + const clang::TargetInfo &TI) = 0; }; struct PartialTranslationUnit; diff --git a/clang/include/clang/Interpreter/OrcIncrementalExecutorBuilder.h b/clang/include/clang/Interpreter/OrcIncrementalExecutorBuilder.h new file mode 100644 index 00000000000000..d443f69f657102 --- /dev/null +++ b/clang/include/clang/Interpreter/OrcIncrementalExecutorBuilder.h @@ -0,0 +1,73 @@ +//===-- OrcIncrementalExecutorBuilder.h - ORC Builder -----------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file declares the builder for ORC incremental execution. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INTERPRETER_ORCINCREMENTALEXECUTORBUILDER_H +#define LLVM_CLANG_INTERPRETER_ORCINCREMENTALEXECUTORBUILDER_H + +#include "clang/Interpreter/IncrementalExecutor.h" +#include "llvm/Support/CodeGen.h" + +#include <cstdint> +#include <functional> +#include <optional> + +namespace llvm::orc { +class LLJITBuilder; +} + +namespace clang { +namespace driver { +class Compilation; +} + +/// Configuration for in-process and out-of-process ORC execution. +class OrcIncrementalExecutorBuilder : public IncrementalExecutorBuilder { +public: + /// Indicates whether out-of-process JIT execution is enabled. + bool IsOutOfProcess = false; + /// Path to the out-of-process JIT executor. + std::string OOPExecutor = ""; + std::string OOPExecutorConnect = ""; + /// Indicates whether to use shared memory for communication. + bool UseSharedMemory = false; + /// Representing the slab allocation size for memory management in kb. + unsigned SlabAllocateSize = 0; + /// Path to the ORC runtime library. + std::string OrcRuntimePath = ""; + /// PID of the out-of-process JIT executor. + uint32_t ExecutorPID = 0; + /// Custom lambda to be executed inside child process/executor + std::function<void()> CustomizeFork = nullptr; + /// An optional code model to provide to the JITTargetMachineBuilder + std::optional<llvm::CodeModel::Model> CM = std::nullopt; + /// An optional external ORC JIT builder. + std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder; + /// A default callback that can be used in the IncrementalCompilerBuilder to + /// retrieve the path to the orc runtime. + std::function<llvm::Error(const driver::Compilation &)> + UpdateOrcRuntimePathCB = [this](const driver::Compilation &C) { + return UpdateOrcRuntimePath(C); + }; + + OrcIncrementalExecutorBuilder(); + ~OrcIncrementalExecutorBuilder() override; + +private: + llvm::Expected<std::unique_ptr<IncrementalExecutor>> + createExecutor(llvm::orc::ThreadSafeContext &TSC, + const clang::TargetInfo &TI) override; + llvm::Error UpdateOrcRuntimePath(const driver::Compilation &C); +}; + +} // namespace clang + +#endif // LLVM_CLANG_INTERPRETER_ORCINCREMENTALEXECUTORBUILDER_H diff --git a/clang/lib/Interpreter/CMakeLists.txt b/clang/lib/Interpreter/CMakeLists.txt index 95bac67e1b9dbb..e945ba96afbee5 100644 --- a/clang/lib/Interpreter/CMakeLists.txt +++ b/clang/lib/Interpreter/CMakeLists.txt @@ -22,13 +22,14 @@ else() OrcShared OrcTargetProcess ) - set(EXECUTOR_SOURCES IncrementalExecutor.cpp OrcIncrementalExecutor.cpp) + set(EXECUTOR_SOURCES OrcIncrementalExecutorBuilder.cpp OrcIncrementalExecutor.cpp) endif() add_clang_library(clangInterpreter DeviceOffload.cpp CodeCompletion.cpp IncrementalAction.cpp + IncrementalExecutorBuilder.cpp IncrementalParser.cpp Interpreter.cpp InterpreterValuePrinter.cpp diff --git a/clang/lib/Interpreter/IncrementalExecutorBuilder.cpp b/clang/lib/Interpreter/IncrementalExecutorBuilder.cpp new file mode 100644 index 00000000000000..f1110ebca99afa --- /dev/null +++ b/clang/lib/Interpreter/IncrementalExecutorBuilder.cpp @@ -0,0 +1,27 @@ +//===-- IncrementalExecutorBuilder.cpp - Executor Builder ------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This implements the common incremental executor builder. +// +//===----------------------------------------------------------------------===// + +#include "clang/Interpreter/IncrementalExecutor.h" + +namespace clang { + +IncrementalExecutorBuilder::~IncrementalExecutorBuilder() = default; + +llvm::Expected<std::unique_ptr<IncrementalExecutor>> +IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC, + const clang::TargetInfo &TI) { + if (IE) + return std::move(IE); + return createExecutor(TSC, TI); +} + +} // namespace clang diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index 85cefa12700497..0070f2ee353472 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -609,7 +609,7 @@ llvm::Error Interpreter::CreateExecutor() { std::error_code()); if (!IncrExecutorBuilder) - IncrExecutorBuilder = std::make_unique<IncrementalExecutorBuilder>(); + IncrExecutorBuilder = IncrementalExecutorBuilder::createDefault(); // Propagate mllvm args so the wasm executor can restore them after each // lldMain invocation (which resets all cl options for test isolation). diff --git a/clang/lib/Interpreter/IncrementalExecutor.cpp b/clang/lib/Interpreter/OrcIncrementalExecutorBuilder.cpp similarity index 95% rename from clang/lib/Interpreter/IncrementalExecutor.cpp rename to clang/lib/Interpreter/OrcIncrementalExecutorBuilder.cpp index db805bb4219a4c..59e375a85fab74 100644 --- a/clang/lib/Interpreter/IncrementalExecutor.cpp +++ b/clang/lib/Interpreter/OrcIncrementalExecutorBuilder.cpp @@ -1,4 +1,4 @@ -//===--- IncrementalExecutor.cpp - Incremental Execution --------*- C++ -*-===// +//===-- OrcIncrementalExecutorBuilder.cpp - ORC Builder --------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,11 +6,11 @@ // //===----------------------------------------------------------------------===// // -// This has the implementation of the base facilities for incremental execution. +// This implements the builder for ORC incremental execution. // //===----------------------------------------------------------------------===// -#include "clang/Interpreter/IncrementalExecutor.h" +#include "clang/Interpreter/OrcIncrementalExecutorBuilder.h" #include "OrcIncrementalExecutor.h" #include "clang/Basic/TargetInfo.h" @@ -81,7 +81,13 @@ static void *getEmuTLSGetAddressPtr() { return nullptr; } #endif namespace clang { -IncrementalExecutorBuilder::~IncrementalExecutorBuilder() = default; +OrcIncrementalExecutorBuilder::OrcIncrementalExecutorBuilder() = default; +OrcIncrementalExecutorBuilder::~OrcIncrementalExecutorBuilder() = default; + +std::unique_ptr<IncrementalExecutorBuilder> +IncrementalExecutorBuilder::createDefault() { + return std::make_unique<OrcIncrementalExecutorBuilder>(); +} static llvm::Expected<llvm::orc::JITTargetMachineBuilder> createJITTargetMachineBuilder(const llvm::Triple &TT) { @@ -321,7 +327,8 @@ createLLJITBuilder(std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC, static llvm::Expected< std::pair<std::unique_ptr<llvm::orc::LLJITBuilder>, uint32_t>> -outOfProcessJITBuilder(const IncrementalExecutorBuilder &IncrExecutorBuilder) { +outOfProcessJITBuilder( + const OrcIncrementalExecutorBuilder &IncrExecutorBuilder) { std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC; uint32_t childPid = -1; if (!IncrExecutorBuilder.OOPExecutor.empty()) { @@ -367,10 +374,8 @@ outOfProcessJITBuilder(const IncrementalExecutorBuilder &IncrExecutorBuilder) { } llvm::Expected<std::unique_ptr<IncrementalExecutor>> -IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC, - const clang::TargetInfo &TI) { - if (IE) - return std::move(IE); +OrcIncrementalExecutorBuilder::createExecutor(llvm::orc::ThreadSafeContext &TSC, + const clang::TargetInfo &TI) { llvm::Triple TT = TI.getTriple(); if (!TT.isOSWindows() && IsOutOfProcess) { if (!JITBuilder) { @@ -433,7 +438,7 @@ IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC, return std::move(Executor); } -llvm::Error IncrementalExecutorBuilder::UpdateOrcRuntimePath( +llvm::Error OrcIncrementalExecutorBuilder::UpdateOrcRuntimePath( const clang::driver::Compilation &C) { if (!IsOutOfProcess) return llvm::Error::success(); diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp index dbafaae85b5562..9351ab642c7074 100644 --- a/clang/lib/Interpreter/Wasm.cpp +++ b/clang/lib/Interpreter/Wasm.cpp @@ -62,19 +62,14 @@ bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS, namespace clang { -IncrementalExecutorBuilder::~IncrementalExecutorBuilder() = default; +std::unique_ptr<IncrementalExecutorBuilder> +IncrementalExecutorBuilder::createDefault() { + return std::make_unique<WasmIncrementalExecutorBuilder>(); +} llvm::Expected<std::unique_ptr<IncrementalExecutor>> -IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC, - const clang::TargetInfo &TI) { - if (IE) - return std::move(IE); - - if (IsOutOfProcess) - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "Out-of-process execution is not supported " - "by the WebAssembly executor"); - +WasmIncrementalExecutorBuilder::createExecutor( + llvm::orc::ThreadSafeContext &TSC, const clang::TargetInfo &TI) { llvm::Error Err = llvm::Error::success(); std::unique_ptr<IncrementalExecutor> Executor = std::make_unique<WasmIncrementalExecutor>(Err, LLVMArgs); @@ -83,15 +78,6 @@ IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC, return std::move(Executor); } -llvm::Error IncrementalExecutorBuilder::UpdateOrcRuntimePath( - const clang::driver::Compilation &C) { - if (IsOutOfProcess) - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "Out-of-process execution is not supported " - "by the WebAssembly executor"); - return llvm::Error::success(); -} - WasmIncrementalExecutor::WasmIncrementalExecutor( llvm::Error &Err, std::vector<std::string> LLVMArgs) : StoredLLVMArgs(std::move(LLVMArgs)) { diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h index 6340393dc6b9b3..3adf82cdacb7c8 100644 --- a/clang/lib/Interpreter/Wasm.h +++ b/clang/lib/Interpreter/Wasm.h @@ -24,6 +24,13 @@ namespace clang { +class WasmIncrementalExecutorBuilder : public IncrementalExecutorBuilder { +private: + llvm::Expected<std::unique_ptr<IncrementalExecutor>> + createExecutor(llvm::orc::ThreadSafeContext &TSC, + const clang::TargetInfo &TI) override; +}; + class WasmIncrementalExecutor : public IncrementalExecutor { public: WasmIncrementalExecutor(llvm::Error &Err, std::vector<std::string> LLVMArgs); diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp index c9873540a5d66b..8957a74ce17cf3 100644 --- a/clang/tools/clang-repl/ClangRepl.cpp +++ b/clang/tools/clang-repl/ClangRepl.cpp @@ -16,8 +16,8 @@ #include "clang/Config/config.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Interpreter/CodeCompletion.h" -#include "clang/Interpreter/IncrementalExecutor.h" #include "clang/Interpreter/Interpreter.h" +#include "clang/Interpreter/OrcIncrementalExecutorBuilder.h" #include "clang/Lex/Preprocessor.h" #include "clang/Sema/Sema.h" @@ -294,7 +294,7 @@ int main(int argc, const char **argv) { clang::IncrementalCompilerBuilder CB; CB.SetCompilerArgs(ClangArgv); - auto IEB = std::make_unique<clang::IncrementalExecutorBuilder>(); + auto IEB = std::make_unique<clang::OrcIncrementalExecutorBuilder>(); IEB->IsOutOfProcess = !OOPExecutor.empty() || !OOPExecutorConnect.empty(); IEB->OOPExecutor = OOPExecutor; if (!OrcRuntimePath.empty()) diff --git a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp index 642869e124b8bd..f1cd3b38fbfd92 100644 --- a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp +++ b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp @@ -13,6 +13,7 @@ #include "InterpreterTestFixture.h" #include "clang/Interpreter/Interpreter.h" +#include "clang/Interpreter/OrcIncrementalExecutorBuilder.h" #include "clang/AST/Expr.h" #include "clang/Frontend/CompilerInstance.h" @@ -125,7 +126,7 @@ TEST_F(InterpreterExtensionsTest, CustomCrossJIT) { JIT = &J; return llvm::Error::success(); }); - auto IEB = std::make_unique<IncrementalExecutorBuilder>(); + auto IEB = std::make_unique<OrcIncrementalExecutorBuilder>(); IEB->JITBuilder = std::move(JB); llvm::Error ErrOut = llvm::Error::success(); CustomJBInterpreter Interp(std::move(CI), ErrOut, std::move(IEB)); @@ -181,7 +182,7 @@ TEST_F(InterpreterExtensionsTest, CustomIncrementalExecutor) { }; // Prepare a builder that hands out our recording executor. - auto B = std::make_unique<IncrementalExecutorBuilder>(); + auto B = IncrementalExecutorBuilder::createDefault(); B->IE = std::make_unique<RecordingIncrementalExecutor>(); IncrementalCompilerBuilder CB; diff --git a/clang/unittests/Interpreter/OutOfProcessInterpreterTests.cpp b/clang/unittests/Interpreter/OutOfProcessInterpreterTests.cpp index 3684e7e8148fa5..f67ad02d918c64 100644 --- a/clang/unittests/Interpreter/OutOfProcessInterpreterTests.cpp +++ b/clang/unittests/Interpreter/OutOfProcessInterpreterTests.cpp @@ -18,8 +18,8 @@ #include "clang/AST/Mangle.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/TextDiagnosticPrinter.h" -#include "clang/Interpreter/IncrementalExecutor.h" #include "clang/Interpreter/Interpreter.h" +#include "clang/Interpreter/OrcIncrementalExecutorBuilder.h" #include "clang/Interpreter/Value.h" #include "clang/Sema/Lookup.h" #include "clang/Sema/Sema.h" @@ -115,7 +115,7 @@ createInterpreterWithRemoteExecution(std::shared_ptr<IOContext> io_ctx, Args ClangArgs = {"-Xclang", "-emit-llvm-only"}; llvm::append_range(ClangArgs, ExtraArgs); - auto Config = std::make_unique<IncrementalExecutorBuilder>(); + auto Config = std::make_unique<OrcIncrementalExecutorBuilder>(); Config->IsOutOfProcess = true; Config->OOPExecutor = getExecutorPath(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
