llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Anutosh Bhat (anutosh491)

<details>
<summary>Changes</summary>



`clang-repl --cuda` was previously crashing with a segmentation fault, instead 
of reporting a clean error
```
(base) anutosh491@<!-- -->Anutoshs-MacBook-Air bin % ./clang-repl --cuda
#<!-- -->0 0x0000000111da4fbc 
llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) 
(/opt/local/libexec/llvm-20/lib/libLLVM.dylib+0x150fbc)
#<!-- -->1 0x0000000111da31dc llvm::sys::RunSignalHandlers() 
(/opt/local/libexec/llvm-20/lib/libLLVM.dylib+0x14f1dc)
#<!-- -->2 0x0000000111da5628 SignalHandler(int) 
(/opt/local/libexec/llvm-20/lib/libLLVM.dylib+0x151628)
#<!-- -->3 0x000000019b242de4 
(/usr/lib/system/libsystem_platform.dylib+0x180482de4)
#<!-- -->4 0x0000000107f638d0 
clang::IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(std::__1::unique_ptr&lt;clang::CompilerInstance,
 std::__1::default_delete&lt;clang::CompilerInstance&gt;&gt;, 
clang::CompilerInstance&amp;, 
llvm::IntrusiveRefCntPtr&lt;llvm::vfs::InMemoryFileSystem&gt;, 
llvm::Error&amp;, std::__1::list&lt;clang::PartialTranslationUnit, 
std::__1::allocator&lt;clang::PartialTranslationUnit&gt;&gt; const&amp;) 
(/opt/local/libexec/llvm-20/lib/libclang-cpp.dylib+0x216b8d0)
#<!-- -->5 0x0000000107f638d0 
clang::IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(std::__1::unique_ptr&lt;clang::CompilerInstance,
 std::__1::default_delete&lt;clang::CompilerInstance&gt;&gt;, 
clang::CompilerInstance&amp;, 
llvm::IntrusiveRefCntPtr&lt;llvm::vfs::InMemoryFileSystem&gt;, 
llvm::Error&amp;, std::__1::list&lt;clang::PartialTranslationUnit, 
std::__1::allocator&lt;clang::PartialTranslationUnit&gt;&gt; const&amp;) 
(/opt/local/libexec/llvm-20/lib/libclang-cpp.dylib+0x216b8d0)
#<!-- -->6 0x0000000107f6bac8 
clang::Interpreter::createWithCUDA(std::__1::unique_ptr&lt;clang::CompilerInstance,
 std::__1::default_delete&lt;clang::CompilerInstance&gt;&gt;, 
std::__1::unique_ptr&lt;clang::CompilerInstance, 
std::__1::default_delete&lt;clang::CompilerInstance&gt;&gt;) 
(/opt/local/libexec/llvm-20/lib/libclang-cpp.dylib+0x2173ac8)
#<!-- -->7 0x000000010206f8a8 main 
(/opt/local/libexec/llvm-20/bin/clang-repl+0x1000038a8)
#<!-- -->8 0x000000019ae8c274 
Segmentation fault: 11
```


The underlying issue was that the `DeviceCompilerInstance` (used for 
device-side CUDA compilation) was never initialized with a `Sema`, which is 
required before constructing the `IncrementalCUDADeviceParser`. 

https://github.com/llvm/llvm-project/blob/89687e6f383b742a3c6542dc673a84d9f82d02de/clang/lib/Interpreter/DeviceOffload.cpp#L32

https://github.com/llvm/llvm-project/blob/89687e6f383b742a3c6542dc673a84d9f82d02de/clang/lib/Interpreter/IncrementalParser.cpp#L31

Unlike the host-side `CompilerInstance` which runs `ExecuteAction` inside the 
Interpreter constructor (thereby setting up Sema), the device-side CI was 
passed into the parser uninitialized, leading to an assertion or crash when 
accessing its internals.

To fix this, I refactored the `Interpreter::create` method to include an 
optional `DeviceCI` parameter. If provided, we know we need to take care of 
this instance too. Only then do we construct the `IncrementalCUDADeviceParser`.



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


3 Files Affected:

- (modified) clang/include/clang/Interpreter/Interpreter.h (+2-1) 
- (modified) clang/lib/Interpreter/DeviceOffload.cpp (+2-1) 
- (modified) clang/lib/Interpreter/Interpreter.cpp (+33-15) 


``````````diff
diff --git a/clang/include/clang/Interpreter/Interpreter.h 
b/clang/include/clang/Interpreter/Interpreter.h
index b1b63aedf86ab..20d22c4437a97 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -129,7 +129,8 @@ class Interpreter {
 public:
   virtual ~Interpreter();
   static llvm::Expected<std::unique_ptr<Interpreter>>
-  create(std::unique_ptr<CompilerInstance> CI);
+  create(std::unique_ptr<CompilerInstance> CI,
+    std::unique_ptr<CompilerInstance> DeviceCI = nullptr);
   static llvm::Expected<std::unique_ptr<Interpreter>>
   createWithCUDA(std::unique_ptr<CompilerInstance> CI,
                  std::unique_ptr<CompilerInstance> DCI);
diff --git a/clang/lib/Interpreter/DeviceOffload.cpp 
b/clang/lib/Interpreter/DeviceOffload.cpp
index 1999d63d1aa04..9a7be006250a0 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -34,14 +34,15 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
       TargetOpts(HostInstance.getTargetOpts()) {
   if (Err)
     return;
-  DeviceCI = std::move(DeviceInstance);
   StringRef Arch = TargetOpts.CPU;
   if (!Arch.starts_with("sm_") || Arch.substr(3).getAsInteger(10, SMVersion)) {
+    DeviceInstance.release();
     Err = llvm::joinErrors(std::move(Err), llvm::make_error<llvm::StringError>(
                                                "Invalid CUDA architecture",
                                                
llvm::inconvertibleErrorCode()));
     return;
   }
+  DeviceCI = std::move(DeviceInstance);
 }
 
 llvm::Expected<TranslationUnitDecl *>
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index f8c8d0a425659..1af3f24774028 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -451,13 +451,44 @@ const char *const Runtimes = R"(
 )";
 
 llvm::Expected<std::unique_ptr<Interpreter>>
-Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
+Interpreter::create(std::unique_ptr<CompilerInstance> CI,
+                    std::unique_ptr<CompilerInstance> DeviceCI) {
   llvm::Error Err = llvm::Error::success();
   auto Interp =
       std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
   if (Err)
     return std::move(Err);
 
+  if (DeviceCI) {
+    // auto DeviceLLVMCtx = std::make_unique<llvm::LLVMContext>();
+    // auto DeviceTSCtx =
+    //     
std::make_unique<llvm::orc::ThreadSafeContext>(std::move(DeviceLLVMCtx));
+
+    // llvm::Error DeviceErr = llvm::Error::success();
+    // llvm::ErrorAsOutParameter EAO(&DeviceErr);
+
+    // auto DeviceAct = std::make_unique<IncrementalAction>(
+    //     *DeviceCI, *DeviceTSCtx->getContext(), DeviceErr, *Interp);
+
+    // if (DeviceErr)
+    //   return std::move(DeviceErr);
+
+    //DeviceCI->ExecuteAction(*DeviceAct);
+    DeviceCI->ExecuteAction(*Interp->Act);
+
+    llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> IMVFS =
+        std::make_unique<llvm::vfs::InMemoryFileSystem>();
+
+    auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
+        std::move(DeviceCI), *Interp->getCompilerInstance(), IMVFS, Err,
+        Interp->PTUs);
+
+    if (Err)
+      return std::move(Err);
+
+    Interp->DeviceParser = std::move(DeviceParser);
+  }
+
   // Add runtime code and set a marker to hide it from user code. Undo will not
   // go through that.
   auto PTU = Interp->Parse(Runtimes);
@@ -481,20 +512,7 @@ 
Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
   OverlayVFS->pushOverlay(IMVFS);
   CI->createFileManager(OverlayVFS);
 
-  auto Interp = Interpreter::create(std::move(CI));
-  if (auto E = Interp.takeError())
-    return std::move(E);
-
-  llvm::Error Err = llvm::Error::success();
-  auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
-      std::move(DCI), *(*Interp)->getCompilerInstance(), IMVFS, Err,
-      (*Interp)->PTUs);
-  if (Err)
-    return std::move(Err);
-
-  (*Interp)->DeviceParser = std::move(DeviceParser);
-
-  return Interp;
+  return Interpreter::create(std::move(CI), std::move(DCI));
 }
 
 const CompilerInstance *Interpreter::getCompilerInstance() const {

``````````

</details>


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

Reply via email to