Skip to content

Commit

Permalink
SWDEV-458637 - Add new comgr compile to reloc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cjatin authored and yanyao-wang committed May 9, 2024
1 parent 7b75645 commit bd86f17
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 83 deletions.
91 changes: 86 additions & 5 deletions hipamd/src/hiprtc/hiprtcComgrHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,87 @@ bool createAction(amd_comgr_action_info_t& action, std::vector<std::string>& opt
return AMD_COMGR_STATUS_SUCCESS;
}

bool compileToExecutable(const amd_comgr_data_set_t compileInputs, const std::string& isa,
std::vector<std::string>& compileOptions,
std::vector<std::string>& linkOptions, std::string& buildLog,
std::vector<char>& 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<std::string>& compileOptions, std::string& buildLog,
std::vector<char>& LLVMBitcode) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -915,17 +995,18 @@ bool fillMangledNames(std::vector<char>& dataVec, std::map<std::string, std::str
return false;
}

for (auto &it : mangledNames) {
for (auto& it : mangledNames) {
size_t Size;
char *data = const_cast<char*>(it.first.data());
char* data = const_cast<char*>(it.first.data());

if (auto res = amd::Comgr::map_name_expression_to_symbol_name(dataObject, &Size, data, NULL)) {
amd::Comgr::release_data(dataObject);
return false;
}

std::unique_ptr<char[]> 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;
}
Expand Down
8 changes: 6 additions & 2 deletions hipamd/src/hiprtc/hiprtcComgrHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ bool extractByteCodeBinary(const amd_comgr_data_set_t inDataSet,
bool createAction(amd_comgr_action_info_t& action, std::vector<std::string>& 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<std::string>& compileOptions,
std::vector<std::string>& linkOptions, std::string& buildLog,
std::vector<char>& exe);
bool compileToBitCode(const amd_comgr_data_set_t compileInputs, const std::string& isa,
std::vector<std::string>& compileOptions, std::string& buildLog,
std::vector<char>& LLVMBitcode);
Expand All @@ -54,8 +58,8 @@ bool dumpIsaFromBC(const amd_comgr_data_set_t isaInputs, const std::string& isa,
std::vector<std::string>& 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<char>& executable, std::map<std::string, std::string>& mangledNames,
bool isBitcode);
bool fillMangledNames(std::vector<char>& executable,
std::map<std::string, std::string>& mangledNames, bool isBitcode);
void GenerateUniqueFileName(std::string& name);
} // namespace helpers
} // namespace hiprtc
77 changes: 11 additions & 66 deletions hipamd/src/hiprtc/hiprtcInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ bool RTCProgram::findIsa() {

// RTC Compile Program Member Functions
void RTCProgram::AppendOptions(const std::string app_env_var, std::vector<std::string>* options) {

if (options == nullptr) {
LogError("Append options passed is nullptr.");
return;
Expand Down Expand Up @@ -261,10 +260,6 @@ bool RTCCompileProgram::transformOptions(std::vector<std::string>& compile_optio
i = "--offload-arch=" + val;
continue;
}
if (i == "--save-temps") {
settings_.dumpISA = true;
continue;
}
}

// Removed consumed options
Expand Down Expand Up @@ -300,78 +295,28 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& 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<char> 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<std::string> 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<std::string> 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;
}
Expand All @@ -380,6 +325,7 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
return true;
}


void RTCCompileProgram::stripNamedExpression(std::string& strippedName) {
if (strippedName.back() == ')') {
strippedName.pop_back();
Expand Down Expand Up @@ -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<unsigned int*>(&options_vals_ptr[opt_idx]));
Expand Down
16 changes: 6 additions & 10 deletions hipamd/src/hiprtc/hiprtcInternal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down Expand Up @@ -107,7 +108,6 @@ static void crashWithMessage(std::string message) {
}

struct Settings {
bool dumpISA{false};
bool offloadArchProvided{false};
};

Expand Down Expand Up @@ -156,10 +156,8 @@ class RTCCompileProgram : public RTCProgram {
bool addBuiltinHeader();
bool transformOptions(std::vector<std::string>& compile_options);
bool findExeOptions(const std::vector<std::string>& options,
std::vector<std::string>& exe_options);
void AppendCompileOptions() {
AppendOptions(HIPRTC_COMPILE_OPTIONS_APPEND, &compile_options_);
}
std::vector<std::string>& exe_options);
void AppendCompileOptions() { AppendOptions(HIPRTC_COMPILE_OPTIONS_APPEND, &compile_options_); }

RTCCompileProgram() = delete;
RTCCompileProgram(RTCCompileProgram&) = delete;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bd86f17

Please sign in to comment.