From c8a1e60effd8d6a667c6ec9f9cf3f45cdf091794 Mon Sep 17 00:00:00 2001 From: foreman Date: Wed, 4 Dec 2019 14:22:03 -0500 Subject: [PATCH] P4 to Git Change 2040890 by skudchad@skudchad_test2_win_opencl on 2019/12/04 14:16:51 SWDEV-203814 - HIPRTC Inprocess runtime changes(part2) ReviewBoardURL = http://ocltc.amd.com/reviews/r/18342/diff/ Affected files ... ... //depot/stg/opencl/drivers/opencl/api/hip/hip_hcc.def.in#37 edit ... //depot/stg/opencl/drivers/opencl/api/hip/hip_hcc.map.in#35 edit ... //depot/stg/opencl/drivers/opencl/api/hip/hip_module.cpp#50 edit ... //depot/stg/opencl/drivers/opencl/api/hip/hip_rtc.cpp#8 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/devprogram.cpp#73 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/devprogram.hpp#38 edit [ROCm/clr commit: 6e9e96493f335657c996484136229e9a5c38ff08] --- projects/clr/hipamd/api/hip/hip_hcc.def.in | 1 + projects/clr/hipamd/api/hip/hip_hcc.map.in | 2 + projects/clr/hipamd/api/hip/hip_module.cpp | 10 ++ projects/clr/hipamd/api/hip/hip_rtc.cpp | 122 +++++++++++++++++++-- 4 files changed, 126 insertions(+), 9 deletions(-) diff --git a/projects/clr/hipamd/api/hip/hip_hcc.def.in b/projects/clr/hipamd/api/hip/hip_hcc.def.in index 145e8ef2cc..6aba1f5a25 100644 --- a/projects/clr/hipamd/api/hip/hip_hcc.def.in +++ b/projects/clr/hipamd/api/hip/hip_hcc.def.in @@ -129,6 +129,7 @@ hipLaunchCooperativeKernelMultiDevice hipHccModuleLaunchKernel hipModuleLoad hipModuleLoadData +hipModuleLoadDataEx hipModuleUnload hipOccupancyMaxActiveBlocksPerMultiprocessor hipOccupancyMaxActiveBlocksPerMultiprocessorWithFlags diff --git a/projects/clr/hipamd/api/hip/hip_hcc.map.in b/projects/clr/hipamd/api/hip/hip_hcc.map.in index 55defef49a..f80d7db23a 100644 --- a/projects/clr/hipamd/api/hip/hip_hcc.map.in +++ b/projects/clr/hipamd/api/hip/hip_hcc.map.in @@ -129,6 +129,7 @@ global: hipLaunchCooperativeKernelMultiDevice; hipModuleLoad; hipModuleLoadData; + hipModuleLoadDataEx; hipModuleUnload; hipOccupancyMaxActiveBlocksPerMultiprocessor; hipOccupancyMaxActiveBlocksPerMultiprocessorWithFlags; @@ -180,6 +181,7 @@ global: hiprtcGetErrorString; extern "C++" { hip_impl::hipLaunchKernelGGLImpl*; + hip_impl::demangle*; hipCreateTextureObject*; hipDestroyTextureObject*; hipGetTextureObjectResourceDesc*; diff --git a/projects/clr/hipamd/api/hip/hip_module.cpp b/projects/clr/hipamd/api/hip/hip_module.cpp index bdd792e8a0..c13a077a0e 100644 --- a/projects/clr/hipamd/api/hip/hip_module.cpp +++ b/projects/clr/hipamd/api/hip/hip_module.cpp @@ -110,6 +110,16 @@ hipError_t hipModuleLoadData(hipModule_t *module, const void *image) HIP_RETURN(ihipModuleLoadData(module, image)); } +hipError_t hipModuleLoadDataEx(hipModule_t *module, const void *image, + unsigned int numOptions, hipJitOption* options, + void** optionsValues) +{ + /* TODO: Pass options to Program */ + HIP_INIT_API(hipModuleLoadData, module, image); + + HIP_RETURN(ihipModuleLoadData(module, image)); +} + extern bool __hipExtractCodeObjectFromFatBinary(const void* data, const std::vector& devices, std::vector>& code_objs); diff --git a/projects/clr/hipamd/api/hip/hip_rtc.cpp b/projects/clr/hipamd/api/hip/hip_rtc.cpp index 9cf3060418..4e2c99e6df 100644 --- a/projects/clr/hipamd/api/hip/hip_rtc.cpp +++ b/projects/clr/hipamd/api/hip/hip_rtc.cpp @@ -24,7 +24,10 @@ THE SOFTWARE. #include "hiprtc_internal.hpp" #include #include "platform/program.hpp" -#include + +extern "C" char * __cxa_demangle(const char *mangled_name, char *output_buffer, + size_t *length, int *status); + namespace hiprtc { thread_local hiprtcResult g_lastRtcError = HIPRTC_SUCCESS; @@ -37,16 +40,18 @@ private: ProgramState() : lock_("Guards program state") {} ~ProgramState() {} - +public: std::unordered_map, std::vector>> progHeaders_; - std::vector nameExpresssion_; -public: + + std::map> nameExpresssion_; + static ProgramState& instance(); void createProgramHeaders(amd::Program* program, int numHeaders, const char** headers, const char** headerNames); void getProgramHeaders(amd::Program* program, int* numHeaders, char** headers, char ** headerNames); uint32_t addNameExpression(const char* name_expression); + char* getLoweredName(const char* name_expression); }; ProgramState* ProgramState::programState_ = nullptr; @@ -82,13 +87,86 @@ void ProgramState::getProgramHeaders(amd::Program* program, int* numHeaders, } } - uint32_t ProgramState::addNameExpression(const char* name_expression) { amd::ScopedLock lock(lock_); - nameExpresssion_.emplace_back(name_expression); + + // Strip clean of any '(' or ')' or '&' + std::string strippedName(name_expression); + if (strippedName.back() == ')') { + strippedName.pop_back(); + strippedName.erase(0, strippedName.find('(')); + } + if (strippedName.front() == '&') { + strippedName.erase(0, 1); + } + auto it = nameExpresssion_.find(name_expression); + if (it == nameExpresssion_.end()) { + nameExpresssion_.insert(std::pair> + (name_expression, std::make_pair(strippedName,""))); + } return nameExpresssion_.size(); } +namespace hip_impl { + +inline std::string demangle(const char* x) { +#ifdef ATI_OS_LINUX + if (!x) { + return {}; + } + + int s = 0; + std::unique_ptr tmp{ + __cxa_demangle(x, nullptr, nullptr, &s), + std::free}; + if (s != 0) { + return {}; + } + + return tmp.get(); +#else + return {}; +#endif +} +} // hip_impl + +std::string handleMangledName(std::string name) { + std::string demangled; + demangled = hip_impl::demangle(name.c_str()); + + if (demangled.empty()) { + return name; + } + + if (demangled.find(".kd") != std::string::npos) { + return {}; + } + + if (demangled.find("void ") == 0) { + demangled.erase(0, strlen("void ")); + } + + auto dx{demangled.find_first_of("(<")}; + + if (dx == std::string::npos) { + return demangled; + } + + if (demangled[dx] == '<') { + auto cnt{1u}; + do { + ++dx; + cnt += (demangled[dx] == '<') ? 1 : ((demangled[dx] == '>') ? -1 : 0); + } while (cnt); + + demangled.erase(++dx); + } else { + demangled.erase(dx); + } + + return demangled; +} + const char* hiprtcGetErrorString(hiprtcResult x) { switch (x) { @@ -192,13 +270,39 @@ hiprtcResult hiprtcAddNameExpression(hiprtcProgram prog, const char* name_expres } hiprtcResult hiprtcGetLoweredName(hiprtcProgram prog, const char* name_expression, - const char** loweredNames) { - HIPRTC_INIT_API(prog, name_expression, loweredNames); + const char** loweredName) { + HIPRTC_INIT_API(prog, name_expression, loweredName); - if (name_expression == nullptr || loweredNames == nullptr) { + if (name_expression == nullptr || loweredName == nullptr) { HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT); } + amd::Program* program = as_amd(reinterpret_cast(prog)); + + device::Program* dev_program + = program->getDeviceProgram(*hip::getCurrentContext()->devices()[0]); + + auto it = ProgramState::instance().nameExpresssion_.find(name_expression); + if (it == ProgramState::instance().nameExpresssion_.end()) { + return HIPRTC_ERROR_NAME_EXPRESSION_NOT_VALID; + } + + std::string strippedName = it->second.first; + std::vector mangledNames; + + if (!dev_program->getLoweredNames(&mangledNames)) { + HIPRTC_RETURN(HIPRTC_ERROR_COMPILATION); + } + + for (auto &name : mangledNames) { + std::string demangledName = handleMangledName(name); + if (demangledName == strippedName) { + it->second.second.assign(name); + } + } + + *loweredName = it->second.second.c_str(); + HIPRTC_RETURN(HIPRTC_SUCCESS); }