[clang] Fix undefined lld::wasm::link symbol while building clangInterpreter for wasm (PR #113446)

2024-10-23 Thread Anutosh Bhat via cfe-commits

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

While building llvm (clang, lld) for wasm using emscripten (recipe hosted on 
emscripten-forge 
https://github.com/emscripten-forge/recipes/tree/main/recipes/recipes_emscripten/llvm)
 I ended up with this error 
```
 │ │ wasm-ld: error: ../../../../lib/libclangInterpreter.a(Wasm.cpp.o): 
undefined symbol: lld::wasm::link(llvm::ArrayRef, 
llvm::raw_ostream&, llvm:
 │ │ :raw_ostream&, bool, bool)
 ```
 This is due to the link function here 
 
https://github.com/llvm/llvm-project/blob/a4819bd46d8baebc3aaa8b38f78065de33593199/clang/lib/Interpreter/Wasm.cpp#L25-L30
 
 This was added through this PR 
(https://github.com/llvm/llvm-project/pull/86402) as an attempt to support 
running clang-repl and executing C++ code interactively inside a Javascript 
engine using WebAssembly when built with Emscripten.
 
 The definition for link is present in lldwasm and when building for the 
emscripten platform we should be linking against it.
 

>From a10d5c42b8685295a5092917750d18a59441bad5 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 23 Oct 2024 16:58:57 +0530
Subject: [PATCH] Fix undefined lld::wasm::link symbol while building
 clangInterpreter for wasm

---
 clang/lib/Interpreter/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index 2cc7c59b61d318..d5ffe78251d253 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -14,6 +14,7 @@ set(LLVM_LINK_COMPONENTS
 
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
+  set(WASM_LINK lldWasm)
 endif()
 
 add_clang_library(clangInterpreter
@@ -44,6 +45,7 @@ add_clang_library(clangInterpreter
   clangParse
   clangSema
   clangSerialization
+  ${WASM_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)

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


[clang] Fix undefined lld::wasm::link symbol while building clangInterpreter for wasm (PR #113446)

2024-10-23 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/113446

>From a19fb1d00dfd9a3be9463396d476ef7415391a0f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 23 Oct 2024 16:58:57 +0530
Subject: [PATCH] Fix undefined lld::wasm::link symbol while building
 clangInterpreter for wasm

---
 clang/lib/Interpreter/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index 2cc7c59b61d318..d5ffe78251d253 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -14,6 +14,7 @@ set(LLVM_LINK_COMPONENTS
 
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
+  set(WASM_LINK lldWasm)
 endif()
 
 add_clang_library(clangInterpreter
@@ -44,6 +45,7 @@ add_clang_library(clangInterpreter
   clangParse
   clangSema
   clangSerialization
+  ${WASM_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)

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


[clang] [clang-repl] Fix undefined lld::wasm::link symbol while building clangInterpreter for wasm (PR #113446)

2024-10-23 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/113446
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix undefined lld::wasm::link symbol while building clangInterpreter for wasm (PR #113446)

2024-10-23 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/113446

>From 921d6d4f2acdd85d95bb8c5904177b0d39b5393b Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 23 Oct 2024 16:58:57 +0530
Subject: [PATCH] Fix undefined lld::wasm::link symbol while building
 clangInterpreter for wasm

---
 clang/lib/Interpreter/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index 2cc7c59b61d318..d5ffe78251d253 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -14,6 +14,7 @@ set(LLVM_LINK_COMPONENTS
 
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
+  set(WASM_LINK lldWasm)
 endif()
 
 add_clang_library(clangInterpreter
@@ -44,6 +45,7 @@ add_clang_library(clangInterpreter
   clangParse
   clangSema
   clangSerialization
+  ${WASM_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)

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


[clang] [clang-repl] Fix undefined lld::wasm::link symbol while building clangInterpreter for wasm (PR #113446)

2024-10-23 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/113446

>From d61f58f66839652ec4c1101521c2a7297cffacff Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 23 Oct 2024 16:58:57 +0530
Subject: [PATCH] Fix undefined lld::wasm::link symbol while building
 clangInterpreter for wasm

---
 clang/lib/Interpreter/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index 2cc7c59b61d318..d5ffe78251d253 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -14,6 +14,7 @@ set(LLVM_LINK_COMPONENTS
 
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
+  set(WASM_LINK lldWasm)
 endif()
 
 add_clang_library(clangInterpreter
@@ -44,6 +45,7 @@ add_clang_library(clangInterpreter
   clangParse
   clangSema
   clangSerialization
+  ${WASM_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)

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


[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-29 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/117978

>From 987f77db9d45dee264c60f434652131438784f6f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 28 Nov 2024 14:02:00 +0530
Subject: [PATCH 1/4] Fix generation of wasm binaries while running clang-repl
 in browser

---
 clang/lib/Interpreter/CMakeLists.txt  |  2 +
 clang/lib/Interpreter/Interpreter.cpp |  1 +
 clang/lib/Interpreter/Wasm.cpp| 57 ---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index df7ea82e0dada5..bf70cdfbee01e1 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
   set(WASM_LINK lldWasm)
+  set(COMMON_LINK lldCommon)
 endif()
 
 add_clang_library(clangInterpreter
@@ -47,6 +48,7 @@ add_clang_library(clangInterpreter
   clangSema
   clangSerialization
   ${WASM_LINK}
+  ${COMMON_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..887b494ff98f19 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -201,6 +201,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..0fd6ad509c2938 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -23,6 +23,31 @@
 #include 
 
 namespace lld {
+enum Flavor {
+  Invalid,
+  Gnu, // -flavor gnu
+  MinGW,   // -flavor gnu MinGW
+  WinLink, // -flavor link
+  Darwin,  // -flavor darwin
+  Wasm,// -flavor wasm
+};
+
+using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &,
+llvm::raw_ostream &, bool, bool);
+
+struct DriverDef {
+  Flavor f;
+  Driver d;
+};
+
+struct Result {
+int retCode;
+bool canRunAgain;
+};
+
+Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
+llvm::raw_ostream &stderrOS, llvm::ArrayRef 
drivers);
+
 namespace wasm {
 bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
   llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
@@ -51,45 +76,47 @@ 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 OutputFileName = PTU.TheModule->getName().str() + ".wasm";
+  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";  // For 
the wasm object
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";  // 
For the wasm binary
 
   std::error_code Error;
-  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), 
Error);
 
   llvm::legacy::PassManager PM;
-  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+  if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
  llvm::CodeGenFileType::ObjectFile)) {
 return llvm::make_error(
 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
   }
 
   if (!PM.run(*PTU.TheModule)) {
-
 return llvm::make_error("Failed to emit Wasm object.",
llvm::inconvertibleErrorCode());
   }
 
-  OutputFile.close();
+  ObjectFileOutput.close();
 
   std::vector LinkerArgs = {"wasm-ld",
   "-shared",
   "--import-memory",
-  "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",
-  OutputFileName.c_str(),
+  ObjectFileName.c_str(),
   "-o",
-  OutputFileName.c_str()};
-  int Result =
-  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
-  if (!Result)
+  BinaryFileName.c_str()};
+
+  const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};
+  std::vector WasmDriverArg

[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-29 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/117978

>From 987f77db9d45dee264c60f434652131438784f6f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 28 Nov 2024 14:02:00 +0530
Subject: [PATCH 1/4] Fix generation of wasm binaries while running clang-repl
 in browser

---
 clang/lib/Interpreter/CMakeLists.txt  |  2 +
 clang/lib/Interpreter/Interpreter.cpp |  1 +
 clang/lib/Interpreter/Wasm.cpp| 57 ---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index df7ea82e0dada5..bf70cdfbee01e1 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
   set(WASM_LINK lldWasm)
+  set(COMMON_LINK lldCommon)
 endif()
 
 add_clang_library(clangInterpreter
@@ -47,6 +48,7 @@ add_clang_library(clangInterpreter
   clangSema
   clangSerialization
   ${WASM_LINK}
+  ${COMMON_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..887b494ff98f19 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -201,6 +201,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..0fd6ad509c2938 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -23,6 +23,31 @@
 #include 
 
 namespace lld {
+enum Flavor {
+  Invalid,
+  Gnu, // -flavor gnu
+  MinGW,   // -flavor gnu MinGW
+  WinLink, // -flavor link
+  Darwin,  // -flavor darwin
+  Wasm,// -flavor wasm
+};
+
+using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &,
+llvm::raw_ostream &, bool, bool);
+
+struct DriverDef {
+  Flavor f;
+  Driver d;
+};
+
+struct Result {
+int retCode;
+bool canRunAgain;
+};
+
+Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
+llvm::raw_ostream &stderrOS, llvm::ArrayRef 
drivers);
+
 namespace wasm {
 bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
   llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
@@ -51,45 +76,47 @@ 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 OutputFileName = PTU.TheModule->getName().str() + ".wasm";
+  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";  // For 
the wasm object
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";  // 
For the wasm binary
 
   std::error_code Error;
-  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), 
Error);
 
   llvm::legacy::PassManager PM;
-  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+  if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
  llvm::CodeGenFileType::ObjectFile)) {
 return llvm::make_error(
 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
   }
 
   if (!PM.run(*PTU.TheModule)) {
-
 return llvm::make_error("Failed to emit Wasm object.",
llvm::inconvertibleErrorCode());
   }
 
-  OutputFile.close();
+  ObjectFileOutput.close();
 
   std::vector LinkerArgs = {"wasm-ld",
   "-shared",
   "--import-memory",
-  "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",
-  OutputFileName.c_str(),
+  ObjectFileName.c_str(),
   "-o",
-  OutputFileName.c_str()};
-  int Result =
-  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
-  if (!Result)
+  BinaryFileName.c_str()};
+
+  const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};
+  std::vector WasmDriverArg

[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-29 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Hey @vgvassilev thanks for the reviews. Addressed them. Once merged, I shall do 
a cherry pick of the required commit !

https://github.com/llvm/llvm-project/pull/117978
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-29 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

/cherry-pick a174aa1e416c4e27945f5a8c646b119126dc8441

https://github.com/llvm/llvm-project/pull/117978
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Remove redundant shared flag while running clang-repl in browser (PR #118107)

2024-11-29 Thread Anutosh Bhat via cfe-commits

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

While running clang-repl in the browser, we would be interested in this cc1 
command 

`
"" -cc1 -triple wasm32-unknown-emscripten -emit-obj -disable-free 
-clear-ast-before-backend -disable-llvm-verifier -discard-value-names 
-main-file-name "<<< inputs >>>" -mrelocation-model static -mframe-pointer=none 
-ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic 
-debugger-tuning=gdb -fdebug-compilation-dir=/ -v -fcoverage-compilation-dir=/ 
-resource-dir /lib/clang/19 -internal-isystem /include/wasm32-emscripten/c++/v1 
-internal-isystem /include/c++/v1 -internal-isystem /lib/clang/19/include 
-internal-isystem /include/wasm32-emscripten -internal-isystem /include 
-std=c++17 -fdeprecated-macro -ferror-limit 19 -fvisibility=default 
-fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions 
-fincremental-extensions -o "<<< inputs >>>.o" -x c++ "<<< inputs >>>"
`

As can be seen shared is anyway overwritten by static which is also what would 
be provided by default. Hence we can get rid of the shared flag here.

>From 594b58c80cc0e3a27a3f4c6b92bd8e54a3118ad5 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Fri, 29 Nov 2024 21:46:25 +0530
Subject: [PATCH] Remove redundant shared flag while running clang-repl in
 browser

---
 clang/lib/Interpreter/Interpreter.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 887b494ff98f19..fa4c1439c92612 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -200,7 +200,6 @@ IncrementalCompilerBuilder::CreateCpp() {
 #ifdef __EMSCRIPTEN__
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
-  Argv.push_back("-shared");
   Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());

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


[clang] [clang-repl] Remove redundant shared flag while running clang-repl in browser (PR #118107)

2024-11-29 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/118107
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-28 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/117978

>From 987f77db9d45dee264c60f434652131438784f6f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 28 Nov 2024 14:02:00 +0530
Subject: [PATCH 1/3] Fix generation of wasm binaries while running clang-repl
 in browser

---
 clang/lib/Interpreter/CMakeLists.txt  |  2 +
 clang/lib/Interpreter/Interpreter.cpp |  1 +
 clang/lib/Interpreter/Wasm.cpp| 57 ---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index df7ea82e0dada5..bf70cdfbee01e1 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
   set(WASM_LINK lldWasm)
+  set(COMMON_LINK lldCommon)
 endif()
 
 add_clang_library(clangInterpreter
@@ -47,6 +48,7 @@ add_clang_library(clangInterpreter
   clangSema
   clangSerialization
   ${WASM_LINK}
+  ${COMMON_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..887b494ff98f19 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -201,6 +201,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..0fd6ad509c2938 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -23,6 +23,31 @@
 #include 
 
 namespace lld {
+enum Flavor {
+  Invalid,
+  Gnu, // -flavor gnu
+  MinGW,   // -flavor gnu MinGW
+  WinLink, // -flavor link
+  Darwin,  // -flavor darwin
+  Wasm,// -flavor wasm
+};
+
+using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &,
+llvm::raw_ostream &, bool, bool);
+
+struct DriverDef {
+  Flavor f;
+  Driver d;
+};
+
+struct Result {
+int retCode;
+bool canRunAgain;
+};
+
+Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
+llvm::raw_ostream &stderrOS, llvm::ArrayRef 
drivers);
+
 namespace wasm {
 bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
   llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
@@ -51,45 +76,47 @@ 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 OutputFileName = PTU.TheModule->getName().str() + ".wasm";
+  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";  // For 
the wasm object
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";  // 
For the wasm binary
 
   std::error_code Error;
-  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), 
Error);
 
   llvm::legacy::PassManager PM;
-  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+  if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
  llvm::CodeGenFileType::ObjectFile)) {
 return llvm::make_error(
 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
   }
 
   if (!PM.run(*PTU.TheModule)) {
-
 return llvm::make_error("Failed to emit Wasm object.",
llvm::inconvertibleErrorCode());
   }
 
-  OutputFile.close();
+  ObjectFileOutput.close();
 
   std::vector LinkerArgs = {"wasm-ld",
   "-shared",
   "--import-memory",
-  "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",
-  OutputFileName.c_str(),
+  ObjectFileName.c_str(),
   "-o",
-  OutputFileName.c_str()};
-  int Result =
-  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
-  if (!Result)
+  BinaryFileName.c_str()};
+
+  const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};
+  std::vector WasmDriverArg

[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-28 Thread Anutosh Bhat via cfe-commits


@@ -109,12 +139,12 @@ llvm::Error WasmIncrementalExecutor::runCtors() const {
   return llvm::Error::success();
 }
 
-llvm::Error WasmIncrementalExecutor::cleanUp() const {

anutosh491 wrote:

I think we would need it. I see a build error otherwise 
```
 │ │ 
/home/runner/work/recipes/recipes/output/bld/rattler-build_llvm_1732028457/work/clang/lib/Interpreter/Wasm.cpp:111:38:
 error: out-of-line definition of 'cl
 │ │ eanUp' does not match any declaration in 'clang::WasmIncrementalExecutor'
 │ │   111 | llvm::Error WasmIncrementalExecutor::cleanUp() const {
 │ │   |  ^~~
 │ │ 
/home/runner/work/recipes/recipes/output/bld/rattler-build_llvm_1732028457/work/clang/lib/Interpreter/Wasm.h:31:15:
 note: member declaration does not match
 │ │  because it is not const qualified
 │ │31 |   llvm::Error cleanUp() override;
 │ │   |   ^ 
 ```
 
 CleanUp is defined like this 
 
https://github.com/llvm/llvm-project/blob/b2d3cb1e7502a5ddb7d6688dce5cf51573f6189c/clang/lib/Interpreter/Wasm.h#L31

https://github.com/llvm/llvm-project/pull/117978
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-28 Thread Anutosh Bhat via cfe-commits

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

None

>From 987f77db9d45dee264c60f434652131438784f6f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 28 Nov 2024 14:02:00 +0530
Subject: [PATCH] Fix generation of wasm binaries while running clang-repl in
 browser

---
 clang/lib/Interpreter/CMakeLists.txt  |  2 +
 clang/lib/Interpreter/Interpreter.cpp |  1 +
 clang/lib/Interpreter/Wasm.cpp| 57 ---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index df7ea82e0dada5..bf70cdfbee01e1 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
   set(WASM_LINK lldWasm)
+  set(COMMON_LINK lldCommon)
 endif()
 
 add_clang_library(clangInterpreter
@@ -47,6 +48,7 @@ add_clang_library(clangInterpreter
   clangSema
   clangSerialization
   ${WASM_LINK}
+  ${COMMON_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..887b494ff98f19 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -201,6 +201,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..0fd6ad509c2938 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -23,6 +23,31 @@
 #include 
 
 namespace lld {
+enum Flavor {
+  Invalid,
+  Gnu, // -flavor gnu
+  MinGW,   // -flavor gnu MinGW
+  WinLink, // -flavor link
+  Darwin,  // -flavor darwin
+  Wasm,// -flavor wasm
+};
+
+using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &,
+llvm::raw_ostream &, bool, bool);
+
+struct DriverDef {
+  Flavor f;
+  Driver d;
+};
+
+struct Result {
+int retCode;
+bool canRunAgain;
+};
+
+Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
+llvm::raw_ostream &stderrOS, llvm::ArrayRef 
drivers);
+
 namespace wasm {
 bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
   llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
@@ -51,45 +76,47 @@ 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 OutputFileName = PTU.TheModule->getName().str() + ".wasm";
+  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";  // For 
the wasm object
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";  // 
For the wasm binary
 
   std::error_code Error;
-  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), 
Error);
 
   llvm::legacy::PassManager PM;
-  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+  if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
  llvm::CodeGenFileType::ObjectFile)) {
 return llvm::make_error(
 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
   }
 
   if (!PM.run(*PTU.TheModule)) {
-
 return llvm::make_error("Failed to emit Wasm object.",
llvm::inconvertibleErrorCode());
   }
 
-  OutputFile.close();
+  ObjectFileOutput.close();
 
   std::vector LinkerArgs = {"wasm-ld",
   "-shared",
   "--import-memory",
-  "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",
-  OutputFileName.c_str(),
+  ObjectFileName.c_str(),
   "-o",
-  OutputFileName.c_str()};
-  int Result =
-  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
-  if (!Result)
+  BinaryFileName.c_str()};
+
+  const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};
+  std::vector WasmDriverA

[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-28 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/117978

>From 987f77db9d45dee264c60f434652131438784f6f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 28 Nov 2024 14:02:00 +0530
Subject: [PATCH 1/2] Fix generation of wasm binaries while running clang-repl
 in browser

---
 clang/lib/Interpreter/CMakeLists.txt  |  2 +
 clang/lib/Interpreter/Interpreter.cpp |  1 +
 clang/lib/Interpreter/Wasm.cpp| 57 ---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index df7ea82e0dada5..bf70cdfbee01e1 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
   set(WASM_LINK lldWasm)
+  set(COMMON_LINK lldCommon)
 endif()
 
 add_clang_library(clangInterpreter
@@ -47,6 +48,7 @@ add_clang_library(clangInterpreter
   clangSema
   clangSerialization
   ${WASM_LINK}
+  ${COMMON_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..887b494ff98f19 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -201,6 +201,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..0fd6ad509c2938 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -23,6 +23,31 @@
 #include 
 
 namespace lld {
+enum Flavor {
+  Invalid,
+  Gnu, // -flavor gnu
+  MinGW,   // -flavor gnu MinGW
+  WinLink, // -flavor link
+  Darwin,  // -flavor darwin
+  Wasm,// -flavor wasm
+};
+
+using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &,
+llvm::raw_ostream &, bool, bool);
+
+struct DriverDef {
+  Flavor f;
+  Driver d;
+};
+
+struct Result {
+int retCode;
+bool canRunAgain;
+};
+
+Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
+llvm::raw_ostream &stderrOS, llvm::ArrayRef 
drivers);
+
 namespace wasm {
 bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
   llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
@@ -51,45 +76,47 @@ 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 OutputFileName = PTU.TheModule->getName().str() + ".wasm";
+  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";  // For 
the wasm object
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";  // 
For the wasm binary
 
   std::error_code Error;
-  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), 
Error);
 
   llvm::legacy::PassManager PM;
-  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+  if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
  llvm::CodeGenFileType::ObjectFile)) {
 return llvm::make_error(
 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
   }
 
   if (!PM.run(*PTU.TheModule)) {
-
 return llvm::make_error("Failed to emit Wasm object.",
llvm::inconvertibleErrorCode());
   }
 
-  OutputFile.close();
+  ObjectFileOutput.close();
 
   std::vector LinkerArgs = {"wasm-ld",
   "-shared",
   "--import-memory",
-  "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",
-  OutputFileName.c_str(),
+  ObjectFileName.c_str(),
   "-o",
-  OutputFileName.c_str()};
-  int Result =
-  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
-  if (!Result)
+  BinaryFileName.c_str()};
+
+  const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};
+  std::vector WasmDriverArg

[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-28 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/117978

>From 987f77db9d45dee264c60f434652131438784f6f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 28 Nov 2024 14:02:00 +0530
Subject: [PATCH 1/2] Fix generation of wasm binaries while running clang-repl
 in browser

---
 clang/lib/Interpreter/CMakeLists.txt  |  2 +
 clang/lib/Interpreter/Interpreter.cpp |  1 +
 clang/lib/Interpreter/Wasm.cpp| 57 ---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index df7ea82e0dada5..bf70cdfbee01e1 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
   set(WASM_LINK lldWasm)
+  set(COMMON_LINK lldCommon)
 endif()
 
 add_clang_library(clangInterpreter
@@ -47,6 +48,7 @@ add_clang_library(clangInterpreter
   clangSema
   clangSerialization
   ${WASM_LINK}
+  ${COMMON_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..887b494ff98f19 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -201,6 +201,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..0fd6ad509c2938 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -23,6 +23,31 @@
 #include 
 
 namespace lld {
+enum Flavor {
+  Invalid,
+  Gnu, // -flavor gnu
+  MinGW,   // -flavor gnu MinGW
+  WinLink, // -flavor link
+  Darwin,  // -flavor darwin
+  Wasm,// -flavor wasm
+};
+
+using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &,
+llvm::raw_ostream &, bool, bool);
+
+struct DriverDef {
+  Flavor f;
+  Driver d;
+};
+
+struct Result {
+int retCode;
+bool canRunAgain;
+};
+
+Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
+llvm::raw_ostream &stderrOS, llvm::ArrayRef 
drivers);
+
 namespace wasm {
 bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
   llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
@@ -51,45 +76,47 @@ 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 OutputFileName = PTU.TheModule->getName().str() + ".wasm";
+  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";  // For 
the wasm object
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";  // 
For the wasm binary
 
   std::error_code Error;
-  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), 
Error);
 
   llvm::legacy::PassManager PM;
-  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+  if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
  llvm::CodeGenFileType::ObjectFile)) {
 return llvm::make_error(
 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
   }
 
   if (!PM.run(*PTU.TheModule)) {
-
 return llvm::make_error("Failed to emit Wasm object.",
llvm::inconvertibleErrorCode());
   }
 
-  OutputFile.close();
+  ObjectFileOutput.close();
 
   std::vector LinkerArgs = {"wasm-ld",
   "-shared",
   "--import-memory",
-  "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",
-  OutputFileName.c_str(),
+  ObjectFileName.c_str(),
   "-o",
-  OutputFileName.c_str()};
-  int Result =
-  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
-  if (!Result)
+  BinaryFileName.c_str()};
+
+  const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};
+  std::vector WasmDriverArg

[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-28 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/117978

>From 987f77db9d45dee264c60f434652131438784f6f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 28 Nov 2024 14:02:00 +0530
Subject: [PATCH 1/2] Fix generation of wasm binaries while running clang-repl
 in browser

---
 clang/lib/Interpreter/CMakeLists.txt  |  2 +
 clang/lib/Interpreter/Interpreter.cpp |  1 +
 clang/lib/Interpreter/Wasm.cpp| 57 ---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index df7ea82e0dada5..bf70cdfbee01e1 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
 if (EMSCRIPTEN AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
   set(WASM_SRC Wasm.cpp)
   set(WASM_LINK lldWasm)
+  set(COMMON_LINK lldCommon)
 endif()
 
 add_clang_library(clangInterpreter
@@ -47,6 +48,7 @@ add_clang_library(clangInterpreter
   clangSema
   clangSerialization
   ${WASM_LINK}
+  ${COMMON_LINK}
   )
 
 if ((MINGW OR CYGWIN) AND BUILD_SHARED_LIBS)
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..887b494ff98f19 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -201,6 +201,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..0fd6ad509c2938 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -23,6 +23,31 @@
 #include 
 
 namespace lld {
+enum Flavor {
+  Invalid,
+  Gnu, // -flavor gnu
+  MinGW,   // -flavor gnu MinGW
+  WinLink, // -flavor link
+  Darwin,  // -flavor darwin
+  Wasm,// -flavor wasm
+};
+
+using Driver = bool (*)(llvm::ArrayRef, llvm::raw_ostream &,
+llvm::raw_ostream &, bool, bool);
+
+struct DriverDef {
+  Flavor f;
+  Driver d;
+};
+
+struct Result {
+int retCode;
+bool canRunAgain;
+};
+
+Result lldMain(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
+llvm::raw_ostream &stderrOS, llvm::ArrayRef 
drivers);
+
 namespace wasm {
 bool link(llvm::ArrayRef args, llvm::raw_ostream &stdoutOS,
   llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);
@@ -51,45 +76,47 @@ 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 OutputFileName = PTU.TheModule->getName().str() + ".wasm";
+  std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";  // For 
the wasm object
+  std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";  // 
For the wasm binary
 
   std::error_code Error;
-  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+  llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), 
Error);
 
   llvm::legacy::PassManager PM;
-  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+  if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
  llvm::CodeGenFileType::ObjectFile)) {
 return llvm::make_error(
 "Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());
   }
 
   if (!PM.run(*PTU.TheModule)) {
-
 return llvm::make_error("Failed to emit Wasm object.",
llvm::inconvertibleErrorCode());
   }
 
-  OutputFile.close();
+  ObjectFileOutput.close();
 
   std::vector LinkerArgs = {"wasm-ld",
   "-shared",
   "--import-memory",
-  "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",
-  OutputFileName.c_str(),
+  ObjectFileName.c_str(),
   "-o",
-  OutputFileName.c_str()};
-  int Result =
-  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
-  if (!Result)
+  BinaryFileName.c_str()};
+
+  const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};
+  std::vector WasmDriverArg

[clang] [clang-repl] Fix generation of wasm binaries while running clang-repl in browser (PR #117978)

2024-11-28 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/117978
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Use default visibility for symbols while building CompilerInstance against emscripten (PR #116779)

2024-11-28 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 closed 
https://github.com/llvm/llvm-project/pull/116779
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Use default visibility for symbols while building CompilerInstance against emscripten (PR #116779)

2024-11-28 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Closing as superseded by https://github.com/llvm/llvm-project/pull/116779

https://github.com/llvm/llvm-project/pull/116779
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Improve flags responsible for generating shared wasm binaries (PR #116735)

2024-11-18 Thread Anutosh Bhat via cfe-commits


@@ -72,13 +72,13 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   OutputFile.close();
 
   std::vector LinkerArgs = {"wasm-ld",
-  "-pie",
+  "-shared",
   "--import-memory",
   "--no-entry",
   "--export-all",
   "--experimental-pic",
-  "--no-export-dynamic",

anutosh491 wrote:

This change has been made cause `-shared` by default invokes `--export-dynamic`.
Not a breaking change that harms us (but surely we would want `shared` instead 
of `pie`) 

https://github.com/llvm/llvm-project/pull/116735
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Improve flags responsible for generating shared wasm binaries (PR #116735)

2024-11-18 Thread Anutosh Bhat via cfe-commits

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

There are a couple changes in this PR that help getting clang-repl to run in 
the browser. Using a jupyterlite instance for the example pasted below

1) Updating flags responsible for generating shared wasm binaries that need to 
be dynamically loaded Most Importantly as can be seen in the changes `shared` 
and `allow-undefined` are crucial.


![image](https://github.com/user-attachments/assets/1183fd44-8951-496a-899a-e4af39a48447)

2) While exiting we encounter this.


![image](https://github.com/user-attachments/assets/9487a3f4-7200-471d-ba88-09e98ccbc47a)


Now as can be seen here 

https://github.com/llvm/llvm-project/blob/cd418030de7ae75750bc4e48d1238baf03c675e5/clang/lib/Interpreter/Interpreter.cpp#L421-L430

We call cleanUP in the destructor. Now cleanUP through IncrementalExecutor 
tries to deinitialize the JIT which wasn't even intialized as runCtors in 
wasm.cpp is a no-op 

https://github.com/llvm/llvm-project/blob/cd418030de7ae75750bc4e48d1238baf03c675e5/clang/lib/Interpreter/IncrementalExecutor.cpp#L94-L101

https://github.com/llvm/llvm-project/blob/cd418030de7ae75750bc4e48d1238baf03c675e5/clang/lib/Interpreter/Wasm.cpp#L107-L109



>From 07371ceff88bbfce990f9bf284bd64b4bbfd851a Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 19 Nov 2024 08:37:36 +0530
Subject: [PATCH] Improve flags responsible for generating shared wasm binaries

---
 clang/lib/Interpreter/IncrementalExecutor.h |  2 +-
 clang/lib/Interpreter/Wasm.cpp  | 11 +--
 clang/lib/Interpreter/Wasm.h|  1 +
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalExecutor.h 
b/clang/lib/Interpreter/IncrementalExecutor.h
index 7954cde36588bd..dbd61f0b8b1ebb 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.h
+++ b/clang/lib/Interpreter/IncrementalExecutor.h
@@ -56,7 +56,7 @@ class IncrementalExecutor {
   virtual llvm::Error addModule(PartialTranslationUnit &PTU);
   virtual llvm::Error removeModule(PartialTranslationUnit &PTU);
   virtual llvm::Error runCtors() const;
-  llvm::Error cleanUp();
+  virtual llvm::Error cleanUp();
   llvm::Expected
   getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 1001410aa0f279..6f00bac8c0bc46 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -72,13 +72,14 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   OutputFile.close();
 
   std::vector LinkerArgs = {"wasm-ld",
-  "-pie",
+  "-shared",
   "--import-memory",
   "--no-entry",
   "--export-all",
   "--experimental-pic",
-  "--no-export-dynamic",
+  "--export-dynamic",
   "--stack-first",
+  "--allow-undefined",
   OutputFileName.c_str(),
   "-o",
   OutputFileName.c_str()};
@@ -109,6 +110,12 @@ llvm::Error WasmIncrementalExecutor::runCtors() const {
   return llvm::Error::success();
 }
 
+llvm::Error WasmIncrementalExecutor::cleanUp() const {
+  // Can't call cleanUp through IncrementalExecutor as it
+  // tries to deinitialize JIT which hasn't been initialized
+  return llvm::Error::success();
+}
+
 WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;
 
 } // namespace clang
diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h
index b1fd88024f14d7..4632613326d39b 100644
--- a/clang/lib/Interpreter/Wasm.h
+++ b/clang/lib/Interpreter/Wasm.h
@@ -28,6 +28,7 @@ class WasmIncrementalExecutor : public IncrementalExecutor {
   llvm::Error addModule(PartialTranslationUnit &PTU) override;
   llvm::Error removeModule(PartialTranslationUnit &PTU) override;
   llvm::Error runCtors() const override;
+  llvm::Error cleanUp() override;
 
   ~WasmIncrementalExecutor() override;
 };

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


[clang] [clang-repl] Use default visibility for symbols while building CompilerInstance against emscripten (PR #116779)

2024-11-20 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

I just realized that cleanUp shouldn't use const. While building against 
emscripten, I go this error 
```
 │ │ 
/home/runner/work/recipes/recipes/output/bld/rattler-build_llvm_1732028457/work/clang/lib/Interpreter/Wasm.cpp:111:38:
 error: out-of-line definition of 'cl
 │ │ eanUp' does not match any declaration in 'clang::WasmIncrementalExecutor'
 │ │   111 | llvm::Error WasmIncrementalExecutor::cleanUp() const {
 │ │   |  ^~~
 │ │ 
/home/runner/work/recipes/recipes/output/bld/rattler-build_llvm_1732028457/work/clang/lib/Interpreter/Wasm.h:31:15:
 note: member declaration does not match
 │ │  because it is not const qualified
 │ │31 |   llvm::Error cleanUp() override;
 │ │   |   ^ 
 ```
 
 Fixed this through the latest commit.

https://github.com/llvm/llvm-project/pull/116779
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Use default visibility for symbols while building CompilerInstance against emscripten (PR #116779)

2024-11-20 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/116779

>From 616ebd6b6487eeaa302e6d33de3a371e6c9843f5 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 19 Nov 2024 15:38:37 +0530
Subject: [PATCH 1/2] Use default visibility for symbols while building
 CompilerInstance against emscripten

---
 clang/lib/Interpreter/Interpreter.cpp | 1 +
 clang/lib/Interpreter/Wasm.cpp| 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 73ad766e655180..999271aae7491d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -197,6 +197,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default")
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..6d4cc478dd6a85 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -75,7 +75,6 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   "-shared",
   "--import-memory",
   "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",

>From fedc699c66ba973798bd452b5a6607d412e4cb75 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Tue, 19 Nov 2024 17:27:59 +0530
Subject: [PATCH 2/2] Update Interpreter.cpp

---
 clang/lib/Interpreter/Interpreter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 999271aae7491d..00111daab01e37 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -197,7 +197,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
-  Argv.push_back("-fvisibility=default")
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 

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


[clang] [clang-repl] Use default visibility for symbols while building CompilerInstance against emscripten (PR #116779)

2024-11-19 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/116779

>From 616ebd6b6487eeaa302e6d33de3a371e6c9843f5 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 19 Nov 2024 15:38:37 +0530
Subject: [PATCH 1/2] Use default visibility for symbols while building
 CompilerInstance against emscripten

---
 clang/lib/Interpreter/Interpreter.cpp | 1 +
 clang/lib/Interpreter/Wasm.cpp| 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 73ad766e655180..999271aae7491d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -197,6 +197,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default")
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..6d4cc478dd6a85 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -75,7 +75,6 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   "-shared",
   "--import-memory",
   "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",

>From fedc699c66ba973798bd452b5a6607d412e4cb75 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Tue, 19 Nov 2024 17:27:59 +0530
Subject: [PATCH 2/2] Update Interpreter.cpp

---
 clang/lib/Interpreter/Interpreter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 999271aae7491d..00111daab01e37 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -197,7 +197,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
-  Argv.push_back("-fvisibility=default")
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 

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


[clang] [clang-repl] Use default visibility for symbols while building CompilerInstance against emscripten (PR #116779)

2024-11-19 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/116779

>From 616ebd6b6487eeaa302e6d33de3a371e6c9843f5 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 19 Nov 2024 15:38:37 +0530
Subject: [PATCH 1/3] Use default visibility for symbols while building
 CompilerInstance against emscripten

---
 clang/lib/Interpreter/Interpreter.cpp | 1 +
 clang/lib/Interpreter/Wasm.cpp| 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 73ad766e655180..999271aae7491d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -197,6 +197,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default")
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..6d4cc478dd6a85 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -75,7 +75,6 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   "-shared",
   "--import-memory",
   "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",

>From fedc699c66ba973798bd452b5a6607d412e4cb75 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Tue, 19 Nov 2024 17:27:59 +0530
Subject: [PATCH 2/3] Update Interpreter.cpp

---
 clang/lib/Interpreter/Interpreter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 999271aae7491d..00111daab01e37 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -197,7 +197,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
-  Argv.push_back("-fvisibility=default")
+  Argv.push_back("-fvisibility=default");
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 

>From 4f05817d03f7bf5a9ec201c8de551810dca176f5 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 19 Nov 2024 21:28:22 +0530
Subject: [PATCH 3/3] fix cleanUp

---
 clang/lib/Interpreter/Wasm.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 6d4cc478dd6a85..8d4bc20bcfe54f 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -108,7 +108,7 @@ llvm::Error WasmIncrementalExecutor::runCtors() const {
   return llvm::Error::success();
 }
 
-llvm::Error WasmIncrementalExecutor::cleanUp() const {
+llvm::Error WasmIncrementalExecutor::cleanUp() {
   // Can't call cleanUp through IncrementalExecutor as it
   // tries to deinitialize JIT which hasn't been initialized
   return llvm::Error::success();

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


[clang] [clang-repl] Use default visibility for symbols while building CompilerInstance against emscripten (PR #116779)

2024-11-19 Thread Anutosh Bhat via cfe-commits

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

When running clang-repl in the browser we end up having something like the 
following 
`
"" -cc1 -triple wasm32-unknown-emscripten .. -main-file-name "<<< inputs 
>>>"  -fvisibility=hidden  -o "<<< inputs >>>.o" -x c++ "<<< inputs >>>"
`


Due to the default introduced through this commit 
(https://github.com/llvm/llvm-project/commit/e3d71e14d756d78f9e2538f2e530aa7c051541cd#diff-b5496baaf5c83e1ffa1a26d0815843b8d3224aba84366cbb6aeecf703808c803R2083)


That being said, when we generated the shared libraries to be loaded on top of 
the main module while running clang-repl in the browser, we have to surpass the 
above through `--export-all`
https://github.com/llvm/llvm-project/blob/b3e2b1a7eb258a7c9c55691d08342eface083499/clang/lib/Interpreter/Wasm.cpp#L78

This is because obviously `incr_module_XX.wasm` might want to access symbols 
out of file from `incr_module_XX-1.wasm` to `incr_mdoule_0.wasm`

But this also exports some problematic things like `__dso_handle` that causes 
conflicts across modules.

![image](https://github.com/user-attachments/assets/007cb2a8-e732-4e7a-9950-ff54785e62a6)

A better way to deal with this is to pass `-fvisibility=default` to the 
`CompilerInstance`.

>From 616ebd6b6487eeaa302e6d33de3a371e6c9843f5 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 19 Nov 2024 15:38:37 +0530
Subject: [PATCH] Use default visibility for symbols while building
 CompilerInstance against emscripten

---
 clang/lib/Interpreter/Interpreter.cpp | 1 +
 clang/lib/Interpreter/Wasm.cpp| 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 73ad766e655180..999271aae7491d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -197,6 +197,7 @@ IncrementalCompilerBuilder::CreateCpp() {
   Argv.push_back("-target");
   Argv.push_back("wasm32-unknown-emscripten");
   Argv.push_back("-shared");
+  Argv.push_back("-fvisibility=default")
 #endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 79efbaa03982d0..6d4cc478dd6a85 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -75,7 +75,6 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   "-shared",
   "--import-memory",
   "--no-entry",
-  "--export-all",
   "--experimental-pic",
   "--stack-first",
   "--allow-undefined",

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


[clang] [clang-repl] Remove redundant shared flag while running clang-repl in browser (PR #118107)

2024-12-05 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

cc @vgvassilev 

Now that 19.1.5 is out. We can run clang-repl in the browser. And this flag is 
just adding redundancy and doesn't play a role. 
Small change. Should be ready !

https://github.com/llvm/llvm-project/pull/118107
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement value printing of custom types (PR #84769)

2025-01-05 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Hey @vgvassilev 

Just curious to know as to what work is left here and if any help is needed for 
the same ?



https://github.com/llvm/llvm-project/pull/84769
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix error recovery while PTU cleanup (PR #127467)

2025-02-17 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix error recovery while PTU cleanup (PR #127467)

2025-02-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

cc @ferdymercury @vgvassilev 

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix error recovery while PTU cleanup (PR #127467)

2025-02-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Hmmm, the code formatter has a suggestion. Just increases the lines on a simple 
test. Not sure if the code formatter should be respected here !

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix error recovery while PTU cleanup (PR #127467)

2025-02-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

For anyone interested in seeing the error at realtime, I am just leaving the 
static link for xeus-cpp-lite here.

https://compiler-research.org/xeus-cpp/lab/index.html

Possibly trying something like 
```
int x = 5;
auto capture = [&]() { return x * 2; };
```
Should display the assertion error in the console.

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix error recovery while PTU cleanup (PR #127467)

2025-02-17 Thread Anutosh Bhat via cfe-commits

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

Fixes #123300

What is seen 
```
clang-repl> int x = 42;
clang-repl> auto capture = [&]() { return x * 2; };
In file included from <<< inputs >>>:1:
input_line_4:1:17: error: non-local lambda expression cannot have a 
capture-default
1 | auto capture = [&]() { return x * 2; };
  | ^
zsh: segmentation fault  clang-repl --Xcc="-v"
```

Though the error is justified, we shouldn't be interested in exiting through a 
segfault in such cases.

The issue is that empty named decls weren't being taken care of resulting into 
this assert 

https://github.com/llvm/llvm-project/blob/c1a229252617ed58f943bf3f4698bd8204ee0f04/clang/include/clang/AST/DeclarationName.h#L503

Can also be seen when the example is attempted through xeus-cpp-lite.

![image](https://github.com/user-attachments/assets/9b0e6ead-138e-4b06-9ad9-fcb9f8d5bf6e)




>From 6ff448ed506e0ef75db2c9974a628a965e85df2f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp | 4 ++--
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1ebef0e434b3d 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -178,8 +178,8 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
 if (!ND)
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
-!D->getLangOpts().CPlusPlus)
+if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
+!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 578f1d4c0eac6..56ab155ebf5a4 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
+  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot have a capture-default"));
+  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
+
+  RecoverErr = Interp->Parse("int validVar = 10;");
+  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The

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


[clang] Fix error recovery while PTU cleanup (PR #127467)

2025-02-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Should be fixed now 
```
clang-repl> int x = 5; auto capture = [&]() { return x * 2; };
In file included from <<< inputs >>>:1:
input_line_1:1:28: error: non-local lambda expression cannot have a 
capture-default
1 | int x = 5; auto capture = [&]() { return x * 2; };
  |^
error: Parsing failed.
```

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-02-26 Thread Anutosh Bhat via cfe-commits


@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();

anutosh491 wrote:

Shall shift it there if that's what is needed 

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-02-26 Thread Anutosh Bhat via cfe-commits


@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();

anutosh491 wrote:

Hmmm @vgvassilev 

Don't you think we should look at this from an error recovery perspective 
rather than something for lambdas ? If that's the case, the test is placed 
correctly.

The above test cause clang-repl to crash but should work if wrapped in a 
function or not presented as a top level decl.

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-02-26 Thread Anutosh Bhat via cfe-commits


@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();

anutosh491 wrote:

I meant I see quite some error recovery stuff being added in this place hence I 
added it here !

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-02-26 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From 6ff448ed506e0ef75db2c9974a628a965e85df2f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/2] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp | 4 ++--
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1ebef0e434b3d 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -178,8 +178,8 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
 if (!ND)
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
-!D->getLangOpts().CPlusPlus)
+if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
+!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 578f1d4c0eac6..56ab155ebf5a4 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
+  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot have a capture-default"));
+  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
+
+  RecoverErr = Interp->Parse("int validVar = 10;");
+  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The

>From 8b9a112721c45662374fd9995af6da4031507c89 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 26 Feb 2025 16:55:31 +0530
Subject: [PATCH 2/2] Update IncrementalParser.cpp

---
 clang/lib/Interpreter/IncrementalParser.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 1ebef0e434b3d..774219d9fc9ab 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -175,11 +175,11 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty()) 
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
-!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
+if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
+!D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }

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


[clang] [clang-repl] Update Docs related to Annotation Token (PR #127571)

2025-02-26 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

cc @vgvassilev 

https://github.com/llvm/llvm-project/pull/127571
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-02-26 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

cc @vgvassilev 

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] fix error recovery while parsing completely fails (PR #127087)

2025-02-18 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Maybe @vgvassilev might know more about this behaviour and if it is expected.

For some context you can read the discussion from here 
https://github.com/llvm/llvm-project/pull/127087#issuecomment-2664564232

https://github.com/llvm/llvm-project/pull/127087
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] fix error recovery while parsing completely fails (PR #127087)

2025-02-18 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Hmmm confused !

So does it really come down to the flag responsible for enabling assertions.
`LLVM_ENABLE_ASSERTIONS=ON`

https://github.com/llvm/llvm-project/pull/127087
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] fix error recovery while parsing completely fails (PR #127087)

2025-02-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

There are quite some made by @kr-2003 and me here.

1) We realize even without this fix, stuff worked perfectly on our Macos ARM 
devices. So looks like a Ubuntu issue anyways

![image](https://github.com/user-attachments/assets/666d5cc4-1f42-4c4a-8ff6-81aa67bc4650)

2) Obviously the try-catch block works when wrapped inside a class/function 
(not a top level decl)

3) If we build clang-repl with different configs, it works 

i) `-DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_PROJECTS=clang 
-DLLVM_USE_SPLIT_DWARF=ON -DLLVM_USE_LINKER=lld`

With this 
```
(base) abhinav@jarvis-2223:~/Desktop/Coding/llvm-project/build/bin$ 
./clang-repl --version
LLVM (http://llvm.org/):
  LLVM version 20.1.0-rc1
  Optimized build.
(base) abhinav@jarvis-2223:~/Desktop/Coding/llvm-project/build/bin$ ./clang-repl
clang-repl> #include 
clang-repl> try { throw 1; } catch { std::cout << "Caught Exception" << 
std::endl; }
In file included from <<< inputs >>>:1:
input_line_2:1:23: error: expected '('
1 | try { throw 1; } catch { std::cout << "Caught Exception" << std::endl; }
  |   ^
  |   (
error: Parsing failed.
clang-repl>
```

ii) With the following it fails.

```
-DLLVM_ENABLE_PROJECTS=clang\
-DLLVM_TARGETS_TO_BUILD="host;NVPTX"\
-DCMAKE_BUILD_TYPE=Release  \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DCLANG_ENABLE_STATIC_ANALYZER=OFF  \
-DCLANG_ENABLE_ARCMT=OFF\
-DCLANG_ENABLE_FORMAT=OFF   \
-DCLANG_ENABLE_BOOTSTRAP=OFF\
-DLLVM_ENABLE_ZSTD=OFF  \
-DLLVM_ENABLE_TERMINFO=OFF  \
-DLLVM_ENABLE_LIBXML2=OFF   \

```

Abhinav, could you also past the diff between how using two different configs 
affects the build ?
Maybe that might point us to the root cause behind all of this.

https://github.com/llvm/llvm-project/pull/127087
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-02-17 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Lay the foundation of pretty printing for C. (PR #89811)

2025-02-17 Thread Anutosh Bhat via cfe-commits


@@ -571,11 +571,8 @@ StmtResult Parser::ParseExprStatement(ParsedStmtContext 
StmtCtx) {
   }
 
   Token *CurTok = nullptr;
-  // If the semicolon is missing at the end of REPL input, consider if
-  // we want to do value printing. Note this is only enabled in C++ mode
-  // since part of the implementation requires C++ language features.
   // Note we shouldn't eat the token since the callback needs it.
-  if (Tok.is(tok::annot_repl_input_end) && Actions.getLangOpts().CPlusPlus)
+  if (Tok.is(tok::annot_repl_input_end))

anutosh491 wrote:

Hi,

Landed here as was studying a bit on `annot_repl_input_end`

Curious if this change also calls for a change in the docs ? @vgvassilev 

I see the following 

![image](https://github.com/user-attachments/assets/d6e22a0a-9c17-4859-a1cf-ea09124b6226)


https://github.com/llvm/llvm-project/pull/89811
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Address error recovery fixing infinite loop while parsing (PR #127569)

2025-02-17 Thread Anutosh Bhat via cfe-commits

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

I was interested in recovering error for some use cases.

Before the change
```
clang-repl> void foo() { int x = 5;
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
fatal error: too many errors emitted, stopping now [-ferror-limit=]
```

After the change
```
clang-repl> void foo() { int x = 5;
<<< inputs >>>:1:1: error: expected '}'
input_line_3:1:12: note: to match this '{'
1 | void foo() { int x = 5;
  |^
error: Parsing failed.
```

Some other use cases could be the following

Before the change
```
clang-repl> void (*test)() = [](){ if }
In file included from <<< inputs >>>:1:
input_line_1:1:27: error: expected '(' after 'if'
1 | void (*test)() = [](){ if }
  |   ^
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
fatal error: too many errors emitted, stopping now [-ferror-limit=]
```

After the change 
```
clang-repl> void (*test)() = [](){ if }
In file included from <<< inputs >>>:1:
input_line_1:1:27: error: expected '(' after 'if'
1 | void (*test)() = [](){ if }
  |   ^
<<< inputs >>>:1:1: error: expected '}'
input_line_1:1:22: note: to match this '{'
1 | void (*test)() = [](){ if }
  |  ^
input_line_1:1:28: error: expected ';' after top level declarator
1 | void (*test)() = [](){ if }
  |^
  |;
error: Parsing failed.
```

>From 5bda5cb946e8056a43e487e66487632729d37aee Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 18 Feb 2025 11:23:30 +0530
Subject: [PATCH] Address error recovery fixing infinite loop while parsing

---
 clang/lib/Parse/ParseStmt.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index cd4504630f871..a044c8acab927 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -1253,7 +1253,7 @@ StmtResult Parser::ParseCompoundStatementBody(bool 
isStmtExpr) {
 
   bool LastIsError = false;
   while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
- Tok.isNot(tok::eof)) {
+ Tok.isNot(tok::eof) && Tok.isNot(tok::annot_repl_input_end)) {
 if (Tok.is(tok::annot_pragma_unused)) {
   HandlePragmaUnused();
   continue;

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


[clang] [clang-repl] Address error recovery fixing infinite loop while parsing (PR #127569)

2025-02-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

This happens because the while loop is running only taking `eof` into account 
and not `annot_repl_input_end` which is specific to clang-repl i'd assume  
hence it never terminates.

I need to add tests for the same. But that being said here is something 
concerning I think 

**So finding those infinite loops won't be that hard. Even after the above 
change, I see**
```
clang-repl> namespace myspace {
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
fatal error: too many errors emitted, stopping now [-ferror-limit=]
```

This happens because there are quite some places apart from this too where 
`eof` is handled but `annot_repl_input_end` is not.
```
xeus-cpp-wasm-build) anutosh491@Anutoshs-MacBook-Air llvm-project % grep -ir 
"Tok.isNot(tok::eof)"
./clang-tools-extra/clang-tidy/google/ExplicitConstructorCheck.cpp:  } while 
(Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
./clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp:  bool 
NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
./clang/tools/clang-refactor/TestSupport.cpp:  for (Lex.LexFromRawLexer(Tok); 
Tok.isNot(tok::eof);
./clang/lib/Rewrite/HTMLRewrite.cpp:  while (Tok.isNot(tok::eof)) {
./clang/lib/Rewrite/HTMLRewrite.cpp:  while (!Tok.isAtStartOfLine() && 
Tok.isNot(tok::eof)) {
./clang/lib/Rewrite/HTMLRewrite.cpp:  while (Tok.isNot(tok::eof)) {
./clang/lib/Rewrite/TokenRewriter.cpp:  while (RawTok.isNot(tok::eof)) {
./clang/lib/Frontend/FrontendActions.cpp:  } while (Tok.isNot(tok::eof));
./clang/lib/Frontend/FrontendActions.cpp:  while (RawTok.isNot(tok::eof)) {
./clang/lib/Frontend/FrontendActions.cpp:  } while (Tok.isNot(tok::eof));
./clang/lib/Frontend/FrontendActions.cpp:  } while (Tok.isNot(tok::eof));
./clang/lib/Frontend/FrontendActions.cpp:  } while (Tok.isNot(tok::eof));
./clang/lib/Frontend/Rewrite/RewriteMacros.cpp:  } while 
(RawTok.isNot(tok::eof));
./clang/lib/Frontend/Rewrite/RewriteMacros.cpp:  while (RawTok.isNot(tok::eof) 
|| PPTok.isNot(tok::eof)) {
./clang/lib/Frontend/Rewrite/RewriteMacros.cpp:  while 
(!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
./clang/lib/Frontend/Rewrite/InclusionRewriter.cpp:  } while 
(Tok.isNot(tok::eof));
./clang/lib/Frontend/VerifyDiagnosticConsumer.cpp:  while (Tok.isNot(tok::eof)) 
{
./clang/lib/Lex/PPDirectives.cpp:  if (Callbacks && (Tok.isNot(tok::eof) || 
!isRecordingPreamble()))
./clang/lib/Lex/PPDirectives.cpp:assert(TmpTok.isNot(tok::eof) && "Didn't 
find end of -imacros!");
./clang/lib/Lex/Pragma.cpp:if (Tok.isNot(tok::r_paren) && 
Tok.isNot(tok::eof))
./clang/lib/Lex/Pragma.cpp:   Tok.isNot(tok::eof))
./clang/lib/Lex/Pragma.cpp:  while (Tok.isNot(tok::eof)) {
./clang/lib/Lex/PPLexerChange.cpp:  while (Tok.isNot(tok::eod) && 
Tok.isNot(tok::eof))
./clang/lib/Parse/ParseExprCXX.cpp:while (Tok.isNot(tok::eof))
./clang/lib/Parse/ParseDeclCXX.cpp:   Tok.isNot(tok::eof)) {
./clang/lib/Parse/ParseDeclCXX.cpp: Tok.isNot(tok::eof)) {
./clang/lib/Parse/ParseDeclCXX.cpp:   Tok.isNot(tok::eof)) {
./clang/lib/Parse/ParseOpenMP.cpp:  while (Cnt && Tok.isNot(tok::eof)) {
./clang/lib/Parse/ParseObjc.cpp:  while (Tok.getLocation() != OrigLoc && 
Tok.isNot(tok::eof))
./clang/lib/Parse/ParseDecl.cpp:  while (Tok.isNot(tok::eof))
./clang/lib/Parse/ParseDecl.cpp: Tok.isNot(tok::eof)) {
./clang/lib/Parse/ParseDecl.cpp:  (Tok.isNot(tok::eof) || Tok.getEofData() 
!= TypeStr.data())) {
./clang/lib/Parse/ParseDecl.cpp:  while (Tok.isNot(tok::eof))
./clang/lib/Parse/ParseHLSL.cpp:  while (Tok.isNot(tok::r_brace) && 
Tok.isNot(tok::eof)) {
./clang/lib/Parse/ParseCXXInlineMethods.cpp:if (Tok.isNot(tok::eof) || 
Tok.getEofData() != Param) {
./clang/lib/Parse/ParseCXXInlineMethods.cpp:  while (Tok.isNot(tok::eof))
./clang/lib/Parse/ParseCXXInlineMethods.cpp:if (Tok.isNot(tok::eof) || 
Tok.getEofData() != LM.Method)
./clang/lib/Parse/ParseCXXInlineMethods.cpp:while (Tok.isNot(tok::eof))
./clang/lib/Parse/ParseCXXInlineMethods.cpp:while (Tok.isNot(tok::eof))
./clang/lib/Parse/ParseCXXInlineMethods.cpp:  while (Tok.isNot(tok::

[clang] [clang-repl] Address error recovery fixing infinite loop while parsing (PR #127569)

2025-02-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

cc @ferdymercury

https://github.com/llvm/llvm-project/pull/127569
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Lay the foundation of pretty printing for C. (PR #89811)

2025-02-17 Thread Anutosh Bhat via cfe-commits


@@ -571,11 +571,8 @@ StmtResult Parser::ParseExprStatement(ParsedStmtContext 
StmtCtx) {
   }
 
   Token *CurTok = nullptr;
-  // If the semicolon is missing at the end of REPL input, consider if
-  // we want to do value printing. Note this is only enabled in C++ mode
-  // since part of the implementation requires C++ language features.
   // Note we shouldn't eat the token since the callback needs it.
-  if (Tok.is(tok::annot_repl_input_end) && Actions.getLangOpts().CPlusPlus)
+  if (Tok.is(tok::annot_repl_input_end))

anutosh491 wrote:

Cool going ahead and raising a PR for the same !

https://github.com/llvm/llvm-project/pull/89811
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Update Docs related to Annotation Token (PR #127571)

2025-02-17 Thread Anutosh Bhat via cfe-commits

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

The update in the source code was made through #89811 

>From a22878cde1e22796bdd629fda9c30be99c82ca79 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 18 Feb 2025 11:42:24 +0530
Subject: [PATCH] [clang-repl] Update Docs related to Annotation Token

---
 clang/docs/ClangRepl.rst | 9 +
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/clang/docs/ClangRepl.rst b/clang/docs/ClangRepl.rst
index 5399036c123fb..d58d8c50430ef 100644
--- a/clang/docs/ClangRepl.rst
+++ b/clang/docs/ClangRepl.rst
@@ -563,18 +563,11 @@ This is accomplished by identifying the end position of 
the user input
 (expression statement). This helps store and return the expression statement
 effectively, so that it can be printed (displayed to the user automatically).
 
-**Note:** This logic is only available for C++ for now, since part of the
-implementation itself requires C++ features. Future versions may support more
-languages.
-
 .. code-block:: console
 
   Token *CurTok = nullptr;
-  // If the semicolon is missing at the end of REPL input, consider if
-  // we want to do value printing. Note this is only enabled in C++ mode
-  // since part of the implementation requires C++ language features.
   // Note we shouldn't eat the token since the callback needs it.
-  if (Tok.is(tok::annot_repl_input_end) && Actions.getLangOpts().CPlusPlus)
+  if (Tok.is(tok::annot_repl_input_end))
 CurTok = &Tok;
   else
 // Otherwise, eat the semicolon.

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


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-10 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From 6ff448ed506e0ef75db2c9974a628a965e85df2f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/4] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp | 4 ++--
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1ebef0e434b3d 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -178,8 +178,8 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
 if (!ND)
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
-!D->getLangOpts().CPlusPlus)
+if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
+!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 578f1d4c0eac6..56ab155ebf5a4 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
+  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot have a capture-default"));
+  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
+
+  RecoverErr = Interp->Parse("int validVar = 10;");
+  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The

>From 8b9a112721c45662374fd9995af6da4031507c89 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 26 Feb 2025 16:55:31 +0530
Subject: [PATCH 2/4] Update IncrementalParser.cpp

---
 clang/lib/Interpreter/IncrementalParser.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 1ebef0e434b3d..774219d9fc9ab 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -175,11 +175,11 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty()) 
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
-!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
+if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
+!D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }

>From dae6ca63f0d443a6ec0df12e1abef183203c12de Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 11 Mar 2025 11:29:32 +0530
Subject: [PATCH 3/4] Add tests in lambda.cpp

---
 clang/test/Interpreter/lambda.cpp   | 11 +--
 clang/unittests/Interpreter/InterpreterTest.cpp |  7 ---
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..135f5fe604ae5 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 56ab155ebf5a4..578f1d4c0eac6 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,13 +114,6 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
-
-  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
-  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot ha

[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-10 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From bd1b0b2a14afeb73755db3a7deb6bffd4f50778c Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp |  2 +-
 clang/test/Interpreter/lambda.cpp   | 11 +--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 41d6304bd5f65..6343f17ed822a 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -176,7 +176,7 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty())
   continue;
 // Check if we need to clean up the IdResolver chain.
 if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..8f49f870fddb6 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck 
%s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file

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


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-10 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From 6ff448ed506e0ef75db2c9974a628a965e85df2f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/3] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp | 4 ++--
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1ebef0e434b3d 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -178,8 +178,8 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
 if (!ND)
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
-!D->getLangOpts().CPlusPlus)
+if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
+!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 578f1d4c0eac6..56ab155ebf5a4 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
+  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot have a capture-default"));
+  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
+
+  RecoverErr = Interp->Parse("int validVar = 10;");
+  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The

>From 8b9a112721c45662374fd9995af6da4031507c89 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 26 Feb 2025 16:55:31 +0530
Subject: [PATCH 2/3] Update IncrementalParser.cpp

---
 clang/lib/Interpreter/IncrementalParser.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 1ebef0e434b3d..774219d9fc9ab 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -175,11 +175,11 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty()) 
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
-!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
+if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
+!D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }

>From dae6ca63f0d443a6ec0df12e1abef183203c12de Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 11 Mar 2025 11:29:32 +0530
Subject: [PATCH 3/3] Add tests in lambda.cpp

---
 clang/test/Interpreter/lambda.cpp   | 11 +--
 clang/unittests/Interpreter/InterpreterTest.cpp |  7 ---
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..135f5fe604ae5 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 56ab155ebf5a4..578f1d4c0eac6 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,13 +114,6 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
-
-  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
-  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot ha

[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-10 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From 6ff448ed506e0ef75db2c9974a628a965e85df2f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/3] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp | 4 ++--
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1ebef0e434b3d 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -178,8 +178,8 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
 if (!ND)
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
-!D->getLangOpts().CPlusPlus)
+if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
+!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 578f1d4c0eac6..56ab155ebf5a4 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
+  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot have a capture-default"));
+  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
+
+  RecoverErr = Interp->Parse("int validVar = 10;");
+  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The

>From 8b9a112721c45662374fd9995af6da4031507c89 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 26 Feb 2025 16:55:31 +0530
Subject: [PATCH 2/3] Update IncrementalParser.cpp

---
 clang/lib/Interpreter/IncrementalParser.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 1ebef0e434b3d..774219d9fc9ab 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -175,11 +175,11 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty()) 
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
-!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
+if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
+!D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }

>From dae6ca63f0d443a6ec0df12e1abef183203c12de Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 11 Mar 2025 11:29:32 +0530
Subject: [PATCH 3/3] Add tests in lambda.cpp

---
 clang/test/Interpreter/lambda.cpp   | 11 +--
 clang/unittests/Interpreter/InterpreterTest.cpp |  7 ---
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..135f5fe604ae5 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 56ab155ebf5a4..578f1d4c0eac6 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,13 +114,6 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
-
-  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
-  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot ha

[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-11 Thread Anutosh Bhat via cfe-commits


@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck 
%s

anutosh491 wrote:

Sure shall take care of it. Thanks 

https://github.com/llvm/llvm-project/pull/127467
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-11 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From 6ff448ed506e0ef75db2c9974a628a965e85df2f Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/5] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp | 4 ++--
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1ebef0e434b3d 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -178,8 +178,8 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
 if (!ND)
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
-!D->getLangOpts().CPlusPlus)
+if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
+!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 578f1d4c0eac6..56ab155ebf5a4 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
+  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot have a capture-default"));
+  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
+
+  RecoverErr = Interp->Parse("int validVar = 10;");
+  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The

>From 8b9a112721c45662374fd9995af6da4031507c89 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 26 Feb 2025 16:55:31 +0530
Subject: [PATCH 2/5] Update IncrementalParser.cpp

---
 clang/lib/Interpreter/IncrementalParser.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 1ebef0e434b3d..774219d9fc9ab 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -175,11 +175,11 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty()) 
   continue;
 // Check if we need to clean up the IdResolver chain.
-if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
-!D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
+if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
+!D->getLangOpts().CPlusPlus)
   S.IdResolver.RemoveDecl(ND);
   }
 }

>From dae6ca63f0d443a6ec0df12e1abef183203c12de Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Tue, 11 Mar 2025 11:29:32 +0530
Subject: [PATCH 3/5] Add tests in lambda.cpp

---
 clang/test/Interpreter/lambda.cpp   | 11 +--
 clang/unittests/Interpreter/InterpreterTest.cpp |  7 ---
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..135f5fe604ae5 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 56ab155ebf5a4..578f1d4c0eac6 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,13 +114,6 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
-
-  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; 
};").takeError();
-  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression 
cannot ha

[clang] [clang-repl] Fix target creation in Wasm.cpp (PR #130909)

2025-03-12 Thread Anutosh Bhat via cfe-commits

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

After #129868 went in, I realize some updates have been made to the Triple.

Not sure if @nikic overlooked including this change in his PR (hence I have 
having build issue when compiling clang against emscripten while building for 
wasm)

But yeah just like the change was addressed in other files, we need to make the 
change here too I think

https://github.com/llvm/llvm-project/blob/d921bf233c4fd3840953b1a4b5bb35ad594da773/clang/lib/Interpreter/DeviceOffload.cpp#L79-L86

>From 18e1f9713b34bf1f9c0e946cd915c0ae04748607 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 12 Mar 2025 12:32:02 +0530
Subject: [PATCH] Fix target creation in Wasm.cpp

---
 clang/lib/Interpreter/Wasm.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index aa10b160ccf84..85a62e05f6e82 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -66,7 +66,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   std::string ErrorString;
 
   const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(
-  PTU.TheModule->getTargetTriple(), ErrorString);
+  PTU.TheModule->getTargetTriple().str(), ErrorString);
   if (!Target) {
 return llvm::make_error("Failed to create Wasm Target: 
",
llvm::inconvertibleErrorCode());
@@ -74,7 +74,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
 
   llvm::TargetOptions TO = llvm::TargetOptions();
   llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
-  PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);
+  PTU.TheModule->getTargetTriple().str(), "", "", 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";

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


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-12 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From bd1b0b2a14afeb73755db3a7deb6bffd4f50778c Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/2] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp |  2 +-
 clang/test/Interpreter/lambda.cpp   | 11 +--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 41d6304bd5f65..6343f17ed822a 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -176,7 +176,7 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty())
   continue;
 // Check if we need to clean up the IdResolver chain.
 if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..8f49f870fddb6 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck 
%s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file

>From 1963cc341cd356f06a6c582235e47ac3cf144a63 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 12 Mar 2025 16:43:45 +0530
Subject: [PATCH 2/2] Fix test as per review

---
 clang/test/Interpreter/lambda.cpp | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index 8f49f870fddb6..7e8dcd61f20c8 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck 
%s
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck %s
 
 extern "C" int printf(const char *, ...);
 
@@ -21,4 +21,7 @@ int x = 42;
 // expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
 auto capture = [&]() { return x * 2; };
 
+// Ensure valid C++ code before exiting
+x = 100;
+
 %quit
\ No newline at end of file

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


[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

2025-03-12 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/127467

>From bd1b0b2a14afeb73755db3a7deb6bffd4f50778c Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/3] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp |  2 +-
 clang/test/Interpreter/lambda.cpp   | 11 +--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 41d6304bd5f65..6343f17ed822a 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -176,7 +176,7 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl 
*MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
 auto *ND = dyn_cast(D);
-if (!ND)
+if (!ND || ND->getDeclName().isEmpty())
   continue;
 // Check if we need to clean up the IdResolver chain.
 if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..8f49f870fddb6 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck 
%s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file

>From 1963cc341cd356f06a6c582235e47ac3cf144a63 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 12 Mar 2025 16:43:45 +0530
Subject: [PATCH 2/3] Fix test as per review

---
 clang/test/Interpreter/lambda.cpp | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index 8f49f870fddb6..7e8dcd61f20c8 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck 
%s
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck %s
 
 extern "C" int printf(const char *, ...);
 
@@ -21,4 +21,7 @@ int x = 42;
 // expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
 auto capture = [&]() { return x * 2; };
 
+// Ensure valid C++ code before exiting
+x = 100;
+
 %quit
\ No newline at end of file

>From 45924cdddab5fdb89823792ebf5577469e474432 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 12 Mar 2025 17:31:35 +0530
Subject: [PATCH 3/3] fix failing lambda.cpp

---
 clang/test/Interpreter/lambda.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Interpreter/lambda.cpp 
b/clang/test/Interpreter/lambda.cpp
index 7e8dcd61f20c8..07eb6fb017360 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -18,7 +18,7 @@ auto r3 = l2();
 // Verify non-local lambda capture error is correctly reported
 int x = 42;
 
-// expected-error@+1 {{non-local lambda expression cannot have a 
capture-default}}
+// expected-error {{non-local lambda expression cannot have a capture-default}}
 auto capture = [&]() { return x * 2; };
 
 // Ensure valid C++ code before exiting

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


[clang] [clang-repl] Fix target creation in Wasm.cpp (PR #130909)

2025-03-12 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/130909

>From 18e1f9713b34bf1f9c0e946cd915c0ae04748607 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 12 Mar 2025 12:32:02 +0530
Subject: [PATCH 1/2] Fix target creation in Wasm.cpp

---
 clang/lib/Interpreter/Wasm.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index aa10b160ccf84..85a62e05f6e82 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -66,7 +66,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   std::string ErrorString;
 
   const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(
-  PTU.TheModule->getTargetTriple(), ErrorString);
+  PTU.TheModule->getTargetTriple().str(), ErrorString);
   if (!Target) {
 return llvm::make_error("Failed to create Wasm Target: 
",
llvm::inconvertibleErrorCode());
@@ -74,7 +74,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
 
   llvm::TargetOptions TO = llvm::TargetOptions();
   llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
-  PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);
+  PTU.TheModule->getTargetTriple().str(), "", "", 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";

>From ca6163e2e4cf1d0a7e3ba60feb28ede52dcd4f8d Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 12 Mar 2025 12:36:33 +0530
Subject: [PATCH 2/2] Update Wasm.cpp

---
 clang/lib/Interpreter/Wasm.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 85a62e05f6e82..c12412c26d6e0 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -66,7 +66,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   std::string ErrorString;
 
   const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(
-  PTU.TheModule->getTargetTriple().str(), ErrorString);
+  PTU.TheModule->getTargetTriple(), ErrorString);
   if (!Target) {
 return llvm::make_error("Failed to create Wasm Target: 
",
llvm::inconvertibleErrorCode());
@@ -146,4 +146,4 @@ llvm::Error WasmIncrementalExecutor::cleanUp() {
 
 WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;
 
-} // namespace clang
\ No newline at end of file
+} // namespace clang

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


[clang] [clang-repl] Fix target creation in Wasm.cpp (PR #130909)

2025-03-12 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/130909

>From 18e1f9713b34bf1f9c0e946cd915c0ae04748607 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 12 Mar 2025 12:32:02 +0530
Subject: [PATCH 1/3] Fix target creation in Wasm.cpp

---
 clang/lib/Interpreter/Wasm.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index aa10b160ccf84..85a62e05f6e82 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -66,7 +66,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   std::string ErrorString;
 
   const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(
-  PTU.TheModule->getTargetTriple(), ErrorString);
+  PTU.TheModule->getTargetTriple().str(), ErrorString);
   if (!Target) {
 return llvm::make_error("Failed to create Wasm Target: 
",
llvm::inconvertibleErrorCode());
@@ -74,7 +74,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
 
   llvm::TargetOptions TO = llvm::TargetOptions();
   llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
-  PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);
+  PTU.TheModule->getTargetTriple().str(), "", "", 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";

>From ca6163e2e4cf1d0a7e3ba60feb28ede52dcd4f8d Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 12 Mar 2025 12:36:33 +0530
Subject: [PATCH 2/3] Update Wasm.cpp

---
 clang/lib/Interpreter/Wasm.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 85a62e05f6e82..c12412c26d6e0 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -66,7 +66,7 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   std::string ErrorString;
 
   const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(
-  PTU.TheModule->getTargetTriple().str(), ErrorString);
+  PTU.TheModule->getTargetTriple(), ErrorString);
   if (!Target) {
 return llvm::make_error("Failed to create Wasm Target: 
",
llvm::inconvertibleErrorCode());
@@ -146,4 +146,4 @@ llvm::Error WasmIncrementalExecutor::cleanUp() {
 
 WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;
 
-} // namespace clang
\ No newline at end of file
+} // namespace clang

>From b0359aa9ceee02702fd0027d672a1e4c283d121c Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Wed, 12 Mar 2025 12:42:02 +0530
Subject: [PATCH 3/3] Code formatting

---
 clang/lib/Interpreter/Wasm.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index c12412c26d6e0..6f584fab52ba9 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -73,8 +73,9 @@ llvm::Error 
WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   }
 
   llvm::TargetOptions TO = llvm::TargetOptions();
-  llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
-  PTU.TheModule->getTargetTriple().str(), "", "", TO, 
llvm::Reloc::Model::PIC_);
+  llvm::TargetMachine *TargetMachine =
+  Target->createTargetMachine(PTU.TheModule->getTargetTriple().str(), "",
+  "", 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";

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


[clang] [clang-repl] Fix target creation in Wasm.cpp (PR #130909)

2025-03-12 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/130909
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Hey,

Thanks for the review. The CI is green now !

https://github.com/llvm/llvm-project/pull/131578
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

/cherry-pick 
[e57cd10](https://github.com/llvm/llvm-project/commit/e57cd100ca297cf81854e35cccbf703e4aad)

https://github.com/llvm/llvm-project/pull/131578
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/131578
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Address error recovery fixing infinite loop while parsing (PR #127569)

2025-03-16 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Hi @vgvassilev,

Would be nice to know your thoughts here

https://github.com/llvm/llvm-project/pull/127569
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Hey @serge-sans-paille 

One final request here would be if you could add the milestone (**LLVM 20.X 
Release** I think)

So that this is picked up in the next release (20.0.2) and we wouldn't need to 
use the patch then !

https://github.com/llvm/llvm-project/pull/131578
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang-repl] Implementation for removeModule for wasm use case (PR #131558)

2025-03-16 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

Has some debug logs for now (shall be removed once ready)

Also the issue tracking this on emscripten is 
https://github.com/emscripten-core/emscripten/issues/23793

https://github.com/llvm/llvm-project/pull/131558
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang-repl] Implementation for removeModule for wasm use case (PR #131558)

2025-03-16 Thread Anutosh Bhat via cfe-commits

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 
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("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(
+"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(
+"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 LoadedModules;
 };
 
 } // namespace clang

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


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

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

While building llvm (clang, lld) against emscripten we see this 
[error](https://github.com/emscripten-forge/recipes/actions/runs/13803029307/job/38608794602#step:9:1715)

```
 │ │ In file included from $SRC_DIR/llvm/lib/Frontend/OpenACC/ACC.cpp:9:
 │ │ $SRC_DIR/build/include/llvm/Frontend/OpenACC/ACC.h.inc:192:1: error: 
unknown type name 'LLVM_ABI'
 │ │   192 | LLVM_ABI Directive getOpenACCDirectiveKind(llvm::StringRef Str);
 │ │   | ^
 │ │ $SRC_DIR/build/include/llvm/Frontend/OpenACC/ACC.h.inc:192:19: error: 
expected ';' after top level declarator
 │ │   192 | LLVM_ABI Directive getOpenACCDirectiveKind(llvm::StringRef Str);
 │ │   |   ^
 ```

Now this was happening because we weren't defining LLVM_ABI correctly when 
building against emscripten. If you see 
[llvm/Support/Compiler.h](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L206-L210),
 the condition only checked for the platform __WASM__ . Now Emscripten targets 
WebAssembly but doesn't imply the platform by default so the check isn't 
complete to define LLVM_ABI.

>From ed4c4946bdcce63eda9b2ef9072210197202c3a6 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 17 Mar 2025 13:22:49 +0530
Subject: [PATCH] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds

---
 clang/include/clang/Support/Compiler.h | 2 +-
 llvm/include/llvm/Support/Compiler.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Support/Compiler.h 
b/clang/include/clang/Support/Compiler.h
index 13582b899dc2a..5a74f8e3b6723 100644
--- a/clang/include/clang/Support/Compiler.h
+++ b/clang/include/clang/Support/Compiler.h
@@ -54,7 +54,7 @@
 #define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #define CLANG_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #define CLANG_EXPORT_TEMPLATE
-#elif defined(__MACH__) || defined(__WASM__)
+#elif defined(__MACH__) || defined(__WASM__) || defined(__EMSCRIPTEN__)
 #define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #define CLANG_TEMPLATE_ABI
 #define CLANG_EXPORT_TEMPLATE
diff --git a/llvm/include/llvm/Support/Compiler.h 
b/llvm/include/llvm/Support/Compiler.h
index f9c57b89f1f03..dc8b5389069eb 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -203,7 +203,7 @@
 #define LLVM_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#elif defined(__MACH__) || defined(__WASM__)
+#elif defined(__MACH__) || defined(__WASM__) || defined(__EMSCRIPTEN__)
 #define LLVM_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #define LLVM_TEMPLATE_ABI
 #define LLVM_EXPORT_TEMPLATE

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


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 ready_for_review 
https://github.com/llvm/llvm-project/pull/131578
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

cc @serge-sans-paille for review.

https://github.com/llvm/llvm-project/pull/131578
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Define LLVM_ABI and CLANG_ABI for __EMSCRIPTEN__ builds (PR #131578)

2025-03-17 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/131578
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Handle frontend options for clang-repl before calling executeAction (PR #132670)

2025-03-23 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

After handling these args we should see 
```
(base) anutosh491@Anutoshs-MacBook-Air bin % ./clang-repl --Xcc -Xclang --Xcc 
-version  
LLVM (http://llvm.org/):
  LLVM version 21.0.0git
  Optimized build.
clang-repl: Version displayed
```
rather than the abort.

https://github.com/llvm/llvm-project/pull/132670
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Handle frontend options for clang-repl before calling executeAction (PR #132670)

2025-03-23 Thread Anutosh Bhat via cfe-commits

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

1) How usual clang works

It goes from Creating the Compiler Instance -> [Addressing these 
llvmargs](https://github.com/llvm/llvm-project/blob/4e4e4a190fb7c74453994935c843b09cc682f4bb/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp#L231-L239)
  -> calling 
[executeAction](https://github.com/llvm/llvm-project/blob/4e4e4a190fb7c74453994935c843b09cc682f4bb/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp#L280)

2) what clang-repl does is create the Compiler Instance -> calls executeAction 
(and doesn't handle some options in between)

So ExecuteAction is framed like this 
```
bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
  assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
  assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
  assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");

```

And clang-repl would need to handle these before getting to executeAction.

Otherwise 

1) processing the -Xclang -version flag would give us the following (as shown 
while running clang-repl in the browser)

![image](https://github.com/user-attachments/assets/68e42810-4288-4d88-af9a-de8a95a0be2d)

2) This would also help us process a try-catch block while running clang-repl 
in the browser which can't be done currently 

![image](https://github.com/user-attachments/assets/c928d07e-e83c-46e1-8e48-39fe5e34a874)










>From 6545414a97b6458333f399c7252ae55c88a42d62 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 24 Mar 2025 10:09:59 +0530
Subject: [PATCH] Handle frontend options for clang-repl before calling
 executeAction

---
 clang/lib/Interpreter/Interpreter.cpp | 38 ++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index fa4c1439c9261..5f48117dbf3b8 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -141,6 +141,37 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
   return std::move(Clang);
 }
 
+static llvm::Error HandleFrontendOptions(const CompilerInstance &CI) {
+  const auto &FrontendOpts = CI.getFrontendOpts();
+
+  if (FrontendOpts.ShowHelp) {
+driver::getDriverOptTable().printHelp(
+llvm::outs(), "clang -cc1 [options] file...",
+"LLVM 'Clang' Compiler: http://clang.llvm.org";,
+/*ShowHidden=*/false, /*ShowAllAliases=*/false,
+llvm::opt::Visibility(driver::options::CC1Option));
+return llvm::createStringError(llvm::errc::not_supported, "Help 
displayed");
+  }
+
+  if (FrontendOpts.ShowVersion) {
+llvm::cl::PrintVersionMessage();
+return llvm::createStringError(llvm::errc::not_supported, "Version 
displayed");
+  }
+
+  if (!FrontendOpts.LLVMArgs.empty()) {
+unsigned NumArgs = FrontendOpts.LLVMArgs.size();
+auto Args = std::make_unique(NumArgs + 2);
+Args[0] = "clang-repl (LLVM option parsing)";
+for (unsigned i = 0; i != NumArgs; ++i)
+  Args[i + 1] = FrontendOpts.LLVMArgs[i].c_str();
+Args[NumArgs + 1] = nullptr;
+llvm::errs() << "Parsing LLVM backend options via 
cl::ParseCommandLineOptions...\n";
+llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
+  }
+
+  return llvm::Error::success();
+}
+
 } // anonymous namespace
 
 namespace clang {
@@ -451,7 +482,12 @@ const char *const Runtimes = R"(
 
 llvm::Expected>
 Interpreter::create(std::unique_ptr CI) {
-  llvm::Error Err = llvm::Error::success();
+
+  llvm::Error Err = HandleFrontendOptions(*CI);
+  if (Err) {
+return std::move(Err);
+  }
+
   auto Interp =
   std::unique_ptr(new Interpreter(std::move(CI), Err));
   if (Err)

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


[clang] [clang-repl] Handle frontend options for clang-repl before calling executeAction (PR #132670)

2025-03-23 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/132670

>From 6545414a97b6458333f399c7252ae55c88a42d62 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 24 Mar 2025 10:09:59 +0530
Subject: [PATCH 1/2] Handle frontend options for clang-repl before calling
 executeAction

---
 clang/lib/Interpreter/Interpreter.cpp | 38 ++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index fa4c1439c9261..5f48117dbf3b8 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -141,6 +141,37 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
   return std::move(Clang);
 }
 
+static llvm::Error HandleFrontendOptions(const CompilerInstance &CI) {
+  const auto &FrontendOpts = CI.getFrontendOpts();
+
+  if (FrontendOpts.ShowHelp) {
+driver::getDriverOptTable().printHelp(
+llvm::outs(), "clang -cc1 [options] file...",
+"LLVM 'Clang' Compiler: http://clang.llvm.org";,
+/*ShowHidden=*/false, /*ShowAllAliases=*/false,
+llvm::opt::Visibility(driver::options::CC1Option));
+return llvm::createStringError(llvm::errc::not_supported, "Help 
displayed");
+  }
+
+  if (FrontendOpts.ShowVersion) {
+llvm::cl::PrintVersionMessage();
+return llvm::createStringError(llvm::errc::not_supported, "Version 
displayed");
+  }
+
+  if (!FrontendOpts.LLVMArgs.empty()) {
+unsigned NumArgs = FrontendOpts.LLVMArgs.size();
+auto Args = std::make_unique(NumArgs + 2);
+Args[0] = "clang-repl (LLVM option parsing)";
+for (unsigned i = 0; i != NumArgs; ++i)
+  Args[i + 1] = FrontendOpts.LLVMArgs[i].c_str();
+Args[NumArgs + 1] = nullptr;
+llvm::errs() << "Parsing LLVM backend options via 
cl::ParseCommandLineOptions...\n";
+llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
+  }
+
+  return llvm::Error::success();
+}
+
 } // anonymous namespace
 
 namespace clang {
@@ -451,7 +482,12 @@ const char *const Runtimes = R"(
 
 llvm::Expected>
 Interpreter::create(std::unique_ptr CI) {
-  llvm::Error Err = llvm::Error::success();
+
+  llvm::Error Err = HandleFrontendOptions(*CI);
+  if (Err) {
+return std::move(Err);
+  }
+
   auto Interp =
   std::unique_ptr(new Interpreter(std::move(CI), Err));
   if (Err)

>From fa4e295389e51da41b6b96baed099e739664b730 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Mon, 24 Mar 2025 10:29:03 +0530
Subject: [PATCH 2/2] apply code formatting changes

---
 clang/lib/Interpreter/Interpreter.cpp | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 5f48117dbf3b8..3fbfd3c746bc7 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -155,17 +155,19 @@ static llvm::Error HandleFrontendOptions(const 
CompilerInstance &CI) {
 
   if (FrontendOpts.ShowVersion) {
 llvm::cl::PrintVersionMessage();
-return llvm::createStringError(llvm::errc::not_supported, "Version 
displayed");
+return llvm::createStringError(llvm::errc::not_supported,
+   "Version displayed");
   }
 
   if (!FrontendOpts.LLVMArgs.empty()) {
 unsigned NumArgs = FrontendOpts.LLVMArgs.size();
-auto Args = std::make_unique(NumArgs + 2);
+auto Args = std::make_unique(NumArgs + 2);
 Args[0] = "clang-repl (LLVM option parsing)";
 for (unsigned i = 0; i != NumArgs; ++i)
   Args[i + 1] = FrontendOpts.LLVMArgs[i].c_str();
 Args[NumArgs + 1] = nullptr;
-llvm::errs() << "Parsing LLVM backend options via 
cl::ParseCommandLineOptions...\n";
+llvm::errs()
+<< "Parsing LLVM backend options via cl::ParseCommandLineOptions...\n";
 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
   }
 

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


[clang] [clang-repl] Handle frontend options for clang-repl before calling executeAction (PR #132670)

2025-03-23 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

I see clang handle 4 types of FrontendOpts before calling ExecuteAction

1) help
2) version
3) Plugins through 
[LoadRequestedPlugins](https://github.com/llvm/llvm-project/blob/4e4e4a190fb7c74453994935c843b09cc682f4bb/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp#L225)
4) llvmargs

We might be interesting in 1, 2 and 4 for clang-repl
Also clang-repl uses IncrementalAction unlike FrontendAction so simply calling 
ExecuteCompilerInvocation won't do the job. So not sure if we can maybe 
refactor out 1/2/4 and use it from clang itself.

https://github.com/llvm/llvm-project/pull/132670
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/133037

>From dfe49e826705a5e9371e17e66e40c31602beea8e Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 10:33:37 +0530
Subject: [PATCH 1/3] 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..b4c68f340abc0 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 
 #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(
+"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 cb274780d84c5355c4aede7b8fe7501dc300bc02 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 11:14:06 +0530
Subject: [PATCH 2/3] formatting

---
 clang/lib/Interpreter/Interpreter.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index b4c68f340abc0..f8c8d0a425659 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -713,11 +713,11 @@ llvm::Error Interpreter::Undo(unsigned N) {
 
 llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
 #ifdef __EMSCRIPTEN__
-  void* handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
+  void *handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
   if (!handle) {
 llvm::errs() << dlerror() << '\n';
-return llvm::make_error(
-"Failed to load dynamic library", llvm::inconvertibleErrorCode());
+return llvm::make_error("Failed to load dynamic 
library",
+   llvm::inconvertibleErrorCode());
   }
 #else
   auto EE = getExecutionEngine();

>From 0911ef1d634603718d5418694a02b0e9665d4552 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 27 Mar 2025 12:34:23 +0530
Subject: [PATCH 3/3] 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
+  virtual llvm::Expected
   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..c91fe950d428d 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
+WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
+  SymbolNameKind NameKind) const {
+  void* Sym = dlsym(RTLD_DEFAULT, Name.str().c_str());
+  if (!Sym) {
+return llvm::make_error(
+"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
+  getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
 
   ~WasmIncrementalExecutor() override;
 };

__

[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-04-02 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

The above failure reported doesn't look related. Cherry picking commit for 
20.1.3

/cherry-pick 
[8f56394](https://github.com/llvm/llvm-project/commit/8f56394487a4d454be0637667267ad37bd636d0f)

https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-25 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-25 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-25 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/133037

>From dfe49e826705a5e9371e17e66e40c31602beea8e Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 10:33:37 +0530
Subject: [PATCH 1/2] 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..b4c68f340abc0 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 
 #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(
+"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 cb274780d84c5355c4aede7b8fe7501dc300bc02 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 11:14:06 +0530
Subject: [PATCH 2/2] formatting

---
 clang/lib/Interpreter/Interpreter.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index b4c68f340abc0..f8c8d0a425659 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -713,11 +713,11 @@ llvm::Error Interpreter::Undo(unsigned N) {
 
 llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
 #ifdef __EMSCRIPTEN__
-  void* handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
+  void *handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
   if (!handle) {
 llvm::errs() << dlerror() << '\n';
-return llvm::make_error(
-"Failed to load dynamic library", llvm::inconvertibleErrorCode());
+return llvm::make_error("Failed to load dynamic 
library",
+   llvm::inconvertibleErrorCode());
   }
 #else
   auto EE = getExecutionEngine();

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


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-25 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

For anyone concerned,

Such a feature would be tested out in downstream projects like 
[cppinterop](https://github.com/compiler-research/CppInterOp) and 
[xeus-cpp](https://github.com/compiler-research/xeus-cpp) as currently llvm 
doesn't have a concrete structure to test clang-repl in the browser.

https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-25 Thread Anutosh Bhat via cfe-commits

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

None

>From dfe49e826705a5e9371e17e66e40c31602beea8e Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 10:33:37 +0530
Subject: [PATCH] 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..b4c68f340abc0 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 
 #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(
+"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();
 }

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


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/133037

>From a7ccd0e0b261c303639d3880fc51b0ed99e17363 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 10:33:37 +0530
Subject: [PATCH 1/3] 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 
 #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("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 
Date: Thu, 27 Mar 2025 12:34:23 +0530
Subject: [PATCH 2/3] 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
+  virtual llvm::Expected
   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
+WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
+  SymbolNameKind NameKind) const {
+  void *Sym = dlsym(RTLD_DEFAULT, Name.str().c_str());
+  if (!Sym) {
+return llvm::make_error("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
+  getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
 
   ~WasmIncrementalExecutor() override;
 };

>From 60d4622e23902bf7c5fd1040c9f82f9b03e134ce Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
Date: Thu, 27 Mar 2025 14:05:35 +0530
Subject: [PATCH 3/3] 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
-  getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
+  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


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/133037

>From a7ccd0e0b261c303639d3880fc51b0ed99e17363 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 10:33:37 +0530
Subject: [PATCH 1/2] 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 
 #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("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 
Date: Thu, 27 Mar 2025 12:34:23 +0530
Subject: [PATCH 2/2] 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
+  virtual llvm::Expected
   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
+WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
+  SymbolNameKind NameKind) const {
+  void *Sym = dlsym(RTLD_DEFAULT, Name.str().c_str());
+  if (!Sym) {
+return llvm::make_error("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
+  getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
 
   ~WasmIncrementalExecutor() override;
 };

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


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits


@@ -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("Failed to load dynamic 
library",
+   llvm::inconvertibleErrorCode());
+  }
+#else

anutosh491 wrote:

Additionally, path resolution isn't a concern in the wasm case for a few 
reasons (which is why a simple combination of dlopen + dlsym should do the job 
isn't it ?) 

1) All dynamic libraries are preloaded into MEMFS at known paths during 
compilation or initialization (e.g., via Emscripten’s --preload-file flag). 
This means the library’s location is fully deterministic at runtime.

2) No concept of system-level search paths like LD_LIBRARY_PATH in the browser 
— we can’t rely on an OS-level dynamic loader. Instead, we always load by the 
full path (e.g., dlopen("/libsymengine.so")), which resolves directly in the 
virtual MEMFS.

3) Because users control the preload path, and because dlopen in Emscripten 
expects an exact match in the in-memory filesystem, we don’t need 
infrastructure for path lookup, search path prioritization, or 
canonicalization. A simple, direct path-based dlopen is all that’s required.

https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/133037

>From dfe49e826705a5e9371e17e66e40c31602beea8e Mon Sep 17 00:00:00 2001
From: anutosh491 
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..b4c68f340abc0 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 
 #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(
+"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 cb274780d84c5355c4aede7b8fe7501dc300bc02 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Wed, 26 Mar 2025 11:14:06 +0530
Subject: [PATCH 2/4] formatting

---
 clang/lib/Interpreter/Interpreter.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index b4c68f340abc0..f8c8d0a425659 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -713,11 +713,11 @@ llvm::Error Interpreter::Undo(unsigned N) {
 
 llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
 #ifdef __EMSCRIPTEN__
-  void* handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
+  void *handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
   if (!handle) {
 llvm::errs() << dlerror() << '\n';
-return llvm::make_error(
-"Failed to load dynamic library", llvm::inconvertibleErrorCode());
+return llvm::make_error("Failed to load dynamic 
library",
+   llvm::inconvertibleErrorCode());
   }
 #else
   auto EE = getExecutionEngine();

>From 0911ef1d634603718d5418694a02b0e9665d4552 Mon Sep 17 00:00:00 2001
From: anutosh491 
Date: Thu, 27 Mar 2025 12:34:23 +0530
Subject: [PATCH 3/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
+  virtual llvm::Expected
   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..c91fe950d428d 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
+WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
+  SymbolNameKind NameKind) const {
+  void* Sym = dlsym(RTLD_DEFAULT, Name.str().c_str());
+  if (!Sym) {
+return llvm::make_error(
+"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
+  getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
 
   ~WasmIncrementalExecutor() override;
 };

>From b717060a8a0cf85e2ca218d7c303

[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits


@@ -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("Failed to load dynamic 
library",
+   llvm::inconvertibleErrorCode());
+  }
+#else

anutosh491 wrote:

Hi, thanks for the suggestion.

I did some surface level introspection on `DynamicLibrarySearchGenerator::Load` 
and I see it calls dlopen internally but I think the handle is wrapped in a 
DynamicLibrarySearchGenerator, which lets the ORC JIT symbol resolution system 
look inside the .so for symbols during later getSymbolAddress(...) calls.

So even if DynamicLibrarySearchGenerator::Load(...) works under WASM (which it 
might), it:
i) Creates a JITDylib search generator
ii) Which requires an ORC execution engine
iii) Which you are not using in the wasm path (we're using 
WasmIncrementalExecutor instead)

So as per what I see DynamicLibrarySearchGenerator infrastructure creates 
symbol generators for ORC JIT Dylibs, which isn’t used in the wasm execution 
model. So while the underlying dlopen(...) mechanism is similar, 
DynamicLibrarySearchGenerator wouldn’t integrate meaningfully unless the wasm 
executor also moved to using ORC JIT infrastructure, which it currently doesn’t.

https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits


@@ -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("Failed to load dynamic 
library",
+   llvm::inconvertibleErrorCode());
+  }
+#else

anutosh491 wrote:

Check how we can use the changes here in cppinterop to get dynamic library 
loaded and symbol addresses fetched at runtime !

https://github.com/compiler-research/CppInterOp/compare/main...anutosh491:CppInterOp:shared_libs

Everything boils down to preload as we know where in the MEMFS our shared lib 
exists !

https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-27 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 edited 
https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-31 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

> We really need to figure out how to add in-tree tests.

Yupp sadly I was only able to test this through cppinterop and xeus-cpp as I've 
commented above 
(https://github.com/llvm/llvm-project/pull/133037#discussion_r2015819533) where 
it works as expected :\

I shall take out some time in the near future to ideate on this.



https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-03-31 Thread Anutosh Bhat via cfe-commits

anutosh491 wrote:

> I am wondering if we can add such CI for llvm, too... maybe you can discuss 
> that at the llvm discord's infrastructure channel...

Absolutely, I shall do that and get back to you on this (I think there are 
people at llvm looking into llvm+emscripten use cases and should support 
something like this)

Shall keep you updated. 

In the meantime would be great to have your views on the above ... and possibly 
if you could give me the green flag here :)
I think the 
[reasoning](https://github.com/llvm/llvm-project/pull/133037#discussion_r2015816316)
 sits well in a wasm context and would expand the interpreter's functionality 
to put any 3rd party/custom shared lib to use (issue description shows the 
interpreter loading **symengine** at runtime) . We also have a decent framework 
now through downstream projects to test something like the above.

https://github.com/llvm/llvm-project/pull/133037
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] Implement LoadDynamicLibrary for clang-repl wasm use cases (PR #133037)

2025-04-01 Thread Anutosh Bhat via cfe-commits

https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/133037

>From a7ccd0e0b261c303639d3880fc51b0ed99e17363 Mon Sep 17 00:00:00 2001
From: anutosh491 
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 
 #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("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 
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
+  virtual llvm::Expected
   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
+WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
+  SymbolNameKind NameKind) const {
+  void *Sym = dlsym(RTLD_DEFAULT, Name.str().c_str());
+  if (!Sym) {
+return llvm::make_error("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
+  getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
 
   ~WasmIncrementalExecutor() override;
 };

>From 60d4622e23902bf7c5fd1040c9f82f9b03e134ce Mon Sep 17 00:00:00 2001
From: Anutosh Bhat 
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
-  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 
Date: Tue, 1 Apr 2025 10:40:57 +0530
Subject: [PATCH 4/4] fix clang format

---
 clang/lib/Interpreter/Wasm.h | 3 ++