Skip to content

Commit 186f2ac

Browse files
committed
[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
1 parent c02160c commit 186f2ac

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

clang/lib/Headers/__clang_hip_runtime_wrapper.h

+17
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ typedef __SIZE_TYPE__ size_t;
5151
#define nullptr NULL;
5252
#endif
5353

54+
#ifdef __cplusplus
55+
extern "C" {
56+
__attribute__((__visibility__("default")))
57+
__attribute__((weak))
58+
__attribute__((noreturn))
59+
__device__ void __cxa_pure_virtual(void) {
60+
__builtin_trap();
61+
}
62+
__attribute__((__visibility__("default")))
63+
__attribute__((weak))
64+
__attribute__((noreturn))
65+
__device__ void __cxa_deleted_virtual(void) {
66+
__builtin_trap();
67+
}
68+
}
69+
#endif //__cplusplus
70+
5471
#if __HIP_ENABLE_DEVICE_MALLOC__
5572
extern "C" __device__ void *__hip_malloc(size_t __size);
5673
extern "C" __device__ void *__hip_free(void *__ptr);

clang/test/Headers/hip-header.hip

+23
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@
1414

1515
// expected-no-diagnostics
1616

17+
// Check support for pure and deleted virtual functions
18+
struct base {
19+
__host__
20+
__device__
21+
virtual void pv() = 0;
22+
__host__
23+
__device__
24+
virtual void dv() = delete;
25+
};
26+
struct derived:base {
27+
__host__
28+
__device__
29+
virtual void pv() override {};
30+
};
31+
__device__ void test_vf() {
32+
derived d;
33+
}
34+
// 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
35+
// 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
36+
37+
// CHECK: define{{.*}}void @__cxa_pure_virtual()
38+
// CHECK: define{{.*}}void @__cxa_deleted_virtual()
39+
1740
struct Number {
1841
__device__ Number(float _x) : x(_x) {}
1942
float x;

0 commit comments

Comments
 (0)