compilation error : when using virtual __device__ functions
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
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; }
@mangupta @AlexVlx Any suggestions or thoughts ?
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).
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 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.
Still no specific plan for supporting virtual functions?
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
@NEELMCW Please advise if we can close this ticket? Thanks!
@NEELMCW Closing ticket. Please re-open if you still need assistance with this ticket. Thanks!