<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/68120>68120</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLVM 17.x fails to build AdaptiveCpp for AMDGPU due to `invalid addrspacecast`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU,
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          fodinabor
      </td>
    </tr>
</table>

<pre>
    For any of the current LLVM 17.x releases, [AdaptiveCpp](https://github.com/AdaptiveCpp/AdaptiveCpp) (hipSYCL) fails to build its test suit for AMDGPU, independent of the used ROCm version with the error `invalid addrspacecast`.
This can be found e.g. in the CI here: https://github.com/fodinabor/hipSYCL-LLVM-upstream-ci/actions/runs/6381943553/job/17319482716#step:12:569

I didn't take a closer look a the IR, yet.
[LLVM 16](https://github.com/AdaptiveCpp/AdaptiveCpp/actions/runs/6343017764/job/17229994676#step:14:14) and [upstream LLVM](https://github.com/fodinabor/hipSYCL-LLVM-upstream-ci/actions/runs/6378018187/job/17307792193) are fine.

I bisected both LLVM 16-17 and LLVM 17-main:
the first bad commit is: https://github.com/llvm/llvm-project/commit/72fc08a5412ec7ee7f0b904926db16cd86c1f876
the first good-again commit is: https://github.com/llvm/llvm-project/commit/d77c62053c944652846c00a35c921e14b43b1877

Applying d77c62053c944652846c00a35c921e14b43b1877 to `release/17.x` solves the issue, but not sure that's something we want to backport since it changes ABI..?

See the following scripts for reproducing it:
```bash
#!/bin/bash

# Use local dir as workdir
export WORKDIR=`pwd`

# Set ccache size to 512GB and use /tmp/ccache as the cache directory
export CCACHE_MAXSIZE=512G
export CCACHE_DIR=/tmp/ccache

# Clone the AdaptiveCpp repo
git clone https://github.com/AdaptiveCpp/AdaptiveCpp.git acpp

# Clone the llvm-project repository
git clone https://github.com/llvm/llvm-project.git

# Change into the llvm-project directory
cd llvm-project

# Start the bisect process
git bisect start

# Specify the good and bad commits
# git bisect old llvmorg-18-init
# git bisect new main
git bisect old llvmorg-17-init
git bisect new llvmorg-18-init

# Write a script to test the build
# This script should exit with code 0 if the build is successful
# and a non-zero code if the build fails
# For example:
# ./build_and_test.sh

# Run the bisect process
git bisect run ~/build_and_test.sh

# Once the bisect process is complete, reset the repository to the original state
git bisect reset
```

build_and_test.sh
```bash
#!/bin/bash

rm -r $WORKDIR/install/*

mkdir -p build
cd build
cmake -G Ninja ../llvm -DLLVM_ENABLE_PROJECTS="clang;compiler-rt;lld;openmp" \
 -DOPENMP_ENABLE_LIBOMPTARGET=OFF \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_INSTALL_PREFIX=$WORKDIR/install/llvm \
 -DLLVM_ENABLE_ASSERTIONS=OFF \
 -DLLVM_TARGETS_TO_BUILD="X86;AMDGPU" \
 -DCLANG_ANALYZER_ENABLE_Z3_SOLVER=0 \
 -DLLVM_INCLUDE_BENCHMARKS=0 \
 -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON \
 -DCMAKE_INSTALL_RPATH=$WORKDIR/install/llvm/lib \
 -DLLVM_ENABLE_OCAMLDOC=OFF \
               -DLLVM_ENABLE_BINDINGS=OFF \
               -DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN=OFF \
 -DLLVM_ENABLE_DUMP=OFF \
 -DLLVM_CCACHE_BUILD=ON
if [ $? -ne 0 ]; then
    echo "LLVM CMake failed"
    exit 125
fi

ninja -j $OMP_NUM_THREADS install
if [ $? -ne 0 ]; then
    echo "LLVM install failed"
    exit 125
fi

cd $WORKDIR/acpp
rm -r build tests/build
mkdir -p build
cd build

cmake -G Ninja .. \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_INSTALL_PREFIX=$WORKDIR/install/acpp \
 -DLLVM_DIR=$WORKDIR/install/llvm/lib/cmake/llvm \
 -DWITH_ROCM_BACKEND=ON \
    -DWITH_CUDA_BACKEND=OFF \
 -DWITH_OPENCL_BACKEND=OFF \
    -DWITH_SSCP_COMPILER=OFF \
 -DWITH_STDPAR_COMPILER=OFF 
if [ $? -ne 0 ]; then
    echo "AdaptiveCpp CMake failed"
    exit 125
fi

ninja -j $OMP_NUM_THREADS install
if [ $? -ne 0 ]; then
    echo "Buliding AdaptiveCpp failed"
    exit 125
fi

cd $WORKDIR/acpp/tests
mkdir -p build
cd build
cmake -G Ninja .. \
 -DCMAKE_BUILD_TYPE=Release \
 -DAdaptiveCpp_DIR=$WORKDIR/install/acpp/lib/cmake/AdaptiveCpp \
 -DACPP_TARGETS="hip:gfx906"
if [ $? -ne 0 ]; then
    echo "AdaptiveCpp Tests CMake failed"
    exit 125
fi

ninja -j $OMP_NUM_THREADS |& tee build.log
if grep -q "error: invalid addrspacecast" build.log; then
    echo "AdaptiveCpp Tests build failed due to invalid addrspacecast"
    exit 1
fi
if [ $? -ne 0 ]; then
    echo "AdaptiveCpp Tests build failed"
    exit 125
fi

exit 0
```

cc @illuhad 

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWFtz4joS_jXKiwrKlo0NDzwYAwk73ArImTPz4pIlGTRjLK8kT5LzsL99SzIEc8lM5rJVm0rhst2Xr1vdn9rCSvFtwVgfdAagM7zDld4J2c8E5QVOhbxLBX3pj4WEuHiBIoN6xyCppGSFhtPpXzPohu1nKFnOsGIKoBiCziCiuNT8G4vLEnSGAHV3WpcKeBFAY4DGW653VdomYg_QuCl7cdeDRpWX60_x1NxlmOcKagHTiucUcq2gZkpDVXENMyFhNBveLx8NCF5QVrKCGpgH1JViFK4W8R5-Y1JxUcAnrnf2FZNSSAgChxffcM4pxJRKVWLCCFYaBE4bOEPgRJsdV5DgAqYMZqIqKGTtbRvywlqJJ3DHJANeBL8T72tqARofYmuZRLaqUmnJ8L5FOEBjTDQXhQJoLCt7Cbyu2_O9TscDaPxFpACN3dBze34XhW4AkKc0K4EXuQh4USfo1Yjr3wmknBYAhRpq_JVBDEkuF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