Package: clang-15
Version: 1:15.0.7-1
Severity: normal
X-Debbugs-Cc: c...@slerp.xyz, debian...@lists.debian.org

Dear Maintainer,

Clang does not search the system paths when looking for the ROCm
components that it requires for building HIP langauge code. Consider the
following sample program:

main.hip:

    #include <stdio.h>
    #include <stdlib.h>
    #include <hip/hip_runtime.h>
    
    #define CHECK_HIP(expr) do {              \
      hipError_t result = (expr);             \
      if (result != hipSuccess) {             \
        fprintf(stderr, "%s:%d: %s (%d)\n",   \
          __FILE__, __LINE__,                 \
          hipGetErrorString(result), result); \
        exit(EXIT_FAILURE);                   \
      }                                       \
    } while(0)
    
    __global__ void sq_arr(float *arr, int n) {
      int tid = blockDim.x*blockIdx.x + threadIdx.x;
      if (tid < n) {
        arr[tid] = arr[tid] * arr[tid];
      }
    }
    
    int main() {
      enum { N = 5 };
      float hArr[N] = { 1, 2, 3, 4, 5 };
      float *dArr;
      CHECK_HIP(hipMalloc(&dArr, sizeof(float) * N));
      CHECK_HIP(hipMemcpy(dArr, hArr, sizeof(float) * N, 
hipMemcpyHostToDevice));
      sq_arr<<<dim3(1), dim3(32,1,1), 0, 0>>>(dArr, N);
      CHECK_HIP(hipMemcpy(hArr, dArr, sizeof(float) * N, 
hipMemcpyDeviceToHost));
      for (int i = 0; i < N; ++i) {
        printf("%f\n", hArr[i]);
      }
      CHECK_HIP(hipFree(dArr));
      return 0;
    }

It should be possible to build this sample program using the commands:

    apt install hipcc
    clang++-15 -x hip -lamdhip64 main.hip

At present, however, that will result in the following error messages:

    clang: error: cannot find ROCm device library; provide its path via 
'--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build without 
ROCm device library
    clang: error: cannot find HIP runtime; provide its path via '--rocm-path', 
or pass '-nogpuinc' to build without HIP runtime
    clang: error: cannot find HIP runtime; provide its path via '--rocm-path', 
or pass '-nogpuinc' to build without HIP runtime

The errors can be resolved with a few additional flags:

  clang++-15 -x hip --rocm-path=/usr \
    --rocm-device-lib-path=/usr/lib/x86_64-linux-gnu/amdgcn/bitcode \
    --hip-version=5.2.21153 \
    -lamdhip64 main.hip

However, this should not be necessary. The first step should be to
backport the upstream HIP detection for Debian and Fedora [1]. That will
solve 2/3rds of this problem by eliminating the need to pass --rocm-path
and --hip-version. 

It's less clear what should be done about the ROCm Device Lib path. The
upstream LLVM project searches for the ROCm device libs at
/usr/lib/llvm-15/lib/clang/15.0.7/lib/amdgcn/bitcode [2], which seems
like the ideal place to put them. However, placing the device libs in a
path that contains the LLVM version number would require a lot of
coordination between packages.

My understanding is that the bitcode files are LLVM IR and newer
versions of LLVM can use bitcode files that were compiled with older
versions of LLVM [3], so it should be fine to leave the bitcode files in
an unversioned path like /usr/lib/$(DEB_HOST_MULTIARCH)/amdgcn/bitcode
and still not have to worry about what happens when LLVM is updated.

After considering these trade-offs, my conclusion is that
/usr/lib/$(DEB_HOST_MULTIARCH)/amdgcn/bitcode should be added to the
search paths for the ROCm device libs.

Sincerely,
Cory Bloor

[1]: 
https://github.com/llvm/llvm-project/commit/082593ff7aff68060bd66dccfa43493d07d9c255
[2]: 
https://github.com/llvm/llvm-project/blob/419948fe6708021524f86e15d755719d634ab8d2/clang/lib/Driver/ToolChains/AMDGPU.cpp#L416
[3]: https://llvm.org/docs/DeveloperPolicy.html#ir-backwards-compatibility

-- System Information:
Debian Release: bookworm/sid
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: amd64 (x86_64)

Kernel: Linux 6.1.0-3-amd64 (SMP w/32 CPU threads; PREEMPT)
Locale: LANG=C, LC_CTYPE=C.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: unable to detect

Versions of packages clang-15 depends on:
ii  binutils                2.40-2
ii  libc6                   2.36-8
ii  libc6-dev               2.36-8
ii  libclang-common-15-dev  1:15.0.7-1
ii  libclang-cpp15          1:15.0.7-1
ii  libclang1-15            1:15.0.7-1
ii  libgcc-12-dev           12.2.0-14
ii  libgcc-s1               12.2.0-14
ii  libllvm15               1:15.0.7-1
ii  libobjc-12-dev          12.2.0-14
ii  libstdc++-12-dev        12.2.0-14
ii  libstdc++6              12.2.0-14
ii  llvm-15-linker-tools    1:15.0.7-1

Versions of packages clang-15 recommends:
ii  llvm-15-dev  1:15.0.7-1
ii  python3      3.11.2-1

Versions of packages clang-15 suggests:
pn  clang-15-doc  <none>
pn  wasi-libc     <none>

-- no debconf information

Reply via email to