Skip to content

Commit bd86f17

Browse files
cjatinyanyao-wang
authored andcommitted
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
1 parent 7b75645 commit bd86f17

4 files changed

Lines changed: 109 additions & 83 deletions

File tree

hipamd/src/hiprtc/hiprtcComgrHelper.cpp

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,87 @@ bool createAction(amd_comgr_action_info_t& action, std::vector<std::string>& opt
585585
return AMD_COMGR_STATUS_SUCCESS;
586586
}
587587

588+
bool compileToExecutable(const amd_comgr_data_set_t compileInputs, const std::string& isa,
589+
std::vector<std::string>& compileOptions,
590+
std::vector<std::string>& linkOptions, std::string& buildLog,
591+
std::vector<char>& exe) {
592+
amd_comgr_language_t lang = AMD_COMGR_LANGUAGE_HIP;
593+
amd_comgr_action_info_t action;
594+
amd_comgr_data_set_t reloc;
595+
amd_comgr_data_set_t output;
596+
amd_comgr_data_set_t input = compileInputs;
597+
598+
if (auto res = createAction(action, compileOptions, isa, lang); res != AMD_COMGR_STATUS_SUCCESS) {
599+
return false;
600+
}
601+
602+
if (auto res = amd::Comgr::create_data_set(&reloc); res != AMD_COMGR_STATUS_SUCCESS) {
603+
amd::Comgr::destroy_action_info(action);
604+
return false;
605+
}
606+
607+
if (auto res = amd::Comgr::create_data_set(&output); res != AMD_COMGR_STATUS_SUCCESS) {
608+
amd::Comgr::destroy_action_info(action);
609+
amd::Comgr::destroy_data_set(reloc);
610+
return false;
611+
}
612+
613+
if (auto res = amd::Comgr::do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_TO_RELOCATABLE, action,
614+
input, reloc);
615+
res != AMD_COMGR_STATUS_SUCCESS) {
616+
extractBuildLog(reloc, buildLog);
617+
amd::Comgr::destroy_action_info(action);
618+
amd::Comgr::destroy_data_set(reloc);
619+
amd::Comgr::destroy_data_set(output);
620+
return false;
621+
}
622+
623+
if (!extractBuildLog(reloc, buildLog)) {
624+
amd::Comgr::destroy_action_info(action);
625+
amd::Comgr::destroy_data_set(reloc);
626+
amd::Comgr::destroy_data_set(output);
627+
return false;
628+
}
629+
630+
amd::Comgr::destroy_action_info(action);
631+
if (auto res = createAction(action, linkOptions, isa, lang); res != AMD_COMGR_STATUS_SUCCESS) {
632+
amd::Comgr::destroy_action_info(action);
633+
amd::Comgr::destroy_data_set(reloc);
634+
amd::Comgr::destroy_data_set(output);
635+
return false;
636+
}
637+
638+
if (auto res = amd::Comgr::do_action(AMD_COMGR_ACTION_LINK_RELOCATABLE_TO_EXECUTABLE, action,
639+
reloc, output);
640+
res != AMD_COMGR_STATUS_SUCCESS) {
641+
extractBuildLog(output, buildLog);
642+
amd::Comgr::destroy_action_info(action);
643+
amd::Comgr::destroy_data_set(output);
644+
amd::Comgr::destroy_data_set(reloc);
645+
return false;
646+
}
647+
648+
if (!extractBuildLog(output, buildLog)) {
649+
amd::Comgr::destroy_action_info(action);
650+
amd::Comgr::destroy_data_set(output);
651+
amd::Comgr::destroy_data_set(reloc);
652+
return false;
653+
}
654+
655+
if (!extractByteCodeBinary(output, AMD_COMGR_DATA_KIND_EXECUTABLE, exe)) {
656+
amd::Comgr::destroy_action_info(action);
657+
amd::Comgr::destroy_data_set(output);
658+
amd::Comgr::destroy_data_set(reloc);
659+
return false;
660+
}
661+
662+
// Clean up
663+
amd::Comgr::destroy_action_info(action);
664+
amd::Comgr::destroy_data_set(output);
665+
amd::Comgr::destroy_data_set(reloc);
666+
return true;
667+
}
668+
588669
bool compileToBitCode(const amd_comgr_data_set_t compileInputs, const std::string& isa,
589670
std::vector<std::string>& compileOptions, std::string& buildLog,
590671
std::vector<char>& LLVMBitcode) {
@@ -646,8 +727,7 @@ bool linkLLVMBitcode(const amd_comgr_data_set_t linkInputs, const std::string& i
646727
return false;
647728
}
648729

649-
if (auto res =
650-
amd::Comgr::do_action(AMD_COMGR_ACTION_LINK_BC_TO_BC, action, linkInputs, output);
730+
if (auto res = amd::Comgr::do_action(AMD_COMGR_ACTION_LINK_BC_TO_BC, action, linkInputs, output);
651731
res != AMD_COMGR_STATUS_SUCCESS) {
652732
amd::Comgr::destroy_action_info(action);
653733
amd::Comgr::destroy_data_set(output);
@@ -915,17 +995,18 @@ bool fillMangledNames(std::vector<char>& dataVec, std::map<std::string, std::str
915995
return false;
916996
}
917997

918-
for (auto &it : mangledNames) {
998+
for (auto& it : mangledNames) {
919999
size_t Size;
920-
char *data = const_cast<char*>(it.first.data());
1000+
char* data = const_cast<char*>(it.first.data());
9211001

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

9271007
std::unique_ptr<char[]> mName(new char[Size]());
928-
if (auto res = amd::Comgr::map_name_expression_to_symbol_name(dataObject, &Size, data, mName.get())) {
1008+
if (auto res =
1009+
amd::Comgr::map_name_expression_to_symbol_name(dataObject, &Size, data, mName.get())) {
9291010
amd::Comgr::release_data(dataObject);
9301011
return false;
9311012
}

hipamd/src/hiprtc/hiprtcComgrHelper.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ bool extractByteCodeBinary(const amd_comgr_data_set_t inDataSet,
4141
bool createAction(amd_comgr_action_info_t& action, std::vector<std::string>& options,
4242
const std::string& isa,
4343
const amd_comgr_language_t lang = AMD_COMGR_LANGUAGE_NONE);
44+
bool compileToExecutable(const amd_comgr_data_set_t compileInputs, const std::string& isa,
45+
std::vector<std::string>& compileOptions,
46+
std::vector<std::string>& linkOptions, std::string& buildLog,
47+
std::vector<char>& exe);
4448
bool compileToBitCode(const amd_comgr_data_set_t compileInputs, const std::string& isa,
4549
std::vector<std::string>& compileOptions, std::string& buildLog,
4650
std::vector<char>& LLVMBitcode);
@@ -54,8 +58,8 @@ bool dumpIsaFromBC(const amd_comgr_data_set_t isaInputs, const std::string& isa,
5458
std::vector<std::string>& exeOptions, std::string name, std::string& buildLog);
5559
bool demangleName(const std::string& mangledName, std::string& demangledName);
5660
std::string handleMangledName(std::string loweredName);
57-
bool fillMangledNames(std::vector<char>& executable, std::map<std::string, std::string>& mangledNames,
58-
bool isBitcode);
61+
bool fillMangledNames(std::vector<char>& executable,
62+
std::map<std::string, std::string>& mangledNames, bool isBitcode);
5963
void GenerateUniqueFileName(std::string& name);
6064
} // namespace helpers
6165
} // namespace hiprtc

hipamd/src/hiprtc/hiprtcInternal.cpp

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ bool RTCProgram::findIsa() {
116116

117117
// RTC Compile Program Member Functions
118118
void RTCProgram::AppendOptions(const std::string app_env_var, std::vector<std::string>* options) {
119-
120119
if (options == nullptr) {
121120
LogError("Append options passed is nullptr.");
122121
return;
@@ -261,10 +260,6 @@ bool RTCCompileProgram::transformOptions(std::vector<std::string>& compile_optio
261260
i = "--offload-arch=" + val;
262261
continue;
263262
}
264-
if (i == "--save-temps") {
265-
settings_.dumpISA = true;
266-
continue;
267-
}
268263
}
269264

270265
// Removed consumed options
@@ -300,78 +295,28 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
300295
compileOpts.reserve(compile_options_.size() + options.size() + 2);
301296
compileOpts.insert(compileOpts.end(), options.begin(), options.end());
302297

303-
if (!fgpu_rdc_) {
304-
compileOpts.push_back("-Xclang");
305-
compileOpts.push_back("-disable-llvm-passes");
306-
}
307-
308298
if (!transformOptions(compileOpts)) {
309299
LogError("Error in hiprtc: unable to transform options");
310300
return false;
311301
}
312302

313-
if (!compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) {
314-
LogError("Error in hiprtc: unable to compile source to bitcode");
315-
return false;
316-
}
317-
318-
if (fgpu_rdc_ && !mangled_names_.empty()) {
319-
if (!fillMangledNames(LLVMBitcode_, mangled_names_, true)) {
320-
LogError("Error in hiprtc: unable to fill mangled names");
303+
if (fgpu_rdc_) {
304+
if (!compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) {
305+
LogError("Error in hiprtc: unable to compile source to bitcode");
321306
return false;
322307
}
323-
324-
return true;
325-
}
326-
327-
std::string linkFileName = "linked";
328-
if (!addCodeObjData(link_input_, LLVMBitcode_, linkFileName, AMD_COMGR_DATA_KIND_BC)) {
329-
LogError("Error in hiprtc: unable to add linked code object");
330-
return false;
331-
}
332-
333-
std::vector<char> LinkedLLVMBitcode;
334-
if (!linkLLVMBitcode(link_input_, isa_, link_options_, build_log_, LinkedLLVMBitcode)) {
335-
LogError("Error in hiprtc: unable to add device libs to linked bitcode");
336-
return false;
337-
}
338-
339-
std::string linkedFileName = "LLVMBitcode.bc";
340-
if (!addCodeObjData(exec_input_, LinkedLLVMBitcode, linkedFileName, AMD_COMGR_DATA_KIND_BC)) {
341-
LogError("Error in hiprtc: unable to add device libs linked code object");
342-
return false;
343-
}
344-
345-
std::vector<std::string> exe_options;
346-
// Find the options passed by the app which can be used during BC to Relocatable phase.
347-
if (!findExeOptions(options, exe_options)) {
348-
LogError("Error in hiprtc: unable to find executable options");
349-
return false;
350-
}
351-
352-
std::vector<std::string> exeOpts(exe_options_);
353-
exeOpts.reserve(exeOpts.size() + exe_options.size() + 2);
354-
// Add these below options by default for optimizations during BC to Relocatable phase.
355-
exeOpts.push_back("-mllvm");
356-
exeOpts.push_back("-amdgpu-internalize-symbols");
357-
// User provided options are appended at the end since they can override the above
358-
// default options if necessary
359-
exeOpts.insert(exeOpts.end(), exe_options.begin(), exe_options.end());
360-
361-
if (settings_.dumpISA) {
362-
if (!dumpIsaFromBC(exec_input_, isa_, exeOpts, name_, build_log_)) {
363-
LogError("Error in hiprtc: unable to dump isa code");
308+
} else {
309+
LogInfo("Using the new path of comgr");
310+
if (!compileToExecutable(compile_input_, isa_, compileOpts, link_options_, build_log_,
311+
executable_)) {
312+
LogError("Failing to compile to realloc");
364313
return false;
365314
}
366315
}
367316

368-
if (!createExecutable(exec_input_, isa_, exeOpts, build_log_, executable_)) {
369-
LogError("Error in hiprtc: unable to create executable");
370-
return false;
371-
}
372-
373317
if (!mangled_names_.empty()) {
374-
if (!fillMangledNames(executable_, mangled_names_, false)) {
318+
auto& compile_step_output = fgpu_rdc_ ? LLVMBitcode_ : executable_;
319+
if (!fillMangledNames(compile_step_output, mangled_names_, fgpu_rdc_)) {
375320
LogError("Error in hiprtc: unable to fill mangled names");
376321
return false;
377322
}
@@ -380,6 +325,7 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
380325
return true;
381326
}
382327

328+
383329
void RTCCompileProgram::stripNamedExpression(std::string& strippedName) {
384330
if (strippedName.back() == ')') {
385331
strippedName.pop_back();
@@ -453,7 +399,6 @@ RTCLinkProgram::RTCLinkProgram(std::string name) : RTCProgram(name) {
453399
bool RTCLinkProgram::AddLinkerOptions(unsigned int num_options, hiprtcJIT_option* options_ptr,
454400
void** options_vals_ptr) {
455401
for (size_t opt_idx = 0; opt_idx < num_options; ++opt_idx) {
456-
457402
switch (options_ptr[opt_idx]) {
458403
case HIPRTC_JIT_MAX_REGISTERS:
459404
link_args_.max_registers_ = *(reinterpret_cast<unsigned int*>(&options_vals_ptr[opt_idx]));

hipamd/src/hiprtc/hiprtcInternal.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ static amd::Monitor g_hiprtcInitlock{"hiprtcInit lock"};
7676
#define HIPRTC_INIT_API_INTERNAL(...) \
7777
amd::Thread* thread = amd::Thread::current(); \
7878
if (!VDI_CHECK_THREAD(thread)) { \
79-
ClPrint(amd::LOG_NONE, amd::LOG_ALWAYS, "An internal error has occurred." \
80-
" This may be due to insufficient memory."); \
79+
ClPrint(amd::LOG_NONE, amd::LOG_ALWAYS, \
80+
"An internal error has occurred." \
81+
" This may be due to insufficient memory."); \
8182
HIPRTC_RETURN(HIPRTC_ERROR_INTERNAL_ERROR); \
8283
} \
8384
amd::ScopedLock lock(g_hiprtcInitlock); \
@@ -107,7 +108,6 @@ static void crashWithMessage(std::string message) {
107108
}
108109

109110
struct Settings {
110-
bool dumpISA{false};
111111
bool offloadArchProvided{false};
112112
};
113113

@@ -156,10 +156,8 @@ class RTCCompileProgram : public RTCProgram {
156156
bool addBuiltinHeader();
157157
bool transformOptions(std::vector<std::string>& compile_options);
158158
bool findExeOptions(const std::vector<std::string>& options,
159-
std::vector<std::string>& exe_options);
160-
void AppendCompileOptions() {
161-
AppendOptions(HIPRTC_COMPILE_OPTIONS_APPEND, &compile_options_);
162-
}
159+
std::vector<std::string>& exe_options);
160+
void AppendCompileOptions() { AppendOptions(HIPRTC_COMPILE_OPTIONS_APPEND, &compile_options_); }
163161

164162
RTCCompileProgram() = delete;
165163
RTCCompileProgram(RTCCompileProgram&) = delete;
@@ -288,9 +286,7 @@ class RTCLinkProgram : public RTCProgram {
288286
bool AddLinkerData(void* image_ptr, size_t image_size, std::string link_file_name,
289287
hiprtcJITInputType input_type);
290288
bool LinkComplete(void** bin_out, size_t* size_out);
291-
void AppendLinkerOptions() {
292-
AppendOptions(HIPRTC_LINK_OPTIONS_APPEND, &link_options_);
293-
}
289+
void AppendLinkerOptions() { AppendOptions(HIPRTC_LINK_OPTIONS_APPEND, &link_options_); }
294290
};
295291

296292
// Thread Local Storage Variables Aggregator Class

0 commit comments

Comments
 (0)