Index: clang/docs/HIPSupport.rst =================================================================== --- /dev/null +++ clang/docs/HIPSupport.rst @@ -0,0 +1,124 @@ +.. raw:: html + + + +.. role:: none +.. role:: part +.. role:: good + +.. contents:: + :local: + +=========== +HIP Support +=========== + +`HIP (Heterogeneous-Compute Interface for Portability) `_ +is a C++ Runtime API and Kernel Language that allows developers to create portable applications for +GPUs from single source code. + +Clang supports HIP on `ROCm platform `_. + +Example Usage +============= + +To compile a HIP program, you can use the following command: + +.. code-block:: shell + + clang -c --offload-arch=gfx906 -xhip test.cpp -o test.o + +To link a HIP program, you can use this command: + +.. code-block:: shell + + clang --hip-link --offload-arch=gfx906 test.o -o test + +In the above commands, ``gfx906`` is the GPU architecture that the code is being compiled for. +The supported GPU architectures can be found in the `AMDGPU Processor Table `_. +Alternatively, you can run the ``amdgpu-arch`` tool that comes with Clang to list the GPU architecture on your sytem: + +.. code-block:: shell + + amdgpu-arch + +You can also use ``--offload-arch=native`` to let ``amdgpu-arch`` automatically detect the GPU architecture on your system: + +.. code-block:: shell + + clang -c --offload-arch=native -xhip test.cpp -o test.o + +Path Settings for HIP Dependencies +================================== + +Compiling a HIP program relies on the HIP runtime and device libraries. You can configure the paths to these dependencies using either compiler options or environment variables. + +.. list-table:: + :header-rows: 1 + + * - Compiler Option + - Environment Variable + - Description + - Default Value + * - ``--rocm-path=`` + - ``ROCM_PATH`` + - Path to the ROCm installation. This should be specified if the HIP runtime and device libraries follow the ROCm installation directory structure. + - Compiler tries detecting ROCm path by relative path to clang assuming default ROCm directory structure, then tries detecting ROCm path by its default installation location. + * - ``--hip-path=`` + - ``HIP_PATH`` + - Path to the HIP runtime installation. + - Determined by assuming the ROCm directory structure. + * - ``--hip-device-lib-path=`` + - ``HIP_DEVICE_LIB_PATH`` + - Path to the HIP device library installation. + - Determined by assuming the ROCm directory structure. + +Precedence of Path Settings +=========================== + +1. **Compiler Options**: If you specify paths using the compiler options, these will override the correponding paths set via environment variables. + +2. **HIP Runtime and Device Library Paths**: If you specify individual paths for the HIP runtime or device libraries using either the compiler options or environment variables, these paths will take precedence over the ROCm path. + +3. **ROCm Path**: If you don't specify individual paths for the HIP runtime and device libraries, you can specify the ROCm path. This is used as a base path to find the HIP runtime and device libraries assuming they are located in the default directory structure relative to the ROCm installation. + +4. **Automatic Detection**: If the ROCm path is not specified, the compiler attempts to detect the ROCm installation. Initially, it tries to find it by looking at the relative path to clang assuming the default ROCm directory structure. If this fails, it tries to find the ROCm installation at its default installation location. + +This hierarchy allows you flexibility in how you configure the paths depending on your installation and directory structure. + + +Predefined Macros +================= + +.. list-table:: + :header-rows: 1 + + * - Macro + - Description + * - ``__CLANG_RDC__`` + - Defined when Clang is compiling code in Relocatable Device Code (RDC) mode. RDC, enabled with the ``-fgpu-rdc`` compiler option, is necessary for linking device codes across translation units. + * - ``__HIP__`` + - Defined when compiling with HIP language support, indicating that the code targets the HIP environment. + * - ``__HIPCC__`` + - Alias to ``__HIP__``. + * - ``__HIP_DEVICE_COMPILE__`` + - Defined during device code compilation in Clang's separate compilation process for the host and each offloading GPU architecture. + * - ``__HIP_MEMORY_SCOPE_SINGLETHREAD`` + - Represents single-thread memory scope in HIP (value is 1). + * - ``__HIP_MEMORY_SCOPE_WAVEFRONT`` + - Represents wavefront memory scope in HIP (value is 2). + * - ``__HIP_MEMORY_SCOPE_WORKGROUP`` + - Represents workgroup memory scope in HIP (value is 3). + * - ``__HIP_MEMORY_SCOPE_AGENT`` + - Represents agent memory scope in HIP (value is 4). + * - ``__HIP_MEMORY_SCOPE_SYSTEM`` + - Represents system-wide memory scope in HIP (value is 5). + * - ``__HIP_NO_IMAGE_SUPPORT`` + - Defined with a value of 1 when the target device lacks support for HIP image functions. + * - ``HIP_API_PER_THREAD_DEFAULT_STREAM`` + - Defined when the GPU default stream is set to per-thread mode. +