aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunho Kim <ksunhokim123@gmail.com>2022-06-26 19:02:19 +0900
committerSunho Kim <ksunhokim123@gmail.com>2022-06-26 19:02:19 +0900
commit9de8b05bfe0de2915d2443d06159396c5f9d389f (patch)
tree97fba64aedd6ac38c737ea4a904c3e88c522535a
parentec1922313101f94ffa0f3fe8c721220b83ebcb32 (diff)
downloadllvm-9de8b05bfe0de2915d2443d06159396c5f9d389f.zip
llvm-9de8b05bfe0de2915d2443d06159396c5f9d389f.tar.gz
llvm-9de8b05bfe0de2915d2443d06159396c5f9d389f.tar.bz2
[clang-repl] Support destructors of global objects.
Supports destructors of global objects by properly calling jitdylib deinitialize which calls the global dtors of ir modules. This supersedes https://reviews.llvm.org/D127945. There was an issue when calling deinitialize on windows but it got fixed by https://reviews.llvm.org/D128037. Reviewed By: v.g.vassilev Differential Revision: https://reviews.llvm.org/D128589
-rw-r--r--clang/lib/Interpreter/IncrementalExecutor.cpp6
-rw-r--r--clang/lib/Interpreter/IncrementalExecutor.h1
-rw-r--r--clang/lib/Interpreter/Interpreter.cpp9
-rw-r--r--clang/test/Interpreter/execute.cpp4
-rw-r--r--clang/tools/clang-repl/ClangRepl.cpp4
-rw-r--r--clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp3
6 files changed, 22 insertions, 5 deletions
diff --git a/clang/lib/Interpreter/IncrementalExecutor.cpp b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 75c385a..c5ed9b0 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -52,6 +52,12 @@ IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
IncrementalExecutor::~IncrementalExecutor() {}
+// Clean up the JIT instance.
+llvm::Error IncrementalExecutor::cleanUp() {
+ // This calls the global dtors of registered modules.
+ return Jit->deinitialize(Jit->getMainJITDylib());
+}
+
llvm::Error IncrementalExecutor::addModule(std::unique_ptr<llvm::Module> M) {
return Jit->addIRModule(llvm::orc::ThreadSafeModule(std::move(M), TSCtx));
}
diff --git a/clang/lib/Interpreter/IncrementalExecutor.h b/clang/lib/Interpreter/IncrementalExecutor.h
index 51b4d83d..75c181d 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.h
+++ b/clang/lib/Interpreter/IncrementalExecutor.h
@@ -43,6 +43,7 @@ public:
llvm::Error addModule(std::unique_ptr<llvm::Module> M);
llvm::Error runCtors() const;
+ llvm::Error cleanUp();
llvm::Expected<llvm::JITTargetAddress>
getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
llvm::orc::LLJIT *getExecutionEngine() const { return Jit.get(); }
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 564b24e..c3bbfcf 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -183,7 +183,14 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
*TSCtx->getContext(), Err);
}
-Interpreter::~Interpreter() {}
+Interpreter::~Interpreter() {
+ if (IncrExecutor) {
+ if (llvm::Error Err = IncrExecutor->cleanUp())
+ llvm::report_fatal_error(
+ llvm::Twine("Failed to clean up IncrementalExecutor: ") +
+ toString(std::move(Err)));
+ }
+}
llvm::Expected<std::unique_ptr<Interpreter>>
Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
diff --git a/clang/test/Interpreter/execute.cpp b/clang/test/Interpreter/execute.cpp
index f5d7507..4f01f83 100644
--- a/clang/test/Interpreter/execute.cpp
+++ b/clang/test/Interpreter/execute.cpp
@@ -1,3 +1,4 @@
+// clang-format off
// RUN: clang-repl "int x = 10;" "int y=7; err;" "int y = 10;"
// RUN: clang-repl "int i = 10;" 'extern "C" int printf(const char*,...);' \
// RUN: 'auto r1 = printf("i = %d\n", i);' | FileCheck --check-prefix=CHECK-DRIVER %s
@@ -18,4 +19,7 @@ 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();
+struct D { float f = 1.0; D *m = nullptr; D(){} ~D() { printf("D[f=%f, m=0x%llx]\n", f, reinterpret_cast<unsigned long long>(m)); }} d;
+// CHECK: D[f=1.000000, m=0x0]
+
quit
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index ca784ad..271ec26 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -70,6 +70,8 @@ int main(int argc, const char **argv) {
ExitOnErr.setBanner("clang-repl: ");
llvm::cl::ParseCommandLineOptions(argc, argv);
+ llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
+
std::vector<const char *> ClangArgv(ClangArgs.size());
std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(),
[](const std::string &s) -> const char * { return s.data(); });
@@ -121,7 +123,5 @@ int main(int argc, const char **argv) {
// later errors use the default handling behavior instead.
llvm::remove_fatal_error_handler();
- llvm::llvm_shutdown();
-
return checkDiagErrors(Interp->getCompilerInstance());
}
diff --git a/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp b/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
index 75928d9..73f7a01 100644
--- a/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ b/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -48,6 +48,7 @@ createInterpreter(const Args &ExtraArgs = {},
TEST(InterpreterTest, CatchException) {
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
+ llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
{
auto J = llvm::orc::LLJITBuilder().create();
@@ -131,8 +132,6 @@ extern "C" int throw_exception() {
EXPECT_ANY_THROW(ThrowException());
std::string CapturedStdOut = testing::internal::GetCapturedStdout();
EXPECT_EQ(CapturedStdOut, "Caught: 'To be caught in JIT'\n");
-
- llvm::llvm_shutdown();
}
} // end anonymous namespace