From 0d4efc9f0fc715bdb986221432e2ffd0c224ca03 Mon Sep 17 00:00:00 2001 From: haoyuan2 Date: Tue, 5 Apr 2022 17:26:42 -0700 Subject: [PATCH] SWDEV-328274 - Move DLLMain from VDI layer to HIP/OCL layers Change-Id: Ibd3388f0981672cd209d7370f8a28b2883abdea2 [ROCm/clr commit: cb4aabd2856a000d80964e58dbd3d4d5237ff944] --- projects/clr/hipamd/src/CMakeLists.txt | 3 +- projects/clr/hipamd/src/hip_device.cpp | 6 +-- projects/clr/hipamd/src/hip_runtime.cpp | 60 +++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 projects/clr/hipamd/src/hip_runtime.cpp diff --git a/projects/clr/hipamd/src/CMakeLists.txt b/projects/clr/hipamd/src/CMakeLists.txt index c6e0c6a59a..f81296d340 100644 --- a/projects/clr/hipamd/src/CMakeLists.txt +++ b/projects/clr/hipamd/src/CMakeLists.txt @@ -129,7 +129,8 @@ if(WIN32) target_sources(amdhip64 PRIVATE cl_d3d9.cpp cl_d3d10.cpp - cl_d3d11.cpp) + cl_d3d11.cpp + hip_runtime.cpp) endif() if(BUILD_SHARED_LIBS) diff --git a/projects/clr/hipamd/src/hip_device.cpp b/projects/clr/hipamd/src/hip_device.cpp index 7a2a1c0e42..de8d1a38c1 100644 --- a/projects/clr/hipamd/src/hip_device.cpp +++ b/projects/clr/hipamd/src/hip_device.cpp @@ -90,16 +90,12 @@ Device::~Device() { } -namespace amd { - -void shutDown() { +void ihipDestroyDevice() { for (auto deviceHandle : g_devices) { delete deviceHandle; } } -} // namespace amd - hipError_t ihipDeviceGet(hipDevice_t* device, int deviceId) { if (deviceId < 0 || static_cast(deviceId) >= g_devices.size() || device == nullptr) { return hipErrorInvalidDevice; diff --git a/projects/clr/hipamd/src/hip_runtime.cpp b/projects/clr/hipamd/src/hip_runtime.cpp new file mode 100644 index 0000000000..4c877806ac --- /dev/null +++ b/projects/clr/hipamd/src/hip_runtime.cpp @@ -0,0 +1,60 @@ +/* Copyright (c) 2008 - 2022 Advanced Micro Devices, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. */ + +#include "thread/thread.hpp" + +#include +#include + +void ihipDestroyDevice(); + +#ifdef DEBUG +static int reportHook(int reportType, char* message, int* returnValue) { + if (returnValue) { + *returnValue = 1; + } + std::cerr << message; + ::exit(3); + return TRUE; +} +#endif // DEBUG + +extern "C" BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) { + switch (reason) { + case DLL_PROCESS_ATTACH: +#ifdef DEBUG + if (!::getenv("AMD_OCL_ENABLE_MESSAGE_BOX")) { + _CrtSetReportHook(reportHook); + _set_error_mode(_OUT_TO_STDERR); + } +#endif // DEBUG + break; + case DLL_PROCESS_DETACH: + ihipDestroyDevice(); + break; + case DLL_THREAD_DETACH: { + amd::Thread* thread = amd::Thread::current(); + delete thread; + } break; + default: + break; + } + return true; +}