Issue 125473
Summary [clang, CUDA] Binary compiled with clang produces wrong output
Labels clang
Assignees
Reporter LukasBreitwieser
    Hi All,

If I compile a simple CUDA kernel (see `main.cu`) and execute the created binary, I receive a wrong result starting from commit: [953beb9fe969bf8ab1857924ea0d3dd6ea506ab1](https://github.com/llvm/llvm-project/commit/953beb9fe969bf8ab1857924ea0d3dd6ea506ab1)
Expected: exit code 0 with output: `Test passed`. 
Actual: exit code 1 with output: 
```
Expected 42, but actual is: 0
Test FAILED
```
 
File: `main.cu`
```
#include <cstdlib>
#include <iostream>

// Define CUDA Kernel
__global__ void Kernel(int *out) { *out = 42; }

int main() {
  // Execute CUDA Kernel
  int *deviceOutput = nullptr;
  int hostOutput = 0;
 cudaMalloc((void **)&deviceOutput, sizeof(int));
  Kernel<<<1, 1>>>(deviceOutput);
  cudaGetLastError();
  cudaDeviceSynchronize();
 cudaMemcpy(&hostOutput, deviceOutput, sizeof(int), cudaMemcpyDeviceToHost);

  // Verify result
  if (hostOutput != 42) {
 std::cout << "Expected 42, but actual is: " << hostOutput << std::endl;
 std::cout << "Test FAILED" << std::endl;
    std::exit(1);
  }
 std::cout << "Test passed" << std::endl;
  return 0;
}
```
Compilation of `main.cu`
```
  clang++ \
    -x cuda \
    -lcuda \
    -lcudart \
    -o main \
    main.cu
```

LLVM configuration
```bash
  cmake -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
    -DLLVM_ENABLE_PROJECTS="clang" \
    -DLLVM_TARGETS_TO_BUILD="host;NVPTX" \
 <path-to-llvm-project-dir>/llvm
```

System Info
- OS: `Ubuntu 20.04.1 LTS`
- Compiler used to compile llvm: `g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0`
- CUDA: `12.6`

I am happy to provide more information if needed.
Thank you in advance for taking a look.

Best,
Lukas
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to