From 347e36e31b66cc6c5cf2a29568bdbf53907b1873 Mon Sep 17 00:00:00 2001 From: kjayapra-amd Date: Sun, 3 May 2020 23:01:18 -0400 Subject: [PATCH] SWDEV-232464 - Memory Map modules loaded via file from hipModuleLoad Change-Id: I0e644a161c8000abe1b07fbec72de09f1c0a4b18 --- rocclr/os/os.cpp | 0 rocclr/os/os.hpp | 3 +++ rocclr/os/os_posix.cpp | 39 ++++++++++++++++++++++++++++++++++ rocclr/os/os_win32.cpp | 42 +++++++++++++++++++++++++++++++++++++ rocclr/platform/program.cpp | 4 ++++ rocclr/platform/program.hpp | 13 +++++++++--- 6 files changed, 98 insertions(+), 3 deletions(-) mode change 100644 => 100755 rocclr/os/os.cpp mode change 100644 => 100755 rocclr/os/os.hpp mode change 100644 => 100755 rocclr/os/os_posix.cpp mode change 100644 => 100755 rocclr/os/os_win32.cpp mode change 100644 => 100755 rocclr/platform/program.cpp mode change 100644 => 100755 rocclr/platform/program.hpp diff --git a/rocclr/os/os.cpp b/rocclr/os/os.cpp old mode 100644 new mode 100755 diff --git a/rocclr/os/os.hpp b/rocclr/os/os.hpp old mode 100644 new mode 100755 index baf5017e5d..bb7fa2ccdc --- a/rocclr/os/os.hpp +++ b/rocclr/os/os.hpp @@ -91,6 +91,9 @@ class Os : AllStatic { #endif }; + static bool MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size); + static bool MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size); + private: static const size_t FILE_PATH_MAX_LENGTH = 1024; diff --git a/rocclr/os/os_posix.cpp b/rocclr/os/os_posix.cpp old mode 100644 new mode 100755 index c854d1a7e1..df20ccf2c0 --- a/rocclr/os/os_posix.cpp +++ b/rocclr/os/os_posix.cpp @@ -707,6 +707,45 @@ void Os::getAppPathAndFileName(std::string& appName, std::string& appPathAndName return; } +bool Os::MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size) { + if(munmap(const_cast(mmap_ptr), mmap_size) != 0) { + return false; + } + + return true; +} + +bool Os::MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size) { + if ((mmap_ptr == nullptr) || (mmap_size == nullptr)) { + return false; + } + + FILE* fp = fopen(fname, "r"); + if (fp == nullptr) { + return false; + } + + int fd = fileno(fp); + if (fd < 0 ) { + fclose(fp); + return false; + } + + fseek(fp, 0L, SEEK_END); + *mmap_size = ftell(fp); + fseek(fp, 0L, SEEK_SET); + + *mmap_ptr = mmap(NULL, *mmap_size, PROT_READ, MAP_SHARED, fd, 0); + + fclose(fp); + + if (*mmap_ptr == nullptr) { + return false; + } + + return true; +} + } // namespace amd #endif // !defined(_WIN32) && !defined(__CYGWIN__) diff --git a/rocclr/os/os_win32.cpp b/rocclr/os/os_win32.cpp old mode 100644 new mode 100755 index dbb81cdec8..8045f53b45 --- a/rocclr/os/os_win32.cpp +++ b/rocclr/os/os_win32.cpp @@ -820,6 +820,48 @@ void Os::getAppPathAndFileName(std::string& appName, std::string& appPathAndName delete[] buff; return; } + +bool Os::MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size) { + if(!UnmapViewOfFile(mmap_ptr)) { + return false; + } + + return true; +} + +bool Os::MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size) { + if ((mmap_ptr == nullptr) || (mmap_size == nullptr)) { + return false; + } + + HANDLE map_handle = INVALID_HANDLE_VALUE; + HANDLE file_handle = INVALID_HANDLE_VALUE; + + file_handle = CreateFileA(fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_READONLY, NULL); + if (file_handle == INVALID_HANDLE_VALUE) { + return false; + } + + map_handle = CreateFileMappingA(file_handle, NULL, PAGE_READONLY, 0, 0, NULL); + if (map_handle == INVALID_HANDLE_VALUE) { + CloseHandle(file_handle); + return false; + } + + *mmap_size = GetFileSize(file_handle, NULL); + *mmap_ptr = MapViewOfFile(map_handle, FILE_MAP_READ, 0,0,0); + + CloseHandle(file_handle); + CloseHandle(map_handle); + + if (*mmap_ptr == nullptr) { + return false; + } + + return true; +} + } // namespace amd #endif // _WIN32 || __CYGWIN__ diff --git a/rocclr/platform/program.cpp b/rocclr/platform/program.cpp old mode 100644 new mode 100755 index c961cba550..582b56ced1 --- a/rocclr/platform/program.cpp +++ b/rocclr/platform/program.cpp @@ -67,6 +67,10 @@ Program::~Program() { } } + if (std::get<1>(mmap_) > 0) { + amd::Os::MemoryUnmapFile(std::get<0>(mmap_), std::get<1>(mmap_)); + } + delete symbolTable_; //! @todo Make sure we have destroyed all CPU specific objects } diff --git a/rocclr/platform/program.hpp b/rocclr/platform/program.hpp old mode 100644 new mode 100755 index ac31936fde..ec71c6ff91 --- a/rocclr/platform/program.hpp +++ b/rocclr/platform/program.hpp @@ -80,6 +80,7 @@ class Context; //! A collection of binaries for devices in the associated context. class Program : public RuntimeObject { public: + typedef std::pair mmap_t; typedef std::tuple binary_t; typedef std::set devicelist_t; typedef std::unordered_map devicebinary_t; @@ -113,6 +114,7 @@ class Program : public RuntimeObject { //! The device program objects included in this program deviceprograms_t devicePrograms_; devicelist_t deviceList_; + const mmap_t mmap_; std::string programLog_; //!< Log for parsing options, etc. @@ -133,11 +135,16 @@ class Program : public RuntimeObject { sourceCode_(sourceCode), language_(language), symbolTable_(NULL), - programLog_() {} + mmap_({nullptr, 0}), + programLog_() { + } //! Construct a new program associated with a context. - Program(Context& context, Language language = Binary) - : context_(context), language_(language), symbolTable_(NULL) {} + Program(Context& context, Language language = Binary, + const void* mmap_ptr = nullptr, const size_t mmap_size = 0) + : context_(context), language_(language), + mmap_({mmap_ptr, mmap_size}), + symbolTable_(NULL) {} //! Returns context, associated with the current program. const Context& context() const { return context_(); }