Skip to content

release/20.x: [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (#133037) #137616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Interpreter/IncrementalExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/Support/VirtualFileSystem.h"
#ifdef __EMSCRIPTEN__
#include "Wasm.h"
#include <dlfcn.h>
#endif // __EMSCRIPTEN__

#include "clang/AST/ASTConsumer.h"
Expand Down Expand Up @@ -737,6 +738,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();
Expand All @@ -748,6 +757,7 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
EE->getMainJITDylib().addGenerator(std::move(*DLSG));
else
return DLSG.takeError();
#endif

return llvm::Error::success();
}
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/Interpreter/Wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions clang/lib/Interpreter/Wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ 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 override;

~WasmIncrementalExecutor() override;
};
Expand Down
Loading