HIP icon indicating copy to clipboard operation
HIP copied to clipboard

compilation error : when using virtual __device__ functions

Open NEELMCW opened this issue 7 years ago • 8 comments

Hello,

I have a design code which involves abstract classes with pure device virtual functions. On Rocm hipcc complains with an error while the same builds fine on an Nv stack

any suggestions.

Below is the sample reproduceable code for this error

`#include #include <hip/hip_runtime.h>

class Base { int x; public: virtual host device void fun() = 0; int getX() { return x; } };

// This class inherits from Base and implements fun() class Derived: public Base { int y; public: void host device fun() { return; } };

int main(void) { Derived d; d.fun(); return 0; } ` Error log:

pure_virtual_device_function.cpp:8:5: error: virtual member function is not supported virtual __host__ __device__ void fun() = 0; ^ pure_virtual_device_function.cpp:17:5: error: virtual member function is not supported void __host__ __device__ fun() { return; } ^ 2 errors generated. pure_virtual_device_function.cpp:8:5: error: virtual member function is not supported virtual __host__ __device__ void fun() = 0; ^ pure_virtual_device_function.cpp:17:5: error: virtual member function is not supported void __host__ __device__ fun() { return; }

NEELMCW avatar Jan 11 '19 08:01 NEELMCW

@mangupta @AlexVlx Any suggestions or thoughts ?

NEELMCW avatar Jan 16 '19 07:01 NEELMCW

AMD's compiler currently doesn't support function calls on the device. Whenever you call a device function from a kernel, the compiler inlines the function, instead of doing an actual function call. Device functions that are called via device function pointers (and this includes virtual device functions) cannot be inlined, and require an actual function call mechanism--which is currently not supported. That is more or less what the compiler error message tries to tell you in a less verbose way. I know proper function calls will be supported in the future, but I don't know in which ROCm version that will be (I'm eagerly waiting for support of this functionality myself).

rwvo avatar Feb 25 '19 22:02 rwvo

AMD's compiler currently doesn't support function calls on the device. Whenever you call a device function from a kernel, the compiler inlines the function, instead of doing an actual function call. Device functions that are called via device function pointers (and this includes virtual device functions) cannot be inlined, and require an actual function call mechanism--which is currently not supported. That is more or less what the compiler error message tries to tell you in a less verbose way. I know proper function calls will be supported in the future, but I don't know in which ROCm version that will be (I'm eagerly waiting for support of this functionality myself).

@rwvo Any ETA on this?

dalg24 avatar Mar 09 '21 20:03 dalg24

@dalg24 direct function calls have been supported for a while. Indirect function calls, and hence virtual functions, not yet. I'm not working on this myself, nor am I in the pay grade to decide whether I can comment on this in a public, non-NDA forum. Do you have access to the Frontier COE Confluence? If so, you could bring it up there. Otherwise, send me an email.

rwvo avatar Mar 09 '21 20:03 rwvo

Still no specific plan for supporting virtual functions?

NamjaeChoi avatar Jan 06 '23 23:01 NamjaeChoi

I guess it might be supported but at the same time super inefficient, so it is not compelling to provide it... Have you tried other C++ approaches, like std::variant + std::visit? But often some CPU std::variant implementations use a... virtual table! :-( So you need a special implementation for devices not supporting virtual tables. An idea of an implementation we use in SYCL (but could be easily adapted to HIP): https://github.com/triSYCL/sycl/blob/d06d29185bc82698b80f2d0ff9f7144a824b7669/sycl/test/vitis/simple_tests/variant.cpp

keryell avatar Jan 10 '23 10:01 keryell

@NEELMCW Please advise if we can close this ticket? Thanks!

ppanchad-amd avatar May 28 '24 17:05 ppanchad-amd

@NEELMCW Closing ticket. Please re-open if you still need assistance with this ticket. Thanks!

ppanchad-amd avatar Jun 26 '24 20:06 ppanchad-amd