https://github.com/anutosh491 created 
https://github.com/llvm/llvm-project/pull/131558

Raising as draft for now. This PR implements the removeModule function that can 
be put to use while running clang-repl in the browser.

The implementation works as we would like it to. But due to some issues in how 
the dlopen-dlclose pair works for the wasm case (as compared to the native 
case) this has some shortcomings. Check the following video to understand why I 
say so 


https://github.com/user-attachments/assets/a120e767-79c0-4c41-bbb0-f086e6af883b



>From 56d3e301d01cb8ab8f44751d7684378f38b0661e Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat...@gmail.com>
Date: Fri, 28 Feb 2025 12:11:28 +0530
Subject: [PATCH] Implementation for removeModule

---
 clang/lib/Interpreter/Wasm.cpp | 37 ++++++++++++++++++++++++++++++++--
 clang/lib/Interpreter/Wasm.h   |  4 ++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index aa10b160ccf84..ed746beefd0f5 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -125,12 +125,45 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
         "Failed to load incremental module", llvm::inconvertibleErrorCode());
   }
 
+  LoadedModules[BinaryFileName] = LoadedLibModule;
+
   return llvm::Error::success();
 }
 
 llvm::Error WasmIncrementalExecutor::removeModule(PartialTranslationUnit &PTU) 
{
-  return llvm::make_error<llvm::StringError>("Not implemented yet",
-                                             llvm::inconvertibleErrorCode());
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";
+
+  llvm::errs() << "[DEBUG] Entering removeModule() for: " << BinaryFileName << 
"\n";
+  llvm::errs() << "[DEBUG] Current Loaded Modules:\n";
+  for (const auto &entry : LoadedModules) {
+    llvm::errs() << "  -> " << entry.first << "\n";
+  }
+
+  auto It = LoadedModules.find(BinaryFileName);
+  if (It == LoadedModules.end()) {
+    llvm::errs() << "[ERROR] Module not found in LoadedModules!\n";
+    return llvm::make_error<llvm::StringError>(
+        "Module not found in loaded modules", llvm::inconvertibleErrorCode());
+  }
+
+  llvm::errs() << "[DEBUG] Unloading module: " << BinaryFileName << "\n";
+  // Attempt to unload the module
+
+  if (dlclose(It->second) != 0) {
+    llvm::errs() << "Failed to unload module: " << dlerror() << '\n';
+    return llvm::make_error<llvm::StringError>(
+        "Failed to unload module", llvm::inconvertibleErrorCode());
+  }
+
+  // Remove the module from tracking
+  LoadedModules.erase(It);
+
+  llvm::errs() << "[DEBUG] Remaining Loaded Modules:\n";
+  for (const auto &entry : LoadedModules) {
+    llvm::errs() << "  -> " << entry.first << "\n";
+  }
+
+  return llvm::Error::success();
 }
 
 llvm::Error WasmIncrementalExecutor::runCtors() const {
diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h
index 4632613326d39..7bf597bbf0b0c 100644
--- a/clang/lib/Interpreter/Wasm.h
+++ b/clang/lib/Interpreter/Wasm.h
@@ -31,6 +31,10 @@ class WasmIncrementalExecutor : public IncrementalExecutor {
   llvm::Error cleanUp() override;
 
   ~WasmIncrementalExecutor() override;
+
+private:
+  // Tracks loaded modules and their handles
+  std::unordered_map<std::string, void *> LoadedModules;
 };
 
 } // namespace clang

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to