Skip to content

[clang-repl] Fix destructor for interpreter for the cuda negation case #138091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anutosh491
Copy link
Contributor

Check this error for more context (https://github.com/compiler-research/CppInterOp/actions/runs/14749797085/job/41407625681?pr=491#step:10:531)

This fails with

* thread #1, name = 'CppInterOpTests', stop reason = signal SIGSEGV: address not mapped to object (fault address: 0x55500356d6d3)
  * frame #0: 0x00007fffee41cfe3 libclangCppInterOp.so.21.0gitclang::PragmaNamespace::~PragmaNamespace() + 99
    frame #1: 0x00007fffee435666 libclangCppInterOp.so.21.0gitclang::Preprocessor::~Preprocessor() + 3830
    frame #2: 0x00007fffee20917a libclangCppInterOp.so.21.0gitstd::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() + 58
    frame #3: 0x00007fffee224796 libclangCppInterOp.so.21.0gitclang::CompilerInstance::~CompilerInstance() + 838
    frame #4: 0x00007fffee22494d libclangCppInterOp.so.21.0gitclang::CompilerInstance::~CompilerInstance() + 13
    frame #5: 0x00007fffed95ec62 libclangCppInterOp.so.21.0gitclang::IncrementalCUDADeviceParser::~IncrementalCUDADeviceParser() + 98
    frame #6: 0x00007fffed9551b6 libclangCppInterOp.so.21.0gitclang::Interpreter::~Interpreter() + 102
    frame #7: 0x00007fffed95598d libclangCppInterOp.so.21.0gitclang::Interpreter::~Interpreter() + 13
    frame #8: 0x00007fffed9181e7 libclangCppInterOp.so.21.0gitcompat::createClangInterpreter(std::vector<char const*, std::allocator<char const*>>&) + 2919

Problem :

  1. The destructor currently handles no clearance for the DeviceParser and the DeviceAct. We currently only have this

    Interpreter::~Interpreter() {
    IncrParser.reset();
    Act->FinalizeAction();
    if (IncrExecutor) {

  2. The ownership for DeviceCI currently is present in IncrementalCudaDeviceParser. But this should be similar to how the combination for hostCI, hostAction and hostParser are managed by the Interpreter. As on master the DeviceAct and DeviceParser are managed by the Interpreter but not DeviceCI. This is problematic because :
    IncrementalParser holds a Sema& which points into the DeviceCI. On master, DeviceCI is destroyed before the base class ~IncrementalParser() runs, causing Parser::reset() to access a dangling Sema (and as Sema holds a reference to Preprocessor which owns PragmaNamespace) we see this

  * frame #0: 0x00007fffee41cfe3 libclangCppInterOp.so.21.0gitclang::PragmaNamespace::~PragmaNamespace() + 99
    frame #1: 0x00007fffee435666 libclangCppInterOp.so.21.0gitclang::Preprocessor::~Preprocessor() + 3830
    

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

llvmbot commented May 1, 2025

@llvm/pr-subscribers-clang

Author: Anutosh Bhat (anutosh491)

Changes

Check this error for more context (https://github.com/compiler-research/CppInterOp/actions/runs/14749797085/job/41407625681?pr=491#step:10:531)

This fails with

* thread #<!-- -->1, name = 'CppInterOpTests', stop reason = signal SIGSEGV: address not mapped to object (fault address: 0x55500356d6d3)
  * frame #<!-- -->0: 0x00007fffee41cfe3 libclangCppInterOp.so.21.0gitclang::PragmaNamespace::~PragmaNamespace() + 99
    frame #<!-- -->1: 0x00007fffee435666 libclangCppInterOp.so.21.0gitclang::Preprocessor::~Preprocessor() + 3830
    frame #<!-- -->2: 0x00007fffee20917a libclangCppInterOp.so.21.0gitstd::_Sp_counted_base&lt;(__gnu_cxx::_Lock_policy)2&gt;::_M_release() + 58
    frame #<!-- -->3: 0x00007fffee224796 libclangCppInterOp.so.21.0gitclang::CompilerInstance::~CompilerInstance() + 838
    frame #<!-- -->4: 0x00007fffee22494d libclangCppInterOp.so.21.0gitclang::CompilerInstance::~CompilerInstance() + 13
    frame #<!-- -->5: 0x00007fffed95ec62 libclangCppInterOp.so.21.0gitclang::IncrementalCUDADeviceParser::~IncrementalCUDADeviceParser() + 98
    frame #<!-- -->6: 0x00007fffed9551b6 libclangCppInterOp.so.21.0gitclang::Interpreter::~Interpreter() + 102
    frame #<!-- -->7: 0x00007fffed95598d libclangCppInterOp.so.21.0gitclang::Interpreter::~Interpreter() + 13
    frame #<!-- -->8: 0x00007fffed9181e7 libclangCppInterOp.so.21.0gitcompat::createClangInterpreter(std::vector&lt;char const*, std::allocator&lt;char const*&gt;&gt;&amp;) + 2919

Problem :

  1. The destructor currently handles no clearance for the DeviceParser and the DeviceAct. We currently only have this

    Interpreter::~Interpreter() {
    IncrParser.reset();
    Act->FinalizeAction();
    if (IncrExecutor) {

  2. The ownership for DeviceCI currently is present in IncrementalCudaDeviceParser. But this should be similar to how the combination for hostCI, hostAction and hostParser are managed by the Interpreter. As on master the DeviceAct and DeviceParser are managed by the Interpreter but not DeviceCI. This is problematic because :
    IncrementalParser holds a Sema& which points into the DeviceCI. On master, DeviceCI is destroyed before the base class ~IncrementalParser() runs, causing Parser::reset() to access a dangling Sema (and as Sema holds a reference to Preprocessor which owns PragmaNamespace) we see this

  * frame #<!-- -->0: 0x00007fffee41cfe3 libclangCppInterOp.so.21.0gitclang::PragmaNamespace::~PragmaNamespace() + 99
    frame #<!-- -->1: 0x00007fffee435666 libclangCppInterOp.so.21.0gitclang::Preprocessor::~Preprocessor() + 3830
    

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

4 Files Affected:

  • (modified) clang/include/clang/Interpreter/Interpreter.h (+3)
  • (modified) clang/lib/Interpreter/DeviceOffload.cpp (+3-4)
  • (modified) clang/lib/Interpreter/DeviceOffload.h (+1-2)
  • (modified) clang/lib/Interpreter/Interpreter.cpp (+7-1)
diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index 56213f88b9e30..f8663e3193a18 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -116,6 +116,9 @@ class Interpreter {
   /// Compiler instance performing the incremental compilation.
   std::unique_ptr<CompilerInstance> CI;
 
+  /// An optional compiler instance for CUDA offloading
+  std::unique_ptr<CompilerInstance> DeviceCI;
+
 protected:
   // Derived classes can use an extended interface of the Interpreter.
   Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp
index 7d0125403ea52..8ab6a61993972 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -25,13 +25,13 @@
 namespace clang {
 
 IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
-    std::unique_ptr<CompilerInstance> DeviceInstance,
+    CompilerInstance &DeviceInstance,
     CompilerInstance &HostInstance,
     llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
     llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs)
-    : IncrementalParser(*DeviceInstance, Err), PTUs(PTUs), VFS(FS),
+    : IncrementalParser(DeviceInstance, Err), PTUs(PTUs), VFS(FS),
       CodeGenOpts(HostInstance.getCodeGenOpts()),
-      TargetOpts(DeviceInstance->getTargetOpts()) {
+      TargetOpts(DeviceInstance.getTargetOpts()) {
   if (Err)
     return;
   StringRef Arch = TargetOpts.CPU;
@@ -41,7 +41,6 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
                                                llvm::inconvertibleErrorCode()));
     return;
   }
-  DeviceCI = std::move(DeviceInstance);
 }
 
 llvm::Expected<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() {
diff --git a/clang/lib/Interpreter/DeviceOffload.h b/clang/lib/Interpreter/DeviceOffload.h
index 43645033c4840..c1cb4cd23a4b5 100644
--- a/clang/lib/Interpreter/DeviceOffload.h
+++ b/clang/lib/Interpreter/DeviceOffload.h
@@ -28,7 +28,7 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
 
 public:
   IncrementalCUDADeviceParser(
-      std::unique_ptr<CompilerInstance> DeviceInstance,
+      CompilerInstance &DeviceInstance,
       CompilerInstance &HostInstance,
       llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
       llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs);
@@ -42,7 +42,6 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
   ~IncrementalCUDADeviceParser();
 
 protected:
-  std::unique_ptr<CompilerInstance> DeviceCI;
   int SMVersion;
   llvm::SmallString<1024> PTXCode;
   llvm::SmallVector<char, 1024> FatbinContent;
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 80487eb4ef74a..4ca12f6858c9d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -416,6 +416,10 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
 Interpreter::~Interpreter() {
   IncrParser.reset();
   Act->FinalizeAction();
+  if (DeviceParser)
+    DeviceParser.reset();
+  if (DeviceAct)
+    DeviceAct->FinalizeAction();
   if (IncrExecutor) {
     if (llvm::Error Err = IncrExecutor->cleanUp())
       llvm::report_fatal_error(
@@ -501,8 +505,10 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
 
   DCI->ExecuteAction(*Interp->DeviceAct);
 
+  Interp->DeviceCI = std::move(DCI);
+
   auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
-      std::move(DCI), *Interp->getCompilerInstance(), IMVFS, Err, Interp->PTUs);
+    *Interp->DeviceCI, *Interp->getCompilerInstance(), IMVFS, Err, Interp->PTUs);
 
   if (Err)
     return std::move(Err);

Copy link

github-actions bot commented May 1, 2025

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

@anutosh491
Copy link
Contributor Author

The cuda tests for the negation case should now work (not segfault because libcudart.so is not found but simply get skipped).

1: Test command: /build/anutosh491/CppInterOp/build1/unittests/CppInterOp/CppInterOpTests/unittests/bin/Release/CppInterOpTests
1: Working Directory: /build/anutosh491/CppInterOp/build1/unittests/CppInterOp
1: Environment variables: 
1:  CPLUS_INCLUDE_PATH=/build/anutosh491/CppInterOp/build1/etc
1: Test timeout computed to be: 2400
1: Running main() from /build/anutosh491/CppInterOp/build1/unittests/googletest-prefix/src/googletest/googletest/src/gtest_main.cc
1: [==========] Running 3 tests from 1 test suite.
1: [----------] Global test environment set-up.
1: [----------] 3 tests from CUDATest
1: [ RUN      ] CUDATest.Sanity
1: Failed load libcudart.so runtime:libcudart1.so: cannot open shared object file: No such file or directory
1: Interpreter creation failed
1: /build/anutosh491/CppInterOp/unittests/CppInterOp/CUDATest.cpp:59: Skipped
1: Skipping CUDA tests as CUDA SDK not found
1: 
1: [  SKIPPED ] CUDATest.Sanity (1078 ms)
1: [ RUN      ] CUDATest.CUDAH
1: /build/anutosh491/CppInterOp/unittests/CppInterOp/CUDATest.cpp:68: Skipped
1: Skipping CUDA tests as CUDA SDK not found
1: 
1: [  SKIPPED ] CUDATest.CUDAH (0 ms)
1: [ RUN      ] CUDATest.CUDARuntime
1: /build/anutosh491/CppInterOp/unittests/CppInterOp/CUDATest.cpp:80: Skipped
1: Skipping CUDA tests as CUDA runtime not found
1: 
1: [  SKIPPED ] CUDATest.CUDARuntime (0 ms)
1: [----------] 3 tests from CUDATest (1078 ms total)
1: 
1: [----------] Global test environment tear-down
1: [==========] 3 tests from 1 test suite ran. (1078 ms total)
1: [  PASSED  ] 0 tests.
1: [  SKIPPED ] 3 tests, listed below:
1: [  SKIPPED ] CUDATest.Sanity
1: [  SKIPPED ] CUDATest.CUDAH
1: [  SKIPPED ] CUDATest.CUDARuntime
1/1 Test #1: cppinterop-CppInterOpTests .......   Passed    1.10 sec

And they were anyways working for systems having cuda support

1: Test command: /build/anutosh491/CppInterOp/build1/unittests/CppInterOp/CppInterOpTests/unittests/bin/Release/CppInterOpTests
1: Working Directory: /build/anutosh491/CppInterOp/build1/unittests/CppInterOp
1: Environment variables: 
1:  CPLUS_INCLUDE_PATH=/build/anutosh491/CppInterOp/build1/etc
1: Test timeout computed to be: 2400
1: Running main() from /build/anutosh491/CppInterOp/build1/unittests/googletest-prefix/src/googletest/googletest/src/gtest_main.cc
1: [==========] Running 3 tests from 1 test suite.
1: [----------] Global test environment set-up.
1: [----------] 3 tests from CUDATest
1: [ RUN      ] CUDATest.Sanity
1: [       OK ] CUDATest.Sanity (2177 ms)
1: [ RUN      ] CUDATest.CUDAH
1: [       OK ] CUDATest.CUDAH (1016 ms)
1: [ RUN      ] CUDATest.CUDARuntime
1: In file included from <<< inputs >>>:1:
1: input_line_1:1:1: warning: expression result unused [-Wunused-value]
1:     1 | (bool)cudaGetLastError()
1:       | ^     ~~~~~~~~~~~~~~~~~~
1: /build/anutosh491/CppInterOp/unittests/CppInterOp/CUDATest.cpp:80: Skipped
1: Skipping CUDA tests as CUDA runtime not found
1: 
1: [  SKIPPED ] CUDATest.CUDARuntime (1068 ms)
1: [----------] 3 tests from CUDATest (4263 ms total)
1: 
1: [----------] Global test environment tear-down
1: [==========] 3 tests from 1 test suite ran. (4263 ms total)
1: [  PASSED  ] 2 tests.
1: [  SKIPPED ] 1 test, listed below:
1: [  SKIPPED ] CUDATest.CUDARuntime
1/1 Test #1: cppinterop-CppInterOpTests .......   Passed    4.32 sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants