[clang] [clang-repl] : Fix clang-repl crash with --cuda flag (PR #136404)

Anutosh Bhat via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 24 00:33:30 PDT 2025


anutosh491 wrote:

Hey @vgvassilev @argentite 

With these changes, I am positive we atleast have the design in place to run any of these Cuda tests (https://github.com/llvm/llvm-project/tree/main/clang/test/Interpreter/CUDA)

For example running the sanity.cu file 
```
anutosh491 at vv-nuc:/build/anutosh491/llvm-project/build/bin$ ./clang-repl --cuda
clang-repl> extern "C" int printf(const char*, ...);
clang-repl> __global__ void test_func() {}
clang-repl> test_func<<<1,1>>>();
clang-repl> printf("CUDA Error: %d", cudaGetLastError());
CUDA Error: 0
clang-repl> 
```

The only thing which I am kinda struggling to figure out is how to put the virtual filesystem to use. So to get the above tests working, I had to just comment out what we do through VFS and use the real filesystem 

```
  // VFS->addFile(FatbinFileName, 0,
  //              llvm::MemoryBuffer::getMemBuffer(
  //                  llvm::StringRef(FatbinContent.data(), FatbinContent.size()),
  //                  "", false));
  // Write directly to disk
  std::error_code EC;
  llvm::raw_fd_ostream OS(FatbinFileName, EC);
  if (EC) {
    return llvm::make_error<llvm::StringError>(
        "Failed to write fatbin: " + EC.message(), llvm::inconvertibleErrorCode());
  }
  OS.write(FatbinContent.data(), FatbinContent.size());
  OS.close();
  ```
  
  So hopefully the VFS used here
  https://github.com/llvm/llvm-project/blob/bea110db3ed1fa1215bb8e22d2057019fcbd2d16/clang/lib/Interpreter/DeviceOffload.cpp#L63
  
  and here 
  
  https://github.com/llvm/llvm-project/blob/bea110db3ed1fa1215bb8e22d2057019fcbd2d16/clang/lib/CodeGen/CGCUDANV.cpp#L791-L792
  
  Should be the same, but my logs tell me otherwise. I added some simple logs to verify the same and I see this 
```
[DeviceOffload] Current working directory: /
[DeviceOffload] Fatbin file successfully found in VFS: /incr_module_3.fatbin
[CGCUDANV] CudaGpuBinaryFileName: /incr_module_3.fatbin
[CGCUDANV] VFS CWD: /build/anutosh491/llvm-project/build/bin
[CGCUDANV] status() failed: No such file or directory
```

So if you see both VFS point to different locations.
I tried some workarounds but haven't been able to figure this out and possibly might need some help.

So the current changes would just fail with 
```
anutosh491 at vv-nuc:/build/anutosh491/llvm-project/build/bin$ ./clang-repl --cuda
clang-repl> extern "C" int printf(const char*, ...);
clang-repl> __global__ void test_func() {}
fatal error: cannot open file '/incr_module_3.fatbin': No such file or directory
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  clang-repl 0x0000555557fde34f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 63
1  clang-repl 0x0000555557fdc264 llvm::sys::RunSignalHandlers() + 52
2  clang-repl 0x0000555557fdc5b9
3  libc.so.6  0x00007ffff795c520
4  clang-repl 0x00005555581608e7 clang::Interpreter::GenModule(clang::IncrementalAction*) + 519
5  clang-repl 0x0000555558160dee clang::Interpreter::RegisterPTU(clang::TranslationUnitDecl*, std::unique_ptr<llvm::Module, std::default_delete<llvm::Module>>, clang::IncrementalAction*) + 206
6  clang-repl 0x0000555558161027 clang::Interpreter::Parse(llvm::StringRef) + 471
7  clang-repl 0x0000555558162360 clang::Interpreter::ParseAndExecute(llvm::StringRef, clang::Value*) + 48
8  clang-repl 0x0000555556263075 main + 4085
9  libc.so.6  0x00007ffff7943d90
10 libc.so.6  0x00007ffff7943e40 __libc_start_main + 128
11 clang-repl 0x00005555563da585 _start + 37
Segmentation fault (core dumped)
```

Once we start fetching these files from the correct location, this should be ready !
  
  



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


More information about the cfe-commits mailing list