SWDEV-370552 - Correct the HIPRTC behavior to optimize the ISA only once

Change-Id: Idaf0ea8294657db3666e9548deb6a9629e0ee718


[ROCm/clr commit: 99556449b6]
This commit is contained in:
Satyanvesh Dittakavi
2023-02-01 09:12:56 +00:00
parent fb107094d6
commit 77e78dff33
2 changed files with 51 additions and 16 deletions
@@ -117,11 +117,6 @@ RTCCompileProgram::RTCCompileProgram(std::string name_) : RTCProgram(name_), fgp
compile_options_.reserve(20); // count of options below
compile_options_.push_back("-O3");
#ifdef HIPRTC_EARLY_INLINE
compile_options_.push_back("-mllvm");
compile_options_.push_back("-amdgpu-early-inline-all");
#endif
if (GPU_ENABLE_WGP_MODE) compile_options_.push_back("-mcumode");
if (!GPU_ENABLE_WAVE32_MODE) compile_options_.push_back("-mwavefrontsize64");
@@ -188,7 +183,23 @@ bool RTCCompileProgram::addBuiltinHeader() {
return true;
}
bool RTCCompileProgram::transformOptions() {
bool RTCCompileProgram::findLLVMOptions(const std::vector<std::string>& options,
std::vector<std::string>& llvm_options) {
for (size_t i = 0; i < options.size(); ++i) {
if (options[i] == "-mllvm") {
if (options.size() == (i+1)) {
LogInfo(
"-mllvm option passed by the app, it comes as a pair but there is no option after this");
return false;
}
llvm_options.push_back(options[i]);
llvm_options.push_back(options[i + 1]);
}
}
return true;
}
bool RTCCompileProgram::transformOptions(std::vector<std::string>& compile_options) {
auto getValueOf = [](const std::string& option) {
std::string res;
auto f = std::find(option.begin(), option.end(), '=');
@@ -196,7 +207,7 @@ bool RTCCompileProgram::transformOptions() {
return res;
};
for (auto& i : compile_options_) {
for (auto& i : compile_options) {
if (i == "-hip-pch") {
LogInfo(
"-hip-pch is deprecated option, has no impact on execution of new hiprtc programs, it "
@@ -218,9 +229,9 @@ bool RTCCompileProgram::transformOptions() {
}
if (auto res = std::find_if(
compile_options_.begin(), compile_options_.end(),
compile_options.begin(), compile_options.end(),
[](const std::string& str) { return str.find("--offload-arch=") != std::string::npos; });
res != compile_options_.end()) {
res != compile_options.end()) {
auto isaName = getValueOf(*res);
isa_ = "amdgcn-amd-amdhsa--" + isaName;
settings_.offloadArchProvided = true;
@@ -242,15 +253,21 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
fgpu_rdc_ = fgpu_rdc;
// Append compile options
compile_options_.reserve(compile_options_.size() + options.size());
compile_options_.insert(compile_options_.end(), options.begin(), options.end());
std::vector<std::string> compileOpts(compile_options_);
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()) {
if (!transformOptions(compileOpts)) {
LogError("Error in hiprtc: unable to transform options");
return false;
}
if (!compileToBitCode(compile_input_, isa_, compile_options_, build_log_, LLVMBitcode_)) {
if (!compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) {
LogError("Error in hiprtc: unable to compile source to bitcode");
return false;
}
@@ -287,14 +304,30 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
return false;
}
std::vector<std::string> llvmOptions;
// Find the -mllvm options passed by the app such as "-mllvm" "-amdgpu-early-inline-all=true"
if (!findLLVMOptions(options, llvmOptions)) {
LogError("Error in hiprtc: unable to match -mllvm options");
return false;
}
std::vector<std::string> exeOpts(exe_options_);
exeOpts.reserve(exeOpts.size() + llvmOptions.size() + 2);
// Add these options by default for optimizations during BC to Relocatable phase.
exeOpts.push_back("-mllvm");
exeOpts.push_back("-amdgpu-internalize-symbols");
// User provided -mllvm options are appended at the end since they can override the above
// default options if necessary
exeOpts.insert(exeOpts.end(), llvmOptions.begin(), llvmOptions.end());
if (settings_.dumpISA) {
if (!dumpIsaFromBC(exec_input_, isa_, exe_options_, name_, build_log_)) {
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_, exe_options_, build_log_, executable_)) {
if (!createExecutable(exec_input_, isa_, exeOpts, build_log_, executable_)) {
LogError("Error in hiprtc: unable to create executable");
return false;
}
@@ -146,7 +146,9 @@ class RTCCompileProgram : public RTCProgram {
// Private Member functions
bool addSource_impl();
bool addBuiltinHeader();
bool transformOptions();
bool transformOptions(std::vector<std::string>& compile_options);
bool findLLVMOptions(const std::vector<std::string>& options,
std::vector<std::string>& llvm_options);
RTCCompileProgram() = delete;
RTCCompileProgram(RTCCompileProgram&) = delete;