From 3b1e9dc52f03c3df9ef0ca5407007cf54c7a353c Mon Sep 17 00:00:00 2001 From: Satyanvesh Dittakavi Date: Fri, 22 Jul 2022 13:46:02 +0000 Subject: [PATCH] SWDEV-2 - Fix hiprtcCompileProgram - hiprtcCompileProgram should create isa with temp name when program name is not provided along with --save-temps option Change-Id: I47974e2ac56336c851531622ff7d2a3ffb53455e [ROCm/clr commit: 958b97b4334a5062095d84f86e7a10c6180630a2] --- projects/clr/hipamd/src/hiprtc/hiprtc.cpp | 7 +++- .../hipamd/src/hiprtc/hiprtcComgrHelper.cpp | 35 +++++++++++++++++-- .../hipamd/src/hiprtc/hiprtcComgrHelper.hpp | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/projects/clr/hipamd/src/hiprtc/hiprtc.cpp b/projects/clr/hipamd/src/hiprtc/hiprtc.cpp index 01bb208bce..6d433bd5eb 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtc.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtc.cpp @@ -86,7 +86,12 @@ hiprtcResult hiprtcCreateProgram(hiprtcProgram* prog, const char* src, const cha HIPRTC_RETURN(HIPRTC_ERROR_PROGRAM_CREATION_FAILURE); } - if (!rtcProgram->addSource(std::string(src), std::string("CompileSource"))) { + if (name == nullptr || strlen(name) == 0) { + progName = "CompileSourceXXXXXX"; + hiprtc::helpers::GenerateUniqueFileName(progName); + } + + if (!rtcProgram->addSource(std::string(src), progName)) { delete rtcProgram; HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT); } diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp index e131c58cfc..c9c7edec8f 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp @@ -21,6 +21,9 @@ THE SOFTWARE. */ #include "hiprtcComgrHelper.hpp" +#if defined(_WIN32) + #include +#endif namespace hiprtc { @@ -345,9 +348,26 @@ bool createExecutable(const amd_comgr_data_set_t linkInputs, const std::string& return true; } +void GenerateUniqueFileName(std::string &name) { +#if !defined(_WIN32) + char *name_template = const_cast(name.c_str()); + int temp_fd = mkstemp(name_template); +#else + char *name_template = new char[name.length()+1]; + strcpy_s(name_template, name.length()+1, name.data()); + int sizeinchars = strnlen(name_template, 20) + 1; + _mktemp_s(name_template, sizeinchars); +#endif + name = name_template; +#if !defined(_WIN32) + unlink(name_template); + close(temp_fd); +#endif +} + bool dumpIsaFromBC(const amd_comgr_data_set_t isaInputs, const std::string& isa, std::vector& exeOptions, std::string name, std::string& buildLog) { - if (name.size() == 0) return false; + amd_comgr_action_info_t action; if (auto res = createAction(action, exeOptions, isa); res != AMD_COMGR_STATUS_SUCCESS) { @@ -376,7 +396,18 @@ bool dumpIsaFromBC(const amd_comgr_data_set_t isaInputs, const std::string& isa, return false; } - auto isaFileName = name + ".s"; + if (name.size() == 0) { + // Generate a unique name if the program name is not specified by the user + name = std::string("hiprtcXXXXXX"); + GenerateUniqueFileName(name); + } + std::string isaName = isa; +#if defined(_WIN32) + // Replace special charaters that are not supported by Windows FS. + std::replace(isaName.begin(), isaName.end(), ':', '@'); +#endif + + auto isaFileName = name + std::string("-hip-") + isaName + ".s"; std::ofstream f(isaFileName.c_str(), std::ios::trunc | std::ios::binary); if (f.is_open()) { f.write(isaOutput.data(), isaOutput.size()); diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp index c8abb17707..3d6beeadb2 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp @@ -55,5 +55,6 @@ std::string handleMangledName(std::string loweredName); bool fillMangledNames(std::vector& executable, std::vector& mangledNames); bool getDemangledNames(const std::vector& mangledNames, std::map& demangledNames); +void GenerateUniqueFileName(std::string &name); } // namespace helpers } // namespace hiprtc