Skip to content

Commit 4e1c6e6

Browse files
Jeff Hammondromanovvlad
andcommitted
[SYCL] MacOS support for os_util (#763)
Slight modifications to the original implementation by Alexander Batashev. run clang-format Signed-off-by: Hammond, Jeff R <[email protected]> Signed-off-by: Alexander Batashev <[email protected] > Co-Authored-By: Romanov Vlad <[email protected]>
1 parent ddd24a3 commit 4e1c6e6

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

sycl/include/CL/sycl/detail/os_util.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#elif __linux__
2525
// Linux platform
2626
#define SYCL_RT_OS_LINUX
27+
#define SYCL_RT_OS_POSIX_SUPPORT
28+
#elif defined(__APPLE__) && defined(__MACH__)
29+
// Apple OSX
30+
#define SYCL_RT_OS_DARWIN
31+
#define SYCL_RT_OS_POSIX_SUPPORT
2732
#else
2833
#error "Unsupported compiler or OS"
2934
#endif // _WIN32
@@ -41,7 +46,7 @@
4146
#define __SYCL_EXPORTED __declspec(dllimport)
4247
#endif
4348

44-
#elif defined(SYCL_RT_OS_LINUX)
49+
#elif defined(SYCL_RT_OS_POSIX_SUPPORT)
4550

4651
#define DLL_LOCAL __attribute__((visibility("hidden")))
4752
#define __SYCL_EXPORTED

sycl/source/detail/os_util.cpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@
99
#include <CL/sycl/detail/os_util.hpp>
1010
#include <CL/sycl/exception.hpp>
1111

12+
#ifdef SYCL_RT_OS_POSIX_SUPPORT
13+
#include <cstdlib>
14+
#endif
15+
1216
#if defined(SYCL_RT_OS_LINUX)
1317

1418
#ifndef _GNU_SOURCE
1519
#define _GNU_SOURCE
1620
#endif // _GNU_SOURCE
1721

22+
#include <cstdio>
1823
#include <link.h>
19-
#include <stdio.h>
2024
#include <sys/sysinfo.h>
2125

2226
#elif defined(SYCL_RT_OS_WINDOWS)
2327

2428
#include <Windows.h>
2529
#include <malloc.h>
26-
#endif
30+
31+
#elif defined(SYCL_RT_OS_DARWIN)
32+
33+
#include <dlfcn.h>
34+
#include <sys/sysctl.h>
35+
#include <sys/types.h>
36+
37+
#endif // SYCL_RT_OS
2738

2839
namespace cl {
2940
namespace sycl {
@@ -73,15 +84,23 @@ OSModuleHandle OSUtil::getOSModuleHandle(const void *VirtAddr) {
7384
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
7485
auto LpModuleAddr = reinterpret_cast<LPCSTR>(VirtAddr);
7586
if (!GetModuleHandleExA(Flag, LpModuleAddr, &PhModule)) {
76-
// Expect the caller to check for zero and take
77-
// necessary action
78-
return 0;
87+
// Expect the caller to check for zero and take
88+
// necessary action
89+
return 0;
7990
}
8091
if (PhModule == GetModuleHandleA(nullptr))
8192
return OSUtil::ExeModuleHandle;
8293
return reinterpret_cast<OSModuleHandle>(PhModule);
8394
}
84-
#endif // SYCL_RT_OS_WINDOWS
95+
96+
#elif defined(SYCL_RT_OS_DARWIN)
97+
OSModuleHandle OSUtil::getOSModuleHandle(const void *VirtAddr) {
98+
Dl_info Res;
99+
dladdr(VirtAddr, &Res);
100+
return reinterpret_cast<OSModuleHandle>(Res.dli_fbase);
101+
}
102+
103+
#endif // SYCL_RT_OS
85104

86105
size_t OSUtil::getOSMemSize() {
87106
#if defined(SYCL_RT_OS_LINUX)
@@ -93,19 +112,27 @@ size_t OSUtil::getOSMemSize() {
93112
MemInfo.dwLength = sizeof(MemInfo);
94113
GlobalMemoryStatusEx(&MemInfo);
95114
return static_cast<size_t>(MemInfo.ullTotalPhys);
96-
#endif
115+
#elif defined(SYCL_RT_OS_DARWIN)
116+
int64_t Size = 0;
117+
sysctlbyname("hw.memsize", &Size, nullptr, nullptr, 0);
118+
return static_cast<size_t>(Size);
119+
#endif // SYCL_RT_OS
97120
}
98121

99122
void *OSUtil::alignedAlloc(size_t Alignment, size_t NumBytes) {
100123
#if defined(SYCL_RT_OS_LINUX)
101124
return aligned_alloc(Alignment, NumBytes);
125+
#elif defined(SYCL_RT_OS_POSIX_SUPPORT)
126+
void *Addr = nullptr;
127+
int ReturnCode = posix_memalign(&Addr, Alignment, NumBytes);
128+
return (ReturnCode == 0) ? Addr : nullptr;
102129
#elif defined(SYCL_RT_OS_WINDOWS)
103130
return _aligned_malloc(NumBytes, Alignment);
104131
#endif
105132
}
106133

107134
void OSUtil::alignedFree(void *Ptr) {
108-
#if defined(SYCL_RT_OS_LINUX)
135+
#if defined(SYCL_RT_OS_LINUX) || defined(SYCL_RT_OS_POSIX_SUPPORT)
109136
free(Ptr);
110137
#elif defined(SYCL_RT_OS_WINDOWS)
111138
_aligned_free(Ptr);

0 commit comments

Comments
 (0)