Skip to content

Commit 0f9e288

Browse files
committed
Don't duplicate code
1 parent 1a573d1 commit 0f9e288

File tree

9 files changed

+103
-120
lines changed

9 files changed

+103
-120
lines changed

sycl/include/sycl/detail/os_util.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ class __SYCL_EXPORT OSUtil {
5252
/// Returns a directory component of a path.
5353
static std::string getDirName(const char *Path);
5454

55-
#ifdef __SYCL_RT_OS_WINDOWS
56-
/// Returns an absolute path to a directory where the object was found.
57-
static std::filesystem::path getCurrentDSODirPath();
58-
#endif
59-
6055
#ifdef __SYCL_RT_OS_WINDOWS
6156
static constexpr const char *DirSep = "\\";
6257
#else

sycl/include/sycl/detail/pi.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include <sycl/detail/pi.h> // for piContextCreate, piContextGetInfo
2020

2121
#include <cstdint> // for uint64_t, uint32_t
22+
// DPCPP is supported on some linux systems where default compiler is gcc 7.5 which doesn't provide <filesystem> support.
23+
// On Windows, minimal supported version of Visual sSudio is 2019 where <filesystem> is available (supported since Visual Studio 2017 version 15.7).
24+
// TODO: use <filesystem> on Linux as well when support matrix will change.
2225
#if _WIN32
2326
#include <filesystem>
2427
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===---- windows_os_utils.hpp - OS utilities for Windows header file ----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <Windows.h>
12+
#include <direct.h>
13+
#include <filesystem>
14+
#include <malloc.h>
15+
#include <shlwapi.h>
16+
17+
18+
namespace sycl {
19+
inline namespace _V1 {
20+
namespace detail {
21+
22+
std::filesystem::path getCurrentDSODirPath();
23+
24+
using OSModuleHandle = intptr_t;
25+
static constexpr OSModuleHandle ExeModuleHandle = -1;
26+
27+
OSModuleHandle getOSModuleHandle(const void *VirtAddr);
28+
29+
} // namespace detail
30+
} // namespace _V1
31+
} // namespace sycl

sycl/pi_win_proxy_loader/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project(pi_win_proxy_loader)
2-
add_library(pi_win_proxy_loader SHARED pi_win_proxy_loader.cpp)
2+
add_library(pi_win_proxy_loader SHARED pi_win_proxy_loader.cpp ../source/detail/windows_os_utils.cpp)
33
install(TARGETS pi_win_proxy_loader
44
RUNTIME DESTINATION "bin" COMPONENT pi_win_proxy_loader
55
)
@@ -41,7 +41,7 @@ if (MSVC)
4141
# Handle the debug version for the Microsoft compiler as a special case by
4242
# creating a debug version of the static library that uses the flags used by
4343
# the SYCL runtime
44-
add_library(pi_win_proxy_loaderd SHARED pi_win_proxy_loader.cpp)
44+
add_library(pi_win_proxy_loaderd SHARED pi_win_proxy_loader.cpp ../source/detail/windows_os_utils.cpp)
4545
target_compile_options(pi_win_proxy_loaderd PRIVATE ${WINUNLOAD_CXX_FLAGS_DEBUG})
4646
target_compile_options(pi_win_proxy_loader PRIVATE ${WINUNLOAD_CXX_FLAGS_RELEASE})
4747
target_link_libraries(pi_win_proxy_loaderd PRIVATE shlwapi)

sycl/pi_win_proxy_loader/pi_win_proxy_loader.cpp

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -39,84 +39,9 @@
3939
#include <string>
4040

4141
#include "pi_win_proxy_loader.hpp"
42-
42+
#include <sycl/detail/windows_os_utils.hpp>
4343
#ifdef _WIN32
4444

45-
// ------------------------------------
46-
47-
static constexpr const char *DirSep = "\\";
48-
49-
// cribbed from sycl/source/detail/os_util.cpp
50-
std::string getDirName(const char *Path) {
51-
std::string Tmp(Path);
52-
// Remove trailing directory separators
53-
Tmp.erase(Tmp.find_last_not_of("/\\") + 1, std::string::npos);
54-
55-
size_t pos = Tmp.find_last_of("/\\");
56-
if (pos != std::string::npos)
57-
return Tmp.substr(0, pos);
58-
59-
// If no directory separator is present return initial path like dirname does
60-
return Tmp;
61-
}
62-
63-
// cribbed from sycl/source/detail/os_util.cpp
64-
// TODO: Just inline it.
65-
using OSModuleHandle = intptr_t;
66-
static constexpr OSModuleHandle ExeModuleHandle = -1;
67-
static OSModuleHandle getOSModuleHandle(const void *VirtAddr) {
68-
HMODULE PhModule;
69-
DWORD Flag = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
70-
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
71-
auto LpModuleAddr = reinterpret_cast<LPCSTR>(VirtAddr);
72-
if (!GetModuleHandleExA(Flag, LpModuleAddr, &PhModule)) {
73-
// Expect the caller to check for zero and take
74-
// necessary action
75-
return 0;
76-
}
77-
if (PhModule == GetModuleHandleA(nullptr))
78-
return ExeModuleHandle;
79-
return reinterpret_cast<OSModuleHandle>(PhModule);
80-
}
81-
82-
// cribbed from sycl/source/detail/os_util.cpp
83-
/// Returns an absolute path where the object was found.
84-
std::string getCurrentDSODir() {
85-
char Path[MAX_PATH];
86-
Path[0] = '\0';
87-
Path[sizeof(Path) - 1] = '\0';
88-
auto Handle = getOSModuleHandle(reinterpret_cast<void *>(&getCurrentDSODir));
89-
DWORD Ret = GetModuleFileNameA(
90-
reinterpret_cast<HMODULE>(ExeModuleHandle == Handle ? 0 : Handle),
91-
reinterpret_cast<LPSTR>(&Path), sizeof(Path));
92-
assert(Ret < sizeof(Path) && "Path is longer than PATH_MAX?");
93-
assert(Ret > 0 && "GetModuleFileNameA failed");
94-
(void)Ret;
95-
96-
BOOL RetCode = PathRemoveFileSpecA(reinterpret_cast<LPSTR>(&Path));
97-
assert(RetCode && "PathRemoveFileSpecA failed");
98-
(void)RetCode;
99-
100-
return Path;
101-
}
102-
103-
std::filesystem::path getCurrentDSODirPath() {
104-
wchar_t Path[MAX_PATH];
105-
auto Handle = getOSModuleHandle(reinterpret_cast<void *>(&getCurrentDSODir));
106-
DWORD Ret = GetModuleFileName(
107-
reinterpret_cast<HMODULE>(ExeModuleHandle == Handle ? 0 : Handle),
108-
reinterpret_cast<LPWSTR>(&Path), sizeof(Path));
109-
assert(Ret < sizeof(Path) && "Path is longer than PATH_MAX?");
110-
assert(Ret > 0 && "GetModuleFileName failed");
111-
(void)Ret;
112-
113-
BOOL RetCode = PathRemoveFileSpec(reinterpret_cast<LPWSTR>(&Path));
114-
assert(RetCode && "PathRemoveFileSpec failed");
115-
(void)RetCode;
116-
117-
return std::filesystem::path(std::wstring(Path));
118-
}
119-
12045
// these are cribbed from include/sycl/detail/pi.hpp
12146
// a new plugin must be added to both places.
12247
#ifdef _MSC_VER
@@ -164,7 +89,7 @@ void preloadLibraries() {
16489
}
16590

16691
// this path duplicates sycl/detail/pi.cpp:initializePlugins
167-
std::filesystem::path LibSYCLDir = getCurrentDSODirPath();
92+
std::filesystem::path LibSYCLDir = sycl::detail::getCurrentDSODirPath();
16893

16994
MapT &dllMap = getDllMap();
17095

sycl/source/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ set(SYCL_SOURCES
236236
"spirv_ops.cpp"
237237
"esimd_emulator_device_interface.cpp"
238238
"$<$<PLATFORM_ID:Windows>:detail/windows_pi.cpp>"
239+
"$<$<PLATFORM_ID:Windows>:detail/windows_os_utils.cpp>"
239240
"$<$<OR:$<PLATFORM_ID:Linux>,$<PLATFORM_ID:Darwin>>:detail/posix_pi.cpp>"
240241
)
241242

sycl/source/detail/os_util.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <filesystem>
3535
#include <malloc.h>
3636
#include <shlwapi.h>
37+
#include <sycl/detail/windows_os_utils.hpp>
3738

3839
#elif defined(__SYCL_RT_OS_DARWIN)
3940

@@ -140,24 +141,6 @@ std::string OSUtil::getDirName(const char *Path) {
140141
}
141142

142143
#elif defined(__SYCL_RT_OS_WINDOWS)
143-
// TODO: Just inline it.
144-
using OSModuleHandle = intptr_t;
145-
static constexpr OSModuleHandle ExeModuleHandle = -1;
146-
static OSModuleHandle getOSModuleHandle(const void *VirtAddr) {
147-
HMODULE PhModule;
148-
DWORD Flag = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
149-
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
150-
auto LpModuleAddr = reinterpret_cast<LPCSTR>(VirtAddr);
151-
if (!GetModuleHandleExA(Flag, LpModuleAddr, &PhModule)) {
152-
// Expect the caller to check for zero and take
153-
// necessary action
154-
return 0;
155-
}
156-
if (PhModule == GetModuleHandleA(nullptr))
157-
return ExeModuleHandle;
158-
return reinterpret_cast<OSModuleHandle>(PhModule);
159-
}
160-
161144
/// Returns an absolute path where the object was found.
162145
// pi_win_proxy_loader.dll uses this same logic. If it is changed
163146
// significantly, it might be wise to change it there too.
@@ -193,23 +176,6 @@ std::string OSUtil::getDirName(const char *Path) {
193176
return Tmp;
194177
}
195178

196-
std::filesystem::path OSUtil::getCurrentDSODirPath() {
197-
wchar_t Path[MAX_PATH];
198-
auto Handle = getOSModuleHandle(reinterpret_cast<void *>(&getCurrentDSODir));
199-
DWORD Ret = GetModuleFileName(
200-
reinterpret_cast<HMODULE>(ExeModuleHandle == Handle ? 0 : Handle),
201-
reinterpret_cast<LPWSTR>(&Path), sizeof(Path));
202-
assert(Ret < sizeof(Path) && "Path is longer than PATH_MAX?");
203-
assert(Ret > 0 && "GetModuleFileName failed");
204-
(void)Ret;
205-
206-
BOOL RetCode = PathRemoveFileSpec(reinterpret_cast<LPWSTR>(&Path));
207-
assert(RetCode && "PathRemoveFileSpec failed");
208-
(void)RetCode;
209-
210-
return std::filesystem::path(Path);
211-
}
212-
213179
#elif defined(__SYCL_RT_OS_DARWIN)
214180
std::string OSUtil::getCurrentDSODir() {
215181
auto CurrentFunc = reinterpret_cast<const void *>(&getCurrentDSODir);

sycl/source/detail/pi.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#include <sycl/detail/stl_type_traits.hpp>
2525
#include <sycl/version.hpp>
2626

27+
#if _WIN32
28+
#include <sycl/detail/windows_os_utils.hpp>
29+
#endif
30+
31+
2732
#include <bitset>
2833
#include <cstdarg>
2934
#include <cstring>
@@ -453,7 +458,7 @@ static void initializePlugins(std::vector<PluginPtr> &Plugins) {
453458

454459
#if _WIN32
455460
std::filesystem::path LibSYCLDir =
456-
sycl::detail::OSUtil::getCurrentDSODirPath();
461+
sycl::detail::getCurrentDSODirPath();
457462
#else
458463
const std::string LibSYCLDir =
459464
sycl::detail::OSUtil::getCurrentDSODir() + sycl::detail::OSUtil::DirSep;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===--- windows_os_utils.cpp - OS utilities implementation for Windows ---===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <Windows.h>
10+
#include <direct.h>
11+
#include <filesystem>
12+
#include <malloc.h>
13+
#include <shlwapi.h>
14+
#include <sycl/detail/windows_os_utils.hpp>
15+
#include <cassert>
16+
17+
namespace sycl {
18+
inline namespace _V1 {
19+
namespace detail {
20+
21+
22+
OSModuleHandle getOSModuleHandle(const void *VirtAddr) {
23+
HMODULE PhModule;
24+
DWORD Flag = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
25+
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
26+
auto LpModuleAddr = reinterpret_cast<LPCSTR>(VirtAddr);
27+
if (!GetModuleHandleExA(Flag, LpModuleAddr, &PhModule)) {
28+
// Expect the caller to check for zero and take
29+
// necessary action
30+
return 0;
31+
}
32+
if (PhModule == GetModuleHandleA(nullptr))
33+
return ExeModuleHandle;
34+
return reinterpret_cast<OSModuleHandle>(PhModule);
35+
}
36+
37+
std::filesystem::path getCurrentDSODirPath() {
38+
wchar_t Path[MAX_PATH];
39+
auto Handle = getOSModuleHandle(reinterpret_cast<void *>(&getCurrentDSODirPath));
40+
DWORD Ret = GetModuleFileName(
41+
reinterpret_cast<HMODULE>(ExeModuleHandle == Handle ? 0 : Handle),
42+
reinterpret_cast<LPWSTR>(&Path), sizeof(Path));
43+
assert(Ret < sizeof(Path) && "Path is longer than PATH_MAX?");
44+
assert(Ret > 0 && "GetModuleFileName failed");
45+
(void)Ret;
46+
47+
BOOL RetCode = PathRemoveFileSpec(reinterpret_cast<LPWSTR>(&Path));
48+
assert(RetCode && "PathRemoveFileSpec failed");
49+
(void)RetCode;
50+
51+
return std::filesystem::path(Path);
52+
}
53+
54+
55+
} // namespace detail
56+
} // namespace _V1
57+
} // namespace sycl

0 commit comments

Comments
 (0)