https://github.com/anutosh491 updated https://github.com/llvm/llvm-project/pull/133037
>From a7ccd0e0b261c303639d3880fc51b0ed99e17363 Mon Sep 17 00:00:00 2001 From: anutosh491 <andersonbhat...@gmail.com> Date: Wed, 26 Mar 2025 10:33:37 +0530 Subject: [PATCH 1/4] Implement LoadDynamicLibrary for clang-repl wasm use cases --- clang/lib/Interpreter/Interpreter.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index fa4c1439c9261..f8c8d0a425659 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(); } >From bc41b71965e1e33a32d1a54fd79148d1b6890dce Mon Sep 17 00:00:00 2001 From: anutosh491 <andersonbhat...@gmail.com> Date: Thu, 27 Mar 2025 12:34:23 +0530 Subject: [PATCH 2/4] Override getSymbolAddress --- clang/lib/Interpreter/IncrementalExecutor.h | 2 +- clang/lib/Interpreter/Wasm.cpp | 13 +++++++++++++ clang/lib/Interpreter/Wasm.h | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/clang/lib/Interpreter/IncrementalExecutor.h b/clang/lib/Interpreter/IncrementalExecutor.h index dbd61f0b8b1eb..71d71bc3883e2 100644 --- a/clang/lib/Interpreter/IncrementalExecutor.h +++ b/clang/lib/Interpreter/IncrementalExecutor.h @@ -57,7 +57,7 @@ class IncrementalExecutor { virtual llvm::Error removeModule(PartialTranslationUnit &PTU); virtual llvm::Error runCtors() const; virtual llvm::Error cleanUp(); - llvm::Expected<llvm::orc::ExecutorAddr> + virtual llvm::Expected<llvm::orc::ExecutorAddr> getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const; llvm::orc::LLJIT &GetExecutionEngine() { return *Jit; } diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp index f7cb7598c77f8..0543a3504c9a2 100644 --- a/clang/lib/Interpreter/Wasm.cpp +++ b/clang/lib/Interpreter/Wasm.cpp @@ -144,6 +144,19 @@ llvm::Error WasmIncrementalExecutor::cleanUp() { return llvm::Error::success(); } +llvm::Expected<llvm::orc::ExecutorAddr> +WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name, + SymbolNameKind NameKind) const { + void *Sym = dlsym(RTLD_DEFAULT, Name.str().c_str()); + if (!Sym) { + return llvm::make_error<llvm::StringError>("dlsym failed for symbol: " + + Name.str(), + llvm::inconvertibleErrorCode()); + } + + return llvm::orc::ExecutorAddr::fromPtr(Sym); +} + WasmIncrementalExecutor::~WasmIncrementalExecutor() = default; } // namespace clang diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h index 4632613326d39..beafb01be7d8a 100644 --- a/clang/lib/Interpreter/Wasm.h +++ b/clang/lib/Interpreter/Wasm.h @@ -29,6 +29,8 @@ class WasmIncrementalExecutor : public IncrementalExecutor { llvm::Error removeModule(PartialTranslationUnit &PTU) override; llvm::Error runCtors() const override; llvm::Error cleanUp() override; + llvm::Expected<llvm::orc::ExecutorAddr> + getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const; ~WasmIncrementalExecutor() override; }; >From 60d4622e23902bf7c5fd1040c9f82f9b03e134ce Mon Sep 17 00:00:00 2001 From: Anutosh Bhat <andersonbhat...@gmail.com> Date: Thu, 27 Mar 2025 14:05:35 +0530 Subject: [PATCH 3/4] Update Wasm.h --- clang/lib/Interpreter/Wasm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h index beafb01be7d8a..d27455ec42c02 100644 --- a/clang/lib/Interpreter/Wasm.h +++ b/clang/lib/Interpreter/Wasm.h @@ -30,7 +30,7 @@ class WasmIncrementalExecutor : public IncrementalExecutor { llvm::Error runCtors() const override; llvm::Error cleanUp() override; llvm::Expected<llvm::orc::ExecutorAddr> - getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const; + getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const override;; ~WasmIncrementalExecutor() override; }; >From 50370179c93d5f524fd38e093248d6e74db8155c Mon Sep 17 00:00:00 2001 From: anutosh491 <andersonbhat...@gmail.com> Date: Tue, 1 Apr 2025 10:40:57 +0530 Subject: [PATCH 4/4] fix clang format --- clang/lib/Interpreter/Wasm.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h index d27455ec42c02..9a752934e3185 100644 --- a/clang/lib/Interpreter/Wasm.h +++ b/clang/lib/Interpreter/Wasm.h @@ -30,7 +30,8 @@ class WasmIncrementalExecutor : public IncrementalExecutor { llvm::Error runCtors() const override; llvm::Error cleanUp() override; llvm::Expected<llvm::orc::ExecutorAddr> - getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const override;; + getSymbolAddress(llvm::StringRef Name, + SymbolNameKind NameKind) const override; ~WasmIncrementalExecutor() override; }; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits