From 624a548b9a387042e9e4916e4c396cc52c07a0ad Mon Sep 17 00:00:00 2001 From: Jatin Chaudhary Date: Thu, 18 Jan 2024 00:44:39 +0000 Subject: [PATCH] SWDEV-425605 - Add new comgr compile to reloc use AMD_COMGR_ACTION_COMPILE_SOURCE_TO_RELOCATABLE action to compile source to realoc. Currently we have source->bc, link->bc and bc->realoc. This new action replaces the three steps with one. Change-Id: I6ba551b8d04c7e06f41c4324026e4dcd2db1970f [ROCm/clr commit: ea1487d84d3144af9efdf6e0cb11b55258ed22c9] --- .../hipamd/src/hiprtc/hiprtcComgrHelper.cpp | 90 +++++++++++++++++-- .../hipamd/src/hiprtc/hiprtcComgrHelper.hpp | 7 +- .../clr/hipamd/src/hiprtc/hiprtcInternal.cpp | 76 +++------------- .../clr/hipamd/src/hiprtc/hiprtcInternal.hpp | 1 - 4 files changed, 100 insertions(+), 74 deletions(-) diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp index debecbe910..ce039ac4c6 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp @@ -585,6 +585,86 @@ bool createAction(amd_comgr_action_info_t& action, std::vector& opt return AMD_COMGR_STATUS_SUCCESS; } +bool compileToExecutable(const amd_comgr_data_set_t compileInputs, const std::string& isa, + std::vector& compileOptions, std::string& buildLog, + std::vector& exe) { + amd_comgr_language_t lang = AMD_COMGR_LANGUAGE_HIP; + amd_comgr_action_info_t action; + amd_comgr_data_set_t reloc; + amd_comgr_data_set_t output; + amd_comgr_data_set_t input = compileInputs; + + if (auto res = createAction(action, compileOptions, isa, lang); res != AMD_COMGR_STATUS_SUCCESS) { + return false; + } + + if (auto res = amd::Comgr::create_data_set(&reloc); res != AMD_COMGR_STATUS_SUCCESS) { + amd::Comgr::destroy_action_info(action); + return false; + } + + if (auto res = amd::Comgr::create_data_set(&output); res != AMD_COMGR_STATUS_SUCCESS) { + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(reloc); + return false; + } + + if (auto res = amd::Comgr::do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_TO_RELOCATABLE, action, + input, reloc); + res != AMD_COMGR_STATUS_SUCCESS) { + extractBuildLog(reloc, buildLog); + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(reloc); + amd::Comgr::destroy_data_set(output); + return false; + } + + if (!extractBuildLog(reloc, buildLog)) { + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(reloc); + amd::Comgr::destroy_data_set(output); + return false; + } + + amd::Comgr::destroy_action_info(action); + if (auto res = createAction(action, compileOptions, isa, lang); res != AMD_COMGR_STATUS_SUCCESS) { + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(reloc); + amd::Comgr::destroy_data_set(output); + return false; + } + + if (auto res = amd::Comgr::do_action(AMD_COMGR_ACTION_LINK_RELOCATABLE_TO_EXECUTABLE, action, + reloc, output); + res != AMD_COMGR_STATUS_SUCCESS) { + extractBuildLog(output, buildLog); + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(output); + amd::Comgr::destroy_data_set(reloc); + return false; + } + + if (!extractBuildLog(output, buildLog)) { + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(output); + amd::Comgr::destroy_data_set(reloc); + return false; + } + + if (!extractByteCodeBinary(output, AMD_COMGR_DATA_KIND_EXECUTABLE, exe)) { + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(output); + amd::Comgr::destroy_data_set(reloc); + return false; + } + + // Clean up + amd::Comgr::destroy_action_info(action); + amd::Comgr::destroy_data_set(output); + amd::Comgr::destroy_data_set(reloc); + return true; +} + bool compileToBitCode(const amd_comgr_data_set_t compileInputs, const std::string& isa, std::vector& compileOptions, std::string& buildLog, std::vector& LLVMBitcode) { @@ -646,8 +726,7 @@ bool linkLLVMBitcode(const amd_comgr_data_set_t linkInputs, const std::string& i return false; } - if (auto res = - amd::Comgr::do_action(AMD_COMGR_ACTION_LINK_BC_TO_BC, action, linkInputs, output); + if (auto res = amd::Comgr::do_action(AMD_COMGR_ACTION_LINK_BC_TO_BC, action, linkInputs, output); res != AMD_COMGR_STATUS_SUCCESS) { amd::Comgr::destroy_action_info(action); amd::Comgr::destroy_data_set(output); @@ -915,9 +994,9 @@ bool fillMangledNames(std::vector& dataVec, std::map(it.first.data()); + char* data = const_cast(it.first.data()); if (auto res = amd::Comgr::map_name_expression_to_symbol_name(dataObject, &Size, data, NULL)) { amd::Comgr::release_data(dataObject); @@ -925,7 +1004,8 @@ bool fillMangledNames(std::vector& dataVec, std::map mName(new char[Size]()); - if (auto res = amd::Comgr::map_name_expression_to_symbol_name(dataObject, &Size, data, mName.get())) { + if (auto res = + amd::Comgr::map_name_expression_to_symbol_name(dataObject, &Size, data, mName.get())) { amd::Comgr::release_data(dataObject); return false; } diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp index d34c9264c6..ff51f668af 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp @@ -41,6 +41,9 @@ bool extractByteCodeBinary(const amd_comgr_data_set_t inDataSet, bool createAction(amd_comgr_action_info_t& action, std::vector& options, const std::string& isa, const amd_comgr_language_t lang = AMD_COMGR_LANGUAGE_NONE); +bool compileToExecutable(const amd_comgr_data_set_t compileInputs, const std::string& isa, + std::vector& compileOptions, std::string& buildLog, + std::vector& exe); bool compileToBitCode(const amd_comgr_data_set_t compileInputs, const std::string& isa, std::vector& compileOptions, std::string& buildLog, std::vector& LLVMBitcode); @@ -54,8 +57,8 @@ bool dumpIsaFromBC(const amd_comgr_data_set_t isaInputs, const std::string& isa, std::vector& exeOptions, std::string name, std::string& buildLog); bool demangleName(const std::string& mangledName, std::string& demangledName); std::string handleMangledName(std::string loweredName); -bool fillMangledNames(std::vector& executable, std::map& mangledNames, - bool isBitcode); +bool fillMangledNames(std::vector& executable, + std::map& mangledNames, bool isBitcode); void GenerateUniqueFileName(std::string& name); } // namespace helpers } // namespace hiprtc diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp index 46a034c828..1323dc9148 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp @@ -116,7 +116,6 @@ bool RTCProgram::findIsa() { // RTC Compile Program Member Functions void RTCProgram::AppendOptions(const std::string app_env_var, std::vector* options) { - if (options == nullptr) { LogError("Append options passed is nullptr."); return; @@ -261,10 +260,6 @@ bool RTCCompileProgram::transformOptions(std::vector& compile_optio i = "--offload-arch=" + val; continue; } - if (i == "--save-temps") { - settings_.dumpISA = true; - continue; - } } // Removed consumed options @@ -300,78 +295,27 @@ bool RTCCompileProgram::compile(const std::vector& options, bool fg compileOpts.reserve(compile_options_.size() + options.size() + 2); compileOpts.insert(compileOpts.end(), options.begin(), options.end()); - if (!fgpu_rdc_) { - compileOpts.push_back("-Xclang"); - compileOpts.push_back("-disable-llvm-passes"); - } - if (!transformOptions(compileOpts)) { LogError("Error in hiprtc: unable to transform options"); return false; } - if (!compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) { - LogError("Error in hiprtc: unable to compile source to bitcode"); - return false; - } - - if (fgpu_rdc_ && !mangled_names_.empty()) { - if (!fillMangledNames(LLVMBitcode_, mangled_names_, true)) { - LogError("Error in hiprtc: unable to fill mangled names"); + if (fgpu_rdc_) { + if (!compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) { + LogError("Error in hiprtc: unable to compile source to bitcode"); return false; } - - return true; - } - - std::string linkFileName = "linked"; - if (!addCodeObjData(link_input_, LLVMBitcode_, linkFileName, AMD_COMGR_DATA_KIND_BC)) { - LogError("Error in hiprtc: unable to add linked code object"); - return false; - } - - std::vector LinkedLLVMBitcode; - if (!linkLLVMBitcode(link_input_, isa_, link_options_, build_log_, LinkedLLVMBitcode)) { - LogError("Error in hiprtc: unable to add device libs to linked bitcode"); - return false; - } - - std::string linkedFileName = "LLVMBitcode.bc"; - if (!addCodeObjData(exec_input_, LinkedLLVMBitcode, linkedFileName, AMD_COMGR_DATA_KIND_BC)) { - LogError("Error in hiprtc: unable to add device libs linked code object"); - return false; - } - - std::vector exe_options; - // Find the options passed by the app which can be used during BC to Relocatable phase. - if (!findExeOptions(options, exe_options)) { - LogError("Error in hiprtc: unable to find executable options"); - return false; - } - - std::vector exeOpts(exe_options_); - exeOpts.reserve(exeOpts.size() + exe_options.size() + 2); - // Add these below options by default for optimizations during BC to Relocatable phase. - exeOpts.push_back("-mllvm"); - exeOpts.push_back("-amdgpu-internalize-symbols"); - // User provided options are appended at the end since they can override the above - // default options if necessary - exeOpts.insert(exeOpts.end(), exe_options.begin(), exe_options.end()); - - if (settings_.dumpISA) { - if (!dumpIsaFromBC(exec_input_, isa_, exeOpts, name_, build_log_)) { - LogError("Error in hiprtc: unable to dump isa code"); + } else { + LogInfo("Using the new path of comgr"); + if (!compileToExecutable(compile_input_, isa_, compileOpts, build_log_, executable_)) { + LogError("Failing to compile to realloc"); return false; } } - if (!createExecutable(exec_input_, isa_, exeOpts, build_log_, executable_)) { - LogError("Error in hiprtc: unable to create executable"); - return false; - } - if (!mangled_names_.empty()) { - if (!fillMangledNames(executable_, mangled_names_, false)) { + auto& compile_step_output = fgpu_rdc_ ? LLVMBitcode_ : executable_; + if (!fillMangledNames(compile_step_output, mangled_names_, fgpu_rdc_)) { LogError("Error in hiprtc: unable to fill mangled names"); return false; } @@ -380,6 +324,7 @@ bool RTCCompileProgram::compile(const std::vector& options, bool fg return true; } + void RTCCompileProgram::stripNamedExpression(std::string& strippedName) { if (strippedName.back() == ')') { strippedName.pop_back(); @@ -453,7 +398,6 @@ RTCLinkProgram::RTCLinkProgram(std::string name) : RTCProgram(name) { bool RTCLinkProgram::AddLinkerOptions(unsigned int num_options, hiprtcJIT_option* options_ptr, void** options_vals_ptr) { for (size_t opt_idx = 0; opt_idx < num_options; ++opt_idx) { - switch (options_ptr[opt_idx]) { case HIPRTC_JIT_MAX_REGISTERS: link_args_.max_registers_ = *(reinterpret_cast(&options_vals_ptr[opt_idx])); diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp index a1965d1b19..7e4d123992 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp @@ -107,7 +107,6 @@ static void crashWithMessage(std::string message) { } struct Settings { - bool dumpISA{false}; bool offloadArchProvided{false}; };