[clang] [clang-repl] Expose CreateExecutor() and ResetExecutor() in extended Interpreter interface (PR #84460)
Vassil Vassilev via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 8 05:49:27 PST 2024
Stefan =?utf-8?q?Gränitz?= <stefan.graenitz at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/84460 at github.com>
vgvassilev wrote:
I was currently touching this area to fix a problem of @jeaye reported on irc.
Do you mind incorporating this patch so that we avoid churn?
```diff
diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index 292fa566ae70..792978322349 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -83,7 +83,6 @@ class Interpreter {
Interpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &Err);
- llvm::Error CreateExecutor();
unsigned InitPTUSize = 0;
// This member holds the last result of the value printing. It's a class
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 9f97a3c6b0be..edc19b3a43e4 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -233,6 +233,26 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
IncrParser = std::make_unique<IncrementalParser>(*this, std::move(CI),
*TSCtx->getContext(), Err);
+ if (IncrParser->getCodeGen()) {
+ const clang::TargetInfo &TI =
+ getCompilerInstance()->getASTContext().getTargetInfo();
+ llvm::Error ExecErr = llvm::Error::success();
+ auto Executor = std::make_unique<IncrementalExecutor>(*TSCtx, ExecErr, TI);
+ if (ExecErr) {
+ Err = joinErrors(std::move(Err), std::move(ExecErr));
+ return;
+ }
+
+ IncrExecutor = std::move(Executor);
+
+ // Process the PTUs that came from initialization. For example -include will
+ // give us a header that's processed at initialization of the preprocessor.
+ for (auto &PTU : IncrParser->getPTUs())
+ if (auto ExecErr = Execute(PTU)) {
+ Err = joinErrors(std::move(Err), std::move(ExecErr));
+ return;
+ }
+ }
}
Interpreter::~Interpreter() {
@@ -327,10 +347,10 @@ CompilerInstance *Interpreter::getCompilerInstance() {
}
llvm::Expected<llvm::orc::LLJIT &> Interpreter::getExecutionEngine() {
- if (!IncrExecutor) {
- if (auto Err = CreateExecutor())
- return std::move(Err);
- }
+ if (!IncrExecutor)
+ return llvm::make_error<llvm::StringError>("Operation failed. "
+ "No execution engine",
+ std::error_code());
return IncrExecutor->GetExecutionEngine();
}
@@ -366,24 +386,8 @@ Interpreter::Parse(llvm::StringRef Code) {
return IncrParser->Parse(Code);
}
-llvm::Error Interpreter::CreateExecutor() {
- const clang::TargetInfo &TI =
- getCompilerInstance()->getASTContext().getTargetInfo();
- llvm::Error Err = llvm::Error::success();
- auto Executor = std::make_unique<IncrementalExecutor>(*TSCtx, Err, TI);
- if (!Err)
- IncrExecutor = std::move(Executor);
-
- return Err;
-}
-
llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
assert(T.TheModule);
- if (!IncrExecutor) {
- auto Err = CreateExecutor();
- if (Err)
- return Err;
- }
// FIXME: Add a callback to retain the llvm::Module once the JIT is done.
if (auto Err = IncrExecutor->addModule(T))
return Err;
diff --git a/clang/test/Interpreter/execute.cpp b/clang/test/Interpreter/execute.cpp
index 6e73ed3927e8..36c74703cb85 100644
--- a/clang/test/Interpreter/execute.cpp
+++ b/clang/test/Interpreter/execute.cpp
@@ -7,6 +7,9 @@
// RUN: cat %s | clang-repl | FileCheck %s
// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: clang-repl -Xcc -include -Xcc %s | FileCheck %s
+// RUN: clang-repl -Xcc -fsyntax-only -Xcc -include -Xcc %s
+
extern "C" int printf(const char *, ...);
int i = 42;
auto r1 = printf("i = %d\n", i);
@@ -19,5 +22,3 @@ auto r2 = printf("S[f=%f, m=0x%llx]\n", s.f, reinterpret_cast<unsigned long long
inline int foo() { return 42; }
int r3 = foo();
-
-%quit
```
https://github.com/llvm/llvm-project/pull/84460
More information about the cfe-commits
mailing list