Repository: pytorch
Issue: Cannot compile with latest LLVM-19
Labels: module: build,module: cuda,triaged

### 🐛 Describe the bug

Really not sure if this is a CMake issue or Pytorch one but I will start here as I think it's related to Pytorch cmake scripts.

I ran into two errors trying to compile with LLVM-19. The first one I was able to get around by modifying the main pytorch CMakeLists.txt. The other one has me stumped.

The first issue is this line in CMakeLists.txt:
```
string(APPEND CMAKE_CUDA_FLAGS " -Xfatbin -compress-all")
```
These flags don't seem to be supported by the clang cuda compiler. I just removed this line to get past the error.

The second error from the cmake configuration stage:
```
-- Performing Test COMPILER_SUPPORTS_RDYNAMIC - Failed
#20 4.539 -- Found CUDA: /usr/local/cuda (found version "Ubuntu clang version 19.1.2 (++20241016063431+7ba7d8e2f7b6-1~exp1~20241016063549.55)
#20 4.539 Target: x86_64-pc-linux-gnu
#20 4.539 Thread model: posix
#20 4.539 InstalledDir: /usr/lib/llvm-19/bin
#20 4.539 .Ubuntu clang version 19.1.2 (++20241016063431+7ba7d8e2f7b6-1~exp1~20241016063549.55)
#20 4.539 Target: x86_64-pc-linux-gnu
#20 4.539 Thread model: posix
#20 4.539 InstalledDir: /usr/lib/llvm-19/bin
#20 4.539 ") 
#20 4.969 -- The CUDA compiler identification is Clang 19.1.2
#20 4.997 -- Detecting CUDA compiler ABI info
#20 5.523 -- Detecting CUDA compiler ABI info - done
#20 5.550 -- Check for working CUDA compiler: /usr/bin/clang++-19 - skipped
#20 5.552 -- Detecting CUDA compile features
#20 5.552 -- Detecting CUDA compile features - done
#20 5.554 -- Found CUDAToolkit: /usr/local/cuda/include (found version "12.5.82")
#20 5.557 -- Performing Test CMAKE_HAVE_LIBC_PTHREAD
#20 5.708 -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
#20 5.708 -- Check if compiler accepts -pthread
#20 5.851 -- Check if compiler accepts -pthread - yes
#20 5.852 -- Found Threads: TRUE
#20 5.866 CMake Error at cmake/public/cuda.cmake:64 (message):
#20 5.866   Found two conflicting CUDA versions:
#20 5.866 
#20 5.866   V19.1.2 in '/usr/local/cuda/include' and
#20 5.866 
#20 5.866   V12.5.82 in '/usr/local/cuda/include'
#20 5.866 Call Stack (most recent call first):
#20 5.866   cmake/Dependencies.cmake:44 (include)
#20 5.866   CMakeLists.txt:862 (include)
#20 5.866 
#20 5.866 
#20 5.867 -- Configuring incomplete, errors occurred!
#20 5.867 CMake Warning:
#20 5.867   Value of CUDA_VERSION contained a newline; truncating
```

Dockerfile to reproduce the second error. Some of this may not strictly be necessary.
Build the dockerfile using: `docker build -t llvmdev:latest  -f Dockerfile .`

```Dockerfile
FROM nvidia/cuda:12.5.1-cudnn-devel-ubuntu20.04 AS build

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && apt upgrade -y && apt install -y \
    locales \
    wget \
    curl \
    git \
    apt-transport-https \
    ca-certificates \
    software-properties-common \
    && rm -rf /var/lib/apt/lists/* \
    && apt clean
RUN locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8

# Update certs
RUN update-ca-certificates

# Setup for newer python and gcc installation
RUN apt update && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt update && apt upgrade -y && apt install -y && \
    rm -rf /var/lib/apt/lists/* && \
    apt clean

RUN apt update && apt install -y \
    python3.12 python3.12-distutils && \
    rm -rf /var/lib/apt/lists/* && \
    apt clean

# Use python to install some up-to-date build tools
RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12 && \
    python3.12 -m pip install --upgrade pip setuptools wheel && \
    python3.12 -m pip install cmake meson ninja conan

# Setup dependencies dir
ENV WORKDIR=/opt
WORKDIR ${WORKDIR}
RUN mkdir -p ${WORKDIR}/deps

# Install llvm
RUN wget -qO - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - && \
    echo "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-19 main" | tee /etc/apt/sources.list.d/llvm.list && \
    echo "deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-19 main" | tee -a /etc/apt/sources.list.d/llvm.list && \
    apt update && apt install -y \
    libllvm19 llvm-19 llvm-19-dev llvm-19-doc llvm-19-examples llvm-19-runtime \
    clang-19 clang-tools-19 clang-19-doc libclang-common-19-dev libclang-19-dev libclang1-19 clang-format-19 python3-clang-19 clangd-19 clang-tidy-19 \
    libclang-rt-19-dev \
    lldb-19 \
    lld-19 \
    libc++-19-dev libc++abi-19-dev \
    libomp-19-dev \
    libunwind-19-dev \
    libmlir-19-dev mlir-19-tools

ENV DEPS_DIR=/opt/deps CC=/usr/bin/clang-19 CXX=/usr/bin/clang++-19

# CCCL
# We install the latest version because the newest clang doesn't work with the Cuda Toolkit CCCL
ADD https://github.com/NVIDIA/cccl.git#df845aded1db97374c854264e34cc7789aa87e49 /opt/deps/include/cccl

# Libtorch
ADD https://github.com/pytorch/pytorch.git#v2.5.0 ${DEPS_DIR}/src/pytorch
# Fix error in pytorch CMakeLists.txt?
RUN cd ${DEPS_DIR}/src/pytorch && sed -i.bak -e '662d' CMakeLists.txt
ENV LD_LIBRARY_PATH=/opt/llvm/lib/x86_64-unknown-linux-gnu/:$LD_LIBRARY_PATH \
    CCCL_DIR=/opt/deps/include/cccl \
    C_COMPILER_PATH=/usr/bin/clang-19 \
    CXX_COMPILER_PATH=/usr/bin/clang++-19
ENV CMAKE_CXX_STANDARD=17 \
    CMAKE_CUDA_STANDARD=17 \
    CMAKE_C_COMPILER=$C_COMPILER_PATH \
    CMAKE_CXX_COMPILER=$CXX_COMPILER_PATH \
    CMAKE_CUDA_COMPILER=$CXX_COMPILER_PATH \
    CMAKE_CUDA_HOST_COMPILER=$CXX_COMPILER_PATH \
    CUDACXX=$CXX_COMPILER_PATH \
    CMAKE_CUDA_ARCHITECTURES=75 \
    CMAKE_CXX_FLAGS="-Wall -Wextra -Werror -fPIC -I$CCCL_DIR/cub -I$CCCL_DIR/thrust -I$CCCL_DIR/libcudacxx/include" \
    CMAKE_CUDA_FLAGS="-stdlib=libc++ -D_ALLOW_UNSUPPORTED_LIBCPP" \
    CMAKE_SHARED_LINKER_FLAGS="-static-libstdc++" \
    CMAKE_EXE_LINKER_FLAGS="-static-libstdc++" \
    CMAKE_PREFIX_PATH=/opt/deps
# PyTorch variables
ENV USE_CUDA=ON \
    USE_NCCL=ON \
    USE_CUDNN=ON \
    DEBUG=OFF \
    CUDA_DEVICE_DEBUG=OFF \
    USE_DISTRIBUTED=OFF \
    USE_MKL=OFF \
    USE_MKLDNN=OFF \
    BUILD_TEST=ON \
    USE_FBGEMM=OFF \
    USE_NNPACK=OFF \
    USE_QNNPACK=OFF \
    USE_XNNPACK=OFF
RUN cd ${DEPS_DIR}/src/pytorch && python3.12 setup.py develop
```

### Versions

Python: 3.12.7
PyTorch: 2.5.0
CMake: 3.30.5
Clang/LLVM: 19.1.2
Ninja: 1.11.1

cc @malfet @seemethere @ptrblck @msaroufim