diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp index ce039ac4c6..debecbe910 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.cpp @@ -585,86 +585,6 @@ 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) { @@ -726,7 +646,8 @@ 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); @@ -994,9 +915,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); @@ -1004,8 +925,7 @@ 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 ff51f668af..d34c9264c6 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcComgrHelper.hpp @@ -41,9 +41,6 @@ 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); @@ -57,8 +54,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 1323dc9148..46a034c828 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp @@ -116,6 +116,7 @@ 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; @@ -260,6 +261,10 @@ bool RTCCompileProgram::transformOptions(std::vector& compile_optio i = "--offload-arch=" + val; continue; } + if (i == "--save-temps") { + settings_.dumpISA = true; + continue; + } } // Removed consumed options @@ -295,27 +300,78 @@ 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 (fgpu_rdc_) { - if (!compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) { - LogError("Error in hiprtc: unable to compile source to bitcode"); + 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"); return false; } - } else { - LogInfo("Using the new path of comgr"); - if (!compileToExecutable(compile_input_, isa_, compileOpts, build_log_, executable_)) { - LogError("Failing to compile to realloc"); + + 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"); 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()) { - auto& compile_step_output = fgpu_rdc_ ? LLVMBitcode_ : executable_; - if (!fillMangledNames(compile_step_output, mangled_names_, fgpu_rdc_)) { + if (!fillMangledNames(executable_, mangled_names_, false)) { LogError("Error in hiprtc: unable to fill mangled names"); return false; } @@ -324,7 +380,6 @@ bool RTCCompileProgram::compile(const std::vector& options, bool fg return true; } - void RTCCompileProgram::stripNamedExpression(std::string& strippedName) { if (strippedName.back() == ')') { strippedName.pop_back(); @@ -398,6 +453,7 @@ 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 7e4d123992..a1965d1b19 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp @@ -107,6 +107,7 @@ static void crashWithMessage(std::string message) { } struct Settings { + bool dumpISA{false}; bool offloadArchProvided{false}; };