Skip to content

Conversation

@vgvassilev
Copy link
Contributor

@vgvassilev vgvassilev commented Jan 12, 2026

This patch avoids bloating the current folder with temporary files created by wasm and moves them in a separate tmp directory. This patch is a version of a downstream one resolving this issue.

Co-authored with Anutosh Bhat.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 12, 2026
@llvmbot
Copy link
Member

llvmbot commented Jan 12, 2026

@llvm/pr-subscribers-clang

Author: Vassil Vassilev (vgvassilev)

Changes

This patch avoids bloating the current folder with temporary files created by wasm and moves them in a separate tmp directory. This patch is a version of a downstream one resolving this issue.


Full diff: https://github.com/llvm/llvm-project/pull/175508.diff

2 Files Affected:

  • (modified) clang/lib/Interpreter/IncrementalExecutor.cpp (+1-1)
  • (modified) clang/lib/Interpreter/Wasm.cpp (+27-5)
diff --git a/clang/lib/Interpreter/IncrementalExecutor.cpp b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 001651522c329..74b751d0e2dae 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -404,7 +404,7 @@ IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC,
   llvm::Error Err = llvm::Error::success();
   std::unique_ptr<IncrementalExecutor> Executor;
 #ifdef __EMSCRIPTEN__
-  Executor = std::make_unique<WasmIncrementalExecutor>();
+  Executor = std::make_unique<WasmIncrementalExecutor>(Err);
 #else
   Executor = std::make_unique<OrcIncrementalExecutor>(TSC, *JITBuilder, Err);
 #endif
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 56f51e5d5311e..68bfb871367da 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -57,7 +57,20 @@ bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
 
 namespace clang {
 
-WasmIncrementalExecutor::WasmIncrementalExecutor() = default;
+WasmIncrementalExecutor::WasmIncrementalExecutor(llvm::Error &Err) {
+  llvm::ErrorAsOutParameter EAO(&Err);
+
+  if (Err)
+    return;
+
+  if (auto EC =
+          llvm::sys::fs::createUniqueDirectory("clang-wasm-exec-", TempDir))
+    Err = llvm::make_error<llvm::StringError>(
+        "Failed to create temporary directory for Wasm executor: " +
+            EC.message(),
+        llvm::inconvertibleErrorCode());
+}
+
 WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;
 
 llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
@@ -74,11 +87,20 @@ llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
       PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);
   PTU.TheModule->setDataLayout(TargetMachine->createDataLayout());
-  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";
-  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";
 
-  std::error_code Error;
-  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), Error);
+  llvm::SmallString<256> ObjectFileName(TempDir);
+  llvm::sys::path::append(ObjectFileName,
+                          PTU.TheModule->getName() + ".o");
+
+  llvm::SmallString<256> BinaryFileName(TempDir);
+  llvm::sys::path::append(BinaryFileName,
+                          PTU.TheModule->getName() + ".wasm");
+
+  std::error_code EC;
+  llvm::raw_fd_ostream ObjectFileOutput(ObjectFileName, EC);
+
+  if (EC)
+    return llvm::errorCodeToError(EC);
 
   llvm::legacy::PassManager PM;
   if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,

@github-actions
Copy link

github-actions bot commented Jan 12, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@anutosh491
Copy link
Member

anutosh491 commented Jan 12, 2026

Ahh getting this error while building. Pasted the exact diff as provided. Debugging the above now !

/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:60:26: error: out-of-line definition of 'WasmIncrementalExecutor' does not match any declaration in 'clang::WasmIncrementalExecutor'
   60 | WasmIncrementalExecutor::WasmIncrementalExecutor(llvm::Error &Err) {
      |                          ^~~~~~~~~~~~~~~~~~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.h:24:7: note: WasmIncrementalExecutor defined here
   24 | class WasmIncrementalExecutor : public IncrementalExecutor {
      |       ^~~~~~~~~~~~~~~~~~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:67:26: error: no member named 'createUniqueDirectory' in namespace 'llvm::sys::fs'
   67 |           llvm::sys::fs::createUniqueDirectory("clang-wasm-exec-", TempDir))
      |                          ^~~~~~~~~~~~~~~~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:67:68: error: use of undeclared identifier 'TempDir'
   67 |           llvm::sys::fs::createUniqueDirectory("clang-wasm-exec-", TempDir))
      |                                                                    ^~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:91:41: error: unknown type name 'TempDir'
   91 |   llvm::SmallString<256> ObjectFileName(TempDir);
      |                                         ^
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:91:40: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
   91 |   llvm::SmallString<256> ObjectFileName(TempDir);
      |                                        ^~~~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:91:41: note: add a pair of parentheses to declare a variable
   91 |   llvm::SmallString<256> ObjectFileName(TempDir);
      |                                         ^
      |                                         (
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:92:14: error: no member named 'path' in namespace 'llvm::sys'
   92 |   llvm::sys::path::append(ObjectFileName,
      |   ~~~~~~~~~~~^
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:95:41: error: unknown type name 'TempDir'
   95 |   llvm::SmallString<256> BinaryFileName(TempDir);
      |                                         ^
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:95:40: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
   95 |   llvm::SmallString<256> BinaryFileName(TempDir);
      |                                        ^~~~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:95:41: note: add a pair of parentheses to declare a variable
   95 |   llvm::SmallString<256> BinaryFileName(TempDir);
      |                                         ^
      |                                         (
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.cpp:96:14: error: no member named 'path' in namespace 'llvm::sys'
   96 |   llvm::sys::path::append(BinaryFileName,
      |   ~~~~~~~~~~~^
In file included from /Users/anutosh491/work/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp:13:
In file included from /Users/anutosh491/work/llvm-project/clang/include/clang/Interpreter/IncrementalExecutor.h:16:
In file included from /Users/anutosh491/work/llvm-project/llvm/include/llvm/Support/CodeGen.h:18:
In file included from /Users/anutosh491/work/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/optional:1294:
In file included from /Users/anutosh491/work/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/memory:944:
In file included from /Users/anutosh491/work/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__memory/inout_ptr.h:16:
In file included from /Users/anutosh491/work/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__memory/shared_ptr.h:32:
/Users/anutosh491/work/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__memory/unique_ptr.h:634:30: error: no matching constructor for initialization of 'clang::WasmIncrementalExecutor'
  634 |   return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));
      |                              ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp:407:19: note: in instantiation of function template specialization 'std::make_unique<clang::WasmIncrementalExecutor, llvm::Error &>' requested here
  407 |   Executor = std::make_unique<WasmIncrementalExecutor>(Err);
      |                   ^
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.h:24:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'llvm::Error' to 'const WasmIncrementalExecutor' for 1st argument
   24 | class WasmIncrementalExecutor : public IncrementalExecutor {
      |       ^~~~~~~~~~~~~~~~~~~~~~~
/Users/anutosh491/work/llvm-project/clang/lib/Interpreter/Wasm.h:26:3: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
   26 |   WasmIncrementalExecutor();
      |   ^
1 error generated.
make[3]: *** [tools/clang/lib/Interpreter/CMakeFiles/obj.clangInterpreter.dir/IncrementalExecutor.cpp.o] Error 1
make[3]: *** Waiting for unfinished jobs....
2 warnings and 7 errors generated.
make[3]: *** [tools/clang/lib/Interpreter/CMakeFiles/obj.clangInterpreter.dir/Wasm.cpp.o] Error 1
make[2]: *** [tools/clang/lib/Interpreter/CMakeFiles/obj.clangInterpreter.dir/all] Error 2
make[1]: *** [tools/clang/lib/Interpreter/CMakeFiles/clangInterpreter.dir/rule] Error 2
make: *** [clangInterpreter] Error 2
emmake: error: 'make clangInterpreter ClangReplInterpreterTests -j8' failed (returned 2)

@anutosh491
Copy link
Member

This diff should be enough

anutosh491@Anutoshs-MacBook-Air build % git diff
diff --git a/clang/include/clang/Interpreter/IncrementalExecutor.h b/clang/include/clang/Interpreter/IncrementalExecutor.h
index 913da9230a94..0e879efa0b55 100644
--- a/clang/include/clang/Interpreter/IncrementalExecutor.h
+++ b/clang/include/clang/Interpreter/IncrementalExecutor.h
@@ -90,4 +90,4 @@ public:
 
 } // namespace clang
 
-#endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALEXECUTOR_H
+#endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALEXECUTOR_H
\ No newline at end of file
diff --git a/clang/lib/Interpreter/IncrementalExecutor.cpp b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 001651522c32..74b751d0e2da 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -404,7 +404,7 @@ IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC,
   llvm::Error Err = llvm::Error::success();
   std::unique_ptr<IncrementalExecutor> Executor;
 #ifdef __EMSCRIPTEN__
-  Executor = std::make_unique<WasmIncrementalExecutor>();
+  Executor = std::make_unique<WasmIncrementalExecutor>(Err);
 #else
   Executor = std::make_unique<OrcIncrementalExecutor>(TSC, *JITBuilder, Err);
 #endif
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 56f51e5d5311..79b506f7d168 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -15,6 +15,8 @@
 #include <llvm/IR/LegacyPassManager.h>
 #include <llvm/IR/Module.h>
 #include <llvm/MC/TargetRegistry.h>
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
 #include <llvm/Target/TargetMachine.h>
 
 #include <clang/Interpreter/Interpreter.h>
@@ -57,7 +59,20 @@ bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
 
 namespace clang {
 
-WasmIncrementalExecutor::WasmIncrementalExecutor() = default;
+WasmIncrementalExecutor::WasmIncrementalExecutor(llvm::Error &Err) {
+  llvm::ErrorAsOutParameter EAO(&Err);
+
+  if (Err)
+    return;
+
+  if (auto EC =
+          llvm::sys::fs::createUniqueDirectory("clang-wasm-exec-", TempDir))
+    Err = llvm::make_error<llvm::StringError>(
+        "Failed to create temporary directory for Wasm executor: " +
+            EC.message(),
+        llvm::inconvertibleErrorCode());
+}
+
 WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;
 
 llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
@@ -74,11 +89,20 @@ llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
       PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);
   PTU.TheModule->setDataLayout(TargetMachine->createDataLayout());
-  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";
-  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";
 
-  std::error_code Error;
-  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), Error);
+  llvm::SmallString<256> ObjectFileName(TempDir);
+  llvm::sys::path::append(ObjectFileName,
+                          PTU.TheModule->getName() + ".o");
+
+  llvm::SmallString<256> BinaryFileName(TempDir);
+  llvm::sys::path::append(BinaryFileName,
+                          PTU.TheModule->getName() + ".wasm");
+
+  std::error_code EC;
+  llvm::raw_fd_ostream ObjectFileOutput(ObjectFileName, EC);
+
+  if (EC)
+    return llvm::errorCodeToError(EC);
 
   llvm::legacy::PassManager PM;
   if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
@@ -162,5 +186,6 @@ llvm::Error WasmIncrementalExecutor::LoadDynamicLibrary(const char *name) {
     return llvm::make_error<llvm::StringError>("Failed to load dynamic library",
                                                llvm::inconvertibleErrorCode());
   }
+  return llvm::Error::success();
 }
 } // namespace clang
diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h
index 55f375ac2b5c..bf8777a41eac 100644
--- a/clang/lib/Interpreter/Wasm.h
+++ b/clang/lib/Interpreter/Wasm.h
@@ -18,12 +18,13 @@
 #endif // __EMSCRIPTEN__
 
 #include "clang/Interpreter/IncrementalExecutor.h"
+#include "llvm/ADT/SmallString.h"
 
 namespace clang {
 
 class WasmIncrementalExecutor : public IncrementalExecutor {
 public:
-  WasmIncrementalExecutor();
+  WasmIncrementalExecutor(llvm::Error &Err);
   ~WasmIncrementalExecutor() override;
 
   llvm::Error addModule(PartialTranslationUnit &PTU) override;
@@ -34,6 +35,9 @@ public:
   getSymbolAddress(llvm::StringRef Name,
                    SymbolNameKind NameKind) const override;
   llvm::Error LoadDynamicLibrary(const char *name) override;
+
+private:
+  llvm::SmallString<256> TempDir;
 };
 
 } // namespace clang

@anutosh491
Copy link
Member

  1. All tests pass with the above diff
  2. Let's say I compare master vs the above diff. I try 3 inputs and then for the 4th input, I paste out the content

i) Master : We expose the generated files

image

ii) This commit : The unique dir isn't mounted in the vfs

image

I think that's exactly what we were looking for here !

@vgvassilev vgvassilev requested a review from anutosh491 January 12, 2026 10:46
This patch avoids bloating the current folder with temporary files created by
wasm and moves them in a separate tmp directory. This patch is a version of a
downstream one resolving this issue.

Co-authored with Anutosh Bhat.
Copy link
Member

@anutosh491 anutosh491 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@anutosh491 anutosh491 merged commit 4cec622 into llvm:main Jan 12, 2026
9 of 10 checks passed
@vgvassilev vgvassilev deleted the clang-repl-wasm-tmp branch January 12, 2026 11:28
Priyanshu3820 pushed a commit to Priyanshu3820/llvm-project that referenced this pull request Jan 18, 2026
…er. (llvm#175508)

This patch avoids bloating the current folder with temporary files
created by wasm and moves them in a separate tmp directory. This patch
is a version of a downstream one resolving this issue.

Co-authored with Anutosh Bhat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category clang-repl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants