SWDEV-384013 - Add HIPRTC_JIT_IR_TO_ISA_OPT_EXT

Change-Id: Icfc06c19af32f41d603bf880ab1927bffb391097


[ROCm/clr commit: 300ba5b1f1]
This commit is contained in:
Jatin Chaudhary
2023-02-22 20:17:27 +00:00
committed by Maneesh Gupta
orang tua c33e0bde58
melakukan 421c4149e4
5 mengubah file dengan 192 tambahan dan 171 penghapusan
+28 -18
Melihat File
@@ -240,7 +240,7 @@ hiprtcResult hiprtcVersion(int* major, int* minor) {
HIPRTC_RETURN(HIPRTC_SUCCESS);
}
hiprtcResult hiprtcGetBitcode (hiprtcProgram prog, char* bitcode) {
hiprtcResult hiprtcGetBitcode(hiprtcProgram prog, char* bitcode) {
HIPRTC_INIT_API(prog, bitcode);
if (bitcode == nullptr) {
@@ -278,6 +278,14 @@ hiprtcResult hiprtcLinkCreate(unsigned int num_options, hiprtcJIT_option* option
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
if (num_options != 0) {
for (int i = 0; i < num_options; i++) {
if (options_ptr == nullptr || options_vals_pptr == nullptr) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
}
}
std::string name("Linker Program");
hiprtc::RTCLinkProgram* rtc_link_prog_ptr = new hiprtc::RTCLinkProgram(name);
if (!rtc_link_prog_ptr->AddLinkerOptions(num_options, options_ptr, options_vals_pptr)) {
@@ -298,14 +306,14 @@ hiprtcResult hiprtcLinkAddFile(hiprtcLinkState hip_link_state, hiprtcJITInputTyp
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
if (input_type == HIPRTC_JIT_INPUT_CUBIN || input_type == HIPRTC_JIT_INPUT_PTX
|| input_type == HIPRTC_JIT_INPUT_FATBINARY || input_type == HIPRTC_JIT_INPUT_OBJECT
|| input_type == HIPRTC_JIT_INPUT_LIBRARY || input_type == HIPRTC_JIT_INPUT_NVVM) {
if (input_type == HIPRTC_JIT_INPUT_CUBIN || input_type == HIPRTC_JIT_INPUT_PTX ||
input_type == HIPRTC_JIT_INPUT_FATBINARY || input_type == HIPRTC_JIT_INPUT_OBJECT ||
input_type == HIPRTC_JIT_INPUT_LIBRARY || input_type == HIPRTC_JIT_INPUT_NVVM) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
hiprtc::RTCLinkProgram* rtc_link_prog_ptr
= reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
hiprtc::RTCLinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
if (!rtc_link_prog_ptr->AddLinkerFile(std::string(file_path), input_type)) {
HIPRTC_RETURN(HIPRTC_ERROR_PROGRAM_CREATION_FAILURE);
}
@@ -317,16 +325,15 @@ hiprtcResult hiprtcLinkAddData(hiprtcLinkState hip_link_state, hiprtcJITInputTyp
void* image, size_t image_size, const char* name,
unsigned int num_options, hiprtcJIT_option* options_ptr,
void** option_values) {
HIPRTC_INIT_API(hip_link_state, image, image_size, name, num_options, options_ptr,
option_values);
HIPRTC_INIT_API(hip_link_state, image, image_size, name, num_options, options_ptr, option_values);
if (image == nullptr || image_size <= 0) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
if (input_type == HIPRTC_JIT_INPUT_CUBIN || input_type == HIPRTC_JIT_INPUT_PTX
|| input_type == HIPRTC_JIT_INPUT_FATBINARY || input_type == HIPRTC_JIT_INPUT_OBJECT
|| input_type == HIPRTC_JIT_INPUT_LIBRARY || input_type == HIPRTC_JIT_INPUT_NVVM) {
if (input_type == HIPRTC_JIT_INPUT_CUBIN || input_type == HIPRTC_JIT_INPUT_PTX ||
input_type == HIPRTC_JIT_INPUT_FATBINARY || input_type == HIPRTC_JIT_INPUT_OBJECT ||
input_type == HIPRTC_JIT_INPUT_LIBRARY || input_type == HIPRTC_JIT_INPUT_NVVM) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
@@ -335,8 +342,8 @@ hiprtcResult hiprtcLinkAddData(hiprtcLinkState hip_link_state, hiprtcJITInputTyp
input_name = name;
}
hiprtc::RTCLinkProgram* rtc_link_prog_ptr
= reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
hiprtc::RTCLinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
if (!rtc_link_prog_ptr->AddLinkerData(image, image_size, input_name, input_type)) {
HIPRTC_RETURN(HIPRTC_ERROR_PROGRAM_CREATION_FAILURE);
}
@@ -347,8 +354,12 @@ hiprtcResult hiprtcLinkAddData(hiprtcLinkState hip_link_state, hiprtcJITInputTyp
hiprtcResult hiprtcLinkComplete(hiprtcLinkState hip_link_state, void** bin_out, size_t* size_out) {
HIPRTC_INIT_API(hip_link_state, bin_out, size_out);
hiprtc::RTCLinkProgram* rtc_link_prog_ptr
= reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
if (bin_out == nullptr || size_out == nullptr) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
hiprtc::RTCLinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
if (!rtc_link_prog_ptr->LinkComplete(bin_out, size_out)) {
HIPRTC_RETURN(HIPRTC_ERROR_LINKING);
}
@@ -359,10 +370,9 @@ hiprtcResult hiprtcLinkComplete(hiprtcLinkState hip_link_state, void** bin_out,
hiprtcResult hiprtcLinkDestroy(hiprtcLinkState hip_link_state) {
HIPRTC_INIT_API(hip_link_state);
hiprtc::RTCLinkProgram* rtc_link_prog_ptr
= reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
hiprtc::RTCLinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
delete rtc_link_prog_ptr;
HIPRTC_RETURN(HIPRTC_SUCCESS);
}
@@ -22,7 +22,7 @@ THE SOFTWARE.
#include "hiprtcComgrHelper.hpp"
#if defined(_WIN32)
#include <io.h>
#include <io.h>
#endif
#include "../amd_hsa_elf.hpp"
@@ -41,8 +41,8 @@ constexpr char const* OFFLOAD_KIND_HIPV4 = "hipv4";
constexpr char const* OFFLOAD_KIND_HCC = "hcc";
constexpr char const* AMDGCN_TARGET_TRIPLE = "amdgcn-amd-amdhsa-";
static constexpr size_t bundle_magic_string_size
= strLiteralLength(CLANG_OFFLOAD_BUNDLER_MAGIC_STR);
static constexpr size_t bundle_magic_string_size =
strLiteralLength(CLANG_OFFLOAD_BUNDLER_MAGIC_STR);
struct __ClangOffloadBundleInfo {
uint64_t offset;
@@ -373,8 +373,8 @@ bool isCodeObjectCompatibleWithDevice(std::string co_triple_target_id,
if (co_triple_target_id == agent_triple_target_id) return true;
// Parse code object triple target id
if (!consume(co_triple_target_id, std::string(OFFLOAD_KIND_HIP) + "-"
+ std::string(AMDGCN_TARGET_TRIPLE))) {
if (!consume(co_triple_target_id,
std::string(OFFLOAD_KIND_HIP) + "-" + std::string(AMDGCN_TARGET_TRIPLE))) {
return false;
}
@@ -420,18 +420,17 @@ bool UnbundleBitCode(const std::vector<char>& bundled_llvm_bitcode, const std::s
return true;
}
std::string bundled_llvm_bitcode_s(bundled_llvm_bitcode.begin(), bundled_llvm_bitcode.begin()
+ bundled_llvm_bitcode.size());
std::string bundled_llvm_bitcode_s(bundled_llvm_bitcode.begin(),
bundled_llvm_bitcode.begin() + bundled_llvm_bitcode.size());
const void* data = reinterpret_cast<const void*>(bundled_llvm_bitcode_s.c_str());
const auto obheader
= reinterpret_cast<const __ClangOffloadBundleHeader*>(data);
const auto obheader = reinterpret_cast<const __ClangOffloadBundleHeader*>(data);
const auto* desc = &obheader->desc[0];
for (uint64_t idx=0; idx < obheader->numOfCodeObjects; ++idx,
for (uint64_t idx = 0; idx < obheader->numOfCodeObjects; ++idx,
desc = reinterpret_cast<const __ClangOffloadBundleInfo*>(
reinterpret_cast<uintptr_t>(&desc->bundleEntryId[0]) +
desc->bundleEntryIdSize)) {
const void* image = reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(obheader) +
desc->offset);
reinterpret_cast<uintptr_t>(&desc->bundleEntryId[0]) +
desc->bundleEntryIdSize)) {
const void* image =
reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(obheader) + desc->offset);
const size_t image_size = desc->size;
std::string bundleEntryId{desc->bundleEntryId, desc->bundleEntryIdSize};
@@ -439,8 +438,6 @@ bool UnbundleBitCode(const std::vector<char>& bundled_llvm_bitcode, const std::s
if (isCodeObjectCompatibleWithDevice(bundleEntryId, isa)) {
co_offset = (reinterpret_cast<uintptr_t>(image) - reinterpret_cast<uintptr_t>(data));
co_size = image_size;
std::cout<<"bundleEntryId: "<<bundleEntryId<<" Isa:"<<isa<<" Offset: "<<co_offset<<" Size: "
<< co_size <<std::endl;
break;
}
}
@@ -594,9 +591,8 @@ bool compileToBitCode(const amd_comgr_data_set_t compileInputs, const std::strin
return false;
}
if (auto res =
amd::Comgr::do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC,
action, input, output);
if (auto res = amd::Comgr::do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC,
action, input, output);
res != AMD_COMGR_STATUS_SUCCESS) {
extractBuildLog(output, buildLog);
amd::Comgr::destroy_action_info(action);
@@ -639,7 +635,6 @@ 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_ADD_DEVICE_LIBRARIES, action, linkInputs,
dataSetDevLibs);
res != AMD_COMGR_STATUS_SUCCESS) {
@@ -768,13 +763,13 @@ bool createExecutable(const amd_comgr_data_set_t linkInputs, const std::string&
return true;
}
void GenerateUniqueFileName(std::string &name) {
void GenerateUniqueFileName(std::string& name) {
#if !defined(_WIN32)
char *name_template = const_cast<char*>(name.c_str());
char* name_template = const_cast<char*>(name.c_str());
int temp_fd = mkstemp(name_template);
#else
char *name_template = new char[name.length()+1];
strcpy_s(name_template, name.length()+1, name.data());
char* name_template = new char[name.length() + 1];
strcpy_s(name_template, name.length() + 1, name.data());
int sizeinchars = strnlen(name_template, 20) + 1;
_mktemp_s(name_template, sizeinchars);
#endif
@@ -787,7 +782,6 @@ void GenerateUniqueFileName(std::string &name) {
bool dumpIsaFromBC(const amd_comgr_data_set_t isaInputs, const std::string& isa,
std::vector<std::string>& exeOptions, std::string name, std::string& buildLog) {
amd_comgr_action_info_t action;
if (auto res = createAction(action, exeOptions, isa); res != AMD_COMGR_STATUS_SUCCESS) {
@@ -917,11 +911,11 @@ std::string handleMangledName(std::string loweredName) {
return loweredName;
}
bool fillMangledNames(std::vector<char>& dataVec,
std::vector<std::string>& mangledNames, bool isBitcode) {
bool fillMangledNames(std::vector<char>& dataVec, std::vector<std::string>& mangledNames,
bool isBitcode) {
amd_comgr_data_t dataObject;
if (auto res = amd::Comgr::create_data(isBitcode ? AMD_COMGR_DATA_KIND_BC :
AMD_COMGR_DATA_KIND_EXECUTABLE, &dataObject);
if (auto res = amd::Comgr::create_data(
isBitcode ? AMD_COMGR_DATA_KIND_BC : AMD_COMGR_DATA_KIND_EXECUTABLE, &dataObject);
res != AMD_COMGR_STATUS_SUCCESS) {
return false;
}
@@ -944,7 +938,7 @@ bool fillMangledNames(std::vector<char>& dataVec,
return false;
}
char *mName = new char[Size]();
char* mName = new char[Size]();
if (auto res = amd::Comgr::get_mangled_name(dataObject, i, &Size, mName)) {
amd::Comgr::release_data(dataObject);
return false;
@@ -959,7 +953,7 @@ bool fillMangledNames(std::vector<char>& dataVec,
}
bool getDemangledNames(const std::vector<std::string>& mangledNames,
std::map<std::string, std::string>& demangledNames) {
std::map<std::string, std::string>& demangledNames) {
for (auto& i : mangledNames) {
std::string demangledName;
if (!demangleName(i, demangledName)) return false;
@@ -54,10 +54,10 @@ 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::vector<std::string>& mangledNames, bool isBitcode);
bool fillMangledNames(std::vector<char>& executable, std::vector<std::string>& mangledNames,
bool isBitcode);
bool getDemangledNames(const std::vector<std::string>& mangledNames,
std::map<std::string, std::string>& demangledNames);
void GenerateUniqueFileName(std::string &name);
std::map<std::string, std::string>& demangledNames);
void GenerateUniqueFileName(std::string& name);
} // namespace helpers
} // namespace hiprtc
@@ -23,6 +23,9 @@ THE SOFTWARE.
#include "hiprtcInternal.hpp"
#include <fstream>
#include <streambuf>
#include <vector>
#include <sys/stat.h>
#include "vdi_common.hpp"
@@ -31,7 +34,20 @@ THE SOFTWARE.
namespace hiprtc {
using namespace helpers;
//RTC Program Member Functions
std::vector<std::string> getLinkOptions(const LinkArguments& args) {
std::vector<std::string> res;
auto irArgCount = args.linkerIRArgCount();
if (irArgCount > 0) {
res.reserve(irArgCount);
auto irArg = args.linkerIRArg();
for (size_t i = 0; i < irArgCount; i++) {
res.emplace_back(std::string(irArg[i]));
}
}
return res;
}
// RTC Program Member Functions
RTCProgram::RTCProgram(std::string name) : name_(name) {
constexpr bool kComgrVersioned = true;
std::call_once(amd::Comgr::initialized, amd::Comgr::LoadLib, kComgrVersioned);
@@ -71,11 +87,10 @@ bool RTCProgram::findIsa() {
return false;
}
hipError_t (*dyn_hipGetDevice)(int*) = reinterpret_cast
<hipError_t (*)(int*)>(sym_hipGetDevice);
hipError_t (*dyn_hipGetDevice)(int*) = reinterpret_cast<hipError_t (*)(int*)>(sym_hipGetDevice);
hipError_t (*dyn_hipGetDeviceProperties)(hipDeviceProp_t*, int) = reinterpret_cast
<hipError_t (*)(hipDeviceProp_t*, int)>(sym_hipGetDeviceProperties);
hipError_t (*dyn_hipGetDeviceProperties)(hipDeviceProp_t*, int) =
reinterpret_cast<hipError_t (*)(hipDeviceProp_t*, int)>(sym_hipGetDeviceProperties);
int device;
hipError_t status = dyn_hipGetDevice(&device);
@@ -94,9 +109,8 @@ bool RTCProgram::findIsa() {
return true;
}
//RTC Compile Program Member Functions
// RTC Compile Program Member Functions
RTCCompileProgram::RTCCompileProgram(std::string name_) : RTCProgram(name_), fgpu_rdc_(false) {
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");
@@ -187,9 +201,10 @@ 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)) {
if (options.size() == (i + 1)) {
LogInfo(
"-mllvm option passed by the app, it comes as a pair but there is no option after this");
"-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]);
@@ -228,6 +243,11 @@ bool RTCCompileProgram::transformOptions(std::vector<std::string>& compile_optio
}
}
// Removed consumed options
compile_options.erase(
std::remove(compile_options.begin(), compile_options.end(), std::string("")),
compile_options.end());
if (auto res = std::find_if(
compile_options.begin(), compile_options.end(),
[](const std::string& str) { return str.find("--offload-arch=") != std::string::npos; });
@@ -244,7 +264,6 @@ bool RTCCompileProgram::transformOptions(std::vector<std::string>& compile_optio
amd::Monitor RTCProgram::lock_("HIPRTC Program", true);
bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fgpu_rdc) {
if (!addSource_impl()) {
LogError("Error in hiprtc: unable to add source code");
return false;
@@ -256,7 +275,7 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
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");
@@ -347,7 +366,6 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
}
void RTCCompileProgram::stripNamedExpression(std::string& strippedName) {
if (strippedName.back() == ')') {
strippedName.pop_back();
strippedName.erase(0, strippedName.find('('));
@@ -356,11 +374,9 @@ void RTCCompileProgram::stripNamedExpression(std::string& strippedName) {
strippedName.erase(0, 1);
}
// Removes the spaces from strippedName if present
strippedName.erase(std::remove_if(strippedName.begin(),
strippedName.end(),
[](unsigned char c) {
return std::isspace(c);
}), strippedName.end());
strippedName.erase(std::remove_if(strippedName.begin(), strippedName.end(),
[](unsigned char c) { return std::isspace(c); }),
strippedName.end());
}
bool RTCCompileProgram::trackMangledName(std::string& name) {
@@ -382,7 +398,6 @@ bool RTCCompileProgram::trackMangledName(std::string& name) {
}
bool RTCCompileProgram::getMangledName(const char* name_expression, const char** loweredName) {
std::string strippedName = name_expression;
stripNamedExpression(strippedName);
@@ -397,7 +412,6 @@ bool RTCCompileProgram::getMangledName(const char* name_expression, const char**
}
bool RTCCompileProgram::GetBitcode(char* bitcode) {
if (!fgpu_rdc_ || LLVMBitcode_.size() <= 0) {
return false;
}
@@ -415,7 +429,7 @@ bool RTCCompileProgram::GetBitcodeSize(size_t* bitcode_size) {
return true;
}
//RTC Link Program Member Functions
// RTC Link Program Member Functions
RTCLinkProgram::RTCLinkProgram(std::string name) : RTCProgram(name) {
if (amd::Comgr::create_data_set(&link_input_) != AMD_COMGR_STATUS_SUCCESS) {
crashWithMessage("Failed to allocate internal hiprtc structure");
@@ -424,21 +438,19 @@ 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) {
if (options_vals_ptr[opt_idx] == nullptr) {
crashWithMessage("JIT Options value ptr cannot be null");
LogError("Options value can not be nullptr");
return false;
}
switch(options_ptr[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]));
break;
case HIPRTC_JIT_THREADS_PER_BLOCK:
link_args_.threads_per_block_
= *(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
link_args_.threads_per_block_ =
*(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_WALL_TIME:
link_args_.wall_time_ = *(reinterpret_cast<long*>(options_vals_ptr[opt_idx]));
@@ -456,19 +468,19 @@ bool RTCLinkProgram::AddLinkerOptions(unsigned int num_options, hiprtcJIT_option
link_args_.error_log_size_ = (reinterpret_cast<size_t>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_OPTIMIZATION_LEVEL:
link_args_.optimization_level_
= *(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
link_args_.optimization_level_ =
*(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_TARGET_FROM_HIPCONTEXT:
link_args_.target_from_hip_context_
= *(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
link_args_.target_from_hip_context_ =
*(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_TARGET:
link_args_.jit_target_ = *(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_FALLBACK_STRATEGY:
link_args_.fallback_strategy_
= *(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
link_args_.fallback_strategy_ =
*(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_GENERATE_DEBUG_INFO:
link_args_.generate_debug_info_ = *(reinterpret_cast<int*>(options_vals_ptr[opt_idx]));
@@ -495,8 +507,8 @@ bool RTCLinkProgram::AddLinkerOptions(unsigned int num_options, hiprtcJIT_option
link_args_.global_symbol_addresses_ = reinterpret_cast<void**>(options_vals_ptr[opt_idx]);
break;
case HIPRTC_JIT_GLOBAL_SYMBOL_COUNT:
link_args_.global_symbol_count_
= *(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
link_args_.global_symbol_count_ =
*(reinterpret_cast<unsigned int*>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_LTO:
link_args_.lto_ = *(reinterpret_cast<int*>(options_vals_ptr[opt_idx]));
@@ -513,7 +525,13 @@ bool RTCLinkProgram::AddLinkerOptions(unsigned int num_options, hiprtcJIT_option
case HIPRTC_JIT_FMA:
link_args_.fma_ = *(reinterpret_cast<int*>(options_vals_ptr[opt_idx]));
break;
default:
case HIPRTC_JIT_IR_TO_ISA_OPT_EXT:
link_args_.linker_ir2isa_args_ = reinterpret_cast<const char**>(options_vals_ptr[opt_idx]);
break;
case HIPRTC_JIT_IR_TO_ISA_OPT_COUNT_EXT:
link_args_.linker_ir2isa_args_count_ = reinterpret_cast<size_t>(options_vals_ptr[opt_idx]);
break;
default:
break;
}
}
@@ -526,17 +544,17 @@ amd_comgr_data_kind_t RTCLinkProgram::GetCOMGRDataKind(hiprtcJITInputType input_
// Map the hiprtc input type to comgr data kind
switch (input_type) {
case HIPRTC_JIT_INPUT_LLVM_BITCODE :
case HIPRTC_JIT_INPUT_LLVM_BITCODE:
data_kind = AMD_COMGR_DATA_KIND_BC;
break;
case HIPRTC_JIT_INPUT_LLVM_BUNDLED_BITCODE :
data_kind = HIPRTC_USE_RUNTIME_UNBUNDLER
? AMD_COMGR_DATA_KIND_BC : AMD_COMGR_DATA_KIND_BC_BUNDLE;
case HIPRTC_JIT_INPUT_LLVM_BUNDLED_BITCODE:
data_kind =
HIPRTC_USE_RUNTIME_UNBUNDLER ? AMD_COMGR_DATA_KIND_BC : AMD_COMGR_DATA_KIND_BC_BUNDLE;
break;
case HIPRTC_JIT_INPUT_LLVM_ARCHIVES_OF_BUNDLED_BITCODE :
case HIPRTC_JIT_INPUT_LLVM_ARCHIVES_OF_BUNDLED_BITCODE:
data_kind = AMD_COMGR_DATA_KIND_AR_BUNDLE;
break;
default :
default:
LogError("Cannot find the corresponding comgr data kind");
break;
}
@@ -544,26 +562,9 @@ amd_comgr_data_kind_t RTCLinkProgram::GetCOMGRDataKind(hiprtcJITInputType input_
return data_kind;
}
bool RTCLinkProgram::AddLinkerFile(std::string file_path, hiprtcJITInputType input_type) {
bool RTCLinkProgram::AddLinkerDataImpl(std::vector<char>& link_data, hiprtcJITInputType input_type,
std::string& link_file_name) {
std::vector<char> llvm_bitcode;
// Get the file size.
struct stat stat_buf;
if (stat(file_path.c_str(), &stat_buf)) {
return false;
}
// Read the file contents
std::string link_file_name("Linker Program");
std::vector<char> link_file_info(stat_buf.st_size);
std::ifstream bc_file(file_path, std::ios_base::in | std::ios_base::binary);
if (!bc_file.good()) {
return true;
}
bc_file.read(link_file_info.data(), stat_buf.st_size);
bc_file.close();
// If this is bundled bitcode then unbundle this.
if (HIPRTC_USE_RUNTIME_UNBUNDLER && input_type == HIPRTC_JIT_INPUT_LLVM_BUNDLED_BITCODE) {
if (!findIsa()) {
@@ -572,19 +573,18 @@ bool RTCLinkProgram::AddLinkerFile(std::string file_path, hiprtcJITInputType inp
size_t co_offset = 0;
size_t co_size = 0;
if(!UnbundleBitCode(link_file_info, isa_, co_offset, co_size)) {
if (!UnbundleBitCode(link_data, isa_, co_offset, co_size)) {
LogError("Error in hiprtc: unable to unbundle the llvm bitcode");
return false;
}
llvm_bitcode.assign(link_file_info.begin() + co_offset,
link_file_info.begin() + co_offset + co_size);
llvm_bitcode.assign(link_data.begin() + co_offset, link_data.begin() + co_offset + co_size);
} else {
llvm_bitcode.assign(link_file_info.begin(), link_file_info.end());
llvm_bitcode.assign(link_data.begin(), link_data.end());
}
amd_comgr_data_kind_t data_kind;
if((data_kind = GetCOMGRDataKind(input_type)) == AMD_COMGR_DATA_KIND_UNDEF) {
if ((data_kind = GetCOMGRDataKind(input_type)) == AMD_COMGR_DATA_KIND_UNDEF) {
LogError("Cannot find the correct COMGR data kind");
return false;
}
@@ -597,52 +597,39 @@ bool RTCLinkProgram::AddLinkerFile(std::string file_path, hiprtcJITInputType inp
return true;
}
bool RTCLinkProgram::AddLinkerFile(std::string file_path, hiprtcJITInputType input_type) {
std::vector<char> link_file_info;
std::ifstream file_stream{file_path};
if (!file_stream.good()) {
return false;
}
std::copy(std::istream_iterator<char>(file_stream), std::istream_iterator<char>(),
std::back_inserter(link_file_info));
file_stream.close();
// Read the file contents
std::string link_file_name("Linker Program");
return AddLinkerDataImpl(link_file_info, input_type, link_file_name);
}
bool RTCLinkProgram::AddLinkerData(void* image_ptr, size_t image_size, std::string link_file_name,
hiprtcJITInputType input_type) {
char* image_char_buf = reinterpret_cast<char*>(image_ptr);
std::vector<char> llvm_bitcode;
std::vector<char> bundled_llvm_bitcode(image_char_buf, image_char_buf + image_size);
if (HIPRTC_USE_RUNTIME_UNBUNDLER && input_type == HIPRTC_JIT_INPUT_LLVM_BUNDLED_BITCODE) {
std::vector<char> bundled_llvm_bitcode(image_char_buf, image_char_buf + image_size);
if (!findIsa()) {
return false;
}
size_t co_offset = 0;
size_t co_size = 0;
if(!UnbundleBitCode(bundled_llvm_bitcode, isa_, co_offset, co_size)) {
LogError("Error in hiprtc: unable to unbundle the llvm bitcode");
return false;
}
llvm_bitcode.assign(bundled_llvm_bitcode.begin() + co_offset,
bundled_llvm_bitcode.begin() + co_offset + co_size);
} else {
llvm_bitcode.assign(image_char_buf, image_char_buf + image_size);
}
amd_comgr_data_kind_t data_kind;
if((data_kind = GetCOMGRDataKind(input_type)) == AMD_COMGR_DATA_KIND_UNDEF) {
LogError("Cannot find the correct COMGR data kind");
return false;
}
if(!addCodeObjData(link_input_, llvm_bitcode , link_file_name, data_kind)) {
LogError("Error in hiprtc: unable to add linked code object");
return false;
}
return true;
return AddLinkerDataImpl(bundled_llvm_bitcode, input_type, link_file_name);
}
bool RTCLinkProgram::LinkComplete(void** bin_out, size_t* size_out) {
if (!findIsa()) {
return false;
}
std::vector<char> linked_llvm_bitcode;
if (!linkLLVMBitcode(link_input_, isa_, link_options_, build_log_, linked_llvm_bitcode)) {
std::vector<std::string> linkopts;
if (!linkLLVMBitcode(link_input_, isa_, linkopts, build_log_, linked_llvm_bitcode)) {
LogError("Error in hiprtc: unable to add device libs to linked bitcode");
return false;
}
@@ -653,7 +640,7 @@ bool RTCLinkProgram::LinkComplete(void** bin_out, size_t* size_out) {
return false;
}
std::vector<std::string> exe_options;
std::vector<std::string> exe_options = getLinkOptions(link_args_);
exe_options.push_back("-O3");
if (!createExecutable(exec_input_, isa_, exe_options, build_log_, executable_)) {
LogError("Error in hiprtc: unable to create exectuable");
@@ -62,7 +62,7 @@ template <typename T, typename... Args> inline std::string ToString(T first, Arg
} // namespace internal
} // namespace hiprtc
static amd::Monitor g_hiprtcInitlock {"hiprtcInit lock"};
static amd::Monitor g_hiprtcInitlock{"hiprtcInit lock"};
#define HIPRTC_INIT_API_INTERNAL(...) \
amd::Thread* thread = amd::Thread::current(); \
if (!VDI_CHECK_THREAD(thread)) { \
@@ -72,7 +72,7 @@ static amd::Monitor g_hiprtcInitlock {"hiprtcInit lock"};
amd::ScopedLock lock(g_hiprtcInitlock); \
if (!amd::Flag::init()) { \
HIPRTC_RETURN(HIPRTC_ERROR_INTERNAL_ERROR); \
} \
}
#define HIPRTC_INIT_API(...) \
HIPRTC_INIT_API_INTERNAL(0, __VA_ARGS__) \
@@ -101,15 +101,13 @@ struct Settings {
};
class RTCProgram {
protected:
protected:
// Lock and control variables
static amd::Monitor lock_;
static std::once_flag initialized_;
RTCProgram(std::string name);
~RTCProgram() {
amd::Comgr::destroy_data_set(exec_input_);
}
~RTCProgram() { amd::Comgr::destroy_data_set(exec_input_); }
// Member Functions
bool findIsa();
@@ -125,7 +123,6 @@ protected:
};
class RTCCompileProgram : public RTCProgram {
// Private Data Members
Settings settings_;
@@ -213,10 +210,43 @@ struct LinkArguments {
int prec_div_;
int prec_sqrt_;
int fma_;
const char** linker_ir2isa_args_;
size_t linker_ir2isa_args_count_;
LinkArguments()
: max_registers_{0},
threads_per_block_{0},
wall_time_{0.0f},
info_log_size_{0},
info_log_{nullptr},
error_log_size_{0},
error_log_{nullptr},
optimization_level_{3},
target_from_hip_context_{0},
jit_target_{0},
fallback_strategy_{0},
generate_debug_info_{0},
log_verbose_{0},
generate_line_info_{0},
cache_mode_{0},
sm3x_opt_{false},
fast_compile_{false},
global_symbol_names_{nullptr},
global_symbol_addresses_{nullptr},
global_symbol_count_{0},
lto_{0},
ftz_{0},
prec_div_{0},
prec_sqrt_{0},
fma_{0},
linker_ir2isa_args_{nullptr},
linker_ir2isa_args_count_{0} {}
size_t linkerIRArgCount() const { return linker_ir2isa_args_count_; }
const char** linkerIRArg() const { return linker_ir2isa_args_; }
};
class RTCLinkProgram : public RTCProgram {
// Private Member Functions (forbid these function calls)
RTCLinkProgram() = delete;
RTCLinkProgram(RTCLinkProgram&) = delete;
@@ -230,29 +260,29 @@ class RTCLinkProgram : public RTCProgram {
// Private Data Members
amd_comgr_data_set_t link_input_;
std::vector<std::string> link_options_;
public:
bool AddLinkerDataImpl(std::vector<char>& link_data, hiprtcJITInputType input_type,
std::string& link_file_name);
public:
RTCLinkProgram(std::string name);
~RTCLinkProgram() {
amd::Comgr::destroy_data_set(link_input_);
}
~RTCLinkProgram() { amd::Comgr::destroy_data_set(link_input_); }
// Public Member Functions
bool AddLinkerOptions(unsigned int num_options, hiprtcJIT_option* options_ptr,
void** options_vals_ptr);
bool AddLinkerFile(std::string file_path, hiprtcJITInputType input_type);
bool AddLinkerData(void* image_ptr, size_t image_size, std::string link_file_name,
hiprtcJITInputType input_type);
hiprtcJITInputType input_type);
bool LinkComplete(void** bin_out, size_t* size_out);
};
// Thread Local Storage Variables Aggregator Class
class TlsAggregator {
public:
public:
hiprtcResult last_rtc_error_;
TlsAggregator(): last_rtc_error_(HIPRTC_SUCCESS) {
}
~TlsAggregator() {
}
TlsAggregator() : last_rtc_error_(HIPRTC_SUCCESS) {}
~TlsAggregator() {}
};
extern thread_local TlsAggregator tls;
} // namespace hiprtc