diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -28,7 +28,7 @@ namespace orc { class LLJIT; class ThreadSafeContext; -} +} // namespace orc } // namespace llvm namespace clang { @@ -52,12 +52,15 @@ Interpreter(std::unique_ptr CI, llvm::Error &Err); + llvm::Error CreateExecutor(); + public: ~Interpreter(); static llvm::Expected> create(std::unique_ptr CI); const CompilerInstance *getCompilerInstance() const; - const llvm::orc::LLJIT *getExecutionEngine() const; + llvm::Expected getExecutionEngine(); + llvm::Expected Parse(llvm::StringRef Code); llvm::Error Execute(PartialTranslationUnit &T); llvm::Error ParseAndExecute(llvm::StringRef Code) { @@ -72,6 +75,9 @@ /// Undo N previous incremental inputs. llvm::Error Undo(unsigned N = 1); + /// Link a dynamic library + llvm::Error LoadDynamicLibrary(const char *name); + /// \returns the \c JITTargetAddress of a \c GlobalDecl. This interface uses /// the CodeGenModule's internal mangling cache to avoid recomputing the /// mangled name. diff --git a/clang/lib/Interpreter/IncrementalExecutor.h b/clang/lib/Interpreter/IncrementalExecutor.h --- a/clang/lib/Interpreter/IncrementalExecutor.h +++ b/clang/lib/Interpreter/IncrementalExecutor.h @@ -53,7 +53,8 @@ llvm::Error cleanUp(); llvm::Expected getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const; - llvm::orc::LLJIT *getExecutionEngine() const { return Jit.get(); } + + llvm::orc::LLJIT &GetExecutionEngine() { return *Jit; } }; } // end namespace clang diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -29,6 +29,7 @@ #include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Lex/PreprocessorOptions.h" +#include "llvm/ExecutionEngine/Orc/LLJIT.h" #include "llvm/IR/Module.h" #include "llvm/Support/Errc.h" #include "llvm/TargetParser/Host.h" @@ -203,10 +204,13 @@ return IncrParser->getCI(); } -const llvm::orc::LLJIT *Interpreter::getExecutionEngine() const { - if (IncrExecutor) - return IncrExecutor->getExecutionEngine(); - return nullptr; +llvm::Expected Interpreter::getExecutionEngine() { + if (!IncrExecutor) { + if (auto Err = CreateExecutor()) + return Err; + } + + return IncrExecutor->GetExecutionEngine(); } llvm::Expected @@ -214,14 +218,21 @@ 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(*TSCtx, Err, TI); + if (!Err) + IncrExecutor = std::move(Executor); + + return Err; +} + llvm::Error Interpreter::Execute(PartialTranslationUnit &T) { assert(T.TheModule); if (!IncrExecutor) { - const clang::TargetInfo &TI = - getCompilerInstance()->getASTContext().getTargetInfo(); - llvm::Error Err = llvm::Error::success(); - IncrExecutor = std::make_unique(*TSCtx, Err, TI); - + auto Err = CreateExecutor(); if (Err) return Err; } @@ -283,3 +294,19 @@ } return llvm::Error::success(); } + +llvm::Error Interpreter::LoadDynamicLibrary(const char *name) { + auto EE = getExecutionEngine(); + if (!EE) + return EE.takeError(); + + auto &DL = EE->getDataLayout(); + + if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load( + name, DL.getGlobalPrefix())) + EE->getMainJITDylib().addGenerator(std::move(*DLSG)); + else + return DLSG.takeError(); + + return llvm::Error::success(); +} diff --git a/clang/test/Interpreter/dynamic-library.cpp b/clang/test/Interpreter/dynamic-library.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Interpreter/dynamic-library.cpp @@ -0,0 +1,24 @@ +// RUN: head -n 7 %s | %clang -xc++ -o %T/libdynamic-library-test.so -fPIC -shared - +int ultimate_answer = 0; + +int calculate_answer() { + ultimate_answer = 42; + return 5; +} + +// REQUIRES: host-supports-jit, system-linux +// RUN: tail -n 16 %s | env LD_LIBRARY_PATH=%T:$LD_LIBRARY_PATH clang-repl | FileCheck %s +#include + +extern int ultimate_answer; +int calculate_answer(); + +%lib libdynamic-library-test.so + +printf("Return value: %d\n", calculate_answer()); +// CHECK: Return value: 5 + +printf("Variable: %d\n", ultimate_answer); +// CHECK-NEXT: Variable: 42 + +%quit diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp --- a/clang/tools/clang-repl/ClangRepl.cpp +++ b/clang/tools/clang-repl/ClangRepl.cpp @@ -123,6 +123,13 @@ } continue; } + if (Line->rfind("%lib ", 0) == 0) { + if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) { + llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: "); + HasError = true; + } + continue; + } if (auto Err = Interp->ParseAndExecute(*Line)) { llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");