From 186f2ac612ad3cd551dee649e3097f4284774ba0 Mon Sep 17 00:00:00 2001 From: "Yaxun (Sam) Liu" Date: Fri, 18 Jun 2021 16:39:41 -0400 Subject: [PATCH] [HIP] Add support functions for C++ polymorphic types Add runtime functions to detect invalid calls to pure or deleted virtual functions. Patch by: Siu Chi Chan Reviewed by: Yaxun Liu Differential Revision: https://reviews.llvm.org/D104392 --- .../lib/Headers/__clang_hip_runtime_wrapper.h | 17 ++++++++++++++ clang/test/Headers/hip-header.hip | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/clang/lib/Headers/__clang_hip_runtime_wrapper.h b/clang/lib/Headers/__clang_hip_runtime_wrapper.h index 58f148f9a268..c557796c8fa0 100644 --- a/clang/lib/Headers/__clang_hip_runtime_wrapper.h +++ b/clang/lib/Headers/__clang_hip_runtime_wrapper.h @@ -51,6 +51,23 @@ typedef __SIZE_TYPE__ size_t; #define nullptr NULL; #endif +#ifdef __cplusplus +extern "C" { + __attribute__((__visibility__("default"))) + __attribute__((weak)) + __attribute__((noreturn)) + __device__ void __cxa_pure_virtual(void) { + __builtin_trap(); + } + __attribute__((__visibility__("default"))) + __attribute__((weak)) + __attribute__((noreturn)) + __device__ void __cxa_deleted_virtual(void) { + __builtin_trap(); + } +} +#endif //__cplusplus + #if __HIP_ENABLE_DEVICE_MALLOC__ extern "C" __device__ void *__hip_malloc(size_t __size); extern "C" __device__ void *__hip_free(void *__ptr); diff --git a/clang/test/Headers/hip-header.hip b/clang/test/Headers/hip-header.hip index 323138613055..5bba1de2ce6c 100644 --- a/clang/test/Headers/hip-header.hip +++ b/clang/test/Headers/hip-header.hip @@ -14,6 +14,29 @@ // expected-no-diagnostics +// Check support for pure and deleted virtual functions +struct base { + __host__ + __device__ + virtual void pv() = 0; + __host__ + __device__ + virtual void dv() = delete; +}; +struct derived:base { + __host__ + __device__ + virtual void pv() override {}; +}; +__device__ void test_vf() { + derived d; +} +// CHECK: @_ZTV7derived = linkonce_odr unnamed_addr addrspace(1) constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* null, i8* bitcast (void (%struct.derived*)* @_ZN7derived2pvEv to i8*), i8* bitcast (void ()* @__cxa_deleted_virtual to i8*)] }, comdat, align 8 +// CHECK: @_ZTV4base = linkonce_odr unnamed_addr addrspace(1) constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* null, i8* bitcast (void ()* @__cxa_pure_virtual to i8*), i8* bitcast (void ()* @__cxa_deleted_virtual to i8*)] }, comdat, align 8 + +// CHECK: define{{.*}}void @__cxa_pure_virtual() +// CHECK: define{{.*}}void @__cxa_deleted_virtual() + struct Number { __device__ Number(float _x) : x(_x) {} float x;