[libclc] Add optimized DPP scan functions for AMDGPU (#197543) Summary: This uses the update_dpp function to efficiently provide scans. DPP allows for swizzling within a group of 16, so we do four of those, followed by a permlane on GFX10, or a native DPP shift on GFX9. Right now these are not actually used because we do not compile for multiple targets yet, but I verified them by setting `CMAKE_CLC_FLAGS=mcpu=gfx1030` or similar. The intention is to have these waiting as a motivational implementation once we start doing variable builds. The output is optimal as far as I am aware. Here is the example for gfx1030 generation: ```asm _Z28sub_group_scan_inclusive_addi: ; @_Z28sub_group_scan_inclusive_addi ; %bb.0: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) v_mov_b32_e32 v1, 0 s_mov_b32 s4, s33 s_mov_b32 s33, s32 s_mov_b32 s33, s4 v_add_nc_u32_dpp v0, v0, v0 row_shr:1 row_mask:0xf bank_mask:0xf bound_ctrl:1 v_add_nc_u32_dpp v0, v0, v0 row_shr:2 row_mask:0xf bank_mask:0xf bound_ctrl:1 v_add_nc_u32_dpp v0, v0, v0 row_shr:4 row_mask:0xf bank_mask:0xf bound_ctrl:1 v_add_nc_u32_dpp v0, v0, v0 row_shr:8 row_mask:0xf bank_mask:0xf bound_ctrl:1 v_permlanex16_b32 v1, v0, -1, -1 v_add_nc_u32_dpp v0, v1, v0 quad_perm:[0,1,2,3] row_mask:0xa bank_mask:0xf s_setpc_b64 s[30:31] ``` GitOrigin-RevId: f230e44ab888a3688d99ac5f28a2e3edbf3aa596
libclc is an open source implementation of the library requirements of the OpenCL C programming language, as specified by the OpenCL 1.1 Specification. The following sections of the specification impose library requirements:
libclc is intended to be used with the Clang compiler's OpenCL frontend.
libclc is designed to be portable and extensible. To this end, it provides generic implementations of most library requirements, allowing the target to override the generic implementation at the granularity of individual functions.
libclc currently supports PTX, AMDGPU, SPIRV and CLSPV targets, but support for more targets is welcome.
libclc is built as part of an LLVM runtimes build.
Select the targets to build with LLVM_RUNTIME_TARGETS, and enable libclc for each selected target with the matching RUNTIMES_<target-triple>_LLVM_ENABLE_RUNTIMES cache entry.
cd llvm-project mkdir build cd build cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release \ -DRUNTIMES_amdgcn-amd-amdhsa-llvm_LLVM_ENABLE_RUNTIMES=libclc \ -DLLVM_RUNTIME_TARGETS="amdgcn-amd-amdhsa-llvm"
cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release \ -DRUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES=libclc \ -DLLVM_RUNTIME_TARGETS="nvptx64-nvidia-cuda"
cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release \ -DRUNTIMES_spirv64-unknown-vulkan_LLVM_ENABLE_RUNTIMES=libclc \ -DLLVM_RUNTIME_TARGETS="spirv64-unknown-vulkan"
cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release \ -DRUNTIMES_spirv-mesa-mesa3d_LLVM_ENABLE_RUNTIMES=libclc \ -DRUNTIMES_spirv64-mesa-mesa3d_LLVM_ENABLE_RUNTIMES=libclc \ -DLLVM_RUNTIME_TARGETS="spirv-mesa-mesa3d;spirv64-mesa-mesa3d"
To build multiple targets, pass them as a semicolon-separated list in LLVM_RUNTIME_TARGETS and provide a matching RUNTIMES_<target-triple>_LLVM_ENABLE_RUNTIMES=libclc entry for each target.
ninja
ninja install
Note you can use the DESTDIR Makefile variable to do staged installs.
DESTDIR=/path/for/staged/install ninja install
libclc utilizes the LLVM testing infrastructure.
To execute all per-target tests for libclc.
ninja check-libclc
check-libclc is a top-level target that aggregates all per-target tests.
If you are working on a specific target, you can run tests for just that target triple:
ninja check-libclc-<target-triple>
Alternatively, you can run target-specific tests via the runtimes build by pointing to the target-specific build directory:
ninja -C runtimes/runtimes-<target-triple>-bins check-libclc
To build out of tree, or in other words, against an existing LLVM build or install:
CC=$(<path-to>/llvm-config --bindir)/clang cmake \ <path-to>/llvm-project/libclc/CMakeLists.txt -DCMAKE_BUILD_TYPE=Release \ -G Ninja -DLLVM_DIR=$(<path-to>/llvm-config --cmakedir) \ -DLLVM_RUNTIMES_TARGET=<target-triple> $ ninja
Then install as before.
In both cases, the LLVM used must include the targets you want libclc support for (AMDGPU and NVPTX are enabled in LLVM by default). Apart from SPIRV where you do not need an LLVM target but you do need the llvm-spirv tool available. Either build this in-tree, or place it in the directory pointed to by LLVM_TOOLS_BINARY_DIR.