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

2023-01-16 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite created this revision.
Herald added a project: All.
argentite added reviewers: v.g.vassilev, lhames, sgraenitz.
argentite published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This commit adds the %lib  command to load a dynamic library to be
used by the currently running interpreted code. For example `%lib SDL2`
loads libSDL2.so on Linux and SDL2.dll on Windows automatically.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141824

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

Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -123,6 +123,13 @@
 }
 continue;
   }
+  if (Line->rfind(R"(%lib )", 0) == 0) {
+if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) {
+  llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+  HasError = true;
+}
+continue;
+  }
 
   if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
Index: clang/lib/Interpreter/Interpreter.cpp
===
--- clang/lib/Interpreter/Interpreter.cpp
+++ clang/lib/Interpreter/Interpreter.cpp
@@ -29,10 +29,13 @@
 #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/Support/Host.h"
 
+#include 
+
 using namespace clang;
 
 // FIXME: Figure out how to unify with namespace init_convenience from
@@ -203,10 +206,13 @@
   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 +220,21 @@
   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 +296,30 @@
   }
   return llvm::Error::success();
 }
+
+llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
+  auto EE = GetExecutionEngine();
+  if (!EE)
+return EE.takeError();
+
+  auto &DL = EE->getDataLayout();
+
+  std::stringstream filename;
+#if defined(LLVM_ON_UNIX)
+  filename << "lib";
+#endif
+  filename << name;
+#if defined(_WIN32)
+  filename << ".dll";
+#elif defined(LLVM_ON_UNIX)
+  filename << ".so";
+#endif
+
+  if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load(
+  filename.str().c_str(), DL.getGlobalPrefix()))
+EE->getMainJITDylib().addGenerator(std::move(*DLSG));
+  else
+return DLSG.takeError();
+
+  return llvm::Error::success();
+}
Index: clang/lib/Interpreter/IncrementalExecutor.h
===
--- clang/lib/Interpreter/IncrementalExecutor.h
+++ clang/lib/Interpreter/IncrementalExecutor.h
@@ -53,7 +53,8 @@
   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
Index: clang/include/clang/Interpreter/Interpreter.h
===
--- clang/include/clang/Interpreter/Interpreter.h
+++ clang/include/clang/Interpreter/Interpreter.h
@@ -28,7 +28,7 @@
 namespace orc {
 class LLJIT;
 class ThreadSafeContext;
-}
+} // namespace orc
 } // namespace llvm
 
 namespace clang {
@@ -52,12 +52,15 @@
 
   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

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

2023-01-23 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/include/clang/Interpreter/Interpreter.h:62
   const CompilerInstance *getCompilerInstance() const;
-  const llvm::orc::LLJIT *getExecutionEngine() const;
+  llvm::Expected GetExecutionEngine();
+

sgraenitz wrote:
> Do we really want these functions capitalized? IIRC in the clang code-base 
> they are used to highlight core API functions. It looks like these two 
> functions are only used as helpers inside `Interpreter` itself.
I guess we can keep them without capitalizing.



Comment at: clang/lib/Interpreter/Interpreter.cpp:223
 
+llvm::Error Interpreter::CreateExecutor() {
+  const clang::TargetInfo &TI =

sgraenitz wrote:
> Factoring out this function looks like an independent change. Is it related 
> to the load-library command in any way?
The `IncrementalExecutor` was being created on the first call to `Execute()`. 
It is created lazily for `-fsyntax-only` support. It holds the Execution Engine 
that we need for loading the library. So we need to be able to create it in 
`getExecutionEngine()` as well because a library can be loaded before any code 
is executed.



Comment at: clang/tools/clang-repl/ClangRepl.cpp:126
   }
+  if (Line->rfind(R"(%lib )", 0) == 0) {
+if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) {

sgraenitz wrote:
> I see this is analog to existing code, but is there a good reason for using 
> the multi-line string syntax here?
Yeah I am confused as well. But I tried to maintain the style.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

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


[PATCH] D158140: WIP: [clang-repl] Basic WebAssembly support for running inside a JS engine

2023-08-16 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite created this revision.
Herald added subscribers: cfe-commits, pmatos, asb, sunfish, jgravelle-google, 
sbc100, dschuff.
Herald added projects: clang, All.
argentite requested review of this revision.
Herald added a subscriber: aheejin.

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()


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158140

Files:
  clang/lib/Interpreter/CMakeLists.txt
  clang/lib/Interpreter/IncrementalExecutor.cpp
  clang/lib/Interpreter/IncrementalExecutor.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/lib/Interpreter/WASM.cpp
  clang/lib/Interpreter/WASM.h

Index: clang/lib/Interpreter/WASM.h
===
--- /dev/null
+++ clang/lib/Interpreter/WASM.h
@@ -0,0 +1,33 @@
+//===-- WASM.h - 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 execution in WebAssembly.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_LIB_INTERPRETER_WASM_H
+#define LLVM_CLANG_LIB_INTERPRETER_WASM_H
+
+#include "IncrementalExecutor.h"
+
+namespace clang {
+
+class WASMIncrementalExecutor : public IncrementalExecutor {
+public:
+  WASMIncrementalExecutor(llvm::orc::ThreadSafeContext &TSC);
+
+  llvm::Error addModule(PartialTranslationUnit &PTU) override;
+  llvm::Error removeModule(PartialTranslationUnit &PTU) override;
+  llvm::Error runCtors() const override;
+
+  ~WASMIncrementalExecutor() override;
+};
+
+} // namespace clang
+
+#endif // LLVM_CLANG_LIB_INTERPRETER_WASM_H
Index: clang/lib/Interpreter/WASM.cpp
===
--- /dev/null
+++ 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 execution in WebAssembly.
+//
+//===--===//
+
+#include "WASM.h"
+#include "IncrementalExecutor.h"
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+
+namespace clang {
+
+WASMIncrementalExecutor::WASMIncrementalExecutor(
+llvm::orc::ThreadSafeContext &TSC)
+: IncrementalExecutor(TSC) {}
+
+llvm::Error WASMIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
+  PTU.TheModule->dump();
+
+  std::string ErrorString;
+
+  const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(
+  PTU.TheModule->getTargetTriple(), ErrorString);
+  if (!Target) {
+return llvm::make_error("Failed to create WASM Target: ",
+   llvm::inconvertibleErrorCode());
+  }
+
+  llvm::TargetOptions TO = llvm::TargetOptions();
+  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::error_code Error;
+  llvm::raw_fd_ostream OutputFile(llvm::StringRef(OutputFileName), Error);
+
+  llvm::legacy::PassManager PM;
+  if (TargetMachine->addPassesToEmitFile(PM, OutputFile, nullptr,
+ llvm::CGFT_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();
+
+  std::vector LinkerArgs = {"wasm-ld",
+  "-pie",
+  "--import-memory",
+  "--no-entry",
+  "--export-all",
+  "--experimental-pic",
+  "--no-export-dynamic",
+  "--stack-first",
+ 

[PATCH] D158140: WIP: [clang-repl] Basic WebAssembly support for running inside a JS engine

2023-08-16 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added reviewers: v.g.vassilev, sgraenitz, lhames.
argentite added a comment.

This cannot built yet because it depends on lld which does not seem to usable 
from clang directly. I am looking for feedback/ideas on how to proceed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158140/new/

https://reviews.llvm.org/D158140

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


[PATCH] D158140: WIP: [clang-repl] Basic WebAssembly support for running inside a JS engine

2023-08-23 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/lib/Interpreter/WASM.cpp:79
+  int Result =
+  lld::wasm::link(LinkerArgs, llvm::outs(), llvm::errs(), false, false);
+  if (!Result)

v.g.vassilev wrote:
> I am not sure how we can solve that dependency here. Worst case scenario, 
> could we check if `lld` is installed and make a system call?
AFAIK we can't really `exec()` within Emscripten.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158140/new/

https://reviews.llvm.org/D158140

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


[PATCH] D158140: WIP: [clang-repl] Basic WebAssembly support for running inside a JS engine

2023-08-23 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added a comment.

Here's a demo of it in action with clang and lld linked statically out of tree. 
https://wasmdemo.argentite.me


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158140/new/

https://reviews.llvm.org/D158140

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


[PATCH] D158140: WIP: [clang-repl] Basic WebAssembly support for running inside a JS engine

2023-08-24 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added a comment.

Yes, I think that's the simplest solution. `__EMSCRIPTEN__` probably won't work 
because somebody may want to build for Emscripten without lld. 
`STATICALLY_LINKED_LLD` would probably be better. But I could not figure out 
how to do that in the build system.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158140/new/

https://reviews.llvm.org/D158140

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


[PATCH] D151904: [clang-repl][CUDA] Add an unit test for interactive CUDA

2023-08-24 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite edited the summary of this revision.
argentite updated this revision to Diff 553197.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151904/new/

https://reviews.llvm.org/D151904

Files:
  clang/unittests/Interpreter/CMakeLists.txt
  clang/unittests/Interpreter/InteractiveCudaTest.cpp

Index: clang/unittests/Interpreter/InteractiveCudaTest.cpp
===
--- /dev/null
+++ clang/unittests/Interpreter/InteractiveCudaTest.cpp
@@ -0,0 +1,147 @@
+//===- unittests/Interpreter/CudaTest.cpp --- Interactive CUDA tests --===//
+//
+// 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
+//
+//===--===//
+//
+// Unit tests for interactive CUDA in Clang interpreter
+//
+//===--===//
+
+#include "clang/Basic/Version.h"
+#include "clang/Config/config.h"
+
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Interpreter/Interpreter.h"
+
+#include "llvm/Support/TargetSelect.h"
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace {
+
+std::string MakeResourcesPath() {
+  using namespace llvm;
+#ifdef LLVM_BINARY_DIR
+  StringRef Dir = LLVM_BINARY_DIR;
+#else
+  // Dir is bin/ or lib/, depending on where BinaryPath is.
+  void *MainAddr = (void *)(intptr_t)MakeResourcesPath;
+  std::string BinaryPath =
+  llvm::sys::fs::getMainExecutable(/*Argv0=*/nullptr, MainAddr);
+
+  // build/tools/clang/unittests/Interpreter/Executable -> build/
+  StringRef Dir = sys::path::parent_path(BinaryPath);
+
+  Dir = sys::path::parent_path(Dir);
+  Dir = sys::path::parent_path(Dir);
+  Dir = sys::path::parent_path(Dir);
+  Dir = sys::path::parent_path(Dir);
+#endif // LLVM_BINARY_DIR
+  SmallString<128> P(Dir);
+  sys::path::append(P, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
+CLANG_VERSION_MAJOR_STRING);
+  return P.str().str();
+}
+
+static std::unique_ptr
+createInterpreter(const std::vector &ExtraArgs = {}) {
+  static bool firstrun = true;
+  if (firstrun) {
+llvm::InitializeAllTargetInfos();
+llvm::InitializeAllTargets();
+llvm::InitializeAllTargetMCs();
+llvm::InitializeAllAsmPrinters();
+
+firstrun = false;
+  }
+
+  clang::IncrementalCompilerBuilder CB;
+
+  // Help find cuda's runtime headers.
+  std::string ResourceDir = MakeResourcesPath();
+
+  std::vector Args = {"-resource-dir", ResourceDir.c_str(), "-std=c++20"};
+  Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
+  CB.SetCompilerArgs(Args);
+
+  // Create the device code compiler
+  std::unique_ptr DeviceCI;
+  CB.SetOffloadArch("sm_35");
+  DeviceCI = cantFail(CB.CreateCudaDevice());
+
+  std::unique_ptr CI;
+  CI = cantFail(CB.CreateCudaHost());
+
+  auto Interp = cantFail(
+  clang::Interpreter::createWithCUDA(std::move(CI), std::move(DeviceCI)));
+
+  return Interp;
+}
+
+enum {
+  // Defined in CUDA Runtime API
+  cudaErrorNoDevice = 100,
+};
+
+TEST(InteractiveCudaTest, Sanity) {
+  std::unique_ptr Interp = createInterpreter();
+
+#ifdef LIBCUDART_PATH
+  auto Err = Interp->LoadDynamicLibrary(LIBCUDART_PATH);
+  if (Err) { // CUDA runtime is not usable, cannot continue testing
+consumeError(std::move(Err));
+return;
+  }
+#else
+  return;
+#endif
+
+  // Check if we have any GPU for test
+  int CudaError = 0;
+  auto GpuCheckCommand = std::string(R"(
+int device_id = -1;
+int *error = (int *))" + std::to_string((uintptr_t)&CudaError) +
+ R"(;
+*error = cudaGetDevice(&device_id);
+  )");
+  cantFail(Interp->ParseAndExecute(GpuCheckCommand));
+  if (CudaError == cudaErrorNoDevice) {
+// No GPU is available on this machine, cannot continue testing
+return;
+  }
+  ASSERT_EQ(CudaError, 0);
+
+  int HostSum = 0;
+  auto Command1 = std::string(R"(
+__host__ __device__ inline int sum(int a, int b){ return a + b; }
+__global__ void kernel(int * output){ *output = sum(40,2); }
+int *hostsum = (int *) )") +
+  std::to_string((uintptr_t)&HostSum) +
+  R"(;
+*hostsum = sum(41,1);)";
+  cantFail(Interp->ParseAndExecute(Command1));
+
+  int DeviceSum = 0;
+  auto Command2 = std::string(R"(
+int *devicesum = (int *))" +
+  std::to_string((uintptr_t)&DeviceSum) +
+  R"(;
+int *deviceVar;
+*error |= cudaMalloc((void **) &deviceVar, sizeof(int));
+kernel<<<1,1>>>(deviceVar);
+*error |= cudaGetLastError();
+*error |= cudaMemcpy(devicesum, deviceVar, sizeof(int), cudaMemcpyDeviceToHost);
+*error |= cudaGetLastError();
+)");
+  cantFail(Interp->ParseAndExecute(Command2));
+
+  ASSERT_EQ(HostSum, 42);
+  ASSERT_EQ(DeviceSum

[PATCH] D151904: [clang-repl][CUDA] Add an unit test for interactive CUDA

2023-08-24 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/unittests/Interpreter/InteractiveCudaTest.cpp:92
+  std::unique_ptr Interp = createInterpreter();
+  auto Err = Interp->LoadDynamicLibrary("libcudart.so");
+  if (Err) { // CUDA runtime is not installed/usable, cannot continue testing

tra wrote:
> argentite wrote:
> > tra wrote:
> > > This could be a bit of a problem.
> > > 
> > > There may be multiple CUDA SDK versions that may be installed on a system 
> > > at any given time and the libcudart.so you pick here may not be the one 
> > > you want.
> > > E.g it may be from a recent CUDA version which is not supported by NVIDIA 
> > > drivers yet. 
> > > 
> > > I think you may need a way to let the user override CUDA SDK (or 
> > > libcudart.so) location explicitly. I guess they could do that via 
> > > LD_LIBRARY_PATH, but for the CUDA compilation in general, knowing CUDA 
> > > SDK path is essential, as it does affect various compilation options set 
> > > by the driver.
> > > 
> > Yes, this probably would be an issue. It is currently possible to override 
> > the CUDA path with a command line argument in clang-repl. But I am not sure 
> > what we can do inside a test.
> To me it looks like CUDA location should be detected/set at the configuration 
> time and then propagated to the individual tests that need that info.
> CMake has cuda detection mechanisms that could be used for that purpose.
> They are a bit of a pain to use in practice (I'm still not sure what's the 
> reliable way to do it), but it's as close to the 'standard' way of doing it 
> as we have at the moment.
> I believe libc and mlir subtrees in LLVM are already using this mechanism. 
> E.g 
> https://github.com/llvm/llvm-project/blob/main/libc/utils/gpu/loader/CMakeLists.txt#L16
This version uses CMake to detect the paths as in the example above. I guess 
this would tie the test to the build system but it should probably be fine for 
most cases. Unfortunately I don't think we can use the same trick for 
determining the path in ClangREPL because the interpreter is likely to run on a 
different system.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151904/new/

https://reviews.llvm.org/D151904

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


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

2023-03-21 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 507046.
argentite marked 3 inline comments as done.
argentite edited the summary of this revision.
argentite added a comment.

Added a simple test and removed the automatic OS specific library filenames

Full name/path of the library must be provided. This makes the command more
flexible especially when dealing with multiple versions of a library or SDK.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

Files:
  clang/include/clang/Interpreter/Interpreter.h
  clang/lib/Interpreter/IncrementalExecutor.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/test/Interpreter/dynamic-library.cpp
  clang/tools/clang-repl/ClangRepl.cpp

Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -123,6 +123,13 @@
 }
 continue;
   }
+  if (Line->rfind("%lib ", 0) == 0) {
+if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) {
+  llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+  HasError = true;
+}
+continue;
+  }
 
   if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
Index: clang/test/Interpreter/dynamic-library.cpp
===
--- /dev/null
+++ clang/test/Interpreter/dynamic-library.cpp
@@ -0,0 +1,24 @@
+// RUN: head -n 7 %s | %clang -xc++ -o %T/libdynamic-library-test.so -fPIC -shared -
+int ultimate_answer = 0;
+
+int calculate_answer() {
+  ultimate_answer = 42;
+  return 5;
+}
+
+// REQUIRES: host-supports-jit, system-linux
+// RUN: tail -n 16 %s | env LD_LIBRARY_PATH=%T:$LD_LIBRARY_PATH clang-repl | FileCheck %s
+#include 
+
+extern int ultimate_answer;
+int calculate_answer();
+
+%lib libdynamic-library-test.so
+
+printf("Return value: %d\n", calculate_answer());
+// CHECK: Return value: 5
+
+printf("Variable: %d\n", ultimate_answer);
+// CHECK-NEXT: Variable: 42
+
+%quit
Index: clang/lib/Interpreter/Interpreter.cpp
===
--- clang/lib/Interpreter/Interpreter.cpp
+++ 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 @@
   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 @@
   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 @@
   }
   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(
+  name, DL.getGlobalPrefix()))
+EE->getMainJITDylib().addGenerator(std::move(*DLSG));
+  else
+return DLSG.takeError();
+
+  return llvm::Error::success();
+}
Index: clang/lib/Interpreter/IncrementalExecutor.h
===
--- clang/lib/Interpreter/IncrementalExecutor.h
+++ clang/lib/Interpreter/IncrementalExecutor.h
@@ -53,7 +53,8 @@
   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
Index: clang/include/clang/Interpreter/Interpreter.h
===
--- clang/include/clang/Interpreter/I

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

2023-03-28 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 508971.
argentite marked an inline comment as done.
argentite added a comment.

Changed test to use a separate Input file for the dynamic library generation


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

Files:
  clang/include/clang/Interpreter/Interpreter.h
  clang/lib/Interpreter/IncrementalExecutor.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/test/Interpreter/Inputs/dynamic-library-test.cpp
  clang/test/Interpreter/dynamic-library.cpp
  clang/tools/clang-repl/ClangRepl.cpp

Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -123,6 +123,13 @@
 }
 continue;
   }
+  if (Line->rfind("%lib ", 0) == 0) {
+if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) {
+  llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+  HasError = true;
+}
+continue;
+  }
 
   if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
Index: clang/test/Interpreter/dynamic-library.cpp
===
--- /dev/null
+++ clang/test/Interpreter/dynamic-library.cpp
@@ -0,0 +1,19 @@
+// REQUIRES: host-supports-jit, system-linux
+
+// 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
+
+#include 
+
+extern int ultimate_answer;
+int calculate_answer();
+
+%lib libdynamic-library-test.so
+
+printf("Return value: %d\n", calculate_answer());
+// CHECK: Return value: 5
+
+printf("Variable: %d\n", ultimate_answer);
+// CHECK-NEXT: Variable: 42
+
+%quit
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- /dev/null
+++ clang/test/Interpreter/Inputs/dynamic-library-test.cpp
@@ -0,0 +1,6 @@
+int ultimate_answer = 0;
+
+int calculate_answer() {
+  ultimate_answer = 42;
+  return 5;
+}
Index: clang/lib/Interpreter/Interpreter.cpp
===
--- clang/lib/Interpreter/Interpreter.cpp
+++ 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 @@
   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 @@
   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 @@
   }
   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(
+  name, DL.getGlobalPrefix()))
+EE->getMainJITDylib().addGenerator(std::move(*DLSG));
+  else
+return DLSG.takeError();
+
+  return llvm::Error::success();
+}
Index: clang/lib/Interpreter/IncrementalExecutor.h
===
--- clang/lib/Interpreter/IncrementalExecutor.h
+++ clang/lib/Interpreter/IncrementalExecutor.h
@@ -53,7 +53,8 @@
   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
Index: clang/include/clang/Interpreter/Interpreter.h
==

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

2023-03-28 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/test/Interpreter/dynamic-library.cpp:1
+// RUN: head -n 7 %s | %clang -xc++ -o %T/libdynamic-library-test.so -fPIC 
-shared -
+int ultimate_answer = 0;

v.g.vassilev wrote:
> sgraenitz wrote:
> > The use of `head` and `tail` here is a creative solution, but I wonder how 
> > well it scales for similar cases in the future in case this becomes a role 
> > model. We have this situation a lot in LLDB: compile an input file and use 
> > the output in a RUN line. It typically uses separate input files for such 
> > purposes (note that "Inputs" folders must be excluded from test discovery 
> > somewhere in lit config), e.g.:
> > https://github.com/llvm/llvm-project/blob/release/16.x/lldb/test/Shell/Breakpoint/jit-loader_jitlink_elf.test
> > 
> > What do you think?
> I agree with that. I've seen that some clang tests surround the code in 
> question with `#ifdef LIB` and then in the invocation they add `-DLIB`.
The `#ifdef` approach does not seem to work with clang-repl yet. It does not 
like unterminated `#ifdef`. So I am going with separate input file. "Inputs" 
folder seems to be excluded globally in `clang/test/lit.cfg.py`.



Comment at: clang/test/Interpreter/dynamic-library.cpp:9
+
+// REQUIRES: host-supports-jit, system-linux
+// RUN: tail -n 16 %s | env LD_LIBRARY_PATH=%T:$LD_LIBRARY_PATH clang-repl | 
FileCheck %s

sgraenitz wrote:
> This requirement applies to the entire file right? The arrangement here may 
> imply the opposite.
Moved it to the top of the file.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

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


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

2023-03-28 Thread Anubhab Ghosh via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd978730d8e2c: [clang-repl] Add a command to load dynamic 
libraries (authored by argentite).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

Files:
  clang/include/clang/Interpreter/Interpreter.h
  clang/lib/Interpreter/IncrementalExecutor.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/test/Interpreter/Inputs/dynamic-library-test.cpp
  clang/test/Interpreter/dynamic-library.cpp
  clang/tools/clang-repl/ClangRepl.cpp

Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -123,6 +123,13 @@
 }
 continue;
   }
+  if (Line->rfind("%lib ", 0) == 0) {
+if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) {
+  llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+  HasError = true;
+}
+continue;
+  }
 
   if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
Index: clang/test/Interpreter/dynamic-library.cpp
===
--- /dev/null
+++ clang/test/Interpreter/dynamic-library.cpp
@@ -0,0 +1,19 @@
+// REQUIRES: host-supports-jit, system-linux
+
+// 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
+
+#include 
+
+extern int ultimate_answer;
+int calculate_answer();
+
+%lib libdynamic-library-test.so
+
+printf("Return value: %d\n", calculate_answer());
+// CHECK: Return value: 5
+
+printf("Variable: %d\n", ultimate_answer);
+// CHECK-NEXT: Variable: 42
+
+%quit
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- /dev/null
+++ clang/test/Interpreter/Inputs/dynamic-library-test.cpp
@@ -0,0 +1,6 @@
+int ultimate_answer = 0;
+
+int calculate_answer() {
+  ultimate_answer = 42;
+  return 5;
+}
Index: clang/lib/Interpreter/Interpreter.cpp
===
--- clang/lib/Interpreter/Interpreter.cpp
+++ 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 @@
   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 @@
   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 @@
   }
   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(
+  name, DL.getGlobalPrefix()))
+EE->getMainJITDylib().addGenerator(std::move(*DLSG));
+  else
+return DLSG.takeError();
+
+  return llvm::Error::success();
+}
Index: clang/lib/Interpreter/IncrementalExecutor.h
===
--- clang/lib/Interpreter/IncrementalExecutor.h
+++ clang/lib/Interpreter/IncrementalExecutor.h
@@ -53,7 +53,8 @@
   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
Index: clang/include/clang/Interpreter/Interpreter.h
==

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

2023-03-29 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added a comment.

Hi, @dyung ! It seems the dynamic library generation is failing from the lack 
of a linker. We need a test library to load at runtime in clang-repl. Do you 
have any idea how we can create one without a linker? Otherwise we can disable 
the test on PS4 as it is probably an unlikely use case.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

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


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

2023-03-29 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added a comment.

Just to confirm, `UNSUPPORTED: target=x86_64-scei-ps4` should be enough, right?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

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


[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-04-04 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite created this revision.
Herald added subscribers: mattd, carlosgalvezp, yaxunl.
Herald added a project: All.
argentite updated this revision to Diff 507049.
argentite added a comment.
argentite updated this revision to Diff 510808.
argentite edited the summary of this revision.
argentite added reviewers: v.g.vassilev, sgraenitz, lhames.
argentite published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Use full name of CUDA library


argentite added a comment.

Clear LinkModules on every interpreter iteration


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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146389

Files:
  clang/include/clang/Interpreter/Interpreter.h
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Interpreter/CMakeLists.txt
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/lib/Interpreter/Offload.cpp
  clang/lib/Interpreter/Offload.h
  clang/tools/clang-repl/ClangRepl.cpp

Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,9 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +93,29 @@
 return 0;
   }
 
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+auto DeviceArgv = ClangArgv;
+
+DeviceCI = ExitOnErr(
+clang::IncrementalCudaCompilerBuilder::createDevice(DeviceArgv));
+  }
+
   // FIXME: Investigate if we could use runToolOnCodeWithArgs from tooling. It
   // can replace the boilerplate code for creation of the compiler instance.
-  auto CI = ExitOnErr(clang::IncrementalCompilerBuilder::create(ClangArgv));
+  std::unique_ptr CI;
+  if (CudaEnabled) {
+CI = ExitOnErr(clang::IncrementalCudaCompilerBuilder::createHost(
+ClangArgv, "/tmp/clang-repl.fatbin"));
+  } else {
+CI = ExitOnErr(clang::IncrementalCompilerBuilder::createCpp(ClangArgv));
+  }
 
   // Set an error handler, so that any LLVM backend diagnostics go through our
   // error handler.
@@ -102,7 +125,19 @@
   // Load any requested plugins.
   CI->LoadRequestedPlugins();
 
-  auto Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
+  std::unique_ptr Interp;
+  if (CudaEnabled) {
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+Interp = ExitOnErr(clang::Interpreter::createWithCUDA(
+std::move(CI), std::move(DeviceCI), OffloadArch,
+"/tmp/clang-repl.fatbin"));
+
+ExitOnErr(Interp->LoadDynamicLibrary("libcudart.so"));
+  } else
+Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
+
   for (const std::string &input : OptInputs) {
 if (auto Err = Interp->ParseAndExecute(input))
   llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
Index: clang/lib/Interpreter/Offload.h
===
--- /dev/null
+++ clang/lib/Interpreter/Offload.h
@@ -0,0 +1,47 @@
+//===--- Offload.h - CUDA Offloading *- 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 classes required for offloading to CUDA devices.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_LIB_INTERPRETER_OFFLOAD_H
+#define LLVM_CLANG_LIB_INTERPRETER_OFFLOAD_H
+
+#include "IncrementalParser.h"
+
+namespace clang {
+
+class DeviceCodeInlinerAction;
+
+class IncrementalCUDADeviceParser : public IncrementalParser {
+public:
+  IncrementalCUDADeviceParser(std::unique_ptr Instance,
+  llvm::LLVMContext &LLVMCtx, llvm::StringRef Arch,
+  llvm::StringRef FatbinFile, llvm::Error &Err);
+
+  llvm::Expected
+  Parse(llvm::StringRef Input) override;
+
+  // Generate PTX for the last PTU
+  llvm::Expected GeneratePTX();
+
+  // Write last PTX to the fatbinary file
+  llvm::Error WriteFatbinary() const;
+
+  ~IncrementalCUDADeviceParser();
+
+protected:
+  int SMVersion;
+  

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-04-05 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added a comment.

In D146389#4243900 , @tra wrote:

>> Initial interactive CUDA support for clang-repl
>
> What should a user expect to be supported, functionality wise? I assume it 
> should cover parsing and compilation. I'm not so sure about the execution. 
> Should it be expected to actually launch kernels, or will that come in a 
> future patch?

With this patch alone, we can launch kernels with the usual syntax. The 
`__device__` functions need to be `inline` for now. We plan to automate that in 
the future.




Comment at: clang/include/clang/Interpreter/Interpreter.h:43-44
 public:
   static llvm::Expected>
   create(std::vector &ClangArgv);
+  static llvm::Expected>

Hahnfeld wrote:
> If I understand the change correctly, the "old" `create` function on its own 
> is not sufficient anymore to create a fully working `CompilerInstance` - 
> should we make it `private`? (and unify the two `Builder` classes into one?)
> 
> Alternatively, we could keep the current function working and move the bulk 
> of the work to an `createInternal` function. What do you think?
Yes the old `create` should probably be private. I was also thinking we could 
merge `IncrementalCudaCompilerBuilder` with `IncrementalCompilerBuilder` and 
make it stateful with CUDA SDK path for example. Then we could do something 
like:
```
IncrementalCompilerBuilder Builder;
Builder.setCudaPath(...);
auto DeviceCI = Builder.createCudaDevice();
auto HostCI = Builder.createCudaHost();
```



Comment at: clang/lib/CodeGen/CodeGenAction.cpp:296
   }
+  LinkModules.clear();
   return false; // success

Hahnfeld wrote:
> This looks like a change that has implications beyond support for CUDA. Would 
> it make sense to break this out into a separate review, ie does this change 
> something in the current setup?
It actually was a separate patch: D146388 Should I submit that for review?
It seems to be required because we call `LinkInModules()` once for every 
interpreter iteration.




Comment at: clang/lib/Interpreter/IncrementalParser.cpp:125
 
+static CodeGenerator *getCodeGen(FrontendAction *Act);
+

Hahnfeld wrote:
> Is it possible to move the `static` function here instead of forward 
> declaring? Or does it depend on other functions in this file?
We can probably move this. I just wanted to preserve the history,



Comment at: clang/lib/Interpreter/IncrementalParser.cpp:143-156
+  // An initial PTU is needed as CUDA includes some headers automatically
+  auto PTU = ParseOrWrapTopLevelDecl();
+  if (auto E = PTU.takeError()) {
+consumeError(std::move(E)); // FIXME
+return; // PTU.takeError();
+  }
+

Hahnfeld wrote:
> Should this be done as part of the "generic" `IncrementalParser` constructor, 
> or only in the CUDA case by something else? Maybe 
> `Interpreter::createWithCUDA`?
That seems safer. Although I did not notice any side effects.



Comment at: clang/lib/Interpreter/Interpreter.cpp:270-274
+  if (DeviceParser) {
+auto DevicePTU = DeviceParser->Parse(Code);
+if (auto E = DevicePTU.takeError())
+  return E;
+  }

Hahnfeld wrote:
> Just wondering about the order here: Do we have to parse the device side 
> first? Does it make a difference for diagnostics? Maybe you can add a comment 
> about the choice here...
The fatbinary from the device side is used in the host pipeline.



Comment at: clang/lib/Interpreter/Offload.cpp:1
+//===-- Offload.cpp - CUDA Offloading ---*- C++ 
-*-===//
+//

Hahnfeld wrote:
> v.g.vassilev wrote:
> > How about `DeviceOffload.cpp`?
> Or `IncrementalCUDADeviceParser.cpp` for the moment - not sure what other 
> classes will be added in the future, and if they should be in the same TU.
I wanted to avoid "CUDA" in case we use it later for HIP.



Comment at: clang/tools/clang-repl/ClangRepl.cpp:137
+
+ExitOnErr(Interp->LoadDynamicLibrary("libcudart.so"));
+  } else

v.g.vassilev wrote:
> tra wrote:
> > v.g.vassilev wrote:
> > > tra wrote:
> > > > Is there any doc describing the big picture approach to CUDA REPL 
> > > > implementation and how all the pieces tie together?
> > > > 
> > > > From the patch I see that we will compile GPU side of the code to PTX, 
> > > > pack it into fatbinary, but it's not clear now do we get from there to 
> > > > actually launching the kernels. Loading libcudart.so here also does not 
> > > > appear to be tied to anything else. I do not see any direct API calls, 
> > > > and the host-side compilation appears to be done w.o passing the GPU 
> > > > binary to it, which would normally trigger generation of the glue code 
> > > > to register the kernels with CUDA runtime. I may be missing something, 
> > > > too.
> > > > 
> > > > I assum

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-04-11 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 512521.
argentite marked 3 inline comments as done.
argentite added a comment.

Combined IncrementalCompilerBuilder and IncrementalCudaCompilerBuilder
Added --cuda-path support
Use sys::fs::createTemporaryFile() instead of hardcoding the path
Other minor refactoring

I am planning to have the in-memory fat binary file as a separate patch


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  clang/include/clang/Interpreter/Interpreter.h
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Interpreter/CMakeLists.txt
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/lib/Interpreter/Offload.cpp
  clang/lib/Interpreter/Offload.h
  clang/tools/clang-repl/ClangRepl.cpp

Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,40 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  llvm::SmallString<128> CudaTempFile;
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+llvm::sys::fs::createTemporaryFile("clang-repl", "fatbin", CudaTempFile);
+CB.SetCudaTempFatbinFile(CudaTempFile);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDevice());
+  }
+
   // FIXME: Investigate if we could use runToolOnCodeWithArgs from tooling. It
   // can replace the boilerplate code for creation of the compiler instance.
-  auto CI = ExitOnErr(clang::IncrementalCompilerBuilder::create(ClangArgv));
+  std::unique_ptr CI;
+  if (CudaEnabled) {
+CI = ExitOnErr(CB.CreateCudaHost());
+  } else {
+CI = ExitOnErr(CB.CreateCpp());
+  }
 
   // Set an error handler, so that any LLVM backend diagnostics go through our
   // error handler.
@@ -101,8 +136,23 @@
 
   // Load any requested plugins.
   CI->LoadRequestedPlugins();
+  if (CudaEnabled)
+DeviceCI->LoadRequestedPlugins();
+
+  std::unique_ptr Interp;
+  if (CudaEnabled) {
+Interp = ExitOnErr(clang::Interpreter::createWithCUDA(
+std::move(CI), std::move(DeviceCI), CudaTempFile));
+
+if (CudaPath.empty()) {
+  ExitOnErr(Interp->LoadDynamicLibrary("libcudart.so"));
+} else {
+  auto CudaRuntimeLibPath = CudaPath + "/lib/libcudart.so";
+  ExitOnErr(Interp->LoadDynamicLibrary(CudaRuntimeLibPath.c_str()));
+}
+  } else
+Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
 
-  auto Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
   for (const std::string &input : OptInputs) {
 if (auto Err = Interp->ParseAndExecute(input))
   llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
@@ -138,6 +188,9 @@
 }
   }
 
+  if (!CudaTempFile.empty())
+llvm::sys::fs::remove(CudaTempFile);
+
   // Our error handler depends on the Diagnostics object, which we're
   // potentially about to delete. Uninstall the handler now so that any
   // later errors use the default handling behavior instead.
Index: clang/lib/Interpreter/Offload.h
===
--- /dev/null
+++ clang/lib/Interpreter/Offload.h
@@ -0,0 +1,46 @@
+//===--- Offload.h - CUDA Offloading *- 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 classes required for offloading to CUDA devices.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_LIB_INTERPRETER_OFFLOAD_H
+#define LLVM_CLANG_LIB_INTERPRETER_OFFLOAD_H
+
+#include "IncrementalParser.h"
+#include "llvm/Support/FileSystem.h"
+
+namespace clang {
+
+class IncrementalCUDADeviceParser : public IncrementalParser {
+public:
+  IncrementalCUDADeviceParser(std::unique_ptr Ins

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-04-11 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/lib/Interpreter/Offload.cpp:90-91
+  PTXCode += '\0';
+  while (PTXCode.size() % 8)
+PTXCode += '\0';
+  return PTXCode.str();

Hahnfeld wrote:
> Is padding to 8 bytes a requirement for PTX? Maybe add a coment...
This was actually in original Cling implementation but it does not seem to be 
required.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

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


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

2023-04-17 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added a comment.

We should probably also address the lack of linker issue as well. Should we go 
for a precompiled dynamic library file? There seems to be some "precedent" of 
this in other tests.




Comment at: clang/test/Interpreter/dynamic-library.cpp:6
+
+#include 
+

sgraenitz wrote:
> bcain wrote:
> > This test fails for me like below.  
> > 
> > 
> > ```
> > FAIL: Clang :: Interpreter/dynamic-library.cpp (1 of 17751)
> >  TEST 'Clang :: Interpreter/dynamic-library.cpp' FAILED 
> > 
> > Script:
> > --
> > : 'RUN: at line 4';   /local/mnt/workspace/upstream/obj_ubuntu/bin/clang 
> > -xc++ -o 
> > /local/mnt/workspace/upstream/obj_ubuntu/tools/clang/test/Interpreter/Output/libdynamic-library-test.so
> >  -fPIC -shared -DLIBRARY 
> > /local/mnt/workspace/upstream/llvm-project/clang/test/Interpreter/Inputs/dynamic-library-test.cpp
> > : 'RUN: at line 5';   cat 
> > /local/mnt/workspace/upstream/llvm-project/clang/test/Interpreter/dynamic-library.cpp
> >  | env 
> > LD_LIBRARY_PATH=/local/mnt/workspace/upstream/obj_ubuntu/tools/clang/test/Interpreter/Output:$LD_LIBRARY_PATH
> >  /local/mnt/workspace/upstream/obj_ubuntu/bin/clang-repl | 
> > /local/mnt/workspace/upstream/obj_ubuntu/bin/FileCheck 
> > /local/mnt/workspace/upstream/llvm-project/clang/test/Interpreter/dynamic-library.cpp
> > --
> > Exit Code: 1
> > 
> > Command Output (stderr):
> > --
> > In file included from <<< inputs >>>:1:
> > input_line_6:1:10: fatal error: 'cstdio' file not found
> > #include 
> >  ^~~~
> > error: Parsing failed.
> > input_line_12:1:1: error: use of undeclared identifier 'printf'
> > printf("Return value: %d\n", calculate_answer());
> > ^
> > error: Parsing failed.
> > input_line_15:1:1: error: use of undeclared identifier 'printf'
> > printf("Variable: %d\n", ultimate_answer);
> > ^
> > error: Parsing failed.
> > /local/mnt/workspace/upstream/llvm-project/clang/test/Interpreter/dynamic-library.cpp:15:11:
> >  error: CHECK: expected string not found in input
> > // CHECK: Return value: 5
> >   ^
> > :1:1: note: scanning from here
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> 
> > ^
> > :1:231: note: possible intended match here
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> 
> > 
> > 
> > 
> >   ^
> > 
> > Input file: 
> > Check file: 
> > /local/mnt/workspace/upstream/llvm-project/clang/test/Interpreter/dynamic-library.cpp
> > 
> > -dump-input=help explains the following input dump.
> > 
> > Input was:
> > <<
> > 1: clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
> > clang-repl> clang-repl> clang-repl>  
> > check:15'0 
> > X
> >  error: no match found
> > check:15'1  
> > 
> > 
> >  ?   possible intended match
> > >>
> > 
> > --
> > 
> > 
> > 
> > Failed Tests (1):
> >   Clang :: Interpreter/dynamic-library.cpp
> > 
> > ```
> Thanks for your note, sounds reasonable. @argentite It should be sufficient 
> to forward declare `printf`right? While we are here, can we somehow check 
> whether the symbol is non-null before calling it?
Yes I think I can do that.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

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


[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-20 Thread Anubhab Ghosh via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG80e7eed6a610: [clang-repl][CUDA] Initial interactive CUDA 
support for clang-repl (authored by argentite).

Changed prior to commit:
  https://reviews.llvm.org/D146389?vs=522717&id=524000#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  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
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -40,7 +40,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -38,7 +38,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,36 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDevice());
+  }
+
   // FIXME: Investigate if we could use ru

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-20 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 524006.
argentite added a comment.

Added some std::move fixes for Error -> Expected conversions

We need to figure out a solution when NVPTX backend is not enabled. clang-repl 
probably should not depends on that. Example: 
https://lab.llvm.org/buildbot#builders/175/builds/29764


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  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
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -40,7 +40,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -38,7 +38,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,36 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDevice());
+  }
+
   // FIXME: Investigate if we could use runToolOnCodeWithArgs

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-25 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 525572.
argentite added a comment.

Workaround for depending on NVPTX symbols: initialize all available targets 
instead. If NVPTX is not available, it will complain when we try to actually 
execute anything in CUDA mode.
Rebased and fixed conflicts on recent value printing related patches.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  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
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -46,7 +46,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -38,7 +38,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -20,9 +20,13 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ManagedStatic.h" // llvm_shutdown
 #include "llvm/Support/Signals.h"
-#include "llvm/Support/TargetSelect.h" // llvm::Initialize*
+#include "llvm/Support/TargetSelect.h"
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -76,8 +80,11 @@
   std::vector ClangArgv(ClangArgs.size());
   std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(),
  [](const std::string &s) -> const char * { return s.data(); });
-  llvm::InitializeNativeTarget();
-  llvm::InitializeNativeTargetAsmPrinter();
+  // Initialize all targets (required for device offloading)
+  llvm::InitializeAllTargetInfos();
+  llvm::InitializeAllTargets();
+  llvm::InitializeAllTarg

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-27 Thread Anubhab Ghosh via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGddeab07ca632: [clang-repl][CUDA] Re-land: Initial 
interactive CUDA support for clang-repl (authored by argentite).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  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
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -46,7 +46,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -38,7 +38,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -20,9 +20,13 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ManagedStatic.h" // llvm_shutdown
 #include "llvm/Support/Signals.h"
-#include "llvm/Support/TargetSelect.h" // llvm::Initialize*
+#include "llvm/Support/TargetSelect.h"
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -76,8 +80,11 @@
   std::vector ClangArgv(ClangArgs.size());
   std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(),
  [](const std::string &s) -> const char * { return s.data(); });
-  llvm::InitializeNativeTarget();
-  llvm::InitializeNativeTargetAsmPrinter();
+  // Initialize all targets (required for device offloading)
+  llvm::InitializeAllTargetInfos();
+  llvm::InitializeAllTargets();
+  llvm::InitializeAllTargetMCs();
+  llvm::InitializeAllAsmPrinters();
 
   if (OptHostSupportsJit) {
 auto J = llvm::orc::LLJITBuilder().create(

[PATCH] D151904: [clang-repl][CUDA] Add an unit test for interactive CUDA

2023-06-01 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite created this revision.
argentite added a reviewer: v.g.vassilev.
Herald added subscribers: mattd, carlosgalvezp, yaxunl.
Herald added a project: All.
argentite requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This tests interactive CUDA support in clang interpreter and should pass
in case CUDA runtime is not available or no GPUs are available.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151904

Files:
  clang/unittests/Interpreter/CMakeLists.txt
  clang/unittests/Interpreter/InteractiveCudaTest.cpp

Index: clang/unittests/Interpreter/InteractiveCudaTest.cpp
===
--- /dev/null
+++ clang/unittests/Interpreter/InteractiveCudaTest.cpp
@@ -0,0 +1,142 @@
+//===- unittests/Interpreter/CudaTest.cpp --- Interactive CUDA tests --===//
+//
+// 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
+//
+//===--===//
+//
+// Unit tests for interactive CUDA in Clang interpreter
+//
+//===--===//
+
+#include "clang/Basic/Version.h"
+#include "clang/Config/config.h"
+
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Interpreter/Interpreter.h"
+
+#include "llvm/Support/TargetSelect.h"
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace {
+
+std::string MakeResourcesPath() {
+  using namespace llvm;
+#ifdef LLVM_BINARY_DIR
+  StringRef Dir = LLVM_BINARY_DIR;
+#else
+  // Dir is bin/ or lib/, depending on where BinaryPath is.
+  void *MainAddr = (void *)(intptr_t)MakeResourcesPath;
+  std::string BinaryPath =
+  llvm::sys::fs::getMainExecutable(/*Argv0=*/nullptr, MainAddr);
+
+  // build/tools/clang/unittests/Interpreter/Executable -> build/
+  StringRef Dir = sys::path::parent_path(BinaryPath);
+
+  Dir = sys::path::parent_path(Dir);
+  Dir = sys::path::parent_path(Dir);
+  Dir = sys::path::parent_path(Dir);
+  Dir = sys::path::parent_path(Dir);
+#endif // LLVM_BINARY_DIR
+  SmallString<128> P(Dir);
+  sys::path::append(P, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
+CLANG_VERSION_MAJOR_STRING);
+  return P.str().str();
+}
+
+static std::unique_ptr
+createInterpreter(const std::vector &ExtraArgs = {}) {
+  static bool firstrun = true;
+  if (firstrun) {
+llvm::InitializeAllTargetInfos();
+llvm::InitializeAllTargets();
+llvm::InitializeAllTargetMCs();
+llvm::InitializeAllAsmPrinters();
+
+firstrun = false;
+  }
+
+  clang::IncrementalCompilerBuilder CB;
+
+  // Help find cuda's runtime headers.
+  std::string ResourceDir = MakeResourcesPath();
+
+  std::vector Args = {"-resource-dir", ResourceDir.c_str(), "-std=c++20"};
+  Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
+  CB.SetCompilerArgs(Args);
+
+  // Create the device code compiler
+  std::unique_ptr DeviceCI;
+  CB.SetOffloadArch("sm_35");
+  DeviceCI = cantFail(CB.CreateCudaDevice());
+
+  std::unique_ptr CI;
+  CI = cantFail(CB.CreateCudaHost());
+
+  auto Interp = cantFail(
+  clang::Interpreter::createWithCUDA(std::move(CI), std::move(DeviceCI)));
+
+  return Interp;
+}
+
+enum {
+  // Defined in CUDA Runtime API
+  cudaErrorNoDevice = 100,
+};
+
+TEST(InteractiveCudaTest, Sanity) {
+  std::unique_ptr Interp = createInterpreter();
+  auto Err = Interp->LoadDynamicLibrary("libcudart.so");
+  if (Err) { // CUDA runtime is not installed/usable, cannot continue testing
+consumeError(std::move(Err));
+return;
+  }
+
+  // Check if we have any GPU for test
+  int CudaError = 0;
+  auto GpuCheckCommand = std::string(R"(
+int device_id = -1;
+int *error = (int *))" + std::to_string((uintptr_t)&CudaError) +
+ R"(;
+*error = cudaGetDevice(&device_id);
+  )");
+  cantFail(Interp->ParseAndExecute(GpuCheckCommand));
+  if (CudaError == cudaErrorNoDevice) {
+// No GPU is available on this machine, cannot continue testing
+return;
+  }
+  ASSERT_EQ(CudaError, 0);
+
+  int HostSum = 0;
+  auto Command1 = std::string(R"(
+__host__ __device__ inline int sum(int a, int b){ return a + b; }
+__global__ void kernel(int * output){ *output = sum(40,2); }
+int *hostsum = (int *) )") +
+  std::to_string((uintptr_t)&HostSum) +
+  R"(;
+*hostsum = sum(41,1);)";
+  cantFail(Interp->ParseAndExecute(Command1));
+
+  int DeviceSum = 0;
+  auto Command2 = std::string(R"(
+int *devicesum = (int *))" +
+  std::to_string((uintptr_t)&DeviceSum) +
+  R"(;
+int *deviceVar;
+*error |= cudaMalloc((void **) &deviceVar, sizeof(int));
+kernel<<<1,1>>>(deviceVar);
+*error |= cudaGetLastError();
+*error |=

[PATCH] D151904: [clang-repl][CUDA] Add an unit test for interactive CUDA

2023-06-01 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/unittests/Interpreter/InteractiveCudaTest.cpp:92
+  std::unique_ptr Interp = createInterpreter();
+  auto Err = Interp->LoadDynamicLibrary("libcudart.so");
+  if (Err) { // CUDA runtime is not installed/usable, cannot continue testing

tra wrote:
> This could be a bit of a problem.
> 
> There may be multiple CUDA SDK versions that may be installed on a system at 
> any given time and the libcudart.so you pick here may not be the one you want.
> E.g it may be from a recent CUDA version which is not supported by NVIDIA 
> drivers yet. 
> 
> I think you may need a way to let the user override CUDA SDK (or 
> libcudart.so) location explicitly. I guess they could do that via 
> LD_LIBRARY_PATH, but for the CUDA compilation in general, knowing CUDA SDK 
> path is essential, as it does affect various compilation options set by the 
> driver.
> 
Yes, this probably would be an issue. It is currently possible to override the 
CUDA path with a command line argument in clang-repl. But I am not sure what we 
can do inside a test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151904/new/

https://reviews.llvm.org/D151904

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


[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-09 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 520636.
argentite marked 3 inline comments as done.
argentite added a comment.

Added a check to run CUDA tests only on systems with CUDA. We need some ideas 
for the actual tests.
Rename Offload.cpp to DeviceOffload.cpp
Other syntax/style fixes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  clang/include/clang/CodeGen/ModuleBuilder.h
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/test/Interpreter/CUDA/lit.local.cfg
  clang/test/Interpreter/CUDA/sanity.cu
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -40,7 +40,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -21,6 +21,7 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 
+#include "llvm-c/Error.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -38,7 +39,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,36 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDev

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-12 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 521742.
argentite marked an inline comment as done.
argentite added a comment.

Add some CUDA basic functionality tests.
Disallow undo-ing of the initial PTU. This should fix the undo command test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  clang/include/clang/CodeGen/ModuleBuilder.h
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  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
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -40,7 +40,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -38,7 +38,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,36 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDevice());
+  }
+
   // FIXME: Investigate if we could use runToolOnCodeWithArgs from tooling. It
   // can replace t

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-13 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/lib/CodeGen/ModuleBuilder.cpp:39
 const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
-const CodeGenOptions CodeGenOpts;  // Intentionally copied in.
+CodeGenOptions CodeGenOpts;  // Intentionally copied in.
 

v.g.vassilev wrote:
> IIUC history correctly, here the intentional copy was to prevent some 
> layering violation for what was called in 2009 `CompileOpts`. I believe that 
> is not the case, can you check if we can take a const reference here? 
I don't understand how the reference causes layering violation but if I change 
it to a const reference instead, the option modification code becomes slightly 
less awkward and all tests seem to be fine.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

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


[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-14 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 522041.
argentite added a comment.

Remove the copy of CodeGenOpts in CodeGeneratorImpl


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  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
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -40,7 +40,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -38,7 +38,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,36 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDevice());
+  }
+
   // FIXME: Investigate if we could use runToolOnCodeWithArgs from tooling. It
   // can replace the boilerplate code for creation of the compiler instance.
-  auto CI = ExitOnErr(clang::IncrementalCompilerBuilder::create(ClangArgv));
+  std::unique_ptr 

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-05-16 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 522717.
argentite added a comment.

Update the filenames


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  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/DeviceOffload.cpp
  clang/lib/Interpreter/DeviceOffload.h
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  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
  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

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -40,7 +40,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -38,7 +38,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,36 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDevice());
+  }
+
   // FIXME: Investigate if we could use runToolOnCodeWithArgs from tooling. It
   // can replace the boilerplate code for creation of the compiler instance.
-  auto CI = ExitOnErr(clang::IncrementalCompilerBuilder::create(ClangArgv));
+  std::unique_ptr CI;
+  if (CudaEnabled) {
+

[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-04-22 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 516061.
argentite added a comment.
Herald added a subscriber: ormris.

Use virtual file system to store CUDA fatbinaries in memory
Adapted Interpreter tests to use the CompilerBuilder


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

Files:
  clang/include/clang/CodeGen/ModuleBuilder.h
  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/lib/Interpreter/Offload.cpp
  clang/lib/Interpreter/Offload.h
  clang/tools/clang-repl/ClangRepl.cpp
  clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
  clang/unittests/Interpreter/IncrementalProcessingTest.cpp
  clang/unittests/Interpreter/InterpreterTest.cpp

Index: clang/unittests/Interpreter/InterpreterTest.cpp
===
--- clang/unittests/Interpreter/InterpreterTest.cpp
+++ clang/unittests/Interpreter/InterpreterTest.cpp
@@ -40,7 +40,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/unittests/Interpreter/IncrementalProcessingTest.cpp
===
--- clang/unittests/Interpreter/IncrementalProcessingTest.cpp
+++ clang/unittests/Interpreter/IncrementalProcessingTest.cpp
@@ -52,7 +52,9 @@
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
   std::vector ClangArgv = {"-Xclang", "-emit-llvm-only"};
-  auto CI = llvm::cantFail(IncrementalCompilerBuilder::create(ClangArgv));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgv);
+  auto CI = cantFail(CB.CreateCpp());
   auto Interp = llvm::cantFail(Interpreter::create(std::move(CI)));
 
   std::array PTUs;
Index: clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
===
--- clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -21,11 +21,11 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 
+#include "llvm-c/Error.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/TargetSelect.h"
-#include "llvm-c/Error.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -39,7 +39,9 @@
   DiagnosticConsumer *Client = nullptr) {
   Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
   ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
-  auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs));
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
   if (Client)
 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
   return cantFail(clang::Interpreter::create(std::move(CI)));
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -23,6 +23,10 @@
 #include "llvm/Support/TargetSelect.h" // llvm::Initialize*
 #include 
 
+static llvm::cl::opt CudaEnabled("cuda", llvm::cl::Hidden);
+static llvm::cl::opt CudaPath("cuda-path", llvm::cl::Hidden);
+static llvm::cl::opt OffloadArch("offload-arch", llvm::cl::Hidden);
+
 static llvm::cl::list
 ClangArgs("Xcc",
   llvm::cl::desc("Argument to pass to the CompilerInvocation"),
@@ -90,9 +94,36 @@
 return 0;
   }
 
+  clang::IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(ClangArgv);
+
+  std::unique_ptr DeviceCI;
+  if (CudaEnabled) {
+// initialize NVPTX backend
+LLVMInitializeNVPTXTargetInfo();
+LLVMInitializeNVPTXTarget();
+LLVMInitializeNVPTXTargetMC();
+LLVMInitializeNVPTXAsmPrinter();
+
+if (!CudaPath.empty())
+  CB.SetCudaSDK(CudaPath);
+
+if (OffloadArch.empty()) {
+  OffloadArch = "sm_35";
+}
+CB.SetOffloadArch(OffloadArch);
+
+DeviceCI = ExitOnErr(CB.CreateCudaDevice());
+  }
+
   // FIXME: Investigate if we could use run

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

2023-04-22 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite created this revision.
Herald added a project: All.
argentite added reviewers: bcain, zhuhan0, sgraenitz, v.g.vassilev.
argentite published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148992

Files:
  clang/test/Interpreter/Inputs/dynamic-library-test.cpp
  clang/test/Interpreter/Inputs/libdynamic-library-test.so
  clang/test/Interpreter/dynamic-library.cpp


Index: clang/test/Interpreter/dynamic-library.cpp
===
--- clang/test/Interpreter/dynamic-library.cpp
+++ clang/test/Interpreter/dynamic-library.cpp
@@ -1,10 +1,19 @@
 // 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
+//
+// 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 "C" int printf(const char* format, ...);
 
 extern int ultimate_answer;
 int calculate_answer();
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- 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;
-}


Index: clang/test/Interpreter/dynamic-library.cpp
===
--- clang/test/Interpreter/dynamic-library.cpp
+++ clang/test/Interpreter/dynamic-library.cpp
@@ -1,10 +1,19 @@
 // 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
+//
+// 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 "C" int printf(const char* format, ...);
 
 extern int ultimate_answer;
 int calculate_answer();
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- 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;
-}
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-04-22 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added a comment.

I made patch with both precompiled library and without cstdio. D148992 


@dyung We can also enable this now on PS4/5 if you are interested.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141824/new/

https://reviews.llvm.org/D141824

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


[PATCH] D146389: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl

2023-04-23 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite added inline comments.



Comment at: clang/lib/Interpreter/Offload.cpp:1
+//===-- Offload.cpp - CUDA Offloading ---*- C++ 
-*-===//
+//

v.g.vassilev wrote:
> argentite wrote:
> > Hahnfeld wrote:
> > > v.g.vassilev wrote:
> > > > How about `DeviceOffload.cpp`?
> > > Or `IncrementalCUDADeviceParser.cpp` for the moment - not sure what other 
> > > classes will be added in the future, and if they should be in the same TU.
> > I wanted to avoid "CUDA" in case we use it later for HIP.
> Was `DeviceOffload.cpp` not a better name for the file and its intent?
Yeah that seems alright, I will change in the next revision.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146389/new/

https://reviews.llvm.org/D146389

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


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

2023-04-25 Thread Anubhab Ghosh via Phabricator via cfe-commits
argentite updated this revision to Diff 516715.
argentite marked an inline comment as done.
argentite added a comment.

extern "C" and enable PS4/5


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148992/new/

https://reviews.llvm.org/D148992

Files:
  clang/test/Interpreter/Inputs/dynamic-library-test.cpp
  clang/test/Interpreter/Inputs/libdynamic-library-test.so
  clang/test/Interpreter/dynamic-library.cpp


Index: clang/test/Interpreter/dynamic-library.cpp
===
--- clang/test/Interpreter/dynamic-library.cpp
+++ 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
 
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- 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;
-}


Index: clang/test/Interpreter/dynamic-library.cpp
===
--- clang/test/Interpreter/dynamic-library.cpp
+++ 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
 
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- 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;
-}
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-04-25 Thread Anubhab Ghosh via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG7f96ce5e133b: [clang-repl] Fix dynamic library test to avoid 
cstdio and linker (authored by argentite).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148992/new/

https://reviews.llvm.org/D148992

Files:
  clang/test/Interpreter/Inputs/dynamic-library-test.cpp
  clang/test/Interpreter/Inputs/libdynamic-library-test.so
  clang/test/Interpreter/dynamic-library.cpp


Index: clang/test/Interpreter/dynamic-library.cpp
===
--- clang/test/Interpreter/dynamic-library.cpp
+++ 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
 
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- 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;
-}


Index: clang/test/Interpreter/dynamic-library.cpp
===
--- clang/test/Interpreter/dynamic-library.cpp
+++ 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
 
Index: clang/test/Interpreter/Inputs/dynamic-library-test.cpp
===
--- 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;
-}
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits