DPC++ Runtime
Runtime libraries for oneAPI DPC++
windows_pi.cpp
Go to the documentation of this file.
1 //==---------------- windows_pi.cpp ----------------------------------------==//
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 <sycl/backend.hpp>
10 #include <sycl/detail/defines.hpp>
11 
12 #include <cassert>
13 #include <string>
14 #include <windows.h>
15 #include <winreg.h>
16 
18 #include "pi_win_proxy_loader.hpp"
19 
20 namespace sycl {
21 inline namespace _V1 {
22 namespace detail {
23 namespace pi {
24 
25 void *loadOsLibrary(const std::string &LibraryPath) {
26  // Tells the system to not display the critical-error-handler message box.
27  // Instead, the system sends the error to the calling process.
28  // This is crucial for graceful handling of shared libs that can't be
29  // loaded, e.g. due to missing native run-times.
30 
31  UINT SavedMode = SetErrorMode(SEM_FAILCRITICALERRORS);
32  // Exclude current directory from DLL search path
33  if (!SetDllDirectoryA("")) {
34  assert(false && "Failed to update DLL search path");
35  }
36 
37  auto Result = (void *)LoadLibraryExA(LibraryPath.c_str(), NULL, NULL);
38  (void)SetErrorMode(SavedMode);
39  if (!SetDllDirectoryA(nullptr)) {
40  assert(false && "Failed to restore DLL search path");
41  }
42 
43  return Result;
44 }
45 
46 void *loadOsPluginLibrary(const std::string &PluginPath) {
47  // We fetch the preloaded plugin from the pi_win_proxy_loader.
48  // The proxy_loader handles any required error suppression.
49  auto Result = getPreloadedPlugin(PluginPath);
50 
51  return Result;
52 }
53 
54 int unloadOsLibrary(void *Library) {
55  return (int)FreeLibrary((HMODULE)Library);
56 }
57 
58 int unloadOsPluginLibrary(void *Library) {
59  // The mock plugin does not have an associated library, so we allow nullptr
60  // here to avoid it trying to free a non-existent library.
61  if (!Library)
62  return 1;
63  return (int)FreeLibrary((HMODULE)Library);
64 }
65 
66 void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName) {
67  return reinterpret_cast<void *>(
68  GetProcAddress((HMODULE)Library, FunctionName.c_str()));
69 }
70 
71 static std::filesystem::path getCurrentDSODirPath() {
72  wchar_t Path[MAX_PATH];
73  auto Handle =
74  getOSModuleHandle(reinterpret_cast<void *>(&getCurrentDSODirPath));
75  DWORD Ret = GetModuleFileName(
76  reinterpret_cast<HMODULE>(ExeModuleHandle == Handle ? 0 : Handle), Path,
77  MAX_PATH);
78  assert(Ret < MAX_PATH && "Path is longer than MAX_PATH?");
79  assert(Ret > 0 && "GetModuleFileName failed");
80  (void)Ret;
81 
82  BOOL RetCode = PathRemoveFileSpec(Path);
83  assert(RetCode && "PathRemoveFileSpec failed");
84  (void)RetCode;
85 
86  return std::filesystem::path(Path);
87 }
88 
89 // Load plugins corresponding to provided list of plugin names.
90 std::vector<std::tuple<std::string, backend, void *>>
91 loadPlugins(const std::vector<std::pair<std::string, backend>> &&PluginNames) {
92  std::vector<std::tuple<std::string, backend, void *>> LoadedPlugins;
93  const std::filesystem::path LibSYCLDir = getCurrentDSODirPath();
94 
95  for (auto &PluginName : PluginNames) {
96  void *Library = getPreloadedPlugin(LibSYCLDir / PluginName.first);
97  LoadedPlugins.push_back(std::make_tuple(
98  std::move(PluginName.first), std::move(PluginName.second), Library));
99  }
100 
101  return LoadedPlugins;
102 }
103 
104 } // namespace pi
105 } // namespace detail
106 } // namespace _V1
107 } // namespace sycl
int unloadOsLibrary(void *Library)
Definition: posix_pi.cpp:37
void * loadOsPluginLibrary(const std::string &Library)
Definition: posix_pi.cpp:33
void * loadOsLibrary(const std::string &Library)
Definition: posix_pi.cpp:20
static std::filesystem::path getCurrentDSODirPath()
Definition: windows_pi.cpp:71
void * getOsLibraryFuncAddress(void *Library, const std::string &FunctionName)
Definition: posix_pi.cpp:47
int unloadOsPluginLibrary(void *Library)
Definition: posix_pi.cpp:39
std::vector< std::tuple< std::string, backend, void * > > loadPlugins(const std::vector< std::pair< std::string, backend >> &&PluginNames)
Definition: posix_pi.cpp:53
constexpr tuple< Ts... > make_tuple(Ts... Args)
Definition: tuple.hpp:35
Definition: access.hpp:18
OSModuleHandle getOSModuleHandle(const void *VirtAddr)
constexpr OSModuleHandle ExeModuleHandle