[clang] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors (PR #66658)

2023-09-18 Thread Anubhab Ghosh via cfe-commits

https://github.com/argentite created 
https://github.com/llvm/llvm-project/pull/66658

CUDA device code needs to be registered to the runtime before kernels can be 
launched. This is done through a global constructor. User code in Clang 
interpreter, is also executed through `global_ctors`. This patch ensures 
kernels can be launched in the same iteration it is defined in by making the 
registration first in the list.

This allows `#include`-ing a large portion of code that defines device 
functions and also launches kernels in clang-repl. 

>From fb806d7c7d357f1769538df0ba7729e4b328da79 Mon Sep 17 00:00:00 2001
From: Anubhab Ghosh 
Date: Mon, 18 Sep 2023 20:33:19 +0530
Subject: [PATCH] [clang-repl][CUDA] Move CUDA module registration to beginning
 of global_ctors

CUDA device code needs to be registered to the runtime before kernels
can be launched. This is done through a global constructor.
User code in Clang interpreter, is also executed through global_ctors.
This patch ensures kernels can be launched in the same iteration it is
defined in by making the registration first in the list.
---
 clang/lib/CodeGen/CodeGenModule.cpp   |  2 +-
 .../test/Interpreter/CUDA/launch-same-ptu.cu  | 21 +++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Interpreter/CUDA/launch-same-ptu.cu

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 8b0c9340775cbe9..783865409c778f5 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -794,7 +794,7 @@ void CodeGenModule::Release() {
   AddGlobalCtor(ObjCInitFunction);
   if (Context.getLangOpts().CUDA && CUDARuntime) {
 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
-  AddGlobalCtor(CudaCtorFunction);
+  AddGlobalCtor(CudaCtorFunction, 0);
   }
   if (OpenMPRuntime) {
 if (llvm::Function *OpenMPRequiresDirectiveRegFun =
diff --git a/clang/test/Interpreter/CUDA/launch-same-ptu.cu 
b/clang/test/Interpreter/CUDA/launch-same-ptu.cu
new file mode 100644
index 000..93e203a47212fbf
--- /dev/null
+++ b/clang/test/Interpreter/CUDA/launch-same-ptu.cu
@@ -0,0 +1,21 @@
+// Tests __device__ function calls
+// RUN: cat %s | clang-repl --cuda | FileCheck %s
+
+extern "C" int printf(const char*, ...);
+
+int var;
+int* devptr = nullptr;
+printf("cudaMalloc: %d\n", cudaMalloc((void **) &devptr, sizeof(int)));
+// CHECK: cudaMalloc: 0
+
+__device__ inline void test_device(int* value) { *value = 42; } __global__ 
void test_kernel(int* value) { test_device(value); } 
test_kernel<<<1,1>>>(devptr);
+printf("CUDA Error: %d\n", cudaGetLastError());
+// CHECK-NEXT: CUDA Error: 0
+
+printf("cudaMemcpy: %d\n", cudaMemcpy(&var, devptr, sizeof(int), 
cudaMemcpyDeviceToHost));
+// CHECK-NEXT: cudaMemcpy: 0
+
+printf("Value: %d\n", var);
+// CHECK-NEXT: Value: 42
+
+%quit

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


[clang] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors (PR #66658)

2023-09-18 Thread Anubhab Ghosh via cfe-commits

https://github.com/argentite updated 
https://github.com/llvm/llvm-project/pull/66658

>From bed2919f781c5ef71e268c95b31a6b9af5392730 Mon Sep 17 00:00:00 2001
From: Anubhab Ghosh 
Date: Mon, 18 Sep 2023 20:33:19 +0530
Subject: [PATCH] [clang-repl][CUDA] Move CUDA module registration to beginning
 of global_ctors

CUDA device code needs to be registered to the runtime before kernels
can be launched. This is done through a global constructor.
User code in Clang interpreter, is also executed through global_ctors.
This patch ensures kernels can be launched in the same iteration it is
defined in by making the registration first in the list.
---
 clang/lib/CodeGen/CodeGenModule.cpp   |  2 +-
 .../test/Interpreter/CUDA/launch-same-ptu.cu  | 21 +++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Interpreter/CUDA/launch-same-ptu.cu

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 8b0c9340775cbe9..647c8922f27a00f 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -794,7 +794,7 @@ void CodeGenModule::Release() {
   AddGlobalCtor(ObjCInitFunction);
   if (Context.getLangOpts().CUDA && CUDARuntime) {
 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
-  AddGlobalCtor(CudaCtorFunction);
+  AddGlobalCtor(CudaCtorFunction, /*Priority=*/0);
   }
   if (OpenMPRuntime) {
 if (llvm::Function *OpenMPRequiresDirectiveRegFun =
diff --git a/clang/test/Interpreter/CUDA/launch-same-ptu.cu 
b/clang/test/Interpreter/CUDA/launch-same-ptu.cu
new file mode 100644
index 000..93e203a47212fbf
--- /dev/null
+++ b/clang/test/Interpreter/CUDA/launch-same-ptu.cu
@@ -0,0 +1,21 @@
+// Tests __device__ function calls
+// RUN: cat %s | clang-repl --cuda | FileCheck %s
+
+extern "C" int printf(const char*, ...);
+
+int var;
+int* devptr = nullptr;
+printf("cudaMalloc: %d\n", cudaMalloc((void **) &devptr, sizeof(int)));
+// CHECK: cudaMalloc: 0
+
+__device__ inline void test_device(int* value) { *value = 42; } __global__ 
void test_kernel(int* value) { test_device(value); } 
test_kernel<<<1,1>>>(devptr);
+printf("CUDA Error: %d\n", cudaGetLastError());
+// CHECK-NEXT: CUDA Error: 0
+
+printf("cudaMemcpy: %d\n", cudaMemcpy(&var, devptr, sizeof(int), 
cudaMemcpyDeviceToHost));
+// CHECK-NEXT: cudaMemcpy: 0
+
+printf("Value: %d\n", var);
+// CHECK-NEXT: Value: 42
+
+%quit

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


[clang] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors (PR #66658)

2023-09-18 Thread Anubhab Ghosh via cfe-commits


@@ -794,7 +794,7 @@ void CodeGenModule::Release() {
   AddGlobalCtor(ObjCInitFunction);
   if (Context.getLangOpts().CUDA && CUDARuntime) {
 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
-  AddGlobalCtor(CudaCtorFunction);
+  AddGlobalCtor(CudaCtorFunction, /*Priority=*/0);

argentite wrote:

The underlying issues is not actually clang-repl specific, it also affects 
clang. For example, this seems to succeed in `nvcc` but fails with `clang`:
```cpp
#include 

__global__ void kernel() {}

class C {
public:
  C() {
kernel<<<1, 1>>>();
printf("Error: %d\n", cudaGetLastError());
  }
};

C c;

int main() {}
```

This is fixed by this patch. Maybe we can look for a proper solution to this?

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


[clang] d978730 - [clang-repl] Add a command to load dynamic libraries

2023-03-28 Thread Anubhab Ghosh via cfe-commits

Author: Anubhab Ghosh
Date: 2023-03-29T08:04:50+05:30
New Revision: d978730d8e2c10c76867b83bec2f1143d895ee7d

URL: 
https://github.com/llvm/llvm-project/commit/d978730d8e2c10c76867b83bec2f1143d895ee7d
DIFF: 
https://github.com/llvm/llvm-project/commit/d978730d8e2c10c76867b83bec2f1143d895ee7d.diff

LOG: [clang-repl] Add a command to load dynamic libraries

This commit adds the %lib  command to load a dynamic library to be
used by the currently running interpreted code.
For example `%lib libSDL2.so`.

Differential Revision: https://reviews.llvm.org/D141824

Added: 
clang/test/Interpreter/Inputs/dynamic-library-test.cpp
clang/test/Interpreter/dynamic-library.cpp

Modified: 
clang/include/clang/Interpreter/Interpreter.h
clang/lib/Interpreter/IncrementalExecutor.h
clang/lib/Interpreter/Interpreter.cpp
clang/tools/clang-repl/ClangRepl.cpp

Removed: 




diff  --git a/clang/include/clang/Interpreter/Interpreter.h 
b/clang/include/clang/Interpreter/Interpreter.h
index fd22af976613..b20d77e8ef85 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -28,7 +28,7 @@ namespace llvm {
 namespace orc {
 class LLJIT;
 class ThreadSafeContext;
-}
+} // namespace orc
 } // namespace llvm
 
 namespace clang {
@@ -52,12 +52,15 @@ class Interpreter {
 
   Interpreter(std::unique_ptr CI, llvm::Error &Err);
 
+  llvm::Error CreateExecutor();
+
 public:
   ~Interpreter();
   static llvm::Expected>
   create(std::unique_ptr CI);
   const CompilerInstance *getCompilerInstance() const;
-  const llvm::orc::LLJIT *getExecutionEngine() const;
+  llvm::Expected getExecutionEngine();
+
   llvm::Expected Parse(llvm::StringRef Code);
   llvm::Error Execute(PartialTranslationUnit &T);
   llvm::Error ParseAndExecute(llvm::StringRef Code) {
@@ -72,6 +75,9 @@ class Interpreter {
   /// Undo N previous incremental inputs.
   llvm::Error Undo(unsigned N = 1);
 
+  /// Link a dynamic library
+  llvm::Error LoadDynamicLibrary(const char *name);
+
   /// \returns the \c JITTargetAddress of a \c GlobalDecl. This interface uses
   /// the CodeGenModule's internal mangling cache to avoid recomputing the
   /// mangled name.

diff  --git a/clang/lib/Interpreter/IncrementalExecutor.h 
b/clang/lib/Interpreter/IncrementalExecutor.h
index 54d37c76326b..f7922ecb5380 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.h
+++ b/clang/lib/Interpreter/IncrementalExecutor.h
@@ -53,7 +53,8 @@ class IncrementalExecutor {
   llvm::Error cleanUp();
   llvm::Expected
   getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
-  llvm::orc::LLJIT *getExecutionEngine() const { return Jit.get(); }
+
+  llvm::orc::LLJIT &GetExecutionEngine() { return *Jit; }
 };
 
 } // end namespace clang

diff  --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 3f0842c567da..76d5f162a34a 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -29,6 +29,7 @@
 #include "clang/Frontend/TextDiagnosticBuffer.h"
 #include "clang/Lex/PreprocessorOptions.h"
 
+#include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/TargetParser/Host.h"
@@ -203,10 +204,13 @@ const CompilerInstance 
*Interpreter::getCompilerInstance() const {
   return IncrParser->getCI();
 }
 
-const llvm::orc::LLJIT *Interpreter::getExecutionEngine() const {
-  if (IncrExecutor)
-return IncrExecutor->getExecutionEngine();
-  return nullptr;
+llvm::Expected Interpreter::getExecutionEngine() {
+  if (!IncrExecutor) {
+if (auto Err = CreateExecutor())
+  return Err;
+  }
+
+  return IncrExecutor->GetExecutionEngine();
 }
 
 llvm::Expected
@@ -214,14 +218,21 @@ Interpreter::Parse(llvm::StringRef Code) {
   return IncrParser->Parse(Code);
 }
 
+llvm::Error Interpreter::CreateExecutor() {
+  const clang::TargetInfo &TI =
+  getCompilerInstance()->getASTContext().getTargetInfo();
+  llvm::Error Err = llvm::Error::success();
+  auto Executor = std::make_unique(*TSCtx, Err, TI);
+  if (!Err)
+IncrExecutor = std::move(Executor);
+
+  return Err;
+}
+
 llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
   assert(T.TheModule);
   if (!IncrExecutor) {
-const clang::TargetInfo &TI =
-getCompilerInstance()->getASTContext().getTargetInfo();
-llvm::Error Err = llvm::Error::success();
-IncrExecutor = std::make_unique(*TSCtx, Err, TI);
-
+auto Err = CreateExecutor();
 if (Err)
   return Err;
   }
@@ -283,3 +294,19 @@ llvm::Error Interpreter::Undo(unsigned N) {
   }
   return llvm::Error::success();
 }
+
+llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
+  auto EE = getExecutionEngine();
+  if (!EE)
+return EE.takeError();
+
+  auto &DL = EE->getDataLayout();
+
+  if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load(
+  

[clang] 766d048 - [clang-repl] Use std::move when converting Error to Expected

2023-03-28 Thread Anubhab Ghosh via cfe-commits

Author: Anubhab Ghosh
Date: 2023-03-29T08:18:36+05:30
New Revision: 766d048d819a78443da73f67afa04e0a108412b6

URL: 
https://github.com/llvm/llvm-project/commit/766d048d819a78443da73f67afa04e0a108412b6
DIFF: 
https://github.com/llvm/llvm-project/commit/766d048d819a78443da73f67afa04e0a108412b6.diff

LOG: [clang-repl] Use std::move when converting Error to Expected

Added: 


Modified: 
clang/lib/Interpreter/Interpreter.cpp

Removed: 




diff  --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 76d5f162a34a5..a0ccbc20b95f4 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -207,7 +207,7 @@ const CompilerInstance *Interpreter::getCompilerInstance() 
const {
 llvm::Expected Interpreter::getExecutionEngine() {
   if (!IncrExecutor) {
 if (auto Err = CreateExecutor())
-  return Err;
+  return std::move(Err);
   }
 
   return IncrExecutor->GetExecutionEngine();



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


[clang] 80e7eed - [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-20 Thread Anubhab Ghosh via cfe-commits

Author: Anubhab Ghosh
Date: 2023-05-20T14:00:48+05:30
New Revision: 80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31

URL: 
https://github.com/llvm/llvm-project/commit/80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31
DIFF: 
https://github.com/llvm/llvm-project/commit/80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31.diff

LOG: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

CUDA support can be enabled in clang-repl with --cuda flag.
Device code linking is not yet supported. inline must be used with all
__device__ functions.

Differential Revision: https://reviews.llvm.org/D146389

Added: 
clang/lib/Interpreter/DeviceOffload.cpp
clang/lib/Interpreter/DeviceOffload.h
clang/test/Interpreter/CUDA/device-function-template.cu
clang/test/Interpreter/CUDA/device-function.cu
clang/test/Interpreter/CUDA/host-and-device.cu
clang/test/Interpreter/CUDA/lit.local.cfg
clang/test/Interpreter/CUDA/memory.cu
clang/test/Interpreter/CUDA/sanity.cu

Modified: 
clang/include/clang/Interpreter/Interpreter.h
clang/lib/CodeGen/CGCUDANV.cpp
clang/lib/CodeGen/CodeGenAction.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/CodeGen/ModuleBuilder.cpp
clang/lib/Interpreter/CMakeLists.txt
clang/lib/Interpreter/IncrementalParser.cpp
clang/lib/Interpreter/IncrementalParser.h
clang/lib/Interpreter/Interpreter.cpp
clang/test/lit.cfg.py
clang/tools/clang-repl/ClangRepl.cpp
clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
clang/unittests/Interpreter/IncrementalProcessingTest.cpp
clang/unittests/Interpreter/InterpreterTest.cpp

Removed: 




diff  --git a/clang/include/clang/Interpreter/Interpreter.h 
b/clang/include/clang/Interpreter/Interpreter.h
index b3d64458d777c..afb0bbc98079d 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -41,8 +41,34 @@ class IncrementalParser;
 /// Create a pre-configured \c CompilerInstance for incremental processing.
 class IncrementalCompilerBuilder {
 public:
+  IncrementalCompilerBuilder() {}
+
+  void SetCompilerArgs(const std::vector &Args) {
+UserArgs = Args;
+  }
+
+  // General C++
+  llvm::Expected> CreateCpp();
+
+  // Offload options
+  void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
+
+  // CUDA specific
+  void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
+
+  llvm::Expected> CreateCudaHost();
+  llvm::Expected> CreateCudaDevice();
+
+private:
   static llvm::Expected>
   create(std::vector &ClangArgv);
+
+  llvm::Expected> createCuda(bool device);
+
+  std::vector UserArgs;
+
+  llvm::StringRef OffloadArch;
+  llvm::StringRef CudaSDKPath;
 };
 
 /// Provides top-level interfaces for incremental compilation and execution.
@@ -51,6 +77,9 @@ class Interpreter {
   std::unique_ptr IncrParser;
   std::unique_ptr IncrExecutor;
 
+  // An optional parser for CUDA offloading
+  std::unique_ptr DeviceParser;
+
   Interpreter(std::unique_ptr CI, llvm::Error &Err);
 
   llvm::Error CreateExecutor();
@@ -59,6 +88,9 @@ class Interpreter {
   ~Interpreter();
   static llvm::Expected>
   create(std::unique_ptr CI);
+  static llvm::Expected>
+  createWithCUDA(std::unique_ptr CI,
+ std::unique_ptr DCI);
   const CompilerInstance *getCompilerInstance() const;
   llvm::Expected getExecutionEngine();
 

diff  --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp
index 1f429e4305790..c30a08a5722dc 100644
--- a/clang/lib/CodeGen/CGCUDANV.cpp
+++ b/clang/lib/CodeGen/CGCUDANV.cpp
@@ -24,6 +24,7 @@
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/ReplaceConstant.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/VirtualFileSystem.h"
 
 using namespace clang;
 using namespace CodeGen;
@@ -721,8 +722,9 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
   // handle so CUDA runtime can figure out what to call on the GPU side.
   std::unique_ptr CudaGpuBinary = nullptr;
   if (!CudaGpuBinaryFileName.empty()) {
-llvm::ErrorOr> CudaGpuBinaryOrErr =
-llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName);
+auto VFS = CGM.getFileSystem();
+auto CudaGpuBinaryOrErr =
+VFS->getBufferForFile(CudaGpuBinaryFileName, -1, false);
 if (std::error_code EC = CudaGpuBinaryOrErr.getError()) {
   CGM.getDiags().Report(diag::err_cannot_open_file)
   << CudaGpuBinaryFileName << EC.message();

diff  --git a/clang/lib/CodeGen/CodeGenAction.cpp 
b/clang/lib/CodeGen/CodeGenAction.cpp
index 29adf88acd704..784ff77c61727 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -264,6 +264,7 @@ namespace clang {
 // Links each entry in LinkModules into our module.  Returns true on error.
 bool LinkInModules() {
   for (auto &LM : LinkModules) {
+assert(LM.Module && "LinkModule does not actually have a module"

[clang] 0929f5b - Revert "[clang-repl][CUDA] Initial interactive CUDA support for clang-repl"

2023-05-20 Thread Anubhab Ghosh via cfe-commits

Author: Anubhab Ghosh
Date: 2023-05-20T14:40:04+05:30
New Revision: 0929f5b90350aa2f9175d7e1094b1750535c0e44

URL: 
https://github.com/llvm/llvm-project/commit/0929f5b90350aa2f9175d7e1094b1750535c0e44
DIFF: 
https://github.com/llvm/llvm-project/commit/0929f5b90350aa2f9175d7e1094b1750535c0e44.diff

LOG: Revert "[clang-repl][CUDA] Initial interactive CUDA support for clang-repl"

This reverts commit 80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31.

Added: 


Modified: 
clang/include/clang/Interpreter/Interpreter.h
clang/lib/CodeGen/CGCUDANV.cpp
clang/lib/CodeGen/CodeGenAction.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/CodeGen/ModuleBuilder.cpp
clang/lib/Interpreter/CMakeLists.txt
clang/lib/Interpreter/IncrementalParser.cpp
clang/lib/Interpreter/IncrementalParser.h
clang/lib/Interpreter/Interpreter.cpp
clang/test/lit.cfg.py
clang/tools/clang-repl/ClangRepl.cpp
clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
clang/unittests/Interpreter/IncrementalProcessingTest.cpp
clang/unittests/Interpreter/InterpreterTest.cpp

Removed: 
clang/lib/Interpreter/DeviceOffload.cpp
clang/lib/Interpreter/DeviceOffload.h
clang/test/Interpreter/CUDA/device-function-template.cu
clang/test/Interpreter/CUDA/device-function.cu
clang/test/Interpreter/CUDA/host-and-device.cu
clang/test/Interpreter/CUDA/lit.local.cfg
clang/test/Interpreter/CUDA/memory.cu
clang/test/Interpreter/CUDA/sanity.cu



diff  --git a/clang/include/clang/Interpreter/Interpreter.h 
b/clang/include/clang/Interpreter/Interpreter.h
index afb0bbc98079d..b3d64458d777c 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -41,34 +41,8 @@ class IncrementalParser;
 /// Create a pre-configured \c CompilerInstance for incremental processing.
 class IncrementalCompilerBuilder {
 public:
-  IncrementalCompilerBuilder() {}
-
-  void SetCompilerArgs(const std::vector &Args) {
-UserArgs = Args;
-  }
-
-  // General C++
-  llvm::Expected> CreateCpp();
-
-  // Offload options
-  void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
-
-  // CUDA specific
-  void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
-
-  llvm::Expected> CreateCudaHost();
-  llvm::Expected> CreateCudaDevice();
-
-private:
   static llvm::Expected>
   create(std::vector &ClangArgv);
-
-  llvm::Expected> createCuda(bool device);
-
-  std::vector UserArgs;
-
-  llvm::StringRef OffloadArch;
-  llvm::StringRef CudaSDKPath;
 };
 
 /// Provides top-level interfaces for incremental compilation and execution.
@@ -77,9 +51,6 @@ class Interpreter {
   std::unique_ptr IncrParser;
   std::unique_ptr IncrExecutor;
 
-  // An optional parser for CUDA offloading
-  std::unique_ptr DeviceParser;
-
   Interpreter(std::unique_ptr CI, llvm::Error &Err);
 
   llvm::Error CreateExecutor();
@@ -88,9 +59,6 @@ class Interpreter {
   ~Interpreter();
   static llvm::Expected>
   create(std::unique_ptr CI);
-  static llvm::Expected>
-  createWithCUDA(std::unique_ptr CI,
- std::unique_ptr DCI);
   const CompilerInstance *getCompilerInstance() const;
   llvm::Expected getExecutionEngine();
 

diff  --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp
index c30a08a5722dc..1f429e4305790 100644
--- a/clang/lib/CodeGen/CGCUDANV.cpp
+++ b/clang/lib/CodeGen/CGCUDANV.cpp
@@ -24,7 +24,6 @@
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/ReplaceConstant.h"
 #include "llvm/Support/Format.h"
-#include "llvm/Support/VirtualFileSystem.h"
 
 using namespace clang;
 using namespace CodeGen;
@@ -722,9 +721,8 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
   // handle so CUDA runtime can figure out what to call on the GPU side.
   std::unique_ptr CudaGpuBinary = nullptr;
   if (!CudaGpuBinaryFileName.empty()) {
-auto VFS = CGM.getFileSystem();
-auto CudaGpuBinaryOrErr =
-VFS->getBufferForFile(CudaGpuBinaryFileName, -1, false);
+llvm::ErrorOr> CudaGpuBinaryOrErr =
+llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName);
 if (std::error_code EC = CudaGpuBinaryOrErr.getError()) {
   CGM.getDiags().Report(diag::err_cannot_open_file)
   << CudaGpuBinaryFileName << EC.message();

diff  --git a/clang/lib/CodeGen/CodeGenAction.cpp 
b/clang/lib/CodeGen/CodeGenAction.cpp
index 784ff77c61727..29adf88acd704 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -264,7 +264,6 @@ namespace clang {
 // Links each entry in LinkModules into our module.  Returns true on error.
 bool LinkInModules() {
   for (auto &LM : LinkModules) {
-assert(LM.Module && "LinkModule does not actually have a module");
 if (LM.PropagateAttrs)
   for (Function &F : *LM.Module) {
 // Skip intrinsics. Keep consistent with how in

[clang] ddeab07 - [clang-repl][CUDA] Re-land: Initial interactive CUDA support for clang-repl

2023-05-27 Thread Anubhab Ghosh via cfe-commits

Author: Anubhab Ghosh
Date: 2023-05-27T13:54:42+05:30
New Revision: ddeab07ca63235f8d952e1171b56fdb0f2d761c9

URL: 
https://github.com/llvm/llvm-project/commit/ddeab07ca63235f8d952e1171b56fdb0f2d761c9
DIFF: 
https://github.com/llvm/llvm-project/commit/ddeab07ca63235f8d952e1171b56fdb0f2d761c9.diff

LOG: [clang-repl][CUDA] Re-land: Initial interactive CUDA support for clang-repl

CUDA support can be enabled in clang-repl with --cuda flag.
Device code linking is not yet supported. inline must be used with all
__device__ functions.

Differential Revision: https://reviews.llvm.org/D146389

Added: 
clang/lib/Interpreter/DeviceOffload.cpp
clang/lib/Interpreter/DeviceOffload.h
clang/test/Interpreter/CUDA/device-function-template.cu
clang/test/Interpreter/CUDA/device-function.cu
clang/test/Interpreter/CUDA/host-and-device.cu
clang/test/Interpreter/CUDA/lit.local.cfg
clang/test/Interpreter/CUDA/memory.cu
clang/test/Interpreter/CUDA/sanity.cu

Modified: 
clang/include/clang/Interpreter/Interpreter.h
clang/lib/CodeGen/CGCUDANV.cpp
clang/lib/CodeGen/CodeGenAction.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/CodeGen/ModuleBuilder.cpp
clang/lib/Interpreter/CMakeLists.txt
clang/lib/Interpreter/IncrementalParser.cpp
clang/lib/Interpreter/IncrementalParser.h
clang/lib/Interpreter/Interpreter.cpp
clang/test/lit.cfg.py
clang/tools/clang-repl/ClangRepl.cpp
clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
clang/unittests/Interpreter/IncrementalProcessingTest.cpp
clang/unittests/Interpreter/InterpreterTest.cpp

Removed: 




diff  --git a/clang/include/clang/Interpreter/Interpreter.h 
b/clang/include/clang/Interpreter/Interpreter.h
index e680218452d1c..43573fb1a4b89 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -42,8 +42,34 @@ class IncrementalParser;
 /// Create a pre-configured \c CompilerInstance for incremental processing.
 class IncrementalCompilerBuilder {
 public:
+  IncrementalCompilerBuilder() {}
+
+  void SetCompilerArgs(const std::vector &Args) {
+UserArgs = Args;
+  }
+
+  // General C++
+  llvm::Expected> CreateCpp();
+
+  // Offload options
+  void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
+
+  // CUDA specific
+  void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
+
+  llvm::Expected> CreateCudaHost();
+  llvm::Expected> CreateCudaDevice();
+
+private:
   static llvm::Expected>
   create(std::vector &ClangArgv);
+
+  llvm::Expected> createCuda(bool device);
+
+  std::vector UserArgs;
+
+  llvm::StringRef OffloadArch;
+  llvm::StringRef CudaSDKPath;
 };
 
 /// Provides top-level interfaces for incremental compilation and execution.
@@ -52,6 +78,9 @@ class Interpreter {
   std::unique_ptr IncrParser;
   std::unique_ptr IncrExecutor;
 
+  // An optional parser for CUDA offloading
+  std::unique_ptr DeviceParser;
+
   Interpreter(std::unique_ptr CI, llvm::Error &Err);
 
   llvm::Error CreateExecutor();
@@ -66,6 +95,9 @@ class Interpreter {
   ~Interpreter();
   static llvm::Expected>
   create(std::unique_ptr CI);
+  static llvm::Expected>
+  createWithCUDA(std::unique_ptr CI,
+ std::unique_ptr DCI);
   const ASTContext &getASTContext() const;
   ASTContext &getASTContext();
   const CompilerInstance *getCompilerInstance() const;

diff  --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp
index 1f429e4305790..c30a08a5722dc 100644
--- a/clang/lib/CodeGen/CGCUDANV.cpp
+++ b/clang/lib/CodeGen/CGCUDANV.cpp
@@ -24,6 +24,7 @@
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/ReplaceConstant.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/VirtualFileSystem.h"
 
 using namespace clang;
 using namespace CodeGen;
@@ -721,8 +722,9 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
   // handle so CUDA runtime can figure out what to call on the GPU side.
   std::unique_ptr CudaGpuBinary = nullptr;
   if (!CudaGpuBinaryFileName.empty()) {
-llvm::ErrorOr> CudaGpuBinaryOrErr =
-llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName);
+auto VFS = CGM.getFileSystem();
+auto CudaGpuBinaryOrErr =
+VFS->getBufferForFile(CudaGpuBinaryFileName, -1, false);
 if (std::error_code EC = CudaGpuBinaryOrErr.getError()) {
   CGM.getDiags().Report(diag::err_cannot_open_file)
   << CudaGpuBinaryFileName << EC.message();

diff  --git a/clang/lib/CodeGen/CodeGenAction.cpp 
b/clang/lib/CodeGen/CodeGenAction.cpp
index 29adf88acd704..784ff77c61727 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -264,6 +264,7 @@ namespace clang {
 // Links each entry in LinkModules into our module.  Returns true on error.
 bool LinkInModules() {
   for (auto &LM : LinkModules) {
+assert(LM.Module && "

[clang] 7f96ce5 - [clang-repl] Fix dynamic library test to avoid cstdio and linker

2023-04-25 Thread Anubhab Ghosh via cfe-commits

Author: Anubhab Ghosh
Date: 2023-04-26T09:11:09+05:30
New Revision: 7f96ce5e133be54891af177adbf8952d413e0f85

URL: 
https://github.com/llvm/llvm-project/commit/7f96ce5e133be54891af177adbf8952d413e0f85
DIFF: 
https://github.com/llvm/llvm-project/commit/7f96ce5e133be54891af177adbf8952d413e0f85.diff

LOG: [clang-repl] Fix dynamic library test to avoid cstdio and linker

Some platforms do not have a working linker present. The goal is to
only test the loading of a shared library in clang-repl. A precompiled
library is used instead.

The cstdio header may also not be present. We only need printf.

Related discussion in D141824

Differential Revision: https://reviews.llvm.org/D148992

Added: 
clang/test/Interpreter/Inputs/libdynamic-library-test.so

Modified: 
clang/test/Interpreter/dynamic-library.cpp

Removed: 
clang/test/Interpreter/Inputs/dynamic-library-test.cpp



diff  --git a/clang/test/Interpreter/Inputs/dynamic-library-test.cpp 
b/clang/test/Interpreter/Inputs/dynamic-library-test.cpp
deleted file mode 100644
index 1f143ba040cb6..0
--- a/clang/test/Interpreter/Inputs/dynamic-library-test.cpp
+++ /dev/null
@@ -1,6 +0,0 @@
-int ultimate_answer = 0;
-
-int calculate_answer() {
-  ultimate_answer = 42;
-  return 5;
-}

diff  --git a/clang/test/Interpreter/Inputs/libdynamic-library-test.so 
b/clang/test/Interpreter/Inputs/libdynamic-library-test.so
new file mode 100755
index 0..bb7c7b5a54317
Binary files /dev/null and 
b/clang/test/Interpreter/Inputs/libdynamic-library-test.so 
diff er

diff  --git a/clang/test/Interpreter/dynamic-library.cpp 
b/clang/test/Interpreter/dynamic-library.cpp
index 145c58bc2ae26..e2bfc81a383a7 100644
--- a/clang/test/Interpreter/dynamic-library.cpp
+++ b/clang/test/Interpreter/dynamic-library.cpp
@@ -1,13 +1,25 @@
 // REQUIRES: host-supports-jit, system-linux
-// UNSUPPORTED: target={{.*-(ps4|ps5)}}
 
-// RUN: %clang -xc++ -o %T/libdynamic-library-test.so -fPIC -shared -DLIBRARY 
%S/Inputs/dynamic-library-test.cpp
-// RUN: cat %s | env LD_LIBRARY_PATH=%T:$LD_LIBRARY_PATH clang-repl | 
FileCheck %s
+// To generate libdynamic-library-test.so :
+// clang -xc++ -o libdynamic-library-test.so -fPIC -shared
+//
+// extern "C" {
+//
+// int ultimate_answer = 0;
+// 
+// int calculate_answer() {
+//   ultimate_answer = 42;
+//   return 5;
+// }
+//
+// }
 
-#include 
+// RUN: cat %s | env LD_LIBRARY_PATH=%S/Inputs:$LD_LIBRARY_PATH clang-repl | 
FileCheck %s
 
-extern int ultimate_answer;
-int calculate_answer();
+extern "C" int printf(const char* format, ...);
+
+extern "C" int ultimate_answer;
+extern "C" int calculate_answer();
 
 %lib libdynamic-library-test.so
 



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


[clang] e037880 - [clang-repl] Only enable dynamic-library test on x86_64

2023-04-25 Thread Anubhab Ghosh via cfe-commits

Author: Anubhab Ghosh
Date: 2023-04-26T09:46:38+05:30
New Revision: e037880b8eff2b425ff80ea9d6337fda27a64337

URL: 
https://github.com/llvm/llvm-project/commit/e037880b8eff2b425ff80ea9d6337fda27a64337
DIFF: 
https://github.com/llvm/llvm-project/commit/e037880b8eff2b425ff80ea9d6337fda27a64337.diff

LOG: [clang-repl] Only enable dynamic-library test on x86_64

This test includes a precompiled library for x86_64 Linux

Added: 


Modified: 
clang/test/Interpreter/dynamic-library.cpp

Removed: 




diff  --git a/clang/test/Interpreter/dynamic-library.cpp 
b/clang/test/Interpreter/dynamic-library.cpp
index e2bfc81a383a7..6c4621f729c1c 100644
--- a/clang/test/Interpreter/dynamic-library.cpp
+++ b/clang/test/Interpreter/dynamic-library.cpp
@@ -1,4 +1,4 @@
-// REQUIRES: host-supports-jit, system-linux
+// REQUIRES: host-supports-jit, x86_64-linux
 
 // To generate libdynamic-library-test.so :
 // clang -xc++ -o libdynamic-library-test.so -fPIC -shared



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


[clang] [clang-repl] Support wasm execution (PR #86402)

2024-06-05 Thread Anubhab Ghosh via cfe-commits

argentite wrote:

> Unfortunately, this PR cannot be tested in the current testing infrastructure 
> in llvm because it needs a browser to provide a proper execution environment.

Just to clarify, we don't need an actual full browser to test it. Nodejs should 
probably be sufficient for execution.

> That would make sense. I am not sure if we can set a post commit bot though. 
> @argentite what do you think?

I think it is possible. If we use a buildbot, we currently only need to build 
llvm, clang and lld for WASM and targetting WASM. Other subprojects are not 
required for this PR and I am not even sure if they can be built to run in WASM.

Also most of the existing tests in the project (and maybe even the whole test 
infrastructure) probably won't work and/or does not make sense in WASM. So it 
would probably have to be running WASM specific tests.

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


[clang] [clang-repl] Support wasm execution (PR #86402)

2024-06-01 Thread Anubhab Ghosh via cfe-commits

https://github.com/argentite updated 
https://github.com/llvm/llvm-project/pull/86402

>From 4434ceeef152b95998ebd0a3b09a56d105490c4d Mon Sep 17 00:00:00 2001
From: Anubhab Ghosh 
Date: Sat, 23 Mar 2024 15:13:57 +
Subject: [PATCH 1/3] [clang-repl] Support wasm execution.

This commit introduces support for running clang-repl and executing C++ code
interactively inside a Javascript engine using WebAssembly when built with
Emscripten. This is achieved by producing WASM "shared libraries" that can be
loaded by the Emscripten runtime using dlopen()

More discussion is available in https://reviews.llvm.org/D158140
---
 clang/lib/Interpreter/CMakeLists.txt  |   1 +
 clang/lib/Interpreter/IncrementalExecutor.cpp |   2 +
 clang/lib/Interpreter/IncrementalExecutor.h   |  11 +-
 clang/lib/Interpreter/Interpreter.cpp |  11 ++
 clang/lib/Interpreter/WASM.cpp| 107 ++
 clang/lib/Interpreter/WASM.h  |  33 ++
 6 files changed, 161 insertions(+), 4 deletions(-)
 create mode 100644 clang/lib/Interpreter/WASM.cpp
 create mode 100644 clang/lib/Interpreter/WASM.h

diff --git a/clang/lib/Interpreter/CMakeLists.txt 
b/clang/lib/Interpreter/CMakeLists.txt
index 9065f998f73c4..a8a287edf5b04 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -20,6 +20,7 @@ add_clang_library(clangInterpreter
   Interpreter.cpp
   InterpreterUtils.cpp
   Value.cpp
+  WASM.cpp
 
   DEPENDS
   intrinsics_gen
diff --git a/clang/lib/Interpreter/IncrementalExecutor.cpp 
b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 6f036107c14a9..1824a5b4570a9 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -36,6 +36,8 @@ LLVM_ATTRIBUTE_USED void linkComponents() {
 }
 
 namespace clang {
+IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC)
+: TSCtx(TSC) {}
 
 llvm::Expected>
 IncrementalExecutor::createDefaultJITBuilder(
diff --git a/clang/lib/Interpreter/IncrementalExecutor.h 
b/clang/lib/Interpreter/IncrementalExecutor.h
index b4347209e14fe..7954cde36588b 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.h
+++ b/clang/lib/Interpreter/IncrementalExecutor.h
@@ -43,16 +43,19 @@ class IncrementalExecutor {
   llvm::DenseMap
   ResourceTrackers;
 
+protected:
+  IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC);
+
 public:
   enum SymbolNameKind { IRName, LinkerName };
 
   IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
   llvm::orc::LLJITBuilder &JITBuilder, llvm::Error &Err);
-  ~IncrementalExecutor();
+  virtual ~IncrementalExecutor();
 
-  llvm::Error addModule(PartialTranslationUnit &PTU);
-  llvm::Error removeModule(PartialTranslationUnit &PTU);
-  llvm::Error runCtors() const;
+  virtual llvm::Error addModule(PartialTranslationUnit &PTU);
+  virtual llvm::Error removeModule(PartialTranslationUnit &PTU);
+  virtual llvm::Error runCtors() const;
   llvm::Error cleanUp();
   llvm::Expected
   getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index cf31456b6950a..7d572b20cd828 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -15,6 +15,7 @@
 #include "IncrementalExecutor.h"
 #include "IncrementalParser.h"
 #include "InterpreterUtils.h"
+#include "WASM.h"
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Mangle.h"
@@ -183,6 +184,12 @@ IncrementalCompilerBuilder::CreateCpp() {
   std::vector Argv;
   Argv.reserve(5 + 1 + UserArgs.size());
   Argv.push_back("-xc++");
+#ifdef __EMSCRIPTEN__
+  Argv.push_back("-target");
+  Argv.push_back("wasm32-unknown-emscripten");
+  Argv.push_back("-pie");
+  Argv.push_back("-shared");
+#endif
   Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
 
   std::string TT = TargetTriple ? *TargetTriple : 
llvm::sys::getProcessTriple();
@@ -400,7 +407,11 @@ llvm::Error Interpreter::CreateExecutor() {
   if (!JB)
 return JB.takeError();
   llvm::Error Err = llvm::Error::success();
+#ifdef __EMSCRIPTEN__
+  auto Executor = std::make_unique(*TSCtx, **JB, Err);
+#else
   auto Executor = std::make_unique(*TSCtx, **JB, Err);
+#endif
   if (!Err)
 IncrExecutor = std::move(Executor);
 
diff --git a/clang/lib/Interpreter/WASM.cpp b/clang/lib/Interpreter/WASM.cpp
new file mode 100644
index 0..d21d0ada1eafa
--- /dev/null
+++ b/clang/lib/Interpreter/WASM.cpp
@@ -0,0 +1,107 @@
+//===- WASM.cpp - WASM Interpreter --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file implements interpreter support for code ex