Skip to content

Commit d178145

Browse files
committed
[SYCL][ROCm] Add HIP NVIDIA support
1 parent 9d8e94d commit d178145

File tree

6 files changed

+286
-128
lines changed

6 files changed

+286
-128
lines changed

buildbot/configure.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def do_configure(args):
2727
sycl_build_pi_cuda = 'OFF'
2828
sycl_build_pi_esimd_cpu = 'ON'
2929
sycl_build_pi_rocm = 'OFF'
30+
sycl_build_pi_rocm_platform = 'AMD'
3031
sycl_werror = 'ON'
3132
llvm_enable_assertions = 'ON'
3233
llvm_enable_doxygen = 'OFF'
@@ -40,21 +41,26 @@ def do_configure(args):
4041
if args.arm:
4142
llvm_targets_to_build = 'ARM;AArch64'
4243

44+
if args.disable_esimd_cpu:
45+
sycl_build_pi_esimd_cpu = 'OFF'
46+
47+
if args.cuda or args.rocm:
48+
llvm_enable_projects += ';libclc'
49+
4350
if args.cuda:
4451
llvm_targets_to_build += ';NVPTX'
45-
llvm_enable_projects += ';libclc'
4652
libclc_targets_to_build = 'nvptx64--;nvptx64--nvidiacl'
4753
sycl_build_pi_cuda = 'ON'
4854

49-
if args.disable_esimd_cpu:
50-
sycl_build_pi_esimd_cpu = 'OFF'
51-
5255
if args.rocm:
53-
llvm_targets_to_build += ';AMDGPU'
54-
# TODO libclc should be added once,
55-
# TODO when we build DPC++ with both CUDA and ROCM support
56-
llvm_enable_projects += ';libclc'
57-
libclc_targets_to_build = 'amdgcn--;amdgcn--amdhsa'
56+
if args.rocm_platform == 'AMD':
57+
llvm_targets_to_build += ';AMDGPU'
58+
libclc_targets_to_build += ';amdgcn--;amdgcn--amdhsa'
59+
elif args.rocm_platform == 'NVIDIA' and not args.cuda:
60+
llvm_targets_to_build += ';NVPTX'
61+
libclc_targets_to_build += ';nvptx64--;nvptx64--nvidiacl'
62+
63+
sycl_build_pi_rocm_platform = args.rocm_platform
5864
sycl_build_pi_rocm = 'ON'
5965

6066
if args.no_werror:
@@ -92,6 +98,7 @@ def do_configure(args):
9298
"-DLIBCLC_TARGETS_TO_BUILD={}".format(libclc_targets_to_build),
9399
"-DSYCL_BUILD_PI_CUDA={}".format(sycl_build_pi_cuda),
94100
"-DSYCL_BUILD_PI_ROCM={}".format(sycl_build_pi_rocm),
101+
"-DSYCL_BUILD_PI_ROCM_PLATFORM={}".format(sycl_build_pi_rocm_platform),
95102
"-DLLVM_BUILD_TOOLS=ON",
96103
"-DSYCL_ENABLE_WERROR={}".format(sycl_werror),
97104
"-DCMAKE_INSTALL_PREFIX={}".format(install_dir),
@@ -161,7 +168,8 @@ def main():
161168
parser.add_argument("-t", "--build-type",
162169
metavar="BUILD_TYPE", default="Release", help="build type: Debug, Release")
163170
parser.add_argument("--cuda", action='store_true', help="switch from OpenCL to CUDA")
164-
parser.add_argument("--rocm", action='store_true', help="swith from OpenCL to ROCM")
171+
parser.add_argument("--rocm", action='store_true', help="switch from OpenCL to ROCm")
172+
parser.add_argument("--rocm-platform", type=str, choices=['AMD', 'NVIDIA'], default='AMD', help="choose ROCm backend")
165173
parser.add_argument("--arm", action='store_true', help="build ARM support rather than x86")
166174
parser.add_argument("--disable-esimd-cpu", action='store_true', help="build without ESIMD_CPU support")
167175
parser.add_argument("--no-assertions", action='store_true', help="build without assertions")

sycl/doc/GetStartedGuide.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and a wide range of compute accelerators such as GPU and FPGA.
1010
- [Build DPC++ toolchain with libc++ library](#build-dpc-toolchain-with-libc-library)
1111
- [Build DPC++ toolchain with support for NVIDIA CUDA](#build-dpc-toolchain-with-support-for-nvidia-cuda)
1212
- [Build DPC++ toolchain with support for AMD ROCm](#build-dpc-toolchain-with-support-for-amd-rocm)
13+
- [Build DPC++ toolchain with support for NVIDIA ROCm](#build-dpc-toolchain-with-support-for-nvidia-rocm)
1314
- [Build Doxygen documentation](#build-doxygen-documentation)
1415
- [Deployment](#deployment)
1516
- [Use DPC++ toolchain](#use-dpc-toolchain)
@@ -107,6 +108,7 @@ flags can be found by launching the script with `--help`):
107108
* `--no-werror` -> Don't treat warnings as errors when compiling llvm
108109
* `--cuda` -> use the cuda backend (see [Nvidia CUDA](#build-dpc-toolchain-with-support-for-nvidia-cuda))
109110
* `--rocm` -> use the rocm backend (see [AMD ROCm](#build-dpc-toolchain-with-support-for-amd-rocm))
111+
* `--rocm-platform` -> select the platform used by the rocm backend, `AMD` or `NVIDIA` (see [AMD ROCm](#build-dpc-toolchain-with-support-for-amd-rocm) or see [NVIDIA ROCm](#build-dpc-toolchain-with-support-for-nvidia-rocm))
110112
* `--shared-libs` -> Build shared libraries
111113
* `-t` -> Build type (debug or release)
112114
* `-o` -> Path to build directory
@@ -175,6 +177,34 @@ produce a standard ELF shared code object which can be loaded and executed on an
175177
So if you want to support AMD ROCm, you should also build the lld project.
176178
[LLD Build Guide](https://lld.llvm.org/)
177179

180+
The following CMake variables can be updated to change where CMake is looking
181+
for the ROCm installation:
182+
183+
* `SYCL_BUILD_PI_ROCM_INCLUDE_DIR`: Path to HIP include directory (default
184+
`/opt/rocm/hip/include`).
185+
* `SYCL_BUILD_PI_ROCM_HSA_INCLUDE_DIR`: Path to HSA include directory (default
186+
`/opt/rocm/hsa/include`).
187+
* `SYCL_BUILD_PI_ROCM_AMD_LIBRARY`: Path to HIP runtime library (default
188+
`/opt/rocm/hip/lib/libamdhip64.so`).
189+
190+
### Build DPC++ toolchain with support for NVIDIA ROCm
191+
192+
There is experimental support for DPC++ for using ROCm on NVIDIA devices.
193+
194+
This is a compatibility feature and the [CUDA backend](#build-dpc-toolchain-with-support-for-nvidia-cuda)
195+
should be preferred to run on NVIDIA GPUs.
196+
197+
To enable support for NVIDIA ROCm devices, follow the instructions for the Linux
198+
DPC++ toolchain, but add the `--rocm` and `--rocm-platform NVIDIA` flags to
199+
`configure.py`.
200+
201+
Enabling this flag requires ROCm to be installed, more specifically
202+
[HIP NVCC](https://rocmdocs.amd.com/en/latest/Installation_Guide/HIP-Installation.html#nvidia-platform),
203+
as well as CUDA to be installed, see
204+
[NVIDIA CUDA Installation Guide for Linux](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html).
205+
206+
Currently this was only tested on Linux with ROCm 4.2, CUDA 11 and a GeForce GTX
207+
1060 card.
178208

179209
### Build Doxygen documentation
180210

@@ -510,7 +540,7 @@ and run following command:
510540
clang++ -fsycl simple-sycl-app.cpp -o simple-sycl-app.exe
511541
```
512542
513-
When building for CUDA, use the CUDA target triple as follows:
543+
When building for CUDA or NVIDIA ROCm, use the CUDA target triple as follows:
514544
515545
```bash
516546
clang++ -fsycl -fsycl-targets=nvptx64-nvidia-cuda-sycldevice \

sycl/plugins/rocm/CMakeLists.txt

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,76 @@
1-
message(STATUS "Including the PI API ROCM backend.")
1+
# Set default PI ROCM platform to AMD
2+
set(SYCL_BUILD_PI_ROCM_PLATFORM "AMD" CACHE STRING "PI ROCm platform, AMD or NVIDIA")
23

3-
# cannot rely on cmake support for ROCM; it assumes runtime API is being used.
4-
# we only require the ROCM driver API to be used
5-
# rocm_rocm_LIBRARY variable defines the path to libhsa-runtime64.so, the ROCM Driver API library.
4+
message(STATUS "Including the PI API ROCM backend for ${SYCL_BUILD_PI_ROCM_PLATFORM}.")
65

7-
#find_package(ROCM 4.0 REQUIRED)
8-
9-
# Make imported library global to use it within the project.
10-
add_library(rocmdrv SHARED IMPORTED GLOBAL)
11-
12-
13-
set(ROCM_ROCM_LIBRARY "/opt/rocm/hip/lib/libamdhip64.so")
14-
set(ROCM_INCLUDE_DIRS "/opt/rocm/hip/include")
15-
set(hsa_inc_dir "/opt/rocm/hsa/include")
16-
17-
18-
add_definitions(-D__HIP_PLATFORM_HCC__)
19-
20-
set_target_properties(
21-
rocmdrv PROPERTIES
22-
IMPORTED_LOCATION ${ROCM_ROCM_LIBRARY}
23-
INTERFACE_INCLUDE_DIRECTORIES ${ROCM_INCLUDE_DIRS}
24-
)
6+
# Set default ROCM include dirs
7+
set(SYCL_BUILD_PI_ROCM_INCLUDE_DIR "/opt/rocm/hip/include" CACHE STRING "HIP include dir")
8+
set(SYCL_BUILD_PI_ROCM_HSA_INCLUDE_DIR "/opt/rocm/hsa/include" CACHE STRING "HSA include dir")
9+
set(HIP_HEADERS "${SYCL_BUILD_PI_ROCM_INCLUDE_DIR};${SYCL_BUILD_PI_ROCM_HSA_INCLUDE_DIR}")
2510

11+
# Create pi_rocm library
2612
add_library(pi_rocm SHARED
27-
"${sycl_inc_dir}/CL/sycl/detail/pi.h"
28-
"${sycl_inc_dir}/CL/sycl/detail/pi.hpp"
29-
"pi_rocm.hpp"
30-
"pi_rocm.cpp"
13+
"${sycl_inc_dir}/CL/sycl/detail/pi.h"
14+
"${sycl_inc_dir}/CL/sycl/detail/pi.hpp"
15+
"pi_rocm.hpp"
16+
"pi_rocm.cpp"
3117
)
32-
33-
3418
add_dependencies(sycl-toolchain pi_rocm)
35-
3619
set_target_properties(pi_rocm PROPERTIES LINKER_LANGUAGE CXX)
20+
target_link_libraries(pi_rocm PUBLIC OpenCL-Headers)
3721

38-
22+
# Setup include directories
3923
target_include_directories(pi_rocm
40-
PRIVATE
41-
${sycl_inc_dir}
42-
${sycl_plugin_dir}
43-
${ROCM_INCLUDE_DIRS}
44-
${hsa_inc_dir}
24+
PRIVATE
25+
${sycl_inc_dir}
26+
${sycl_plugin_dir}
4527
)
4628

47-
48-
target_link_libraries(pi_rocm PUBLIC OpenCL-Headers rocmdrv)
29+
if("${SYCL_BUILD_PI_ROCM_PLATFORM}" STREQUAL "AMD")
30+
# Import HIP runtime library
31+
set(SYCL_BUILD_PI_ROCM_AMD_LIBRARY "/opt/rocm/hip/lib/libamdhip64.so" CACHE STRING "HIP AMD runtime library")
32+
add_library(rocmdrv SHARED IMPORTED GLOBAL)
33+
34+
set_target_properties(
35+
rocmdrv PROPERTIES
36+
IMPORTED_LOCATION ${SYCL_BUILD_PI_ROCM_AMD_LIBRARY}
37+
INTERFACE_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
38+
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
39+
)
40+
target_link_libraries(pi_rocm PUBLIC rocmdrv)
41+
42+
# Set HIP define to select AMD platform
43+
target_compile_definitions(pi_rocm PRIVATE __HIP_PLATFORM_AMD__)
44+
elseif("${SYCL_BUILD_PI_ROCM_PLATFORM}" STREQUAL "NVIDIA")
45+
# Import CUDA libraries
46+
find_package(CUDA REQUIRED)
47+
list(APPEND HIP_HEADERS ${CUDA_INCLUDE_DIRS})
48+
49+
# cudadrv may be defined by the CUDA plugin
50+
if(NOT TARGET cudadrv)
51+
add_library(cudadrv SHARED IMPORTED GLOBAL)
52+
set_target_properties(
53+
cudadrv PROPERTIES
54+
IMPORTED_LOCATION ${CUDA_CUDA_LIBRARY}
55+
INTERFACE_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
56+
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
57+
)
58+
endif()
59+
60+
add_library(cudart SHARED IMPORTED GLOBAL)
61+
set_target_properties(
62+
cudart PROPERTIES
63+
IMPORTED_LOCATION ${CUDA_CUDART_LIBRARY}
64+
INTERFACE_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
65+
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
66+
)
67+
target_link_libraries(pi_rocm PUBLIC cudadrv cudart)
68+
69+
# Set HIP define to select NVIDIA platform
70+
target_compile_definitions(pi_rocm PRIVATE __HIP_PLATFORM_NVIDIA__)
71+
else()
72+
message(FATAL_ERROR "Unspecified PI ROCM platform please set SYCL_BUILD_PI_ROCM_PLATFORM to 'AMD' or 'NVIDIA'")
73+
endif()
4974

5075
add_common_options(pi_rocm)
5176

0 commit comments

Comments
 (0)