From bd86f17082b0e4f33ea1b044419b40206b3dc2eb Mon Sep 17 00:00:00 2001 From: Jatin Chaudhary Date: Thu, 18 Jan 2024 00:44:39 +0000 Subject: [PATCH] SWDEV-458637 - 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: I8089cbef681e079702fefc2d2085a23bc3578d02 --- hipamd/src/hiprtc/hiprtcComgrHelper.cpp | 91 +++++++++++++++++++++++-- hipamd/src/hiprtc/hiprtcComgrHelper.hpp | 8 ++- hipamd/src/hiprtc/hiprtcInternal.cpp | 77 +++------------------ hipamd/src/hiprtc/hiprtcInternal.hpp | 16 ++--- 4 files changed, 109 insertions(+), 83 deletions(-) diff --git a/hipamd/src/hiprtc/hiprtcComgrHelper.cpp b/hipamd/src/hiprtc/hiprtcComgrHelper.cpp index debecbe91..d213afe0f 100644 --- a/hipamd/src/hiprtc/hiprtcComgrHelper.cpp +++ b/hipamd/src/hiprtc/hiprtcComgrHelper.cpp @@ -585,6 +585,87 @@ 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::vector& linkOptions, 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, linkOptions, 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 +727,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 +995,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 +1005,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/hipamd/src/hiprtc/hiprtcComgrHelper.hpp b/hipamd/src/hiprtc/hiprtcComgrHelper.hpp index d34c9264c..05e1c013d 100644 --- a/hipamd/src/hiprtc/hiprtcComgrHelper.hpp +++ b/hipamd/src/hiprtc/hiprtcComgrHelper.hpp @@ -41,6 +41,10 @@ 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::vector& linkOptions, 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 +58,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/hipamd/src/hiprtc/hiprtcInternal.cpp b/hipamd/src/hiprtc/hiprtcInternal.cpp index 46a034c82..b4d10e0ee 100644 --- a/hipamd/src/hiprtc/hiprtcInternal.cpp +++ b/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,28 @@ 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, link_options_, 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 +325,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 +399,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/hipamd/src/hiprtc/hiprtcInternal.hpp b/hipamd/src/hiprtc/hiprtcInternal.hpp index a1965d1b1..5e196b7de 100644 --- a/hipamd/src/hiprtc/hiprtcInternal.hpp +++ b/hipamd/src/hiprtc/hiprtcInternal.hpp @@ -76,8 +76,9 @@ static amd::Monitor g_hiprtcInitlock{"hiprtcInit lock"}; #define HIPRTC_INIT_API_INTERNAL(...) \ amd::Thread* thread = amd::Thread::current(); \ if (!VDI_CHECK_THREAD(thread)) { \ - ClPrint(amd::LOG_NONE, amd::LOG_ALWAYS, "An internal error has occurred." \ - " This may be due to insufficient memory."); \ + ClPrint(amd::LOG_NONE, amd::LOG_ALWAYS, \ + "An internal error has occurred." \ + " This may be due to insufficient memory."); \ HIPRTC_RETURN(HIPRTC_ERROR_INTERNAL_ERROR); \ } \ amd::ScopedLock lock(g_hiprtcInitlock); \ @@ -107,7 +108,6 @@ static void crashWithMessage(std::string message) { } struct Settings { - bool dumpISA{false}; bool offloadArchProvided{false}; }; @@ -156,10 +156,8 @@ class RTCCompileProgram : public RTCProgram { bool addBuiltinHeader(); bool transformOptions(std::vector& compile_options); bool findExeOptions(const std::vector& options, - std::vector& exe_options); - void AppendCompileOptions() { - AppendOptions(HIPRTC_COMPILE_OPTIONS_APPEND, &compile_options_); - } + std::vector& exe_options); + void AppendCompileOptions() { AppendOptions(HIPRTC_COMPILE_OPTIONS_APPEND, &compile_options_); } RTCCompileProgram() = delete; RTCCompileProgram(RTCCompileProgram&) = delete; @@ -288,9 +286,7 @@ class RTCLinkProgram : public RTCProgram { bool AddLinkerData(void* image_ptr, size_t image_size, std::string link_file_name, hiprtcJITInputType input_type); bool LinkComplete(void** bin_out, size_t* size_out); - void AppendLinkerOptions() { - AppendOptions(HIPRTC_LINK_OPTIONS_APPEND, &link_options_); - } + void AppendLinkerOptions() { AppendOptions(HIPRTC_LINK_OPTIONS_APPEND, &link_options_); } }; // Thread Local Storage Variables Aggregator Class