You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: clang/docs/HIPSupport.rst
+62Lines changed: 62 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -176,3 +176,65 @@ Predefined Macros
176
176
* - ``HIP_API_PER_THREAD_DEFAULT_STREAM``
177
177
- Alias to ``__HIP_API_PER_THREAD_DEFAULT_STREAM__``. Deprecated.
178
178
179
+
Function Pointers Support in Clang with HIP
180
+
===========================================
181
+
182
+
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.
183
+
184
+
.. list-table:: Function Pointers Support Overview
185
+
:widths: 25 25 25
186
+
:header-rows: 1
187
+
188
+
* - Use Case
189
+
- ``-fno-gpu-rdc`` Mode (default)
190
+
- ``-fgpu-rdc`` Mode
191
+
* - Defined and used in the same TU
192
+
- Supported
193
+
- Supported
194
+
* - Defined in one TU and used in another TU
195
+
- Not Supported
196
+
- Supported
197
+
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.
199
+
200
+
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.
201
+
202
+
Virtual Function Support in Clang with HIP
203
+
==========================================
204
+
205
+
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.
206
+
207
+
- **Constructed in Device Code**: Virtual functions of an object can be called in device code if the object is constructed in device code.
208
+
- **Constructed in Host Code**: Virtual functions of an object can be called in host code if the object is constructed in host code.
209
+
210
+
In other scenarios, calling virtual functions is not allowed.
211
+
212
+
Explanation
213
+
-----------
214
+
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.
216
+
217
+
Example Usage
218
+
-------------
219
+
220
+
.. code-block:: c++
221
+
222
+
class Base {
223
+
public:
224
+
__device__ virtual void virtualFunction() {
225
+
// Base virtual function implementation
226
+
}
227
+
};
228
+
229
+
class Derived : public Base {
230
+
public:
231
+
__device__ void virtualFunction() override {
232
+
// Derived virtual function implementation
233
+
}
234
+
};
235
+
236
+
__global__ void kernel() {
237
+
Derived obj;
238
+
Base* basePtr = &obj;
239
+
basePtr->virtualFunction(); // Allowed since obj is constructed in device code
0 commit comments