Index: clang/lib/Headers/__clang_hip_runtime_wrapper.h =================================================================== --- clang/lib/Headers/__clang_hip_runtime_wrapper.h +++ clang/lib/Headers/__clang_hip_runtime_wrapper.h @@ -18,28 +18,6 @@ #if __HIP__ -#if !defined(__HIPCC_RTC__) -#include -#include -#include -#else -typedef __SIZE_TYPE__ size_t; -// Define macros which are needed to declare HIP device API's without standard -// C/C++ headers. This is for readability so that these API's can be written -// the same way as non-hipRTC use case. These macros need to be popped so that -// they do not pollute users' name space. -#pragma push_macro("NULL") -#pragma push_macro("uint32_t") -#pragma push_macro("uint64_t") -#pragma push_macro("CHAR_BIT") -#pragma push_macro("INT_MAX") -#define NULL (void *)0 -#define uint32_t __UINT32_TYPE__ -#define uint64_t __UINT64_TYPE__ -#define CHAR_BIT __CHAR_BIT__ -#define INT_MAX __INTMAX_MAX__ -#endif // __HIPCC_RTC__ - #define __host__ __attribute__((host)) #define __device__ __attribute__((device)) #define __global__ __attribute__((global)) @@ -68,24 +46,58 @@ } #endif //__cplusplus +typedef __SIZE_TYPE__ __hip_size_t; + +#ifdef __cplusplus +extern "C" { +#endif //__cplusplus + #if __HIP_ENABLE_DEVICE_MALLOC__ -extern "C" __device__ void *__hip_malloc(size_t __size); -extern "C" __device__ void *__hip_free(void *__ptr); -static inline __device__ void *malloc(size_t __size) { +__device__ void *__hip_malloc(__hip_size_t __size); +__device__ void *__hip_free(void *__ptr); +__attribute__((weak)) inline __device__ void *malloc(__hip_size_t __size) { return __hip_malloc(__size); } -static inline __device__ void *free(void *__ptr) { return __hip_free(__ptr); } +__attribute__((weak)) inline __device__ void *free(void *__ptr) { + return __hip_free(__ptr); +} #else -static inline __device__ void *malloc(size_t __size) { +__attribute__((weak)) inline __device__ void *malloc(__hip_size_t __size) { __builtin_trap(); return nullptr; } -static inline __device__ void *free(void *__ptr) { +__attribute__((weak)) inline __device__ void *free(void *__ptr) { __builtin_trap(); return nullptr; } #endif +#ifdef __cplusplus +} // extern "C" +#endif //__cplusplus + +#if !defined(__HIPCC_RTC__) +#include +#include +#include +#else +typedef __SIZE_TYPE__ size_t; +// Define macros which are needed to declare HIP device API's without standard +// C/C++ headers. This is for readability so that these API's can be written +// the same way as non-hipRTC use case. These macros need to be popped so that +// they do not pollute users' name space. +#pragma push_macro("NULL") +#pragma push_macro("uint32_t") +#pragma push_macro("uint64_t") +#pragma push_macro("CHAR_BIT") +#pragma push_macro("INT_MAX") +#define NULL (void *)0 +#define uint32_t __UINT32_TYPE__ +#define uint64_t __UINT64_TYPE__ +#define CHAR_BIT __CHAR_BIT__ +#define INT_MAX __INTMAX_MAX__ +#endif // __HIPCC_RTC__ + #include <__clang_hip_libdevice_declares.h> #include <__clang_hip_math.h> Index: clang/test/Headers/hip-header.hip =================================================================== --- clang/test/Headers/hip-header.hip +++ clang/test/Headers/hip-header.hip @@ -26,6 +26,13 @@ // RUN: -target-cpu gfx906 -emit-llvm %s -fcuda-is-device -o - \ // RUN: -D__HIPCC_RTC__ -std=c++14 | FileCheck -check-prefixes=CHECK,CXX14 %s +// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ +// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ +// RUN: -internal-isystem %S/Inputs/include \ +// RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-unknown-unknown \ +// RUN: -target-cpu gfx906 -emit-llvm %s -fcuda-is-device -o - \ +// RUN: -D__HIPCC_RTC__ | FileCheck -check-prefixes=MALLOCFREE %s + // expected-no-diagnostics // Check support for pure and deleted virtual functions @@ -115,3 +122,15 @@ return r ; } + +// Check that device malloc and free do not conflict with std headers. +#include +// MALLOCFREE-LABEL: define{{.*}}@_Z11test_malloc +__device__ void test_malloc(void *a) { + a = malloc(42); +} + +// MALLOCFREE-LABEL: define{{.*}}@_Z9test_free +__device__ void test_free(void *a) { + free(a); +}