[clang] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors (PR #66658)
@@ -794,7 +794,7 @@ void CodeGenModule::Release() { AddGlobalCtor(ObjCInitFunction); if (Context.getLangOpts().CUDA && CUDARuntime) { if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule()) - AddGlobalCtor(CudaCtorFunction); + AddGlobalCtor(CudaCtorFunction, /*Priority=*/0); argentite wrote: The underlying issues is not actually clang-repl specific, it also affects clang. For example, this seems to succeed in `nvcc` but fails with `clang`: ```cpp #include __global__ void kernel() {} class C { public: C() { kernel<<<1, 1>>>(); printf("Error: %d\n", cudaGetLastError()); } }; C c; int main() {} ``` This is fixed by this patch. Maybe we can look for a proper solution to this? https://github.com/llvm/llvm-project/pull/66658 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors (PR #66658)
https://github.com/argentite updated https://github.com/llvm/llvm-project/pull/66658 >From bed2919f781c5ef71e268c95b31a6b9af5392730 Mon Sep 17 00:00:00 2001 From: Anubhab Ghosh Date: Mon, 18 Sep 2023 20:33:19 +0530 Subject: [PATCH] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors CUDA device code needs to be registered to the runtime before kernels can be launched. This is done through a global constructor. User code in Clang interpreter, is also executed through global_ctors. This patch ensures kernels can be launched in the same iteration it is defined in by making the registration first in the list. --- clang/lib/CodeGen/CodeGenModule.cpp | 2 +- .../test/Interpreter/CUDA/launch-same-ptu.cu | 21 +++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 clang/test/Interpreter/CUDA/launch-same-ptu.cu diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 8b0c9340775cbe9..647c8922f27a00f 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -794,7 +794,7 @@ void CodeGenModule::Release() { AddGlobalCtor(ObjCInitFunction); if (Context.getLangOpts().CUDA && CUDARuntime) { if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule()) - AddGlobalCtor(CudaCtorFunction); + AddGlobalCtor(CudaCtorFunction, /*Priority=*/0); } if (OpenMPRuntime) { if (llvm::Function *OpenMPRequiresDirectiveRegFun = diff --git a/clang/test/Interpreter/CUDA/launch-same-ptu.cu b/clang/test/Interpreter/CUDA/launch-same-ptu.cu new file mode 100644 index 000..93e203a47212fbf --- /dev/null +++ b/clang/test/Interpreter/CUDA/launch-same-ptu.cu @@ -0,0 +1,21 @@ +// Tests __device__ function calls +// RUN: cat %s | clang-repl --cuda | FileCheck %s + +extern "C" int printf(const char*, ...); + +int var; +int* devptr = nullptr; +printf("cudaMalloc: %d\n", cudaMalloc((void **) , sizeof(int))); +// CHECK: cudaMalloc: 0 + +__device__ inline void test_device(int* value) { *value = 42; } __global__ void test_kernel(int* value) { test_device(value); } test_kernel<<<1,1>>>(devptr); +printf("CUDA Error: %d\n", cudaGetLastError()); +// CHECK-NEXT: CUDA Error: 0 + +printf("cudaMemcpy: %d\n", cudaMemcpy(, devptr, sizeof(int), cudaMemcpyDeviceToHost)); +// CHECK-NEXT: cudaMemcpy: 0 + +printf("Value: %d\n", var); +// CHECK-NEXT: Value: 42 + +%quit ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors (PR #66658)
https://github.com/argentite created https://github.com/llvm/llvm-project/pull/66658 CUDA device code needs to be registered to the runtime before kernels can be launched. This is done through a global constructor. User code in Clang interpreter, is also executed through `global_ctors`. This patch ensures kernels can be launched in the same iteration it is defined in by making the registration first in the list. This allows `#include`-ing a large portion of code that defines device functions and also launches kernels in clang-repl. >From fb806d7c7d357f1769538df0ba7729e4b328da79 Mon Sep 17 00:00:00 2001 From: Anubhab Ghosh Date: Mon, 18 Sep 2023 20:33:19 +0530 Subject: [PATCH] [clang-repl][CUDA] Move CUDA module registration to beginning of global_ctors CUDA device code needs to be registered to the runtime before kernels can be launched. This is done through a global constructor. User code in Clang interpreter, is also executed through global_ctors. This patch ensures kernels can be launched in the same iteration it is defined in by making the registration first in the list. --- clang/lib/CodeGen/CodeGenModule.cpp | 2 +- .../test/Interpreter/CUDA/launch-same-ptu.cu | 21 +++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 clang/test/Interpreter/CUDA/launch-same-ptu.cu diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 8b0c9340775cbe9..783865409c778f5 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -794,7 +794,7 @@ void CodeGenModule::Release() { AddGlobalCtor(ObjCInitFunction); if (Context.getLangOpts().CUDA && CUDARuntime) { if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule()) - AddGlobalCtor(CudaCtorFunction); + AddGlobalCtor(CudaCtorFunction, 0); } if (OpenMPRuntime) { if (llvm::Function *OpenMPRequiresDirectiveRegFun = diff --git a/clang/test/Interpreter/CUDA/launch-same-ptu.cu b/clang/test/Interpreter/CUDA/launch-same-ptu.cu new file mode 100644 index 000..93e203a47212fbf --- /dev/null +++ b/clang/test/Interpreter/CUDA/launch-same-ptu.cu @@ -0,0 +1,21 @@ +// Tests __device__ function calls +// RUN: cat %s | clang-repl --cuda | FileCheck %s + +extern "C" int printf(const char*, ...); + +int var; +int* devptr = nullptr; +printf("cudaMalloc: %d\n", cudaMalloc((void **) , sizeof(int))); +// CHECK: cudaMalloc: 0 + +__device__ inline void test_device(int* value) { *value = 42; } __global__ void test_kernel(int* value) { test_device(value); } test_kernel<<<1,1>>>(devptr); +printf("CUDA Error: %d\n", cudaGetLastError()); +// CHECK-NEXT: CUDA Error: 0 + +printf("cudaMemcpy: %d\n", cudaMemcpy(, devptr, sizeof(int), cudaMemcpyDeviceToHost)); +// CHECK-NEXT: cudaMemcpy: 0 + +printf("Value: %d\n", var); +// CHECK-NEXT: Value: 42 + +%quit ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] ddeab07 - [clang-repl][CUDA] Re-land: Initial interactive CUDA support for clang-repl
Author: Anubhab Ghosh Date: 2023-05-27T13:54:42+05:30 New Revision: ddeab07ca63235f8d952e1171b56fdb0f2d761c9 URL: https://github.com/llvm/llvm-project/commit/ddeab07ca63235f8d952e1171b56fdb0f2d761c9 DIFF: https://github.com/llvm/llvm-project/commit/ddeab07ca63235f8d952e1171b56fdb0f2d761c9.diff LOG: [clang-repl][CUDA] Re-land: Initial interactive CUDA support for clang-repl CUDA support can be enabled in clang-repl with --cuda flag. Device code linking is not yet supported. inline must be used with all __device__ functions. Differential Revision: https://reviews.llvm.org/D146389 Added: clang/lib/Interpreter/DeviceOffload.cpp clang/lib/Interpreter/DeviceOffload.h clang/test/Interpreter/CUDA/device-function-template.cu clang/test/Interpreter/CUDA/device-function.cu clang/test/Interpreter/CUDA/host-and-device.cu clang/test/Interpreter/CUDA/lit.local.cfg clang/test/Interpreter/CUDA/memory.cu clang/test/Interpreter/CUDA/sanity.cu Modified: clang/include/clang/Interpreter/Interpreter.h clang/lib/CodeGen/CGCUDANV.cpp clang/lib/CodeGen/CodeGenAction.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/ModuleBuilder.cpp clang/lib/Interpreter/CMakeLists.txt clang/lib/Interpreter/IncrementalParser.cpp clang/lib/Interpreter/IncrementalParser.h clang/lib/Interpreter/Interpreter.cpp clang/test/lit.cfg.py clang/tools/clang-repl/ClangRepl.cpp clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp clang/unittests/Interpreter/IncrementalProcessingTest.cpp clang/unittests/Interpreter/InterpreterTest.cpp Removed: diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index e680218452d1c..43573fb1a4b89 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -42,8 +42,34 @@ class IncrementalParser; /// Create a pre-configured \c CompilerInstance for incremental processing. class IncrementalCompilerBuilder { public: + IncrementalCompilerBuilder() {} + + void SetCompilerArgs(const std::vector ) { +UserArgs = Args; + } + + // General C++ + llvm::Expected> CreateCpp(); + + // Offload options + void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; }; + + // CUDA specific + void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; }; + + llvm::Expected> CreateCudaHost(); + llvm::Expected> CreateCudaDevice(); + +private: static llvm::Expected> create(std::vector ); + + llvm::Expected> createCuda(bool device); + + std::vector UserArgs; + + llvm::StringRef OffloadArch; + llvm::StringRef CudaSDKPath; }; /// Provides top-level interfaces for incremental compilation and execution. @@ -52,6 +78,9 @@ class Interpreter { std::unique_ptr IncrParser; std::unique_ptr IncrExecutor; + // An optional parser for CUDA offloading + std::unique_ptr DeviceParser; + Interpreter(std::unique_ptr CI, llvm::Error ); llvm::Error CreateExecutor(); @@ -66,6 +95,9 @@ class Interpreter { ~Interpreter(); static llvm::Expected> create(std::unique_ptr CI); + static llvm::Expected> + createWithCUDA(std::unique_ptr CI, + std::unique_ptr DCI); const ASTContext () const; ASTContext (); const CompilerInstance *getCompilerInstance() const; diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp index 1f429e4305790..c30a08a5722dc 100644 --- a/clang/lib/CodeGen/CGCUDANV.cpp +++ b/clang/lib/CodeGen/CGCUDANV.cpp @@ -24,6 +24,7 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/ReplaceConstant.h" #include "llvm/Support/Format.h" +#include "llvm/Support/VirtualFileSystem.h" using namespace clang; using namespace CodeGen; @@ -721,8 +722,9 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { // handle so CUDA runtime can figure out what to call on the GPU side. std::unique_ptr CudaGpuBinary = nullptr; if (!CudaGpuBinaryFileName.empty()) { -llvm::ErrorOr> CudaGpuBinaryOrErr = -llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName); +auto VFS = CGM.getFileSystem(); +auto CudaGpuBinaryOrErr = +VFS->getBufferForFile(CudaGpuBinaryFileName, -1, false); if (std::error_code EC = CudaGpuBinaryOrErr.getError()) { CGM.getDiags().Report(diag::err_cannot_open_file) << CudaGpuBinaryFileName << EC.message(); diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 29adf88acd704..784ff77c61727 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -264,6 +264,7 @@ namespace clang { // Links each entry in LinkModules into our module. Returns true on error. bool LinkInModules() { for (auto : LinkModules) { +assert(LM.Module && "LinkModule does not actually have a module");
[clang] 0929f5b - Revert "[clang-repl][CUDA] Initial interactive CUDA support for clang-repl"
Author: Anubhab Ghosh Date: 2023-05-20T14:40:04+05:30 New Revision: 0929f5b90350aa2f9175d7e1094b1750535c0e44 URL: https://github.com/llvm/llvm-project/commit/0929f5b90350aa2f9175d7e1094b1750535c0e44 DIFF: https://github.com/llvm/llvm-project/commit/0929f5b90350aa2f9175d7e1094b1750535c0e44.diff LOG: Revert "[clang-repl][CUDA] Initial interactive CUDA support for clang-repl" This reverts commit 80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31. Added: Modified: clang/include/clang/Interpreter/Interpreter.h clang/lib/CodeGen/CGCUDANV.cpp clang/lib/CodeGen/CodeGenAction.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/ModuleBuilder.cpp clang/lib/Interpreter/CMakeLists.txt clang/lib/Interpreter/IncrementalParser.cpp clang/lib/Interpreter/IncrementalParser.h clang/lib/Interpreter/Interpreter.cpp clang/test/lit.cfg.py clang/tools/clang-repl/ClangRepl.cpp clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp clang/unittests/Interpreter/IncrementalProcessingTest.cpp clang/unittests/Interpreter/InterpreterTest.cpp Removed: clang/lib/Interpreter/DeviceOffload.cpp clang/lib/Interpreter/DeviceOffload.h clang/test/Interpreter/CUDA/device-function-template.cu clang/test/Interpreter/CUDA/device-function.cu clang/test/Interpreter/CUDA/host-and-device.cu clang/test/Interpreter/CUDA/lit.local.cfg clang/test/Interpreter/CUDA/memory.cu clang/test/Interpreter/CUDA/sanity.cu diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index afb0bbc98079d..b3d64458d777c 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -41,34 +41,8 @@ class IncrementalParser; /// Create a pre-configured \c CompilerInstance for incremental processing. class IncrementalCompilerBuilder { public: - IncrementalCompilerBuilder() {} - - void SetCompilerArgs(const std::vector ) { -UserArgs = Args; - } - - // General C++ - llvm::Expected> CreateCpp(); - - // Offload options - void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; }; - - // CUDA specific - void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; }; - - llvm::Expected> CreateCudaHost(); - llvm::Expected> CreateCudaDevice(); - -private: static llvm::Expected> create(std::vector ); - - llvm::Expected> createCuda(bool device); - - std::vector UserArgs; - - llvm::StringRef OffloadArch; - llvm::StringRef CudaSDKPath; }; /// Provides top-level interfaces for incremental compilation and execution. @@ -77,9 +51,6 @@ class Interpreter { std::unique_ptr IncrParser; std::unique_ptr IncrExecutor; - // An optional parser for CUDA offloading - std::unique_ptr DeviceParser; - Interpreter(std::unique_ptr CI, llvm::Error ); llvm::Error CreateExecutor(); @@ -88,9 +59,6 @@ class Interpreter { ~Interpreter(); static llvm::Expected> create(std::unique_ptr CI); - static llvm::Expected> - createWithCUDA(std::unique_ptr CI, - std::unique_ptr DCI); const CompilerInstance *getCompilerInstance() const; llvm::Expected getExecutionEngine(); diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp index c30a08a5722dc..1f429e4305790 100644 --- a/clang/lib/CodeGen/CGCUDANV.cpp +++ b/clang/lib/CodeGen/CGCUDANV.cpp @@ -24,7 +24,6 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/ReplaceConstant.h" #include "llvm/Support/Format.h" -#include "llvm/Support/VirtualFileSystem.h" using namespace clang; using namespace CodeGen; @@ -722,9 +721,8 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { // handle so CUDA runtime can figure out what to call on the GPU side. std::unique_ptr CudaGpuBinary = nullptr; if (!CudaGpuBinaryFileName.empty()) { -auto VFS = CGM.getFileSystem(); -auto CudaGpuBinaryOrErr = -VFS->getBufferForFile(CudaGpuBinaryFileName, -1, false); +llvm::ErrorOr> CudaGpuBinaryOrErr = +llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName); if (std::error_code EC = CudaGpuBinaryOrErr.getError()) { CGM.getDiags().Report(diag::err_cannot_open_file) << CudaGpuBinaryFileName << EC.message(); diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 784ff77c61727..29adf88acd704 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -264,7 +264,6 @@ namespace clang { // Links each entry in LinkModules into our module. Returns true on error. bool LinkInModules() { for (auto : LinkModules) { -assert(LM.Module && "LinkModule does not actually have a module"); if (LM.PropagateAttrs) for (Function : *LM.Module) { // Skip intrinsics. Keep consistent with how intrinsics are created @@
[clang] 80e7eed - [clang-repl][CUDA] Initial interactive CUDA support for clang-repl
Author: Anubhab Ghosh Date: 2023-05-20T14:00:48+05:30 New Revision: 80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31 URL: https://github.com/llvm/llvm-project/commit/80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31 DIFF: https://github.com/llvm/llvm-project/commit/80e7eed6a610ab3c7289e6f9b7ec006bc7d7ae31.diff LOG: [clang-repl][CUDA] Initial interactive CUDA support for clang-repl CUDA support can be enabled in clang-repl with --cuda flag. Device code linking is not yet supported. inline must be used with all __device__ functions. Differential Revision: https://reviews.llvm.org/D146389 Added: clang/lib/Interpreter/DeviceOffload.cpp clang/lib/Interpreter/DeviceOffload.h clang/test/Interpreter/CUDA/device-function-template.cu clang/test/Interpreter/CUDA/device-function.cu clang/test/Interpreter/CUDA/host-and-device.cu clang/test/Interpreter/CUDA/lit.local.cfg clang/test/Interpreter/CUDA/memory.cu clang/test/Interpreter/CUDA/sanity.cu Modified: clang/include/clang/Interpreter/Interpreter.h clang/lib/CodeGen/CGCUDANV.cpp clang/lib/CodeGen/CodeGenAction.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/ModuleBuilder.cpp clang/lib/Interpreter/CMakeLists.txt clang/lib/Interpreter/IncrementalParser.cpp clang/lib/Interpreter/IncrementalParser.h clang/lib/Interpreter/Interpreter.cpp clang/test/lit.cfg.py clang/tools/clang-repl/ClangRepl.cpp clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp clang/unittests/Interpreter/IncrementalProcessingTest.cpp clang/unittests/Interpreter/InterpreterTest.cpp Removed: diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index b3d64458d777c..afb0bbc98079d 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -41,8 +41,34 @@ class IncrementalParser; /// Create a pre-configured \c CompilerInstance for incremental processing. class IncrementalCompilerBuilder { public: + IncrementalCompilerBuilder() {} + + void SetCompilerArgs(const std::vector ) { +UserArgs = Args; + } + + // General C++ + llvm::Expected> CreateCpp(); + + // Offload options + void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; }; + + // CUDA specific + void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; }; + + llvm::Expected> CreateCudaHost(); + llvm::Expected> CreateCudaDevice(); + +private: static llvm::Expected> create(std::vector ); + + llvm::Expected> createCuda(bool device); + + std::vector UserArgs; + + llvm::StringRef OffloadArch; + llvm::StringRef CudaSDKPath; }; /// Provides top-level interfaces for incremental compilation and execution. @@ -51,6 +77,9 @@ class Interpreter { std::unique_ptr IncrParser; std::unique_ptr IncrExecutor; + // An optional parser for CUDA offloading + std::unique_ptr DeviceParser; + Interpreter(std::unique_ptr CI, llvm::Error ); llvm::Error CreateExecutor(); @@ -59,6 +88,9 @@ class Interpreter { ~Interpreter(); static llvm::Expected> create(std::unique_ptr CI); + static llvm::Expected> + createWithCUDA(std::unique_ptr CI, + std::unique_ptr DCI); const CompilerInstance *getCompilerInstance() const; llvm::Expected getExecutionEngine(); diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp index 1f429e4305790..c30a08a5722dc 100644 --- a/clang/lib/CodeGen/CGCUDANV.cpp +++ b/clang/lib/CodeGen/CGCUDANV.cpp @@ -24,6 +24,7 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/ReplaceConstant.h" #include "llvm/Support/Format.h" +#include "llvm/Support/VirtualFileSystem.h" using namespace clang; using namespace CodeGen; @@ -721,8 +722,9 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { // handle so CUDA runtime can figure out what to call on the GPU side. std::unique_ptr CudaGpuBinary = nullptr; if (!CudaGpuBinaryFileName.empty()) { -llvm::ErrorOr> CudaGpuBinaryOrErr = -llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName); +auto VFS = CGM.getFileSystem(); +auto CudaGpuBinaryOrErr = +VFS->getBufferForFile(CudaGpuBinaryFileName, -1, false); if (std::error_code EC = CudaGpuBinaryOrErr.getError()) { CGM.getDiags().Report(diag::err_cannot_open_file) << CudaGpuBinaryFileName << EC.message(); diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 29adf88acd704..784ff77c61727 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -264,6 +264,7 @@ namespace clang { // Links each entry in LinkModules into our module. Returns true on error. bool LinkInModules() { for (auto : LinkModules) { +assert(LM.Module && "LinkModule does not actually have a module"); if
[clang] e037880 - [clang-repl] Only enable dynamic-library test on x86_64
Author: Anubhab Ghosh Date: 2023-04-26T09:46:38+05:30 New Revision: e037880b8eff2b425ff80ea9d6337fda27a64337 URL: https://github.com/llvm/llvm-project/commit/e037880b8eff2b425ff80ea9d6337fda27a64337 DIFF: https://github.com/llvm/llvm-project/commit/e037880b8eff2b425ff80ea9d6337fda27a64337.diff LOG: [clang-repl] Only enable dynamic-library test on x86_64 This test includes a precompiled library for x86_64 Linux Added: Modified: clang/test/Interpreter/dynamic-library.cpp Removed: diff --git a/clang/test/Interpreter/dynamic-library.cpp b/clang/test/Interpreter/dynamic-library.cpp index e2bfc81a383a7..6c4621f729c1c 100644 --- a/clang/test/Interpreter/dynamic-library.cpp +++ b/clang/test/Interpreter/dynamic-library.cpp @@ -1,4 +1,4 @@ -// REQUIRES: host-supports-jit, system-linux +// REQUIRES: host-supports-jit, x86_64-linux // To generate libdynamic-library-test.so : // clang -xc++ -o libdynamic-library-test.so -fPIC -shared ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] 7f96ce5 - [clang-repl] Fix dynamic library test to avoid cstdio and linker
Author: Anubhab Ghosh Date: 2023-04-26T09:11:09+05:30 New Revision: 7f96ce5e133be54891af177adbf8952d413e0f85 URL: https://github.com/llvm/llvm-project/commit/7f96ce5e133be54891af177adbf8952d413e0f85 DIFF: https://github.com/llvm/llvm-project/commit/7f96ce5e133be54891af177adbf8952d413e0f85.diff LOG: [clang-repl] Fix dynamic library test to avoid cstdio and linker Some platforms do not have a working linker present. The goal is to only test the loading of a shared library in clang-repl. A precompiled library is used instead. The cstdio header may also not be present. We only need printf. Related discussion in D141824 Differential Revision: https://reviews.llvm.org/D148992 Added: clang/test/Interpreter/Inputs/libdynamic-library-test.so Modified: clang/test/Interpreter/dynamic-library.cpp Removed: clang/test/Interpreter/Inputs/dynamic-library-test.cpp diff --git a/clang/test/Interpreter/Inputs/dynamic-library-test.cpp b/clang/test/Interpreter/Inputs/dynamic-library-test.cpp deleted file mode 100644 index 1f143ba040cb6..0 --- a/clang/test/Interpreter/Inputs/dynamic-library-test.cpp +++ /dev/null @@ -1,6 +0,0 @@ -int ultimate_answer = 0; - -int calculate_answer() { - ultimate_answer = 42; - return 5; -} diff --git a/clang/test/Interpreter/Inputs/libdynamic-library-test.so b/clang/test/Interpreter/Inputs/libdynamic-library-test.so new file mode 100755 index 0..bb7c7b5a54317 Binary files /dev/null and b/clang/test/Interpreter/Inputs/libdynamic-library-test.so diff er diff --git a/clang/test/Interpreter/dynamic-library.cpp b/clang/test/Interpreter/dynamic-library.cpp index 145c58bc2ae26..e2bfc81a383a7 100644 --- a/clang/test/Interpreter/dynamic-library.cpp +++ b/clang/test/Interpreter/dynamic-library.cpp @@ -1,13 +1,25 @@ // REQUIRES: host-supports-jit, system-linux -// UNSUPPORTED: target={{.*-(ps4|ps5)}} -// RUN: %clang -xc++ -o %T/libdynamic-library-test.so -fPIC -shared -DLIBRARY %S/Inputs/dynamic-library-test.cpp -// RUN: cat %s | env LD_LIBRARY_PATH=%T:$LD_LIBRARY_PATH clang-repl | FileCheck %s +// To generate libdynamic-library-test.so : +// clang -xc++ -o libdynamic-library-test.so -fPIC -shared +// +// extern "C" { +// +// int ultimate_answer = 0; +// +// int calculate_answer() { +// ultimate_answer = 42; +// return 5; +// } +// +// } -#include +// RUN: cat %s | env LD_LIBRARY_PATH=%S/Inputs:$LD_LIBRARY_PATH clang-repl | FileCheck %s -extern int ultimate_answer; -int calculate_answer(); +extern "C" int printf(const char* format, ...); + +extern "C" int ultimate_answer; +extern "C" int calculate_answer(); %lib libdynamic-library-test.so ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] 766d048 - [clang-repl] Use std::move when converting Error to Expected
Author: Anubhab Ghosh Date: 2023-03-29T08:18:36+05:30 New Revision: 766d048d819a78443da73f67afa04e0a108412b6 URL: https://github.com/llvm/llvm-project/commit/766d048d819a78443da73f67afa04e0a108412b6 DIFF: https://github.com/llvm/llvm-project/commit/766d048d819a78443da73f67afa04e0a108412b6.diff LOG: [clang-repl] Use std::move when converting Error to Expected Added: Modified: clang/lib/Interpreter/Interpreter.cpp Removed: diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index 76d5f162a34a5..a0ccbc20b95f4 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -207,7 +207,7 @@ const CompilerInstance *Interpreter::getCompilerInstance() const { llvm::Expected Interpreter::getExecutionEngine() { if (!IncrExecutor) { if (auto Err = CreateExecutor()) - return Err; + return std::move(Err); } return IncrExecutor->GetExecutionEngine(); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] d978730 - [clang-repl] Add a command to load dynamic libraries
Author: Anubhab Ghosh Date: 2023-03-29T08:04:50+05:30 New Revision: d978730d8e2c10c76867b83bec2f1143d895ee7d URL: https://github.com/llvm/llvm-project/commit/d978730d8e2c10c76867b83bec2f1143d895ee7d DIFF: https://github.com/llvm/llvm-project/commit/d978730d8e2c10c76867b83bec2f1143d895ee7d.diff LOG: [clang-repl] Add a command to load dynamic libraries This commit adds the %lib command to load a dynamic library to be used by the currently running interpreted code. For example `%lib libSDL2.so`. Differential Revision: https://reviews.llvm.org/D141824 Added: clang/test/Interpreter/Inputs/dynamic-library-test.cpp clang/test/Interpreter/dynamic-library.cpp Modified: clang/include/clang/Interpreter/Interpreter.h clang/lib/Interpreter/IncrementalExecutor.h clang/lib/Interpreter/Interpreter.cpp clang/tools/clang-repl/ClangRepl.cpp Removed: diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index fd22af976613..b20d77e8ef85 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -28,7 +28,7 @@ namespace llvm { namespace orc { class LLJIT; class ThreadSafeContext; -} +} // namespace orc } // namespace llvm namespace clang { @@ -52,12 +52,15 @@ class Interpreter { Interpreter(std::unique_ptr CI, llvm::Error ); + llvm::Error CreateExecutor(); + public: ~Interpreter(); static llvm::Expected> create(std::unique_ptr CI); const CompilerInstance *getCompilerInstance() const; - const llvm::orc::LLJIT *getExecutionEngine() const; + llvm::Expected getExecutionEngine(); + llvm::Expected Parse(llvm::StringRef Code); llvm::Error Execute(PartialTranslationUnit ); llvm::Error ParseAndExecute(llvm::StringRef Code) { @@ -72,6 +75,9 @@ class Interpreter { /// Undo N previous incremental inputs. llvm::Error Undo(unsigned N = 1); + /// Link a dynamic library + llvm::Error LoadDynamicLibrary(const char *name); + /// \returns the \c JITTargetAddress of a \c GlobalDecl. This interface uses /// the CodeGenModule's internal mangling cache to avoid recomputing the /// mangled name. diff --git a/clang/lib/Interpreter/IncrementalExecutor.h b/clang/lib/Interpreter/IncrementalExecutor.h index 54d37c76326b..f7922ecb5380 100644 --- a/clang/lib/Interpreter/IncrementalExecutor.h +++ b/clang/lib/Interpreter/IncrementalExecutor.h @@ -53,7 +53,8 @@ class IncrementalExecutor { llvm::Error cleanUp(); llvm::Expected getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const; - llvm::orc::LLJIT *getExecutionEngine() const { return Jit.get(); } + + llvm::orc::LLJIT () { return *Jit; } }; } // end namespace clang diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index 3f0842c567da..76d5f162a34a 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -29,6 +29,7 @@ #include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Lex/PreprocessorOptions.h" +#include "llvm/ExecutionEngine/Orc/LLJIT.h" #include "llvm/IR/Module.h" #include "llvm/Support/Errc.h" #include "llvm/TargetParser/Host.h" @@ -203,10 +204,13 @@ const CompilerInstance *Interpreter::getCompilerInstance() const { return IncrParser->getCI(); } -const llvm::orc::LLJIT *Interpreter::getExecutionEngine() const { - if (IncrExecutor) -return IncrExecutor->getExecutionEngine(); - return nullptr; +llvm::Expected Interpreter::getExecutionEngine() { + if (!IncrExecutor) { +if (auto Err = CreateExecutor()) + return Err; + } + + return IncrExecutor->GetExecutionEngine(); } llvm::Expected @@ -214,14 +218,21 @@ Interpreter::Parse(llvm::StringRef Code) { return IncrParser->Parse(Code); } +llvm::Error Interpreter::CreateExecutor() { + const clang::TargetInfo = + 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 ) { assert(T.TheModule); if (!IncrExecutor) { -const clang::TargetInfo = -getCompilerInstance()->getASTContext().getTargetInfo(); -llvm::Error Err = llvm::Error::success(); -IncrExecutor = std::make_unique(*TSCtx, Err, TI); - +auto Err = CreateExecutor(); if (Err) return Err; } @@ -283,3 +294,19 @@ llvm::Error Interpreter::Undo(unsigned N) { } return llvm::Error::success(); } + +llvm::Error Interpreter::LoadDynamicLibrary(const char *name) { + auto EE = getExecutionEngine(); + if (!EE) +return EE.takeError(); + + auto = EE->getDataLayout(); + + if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load( + name, DL.getGlobalPrefix())) +