2021-10-01 01:46:34 +05:30
|
|
|
/*
|
2022-12-20 11:56:43 +00:00
|
|
|
Copyright (c) 2022 - 2023 Advanced Micro Devices, Inc. All rights reserved.
|
2021-10-01 01:46:34 +05:30
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "hiprtcInternal.hpp"
|
|
|
|
|
|
2022-03-14 12:36:16 -04:00
|
|
|
#include <fstream>
|
2023-02-22 20:17:27 +00:00
|
|
|
#include <streambuf>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2022-03-14 12:36:16 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
2021-10-01 01:46:34 +05:30
|
|
|
#include "vdi_common.hpp"
|
2024-11-08 10:26:22 +02:00
|
|
|
#include "rocclr/utils/flags.hpp"
|
2021-10-01 01:46:34 +05:30
|
|
|
|
2024-10-09 11:24:44 -07:00
|
|
|
#include "../hip_comgr_helper.hpp"
|
2024-06-10 13:58:27 +01:00
|
|
|
|
2024-10-09 11:24:44 -07:00
|
|
|
namespace hiprtc {
|
2023-04-12 15:07:15 -04:00
|
|
|
|
2023-02-22 20:17:27 +00:00
|
|
|
// RTC Compile Program Member Functions
|
2024-10-09 11:24:44 -07:00
|
|
|
RTCCompileProgram::RTCCompileProgram(std::string name_) : hip::RTCProgram(name_), fgpu_rdc_(false) {
|
2022-03-14 12:36:16 -04:00
|
|
|
if ((amd::Comgr::create_data_set(&compile_input_) != AMD_COMGR_STATUS_SUCCESS) ||
|
|
|
|
|
(amd::Comgr::create_data_set(&link_input_) != AMD_COMGR_STATUS_SUCCESS)) {
|
|
|
|
|
crashWithMessage("Failed to allocate internal hiprtc structure");
|
|
|
|
|
}
|
2021-10-01 01:46:34 +05:30
|
|
|
// Add internal header
|
|
|
|
|
if (!addBuiltinHeader()) {
|
|
|
|
|
crashWithMessage("Unable to add internal header");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add compile options
|
|
|
|
|
const std::string hipVerOpt{"--hip-version=" + std::to_string(HIP_VERSION_MAJOR) + '.' +
|
|
|
|
|
std::to_string(HIP_VERSION_MINOR) + '.' +
|
|
|
|
|
std::to_string(HIP_VERSION_PATCH)};
|
|
|
|
|
const std::string hipVerMajor{"-DHIP_VERSION_MAJOR=" + std::to_string(HIP_VERSION_MAJOR)};
|
|
|
|
|
const std::string hipVerMinor{"-DHIP_VERSION_MINOR=" + std::to_string(HIP_VERSION_MINOR)};
|
|
|
|
|
const std::string hipVerPatch{"-DHIP_VERSION_PATCH=" + std::to_string(HIP_VERSION_PATCH)};
|
|
|
|
|
|
2022-03-14 12:36:16 -04:00
|
|
|
compile_options_.reserve(20); // count of options below
|
|
|
|
|
compile_options_.push_back("-O3");
|
2021-10-01 01:46:34 +05:30
|
|
|
|
2023-12-06 13:52:45 +00:00
|
|
|
if (!(GPU_ENABLE_WGP_MODE)) {
|
|
|
|
|
compile_options_.push_back("-mcumode");
|
|
|
|
|
}
|
2022-03-14 12:36:16 -04:00
|
|
|
|
|
|
|
|
compile_options_.push_back(hipVerOpt);
|
|
|
|
|
compile_options_.push_back(hipVerMajor);
|
|
|
|
|
compile_options_.push_back(hipVerMinor);
|
|
|
|
|
compile_options_.push_back(hipVerPatch);
|
2022-12-20 11:56:43 +00:00
|
|
|
compile_options_.push_back("-Wno-gnu-line-marker");
|
|
|
|
|
compile_options_.push_back("-Wno-missing-prototypes");
|
2021-10-01 01:46:34 +05:30
|
|
|
#ifdef _WIN32
|
2022-03-14 12:36:16 -04:00
|
|
|
compile_options_.push_back("-target");
|
|
|
|
|
compile_options_.push_back("x86_64-pc-windows-msvc");
|
|
|
|
|
compile_options_.push_back("-fms-extensions");
|
|
|
|
|
compile_options_.push_back("-fms-compatibility");
|
2021-10-01 01:46:34 +05:30
|
|
|
#endif
|
2023-04-12 15:07:15 -04:00
|
|
|
AppendCompileOptions();
|
2021-10-01 01:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
2022-03-14 12:36:16 -04:00
|
|
|
bool RTCCompileProgram::addSource(const std::string& source, const std::string& name) {
|
2021-10-01 01:46:34 +05:30
|
|
|
if (source.size() == 0 || name.size() == 0) {
|
|
|
|
|
LogError("Error in hiprtc: source or name is of size 0 in addSource");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-14 12:36:16 -04:00
|
|
|
source_code_ += source;
|
|
|
|
|
source_name_ = name;
|
2021-10-01 01:46:34 +05:30
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// addSource_impl is a different function because we need to add source when we track mangled
|
|
|
|
|
// objects
|
2022-03-14 12:36:16 -04:00
|
|
|
bool RTCCompileProgram::addSource_impl() {
|
|
|
|
|
std::vector<char> vsource(source_code_.begin(), source_code_.end());
|
2024-10-09 11:24:44 -07:00
|
|
|
if (!hip::helpers::addCodeObjData(compile_input_, vsource, source_name_, AMD_COMGR_DATA_KIND_SOURCE)) {
|
2021-10-01 01:46:34 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 12:36:16 -04:00
|
|
|
bool RTCCompileProgram::addHeader(const std::string& source, const std::string& name) {
|
2021-10-01 01:46:34 +05:30
|
|
|
if (source.size() == 0 || name.size() == 0) {
|
|
|
|
|
LogError("Error in hiprtc: source or name is of size 0 in addHeader");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
std::vector<char> vsource(source.begin(), source.end());
|
2024-10-09 11:24:44 -07:00
|
|
|
if (!hip::helpers::addCodeObjData(compile_input_, vsource, name, AMD_COMGR_DATA_KIND_INCLUDE)) {
|
2021-10-01 01:46:34 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 12:36:16 -04:00
|
|
|
bool RTCCompileProgram::addBuiltinHeader() {
|
2021-10-01 01:46:34 +05:30
|
|
|
std::vector<char> source(__hipRTC_header, __hipRTC_header + __hipRTC_header_size);
|
|
|
|
|
std::string name{"hiprtc_runtime.h"};
|
2024-10-09 11:24:44 -07:00
|
|
|
if (!hip::helpers::addCodeObjData(compile_input_, source, name, AMD_COMGR_DATA_KIND_INCLUDE)) {
|
2021-10-01 01:46:34 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 13:48:41 +00:00
|
|
|
bool RTCCompileProgram::findExeOptions(const std::vector<std::string>& options,
|
|
|
|
|
std::vector<std::string>& exe_options) {
|
2023-02-01 09:12:56 +00:00
|
|
|
for (size_t i = 0; i < options.size(); ++i) {
|
2023-09-07 13:48:41 +00:00
|
|
|
// -mllvm options passed by the app such as "-mllvm" "-amdgpu-early-inline-all=true"
|
2023-02-01 09:12:56 +00:00
|
|
|
if (options[i] == "-mllvm") {
|
2023-02-22 20:17:27 +00:00
|
|
|
if (options.size() == (i + 1)) {
|
2023-02-01 09:12:56 +00:00
|
|
|
LogInfo(
|
2023-02-22 20:17:27 +00:00
|
|
|
"-mllvm option passed by the app, it comes as a pair but there is no option after "
|
|
|
|
|
"this");
|
2023-02-01 09:12:56 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-09-07 13:48:41 +00:00
|
|
|
exe_options.push_back(options[i]);
|
|
|
|
|
exe_options.push_back(options[i + 1]);
|
|
|
|
|
}
|
|
|
|
|
// Options like -Rpass=inline
|
|
|
|
|
if (options[i].find("-Rpass=") == 0) {
|
|
|
|
|
exe_options.push_back(options[i]);
|
2023-02-01 09:12:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTCCompileProgram::transformOptions(std::vector<std::string>& compile_options) {
|
2021-10-01 01:46:34 +05:30
|
|
|
auto getValueOf = [](const std::string& option) {
|
|
|
|
|
std::string res;
|
|
|
|
|
auto f = std::find(option.begin(), option.end(), '=');
|
|
|
|
|
if (f != option.end()) res = std::string(f + 1, option.end());
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-01 09:12:56 +00:00
|
|
|
for (auto& i : compile_options) {
|
2021-10-01 01:46:34 +05:30
|
|
|
if (i == "-hip-pch") {
|
|
|
|
|
LogInfo(
|
|
|
|
|
"-hip-pch is deprecated option, has no impact on execution of new hiprtc programs, it "
|
|
|
|
|
"can be removed");
|
|
|
|
|
i.clear();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// Some rtc samples use --gpu-architecture
|
|
|
|
|
if (i.rfind("--gpu-architecture=", 0) == 0) {
|
|
|
|
|
LogInfo("--gpu-architecture is nvcc option, transforming it to --offload-arch option");
|
|
|
|
|
auto val = getValueOf(i);
|
|
|
|
|
i = "--offload-arch=" + val;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 20:17:27 +00:00
|
|
|
// Removed consumed options
|
|
|
|
|
compile_options.erase(
|
|
|
|
|
std::remove(compile_options.begin(), compile_options.end(), std::string("")),
|
|
|
|
|
compile_options.end());
|
|
|
|
|
|
2021-10-01 01:46:34 +05:30
|
|
|
if (auto res = std::find_if(
|
2023-02-01 09:12:56 +00:00
|
|
|
compile_options.begin(), compile_options.end(),
|
2021-10-01 01:46:34 +05:30
|
|
|
[](const std::string& str) { return str.find("--offload-arch=") != std::string::npos; });
|
2023-02-01 09:12:56 +00:00
|
|
|
res != compile_options.end()) {
|
2021-10-01 01:46:34 +05:30
|
|
|
auto isaName = getValueOf(*res);
|
2022-03-14 12:36:16 -04:00
|
|
|
isa_ = "amdgcn-amd-amdhsa--" + isaName;
|
|
|
|
|
settings_.offloadArchProvided = true;
|
2021-10-01 01:46:34 +05:30
|
|
|
return true;
|
|
|
|
|
}
|
2022-03-10 14:29:03 +00:00
|
|
|
// App has not provided the gpu archiecture, need to find it
|
|
|
|
|
return findIsa();
|
2021-10-01 01:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:24:47 -07:00
|
|
|
bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fgpu_rdc) {
|
2021-10-01 01:46:34 +05:30
|
|
|
if (!addSource_impl()) {
|
|
|
|
|
LogError("Error in hiprtc: unable to add source code");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:24:47 -07:00
|
|
|
fgpu_rdc_ = fgpu_rdc;
|
|
|
|
|
|
2021-10-01 01:46:34 +05:30
|
|
|
// Append compile options
|
2023-02-01 09:12:56 +00:00
|
|
|
std::vector<std::string> compileOpts(compile_options_);
|
|
|
|
|
compileOpts.reserve(compile_options_.size() + options.size() + 2);
|
|
|
|
|
compileOpts.insert(compileOpts.end(), options.begin(), options.end());
|
2023-02-22 20:17:27 +00:00
|
|
|
|
2023-02-01 09:12:56 +00:00
|
|
|
if (!transformOptions(compileOpts)) {
|
2021-10-01 01:46:34 +05:30
|
|
|
LogError("Error in hiprtc: unable to transform options");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 00:44:39 +00:00
|
|
|
if (fgpu_rdc_) {
|
2024-10-09 11:24:44 -07:00
|
|
|
if (!hip::helpers::compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) {
|
2024-01-18 00:44:39 +00:00
|
|
|
LogError("Error in hiprtc: unable to compile source to bitcode");
|
2022-10-06 08:49:12 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2024-01-18 00:44:39 +00:00
|
|
|
} else {
|
|
|
|
|
LogInfo("Using the new path of comgr");
|
2024-10-09 11:24:44 -07:00
|
|
|
if (!hip::helpers::compileToExecutable(compile_input_, isa_, compileOpts, link_options_, build_log_,
|
2024-01-18 00:44:39 +00:00
|
|
|
executable_)) {
|
|
|
|
|
LogError("Failing to compile to realloc");
|
2021-10-01 01:46:34 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 15:39:46 +00:00
|
|
|
if (!mangled_names_.empty()) {
|
2024-01-18 00:44:39 +00:00
|
|
|
auto& compile_step_output = fgpu_rdc_ ? LLVMBitcode_ : executable_;
|
2024-10-09 11:24:44 -07:00
|
|
|
if (!hip::helpers::fillMangledNames(compile_step_output, mangled_names_, fgpu_rdc_)) {
|
2023-07-20 15:39:46 +00:00
|
|
|
LogError("Error in hiprtc: unable to fill mangled names");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-10-01 01:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 00:44:39 +00:00
|
|
|
|
2022-06-15 16:13:11 +00:00
|
|
|
void RTCCompileProgram::stripNamedExpression(std::string& strippedName) {
|
2021-10-01 01:46:34 +05:30
|
|
|
if (strippedName.back() == ')') {
|
|
|
|
|
strippedName.pop_back();
|
|
|
|
|
strippedName.erase(0, strippedName.find('('));
|
|
|
|
|
}
|
|
|
|
|
if (strippedName.front() == '&') {
|
|
|
|
|
strippedName.erase(0, 1);
|
|
|
|
|
}
|
2023-07-20 15:39:46 +00:00
|
|
|
|
2022-06-15 16:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTCCompileProgram::trackMangledName(std::string& name) {
|
|
|
|
|
amd::ScopedLock lock(lock_);
|
|
|
|
|
|
|
|
|
|
if (name.size() == 0) return false;
|
|
|
|
|
|
2023-07-20 15:39:46 +00:00
|
|
|
std::string strippedName = name;
|
|
|
|
|
stripNamedExpression(strippedName);
|
2021-10-01 01:46:34 +05:30
|
|
|
|
2023-07-20 15:39:46 +00:00
|
|
|
mangled_names_.insert(std::pair<std::string, std::string>(strippedName, ""));
|
2021-10-01 01:46:34 +05:30
|
|
|
|
2023-07-20 15:39:46 +00:00
|
|
|
std::string gcn_expr = "__amdgcn_name_expr_";
|
|
|
|
|
std::string size = std::to_string(mangled_names_.size());
|
|
|
|
|
const auto var1{"\n static __device__ const void* " + gcn_expr + size + "[]= {\"" + strippedName + "\", (void*)&" + strippedName + "};"};
|
|
|
|
|
const auto var2{"\n static auto __amdgcn_name_expr_stub_" + size + " = " + gcn_expr + size + ";\n"};
|
|
|
|
|
const auto code{var1 + var2};
|
2021-10-01 01:46:34 +05:30
|
|
|
|
2022-03-14 12:36:16 -04:00
|
|
|
source_code_ += code;
|
2021-10-01 01:46:34 +05:30
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 16:13:11 +00:00
|
|
|
bool RTCCompileProgram::getMangledName(const char* name_expression, const char** loweredName) {
|
|
|
|
|
std::string strippedName = name_expression;
|
|
|
|
|
stripNamedExpression(strippedName);
|
|
|
|
|
|
2023-07-20 15:39:46 +00:00
|
|
|
if (auto dres = mangled_names_.find(strippedName); dres != mangled_names_.end()) {
|
2021-10-01 01:46:34 +05:30
|
|
|
if (dres->second.size() != 0) {
|
|
|
|
|
*loweredName = dres->second.c_str();
|
|
|
|
|
return true;
|
2022-06-15 16:13:11 +00:00
|
|
|
} else
|
|
|
|
|
return false;
|
2021-10-01 01:46:34 +05:30
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:24:47 -07:00
|
|
|
bool RTCCompileProgram::GetBitcode(char* bitcode) {
|
|
|
|
|
if (!fgpu_rdc_ || LLVMBitcode_.size() <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::copy(LLVMBitcode_.begin(), LLVMBitcode_.end(), bitcode);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTCCompileProgram::GetBitcodeSize(size_t* bitcode_size) {
|
|
|
|
|
if (!fgpu_rdc_ || LLVMBitcode_.size() <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*bitcode_size = LLVMBitcode_.size();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-10-01 01:46:34 +05:30
|
|
|
} // namespace hiprtc
|