Skip to content

Commit 60c7fd7

Browse files
committed
[HIP] Document func ptr and virtual func
Document clang support for function pointers and virtual functions with HIP
1 parent 52fa4ea commit 60c7fd7

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

clang/docs/HIPSupport.rst

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,36 @@ Predefined Macros
176176
* - ``HIP_API_PER_THREAD_DEFAULT_STREAM``
177177
- Alias to ``__HIP_API_PER_THREAD_DEFAULT_STREAM__``. Deprecated.
178178

179-
Function Pointers Support in Clang with HIP
180-
===========================================
179+
Compilation Modes
180+
=================
181+
182+
Each HIP source file contains intertwined device and host code. Depending on the chosen compilation mode by the compiler options ``-fno-gpu-rdc`` and ``-fgpu-rdc``, these portions of code are compiled differently.
183+
184+
Device Code Compilation
185+
-----------------------
186+
187+
**``-fno-gpu-rdc`` Mode (default)**:
188+
189+
- Compiles to a self-contained, fully linked offloading device binary for each offloading device architecture.
190+
- Device code within a Translation Unit (TU) cannot call functions located in another TU.
191+
192+
**``-fgpu-rdc`` Mode**:
193+
194+
- Compiles to a bitcode for each GPU architecture.
195+
- For each offloading device architecture, the bitcode from different TUs are linked together to create a single offloading device binary.
196+
- Device code in one TU can call functions located in another TU.
197+
198+
Host Code Compilation
199+
---------------------
200+
201+
**Both Modes**:
202+
203+
- Compiles to a relocatable object for each TU.
204+
- These relocatable objects are then linked together.
205+
- Host code within a TU can call host functions and launch kernels from another TU.
206+
207+
Function Pointers Support
208+
=========================
181209

182210
Function pointers' support varies with the usage mode in Clang with HIP. The following table provides an overview of the support status across different use-cases and modes.
183211

@@ -195,24 +223,24 @@ Function pointers' support varies with the usage mode in Clang with HIP. The fol
195223
- Not Supported
196224
- Supported
197225

198-
In the ``-fno-gpu-rdc`` mode, the compiler calculates the resource usage of kernels based only on functions present within the same Translation Unit (TU). This mode does not support the use of function pointers defined in a different TU due to the possibility of incorrect resource usage calculations, leading to undefined behavior.
226+
In the ``-fno-gpu-rdc`` mode, the compiler calculates the resource usage of kernels based only on functions present within the same TU. This mode does not support the use of function pointers defined in a different TU due to the possibility of incorrect resource usage calculations, leading to undefined behavior.
199227

200228
On the other hand, the ``-fgpu-rdc`` mode allows the definition and use of function pointers across different TUs, as resource usage calculations can accommodate functions from disparate TUs.
201229

202-
Virtual Function Support in Clang with HIP
203-
==========================================
230+
Virtual Function Support
231+
========================
204232

205233
In Clang with HIP, support for calling virtual functions of an object in device or host code is contingent on where the object is constructed.
206234

207-
- **Constructed in Device Code**: Virtual functions of an object can be called in device code if the object is constructed in device code.
235+
- **Constructed in Device Code**: Virtual functions of an object can be called in device code on a specific offloading device if the object is constructed in device code on an offloading device with the same architecture.
208236
- **Constructed in Host Code**: Virtual functions of an object can be called in host code if the object is constructed in host code.
209237

210238
In other scenarios, calling virtual functions is not allowed.
211239

212240
Explanation
213241
-----------
214242

215-
An object constructed on the device side contains a pointer to the virtual function table on the device side, which is not accessible in host code, and vice versa. Thus, trying to invoke virtual functions from a context different from where the object was constructed will be disallowed because the appropriate virtual table cannot be accessed.
243+
An object constructed on the device side contains a pointer to the virtual function table on the device side, which is not accessible in host code, and vice versa. Thus, trying to invoke virtual functions from a context different from where the object was constructed will be disallowed because the appropriate virtual table cannot be accessed. The virtual function tables for offloading devices with different architecures are different, therefore trying to invoke virtual functions from an offloading device with a different architecture than where the object is constructed is also disallowed.
216244

217245
Example Usage
218246
-------------

0 commit comments

Comments
 (0)