aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Interpreter/Interpreter.cpp
diff options
context:
space:
mode:
authorAnutosh Bhat <andersonbhat491@gmail.com>2025-04-01 18:03:45 +0530
committerGitHub <noreply@github.com>2025-04-01 15:33:45 +0300
commit8f56394487a4d454be0637667267ad37bd636d0f (patch)
tree55ba045fb8ccc8b4d2deb46a07372064780ba8ed /clang/lib/Interpreter/Interpreter.cpp
parentc192737009584377d99b18bfbc8298c8e58bcd02 (diff)
downloadllvm-8f56394487a4d454be0637667267ad37bd636d0f.zip
llvm-8f56394487a4d454be0637667267ad37bd636d0f.tar.gz
llvm-8f56394487a4d454be0637667267ad37bd636d0f.tar.bz2
[clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (#133037)
**Currently we don't make use of the JIT for the wasm use cases so the approach using the execution engine won't work in these cases.** Rather if we use dlopen. We should be able to do the following (demonstrating through a toy project) 1) Make use of LoadDynamicLibrary through the given implementation ``` extern "C" EMSCRIPTEN_KEEPALIVE int load_library(const char *name) { auto Err = Interp->LoadDynamicLibrary(name); if (Err) { llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "load_library error: "); return -1; } return 0; } ``` 2) Add a button to call load_library once the library has been added in our MEMFS (currently we have symengine built as a SIDE MODULE and we are loading it)
Diffstat (limited to 'clang/lib/Interpreter/Interpreter.cpp')
-rw-r--r--clang/lib/Interpreter/Interpreter.cpp10
1 files changed, 10 insertions, 0 deletions
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index fa4c143..f8c8d0a 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -18,6 +18,7 @@
#include "llvm/Support/VirtualFileSystem.h"
#ifdef __EMSCRIPTEN__
#include "Wasm.h"
+#include <dlfcn.h>
#endif // __EMSCRIPTEN__
#include "clang/AST/ASTConsumer.h"
@@ -711,6 +712,14 @@ llvm::Error Interpreter::Undo(unsigned N) {
}
llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
+#ifdef __EMSCRIPTEN__
+ void *handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
+ if (!handle) {
+ llvm::errs() << dlerror() << '\n';
+ return llvm::make_error<llvm::StringError>("Failed to load dynamic library",
+ llvm::inconvertibleErrorCode());
+ }
+#else
auto EE = getExecutionEngine();
if (!EE)
return EE.takeError();
@@ -722,6 +731,7 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
EE->getMainJITDylib().addGenerator(std::move(*DLSG));
else
return DLSG.takeError();
+#endif
return llvm::Error::success();
}