SWDEV-489106 - Linker API addition to runtime

1) Add Linker APIs to runtime to support SPIRV linking
2) Migrate Internal implementations to runtime and share with rtc
3) Add Support to bundled and unbundled SPIRV Code object linking.

Change-Id: Ic1fd4431f842a208a2468e8aec54a65b5fa6b0e3
This commit is contained in:
Rahul Manocha
2024-10-09 11:24:44 -07:00
committed by Rahul Manocha
parent 9faaf20aae
commit 5930f047bb
15 changed files with 1197 additions and 630 deletions
+2 -2
View File
@@ -73,7 +73,7 @@ if(BUILD_SHARED_LIBS)
endif()
endif()
target_sources(hiprtc PRIVATE hiprtc.cpp hiprtcComgrHelper.cpp hiprtcInternal.cpp)
target_sources(hiprtc PRIVATE hiprtc.cpp ../hip_comgr_helper.cpp hiprtcInternal.cpp)
set_target_properties(hiprtc PROPERTIES
CXX_STANDARD 17
@@ -243,7 +243,7 @@ target_compile_definitions(hiprtc PRIVATE __HIP_ENABLE_RTC)
if(NOT WIN32)
if (BUILD_SHARED_LIBS)
target_sources(amdhip64 PRIVATE hiprtc.cpp hiprtcComgrHelper.cpp hiprtcInternal.cpp)
target_sources(amdhip64 PRIVATE hiprtc.cpp ../hip_comgr_helper.cpp hiprtcInternal.cpp)
endif()
endif()
+21 -17
View File
@@ -20,6 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "../hip_comgr_helper.hpp"
#include <hip/hiprtc.h>
#include "hiprtcInternal.hpp"
@@ -90,7 +91,7 @@ hiprtcResult hiprtcCreateProgram(hiprtcProgram* prog, const char* src, const cha
if (name == nullptr || strlen(name) == 0) {
progName = "CompileSourceXXXXXX";
hiprtc::helpers::GenerateUniqueFileName(progName);
hip::helpers::GenerateUniqueFileName(progName);
}
if (!rtcProgram->addSource(std::string(src), progName)) {
@@ -306,8 +307,9 @@ hiprtcResult hiprtcLinkCreate(unsigned int num_options, hiprtcJIT_option* option
}
std::string name("LinkerProgram");
hiprtc::RTCLinkProgram* rtc_link_prog_ptr = new hiprtc::RTCLinkProgram(name);
if (!rtc_link_prog_ptr->AddLinkerOptions(num_options, options_ptr, options_vals_pptr)) {
hip::LinkProgram* rtc_link_prog_ptr = new hip::LinkProgram(name);
if (!rtc_link_prog_ptr->AddLinkerOptions(num_options, options_ptr,
options_vals_pptr)) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_OPTION);
}
@@ -327,14 +329,15 @@ hiprtcResult hiprtcLinkAddFile(hiprtcLinkState hip_link_state, hiprtcJITInputTyp
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) {
input_type == HIPRTC_JIT_INPUT_LIBRARY || input_type == HIPRTC_JIT_INPUT_NVVM ||
input_type == HIPRTC_JIT_INPUT_SPIRV ) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
hiprtc::RTCLinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
hip::LinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hip::LinkProgram*>(hip_link_state);
if (!hiprtc::RTCLinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
if (!hip::LinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
@@ -357,7 +360,8 @@ hiprtcResult hiprtcLinkAddData(hiprtcLinkState hip_link_state, hiprtcJITInputTyp
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) {
input_type == HIPRTC_JIT_INPUT_LIBRARY || input_type == HIPRTC_JIT_INPUT_NVVM ||
input_type == HIPRTC_JIT_INPUT_SPIRV) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
@@ -366,10 +370,10 @@ hiprtcResult hiprtcLinkAddData(hiprtcLinkState hip_link_state, hiprtcJITInputTyp
input_name = name;
}
hiprtc::RTCLinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
hip::LinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hip::LinkProgram*>(hip_link_state);
if (!hiprtc::RTCLinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
if (!hip::LinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
@@ -387,10 +391,10 @@ hiprtcResult hiprtcLinkComplete(hiprtcLinkState hip_link_state, void** bin_out,
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
hiprtc::RTCLinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hiprtc::RTCLinkProgram*>(hip_link_state);
hip::LinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hip::LinkProgram*>(hip_link_state);
if (!hiprtc::RTCLinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
if (!hip::LinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
@@ -404,10 +408,10 @@ 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);
hip::LinkProgram* rtc_link_prog_ptr =
reinterpret_cast<hip::LinkProgram*>(hip_link_state);
if (!hiprtc::RTCLinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
if (!hip::LinkProgram::isLinkerValid(rtc_link_prog_ptr)) {
HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT);
}
File diff suppressed because it is too large Load Diff
-65
View File
@@ -1,65 +0,0 @@
/*
Copyright (c) 2022 - Present Advanced Micro Devices, Inc. All rights reserved.
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.
*/
#pragma once
#include <vector>
#include <string>
#include "vdi_common.hpp"
#include "rocclr/utils/debug.hpp"
#include "device/comgrctx.hpp"
namespace hiprtc {
namespace helpers {
bool UnbundleBitCode(const std::vector<char>& bundled_bit_code, const std::string& isa,
size_t& co_offset, size_t& co_size);
bool addCodeObjData(amd_comgr_data_set_t& input, const std::vector<char>& source,
const std::string& name, const amd_comgr_data_kind_t type);
bool extractBuildLog(amd_comgr_data_set_t dataSet, std::string& buildLog);
bool extractByteCodeBinary(const amd_comgr_data_set_t inDataSet,
const amd_comgr_data_kind_t dataKind, std::vector<char>& bin);
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);
bool linkLLVMBitcode(const amd_comgr_data_set_t linkInputs, const std::string& isa,
std::vector<std::string>& linkOptions, std::string& buildLog,
std::vector<char>& LinkedLLVMBitcode);
bool createExecutable(const amd_comgr_data_set_t linkInputs, const std::string& isa,
std::vector<std::string>& exeOptions, std::string& buildLog,
std::vector<char>& executable);
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);
void GenerateUniqueFileName(std::string& name);
} // namespace helpers
} // namespace hiprtc
+9 -400
View File
@@ -31,131 +31,12 @@ THE SOFTWARE.
#include "vdi_common.hpp"
#include "rocclr/utils/flags.hpp"
#include "../hip_comgr_helper.hpp"
namespace hiprtc {
using namespace helpers;
std::unordered_set<RTCLinkProgram*>RTCLinkProgram::linker_set_;
std::vector<std::string> getLinkOptions(const LinkArguments& args) {
std::vector<std::string> res;
{ // process optimization level
std::string opt("-O");
opt += std::to_string(args.optimization_level_);
res.push_back(opt);
}
const auto irArgCount = args.linker_ir2isa_args_count_;
if (irArgCount > 0) {
res.reserve(irArgCount);
const auto irArg = args.linker_ir2isa_args_;
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);
if (amd::Comgr::create_data_set(&exec_input_) != AMD_COMGR_STATUS_SUCCESS) {
crashWithMessage("Failed to allocate internal hiprtc structure");
}
}
bool RTCProgram::findIsa() {
#ifdef BUILD_SHARED_LIBS
const char* libName;
#ifdef _WIN32
std::string dll_name = std::string("amdhip64_" + std::to_string(HIP_VERSION_MAJOR) + ".dll");
libName = dll_name.c_str();
#else
libName = "libamdhip64.so";
#endif
void* handle = amd::Os::loadLibrary(libName);
if (!handle) {
LogInfo("hip runtime failed to load using dlopen");
build_log_ +=
"hip runtime failed to load.\n"
"Error: Please provide architecture for which code is to be "
"generated.\n";
return false;
}
void* sym_hipGetDevice = amd::Os::getSymbol(handle, "hipGetDevice");
void* sym_hipGetDeviceProperties =
amd::Os::getSymbol(handle, "hipGetDevicePropertiesR0600"); // Try to find the new symbol
if (sym_hipGetDeviceProperties == nullptr) {
sym_hipGetDeviceProperties =
amd::Os::getSymbol(handle, "hipGetDeviceProperties"); // Fall back to old one
}
if (sym_hipGetDevice == nullptr || sym_hipGetDeviceProperties == nullptr) {
LogInfo("ISA cannot be found to dlsym failure");
build_log_ +=
"ISA cannot be found from hip runtime.\n"
"Error: Please provide architecture for which code is to be "
"generated.\n";
return false;
}
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);
int device;
hipError_t status = dyn_hipGetDevice(&device);
if (status != hipSuccess) {
return false;
}
hipDeviceProp_t props;
status = dyn_hipGetDeviceProperties(&props, device);
if (status != hipSuccess) {
return false;
}
isa_ = "amdgcn-amd-amdhsa--";
isa_.append(props.gcnArchName);
amd::Os::unloadLibrary(handle);
return true;
#else
int device;
hipError_t status = hipGetDevice(&device);
if (status != hipSuccess) {
return false;
}
hipDeviceProp_t props;
status = hipGetDeviceProperties(&props, device);
if (status != hipSuccess) {
return false;
}
isa_ = "amdgcn-amd-amdhsa--";
isa_.append(props.gcnArchName);
return true;
#endif
}
// 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;
}
std::stringstream ss(app_env_var);
std::istream_iterator<std::string> begin{ss}, end;
options->insert(options->end(), begin, end);
}
// RTC Compile Program Member Functions
RTCCompileProgram::RTCCompileProgram(std::string name_) : RTCProgram(name_), fgpu_rdc_(false) {
RTCCompileProgram::RTCCompileProgram(std::string name_) : hip::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");
@@ -209,7 +90,7 @@ bool RTCCompileProgram::addSource(const std::string& source, const std::string&
// objects
bool RTCCompileProgram::addSource_impl() {
std::vector<char> vsource(source_code_.begin(), source_code_.end());
if (!addCodeObjData(compile_input_, vsource, source_name_, AMD_COMGR_DATA_KIND_SOURCE)) {
if (!hip::helpers::addCodeObjData(compile_input_, vsource, source_name_, AMD_COMGR_DATA_KIND_SOURCE)) {
return false;
}
return true;
@@ -221,7 +102,7 @@ bool RTCCompileProgram::addHeader(const std::string& source, const std::string&
return false;
}
std::vector<char> vsource(source.begin(), source.end());
if (!addCodeObjData(compile_input_, vsource, name, AMD_COMGR_DATA_KIND_INCLUDE)) {
if (!hip::helpers::addCodeObjData(compile_input_, vsource, name, AMD_COMGR_DATA_KIND_INCLUDE)) {
return false;
}
return true;
@@ -230,7 +111,7 @@ bool RTCCompileProgram::addHeader(const std::string& source, const std::string&
bool RTCCompileProgram::addBuiltinHeader() {
std::vector<char> source(__hipRTC_header, __hipRTC_header + __hipRTC_header_size);
std::string name{"hiprtc_runtime.h"};
if (!addCodeObjData(compile_input_, source, name, AMD_COMGR_DATA_KIND_INCLUDE)) {
if (!hip::helpers::addCodeObjData(compile_input_, source, name, AMD_COMGR_DATA_KIND_INCLUDE)) {
return false;
}
return true;
@@ -301,9 +182,6 @@ bool RTCCompileProgram::transformOptions(std::vector<std::string>& compile_optio
return findIsa();
}
// HIPRTC Program lock
amd::Monitor RTCProgram::lock_(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");
@@ -323,13 +201,13 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
}
if (fgpu_rdc_) {
if (!compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) {
if (!hip::helpers::compileToBitCode(compile_input_, isa_, compileOpts, build_log_, LLVMBitcode_)) {
LogError("Error in hiprtc: unable to compile source to bitcode");
return false;
}
} else {
LogInfo("Using the new path of comgr");
if (!compileToExecutable(compile_input_, isa_, compileOpts, link_options_, build_log_,
if (!hip::helpers::compileToExecutable(compile_input_, isa_, compileOpts, link_options_, build_log_,
executable_)) {
LogError("Failing to compile to realloc");
return false;
@@ -338,7 +216,7 @@ bool RTCCompileProgram::compile(const std::vector<std::string>& options, bool fg
if (!mangled_names_.empty()) {
auto& compile_step_output = fgpu_rdc_ ? LLVMBitcode_ : executable_;
if (!fillMangledNames(compile_step_output, mangled_names_, fgpu_rdc_)) {
if (!hip::helpers::fillMangledNames(compile_step_output, mangled_names_, fgpu_rdc_)) {
LogError("Error in hiprtc: unable to fill mangled names");
return false;
}
@@ -410,273 +288,4 @@ bool RTCCompileProgram::GetBitcodeSize(size_t* bitcode_size) {
*bitcode_size = LLVMBitcode_.size();
return true;
}
// 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");
}
amd::ScopedLock lock(lock_);
linker_set_.insert(this);
}
bool RTCLinkProgram::isLinkerValid(RTCLinkProgram* link_program) {
amd::ScopedLock lock(lock_);
if (linker_set_.find(link_program) == linker_set_.end()) {
return false;
}
return true;
}
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]));
break;
case HIPRTC_JIT_THREADS_PER_BLOCK:
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]));
break;
case HIPRTC_JIT_INFO_LOG_BUFFER: {
if (options_vals_ptr[opt_idx] == nullptr) {
LogError("Options value can not be nullptr");
return false;
}
link_args_.info_log_ = (reinterpret_cast<char*>(options_vals_ptr[opt_idx]));
break;
}
case HIPRTC_JIT_INFO_LOG_BUFFER_SIZE_BYTES:
link_args_.info_log_size_ = (reinterpret_cast<size_t>(options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_ERROR_LOG_BUFFER: {
if (options_vals_ptr[opt_idx] == nullptr) {
LogError("Options value can not be nullptr");
return false;
}
link_args_.error_log_ = reinterpret_cast<char*>(options_vals_ptr[opt_idx]);
break;
}
case HIPRTC_JIT_ERROR_LOG_BUFFER_SIZE_BYTES:
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]));
break;
case HIPRTC_JIT_TARGET_FROM_HIPCONTEXT:
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]));
break;
case HIPRTC_JIT_GENERATE_DEBUG_INFO:
link_args_.generate_debug_info_ = *(reinterpret_cast<int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_LOG_VERBOSE:
link_args_.log_verbose_ = reinterpret_cast<size_t>(options_vals_ptr[opt_idx]);
break;
case HIPRTC_JIT_GENERATE_LINE_INFO:
link_args_.generate_line_info_ = *(reinterpret_cast<int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_CACHE_MODE:
link_args_.cache_mode_ = *(reinterpret_cast<unsigned int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_NEW_SM3X_OPT:
link_args_.sm3x_opt_ = *(reinterpret_cast<bool*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_FAST_COMPILE:
link_args_.fast_compile_ = *(reinterpret_cast<bool*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_GLOBAL_SYMBOL_NAMES: {
if (options_vals_ptr[opt_idx] == nullptr) {
LogError("Options value can not be nullptr");
return false;
}
link_args_.global_symbol_names_ = reinterpret_cast<const char**>(options_vals_ptr[opt_idx]);
break;
}
case HIPRTC_JIT_GLOBAL_SYMBOL_ADDRESS: {
if (options_vals_ptr[opt_idx] == nullptr) {
LogError("Options value can not be nullptr");
return false;
}
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]));
break;
case HIPRTC_JIT_LTO:
link_args_.lto_ = *(reinterpret_cast<int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_FTZ:
link_args_.ftz_ = *(reinterpret_cast<int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_PREC_DIV:
link_args_.prec_div_ = *(reinterpret_cast<int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_PREC_SQRT:
link_args_.prec_sqrt_ = *(reinterpret_cast<int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_FMA:
link_args_.fma_ = *(reinterpret_cast<int*>(&options_vals_ptr[opt_idx]));
break;
case HIPRTC_JIT_IR_TO_ISA_OPT_EXT: {
if (options_vals_ptr[opt_idx] == nullptr) {
LogError("Options value can not be nullptr");
return false;
}
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;
}
}
return true;
}
amd_comgr_data_kind_t RTCLinkProgram::GetCOMGRDataKind(hiprtcJITInputType input_type) {
amd_comgr_data_kind_t data_kind = AMD_COMGR_DATA_KIND_UNDEF;
// Map the hiprtc input type to comgr data kind
switch (input_type) {
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;
break;
case HIPRTC_JIT_INPUT_LLVM_ARCHIVES_OF_BUNDLED_BITCODE:
data_kind = AMD_COMGR_DATA_KIND_AR_BUNDLE;
break;
default:
LogError("Cannot find the corresponding comgr data kind");
break;
}
return data_kind;
}
bool RTCLinkProgram::AddLinkerDataImpl(std::vector<char>& link_data, hiprtcJITInputType input_type,
std::string& link_file_name) {
std::vector<char> llvm_bitcode;
// If this is bundled bitcode then unbundle this.
if (HIPRTC_USE_RUNTIME_UNBUNDLER && input_type == HIPRTC_JIT_INPUT_LLVM_BUNDLED_BITCODE) {
if (!findIsa()) {
return false;
}
size_t co_offset = 0;
size_t co_size = 0;
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_data.begin() + co_offset, link_data.begin() + co_offset + co_size);
} else {
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) {
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;
}
bool RTCLinkProgram::AddLinkerFile(std::string file_path, hiprtcJITInputType input_type) {
std::ifstream file_stream{file_path, std::ios_base::in | std::ios_base::binary};
if (!file_stream.good()) {
return false;
}
file_stream.seekg(0, std::ios::end);
std::streampos file_size = file_stream.tellg();
file_stream.seekg(0, std::ios::beg);
// Read the file contents
std::vector<char> link_file_info(file_size);
file_stream.read(link_file_info.data(), file_size);
file_stream.close();
std::string link_file_name("LinkerProgram");
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> bundled_llvm_bitcode(image_char_buf, image_char_buf + image_size);
return AddLinkerDataImpl(bundled_llvm_bitcode, input_type, link_file_name);
}
bool RTCLinkProgram::LinkComplete(void** bin_out, size_t* size_out) {
if (!findIsa()) {
return false;
}
AppendLinkerOptions();
std::vector<char> linked_llvm_bitcode;
if (!linkLLVMBitcode(link_input_, isa_, link_options_, build_log_, linked_llvm_bitcode)) {
LogError("Error in hiprtc: unable to add device libs to linked bitcode");
return false;
}
std::string linkedFileName = "LLVMBitcode.bc";
if (!addCodeObjData(exec_input_, linked_llvm_bitcode, linkedFileName, AMD_COMGR_DATA_KIND_BC)) {
LogError("Error in hiprtc: unable to add linked bitcode");
return false;
}
std::vector<std::string> exe_options = getLinkOptions(link_args_);
LogPrintfInfo("Exe options forwarded to compiler: %s",
[&]() {
std::string ret;
for (const auto& i : exe_options) {
ret += i;
ret += " ";
}
return ret;
}()
.c_str());
if (!createExecutable(exec_input_, isa_, exe_options, build_log_, executable_)) {
LogPrintfInfo("Error in hiprtc: unable to create exectuable: %s", build_log_.c_str());
return false;
}
*size_out = executable_.size();
*bin_out = executable_.data();
return true;
}
} // namespace hiprtc
+8 -122
View File
@@ -19,6 +19,9 @@ 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.
*/
#pragma once
#include <hip/hip_runtime.h>
#include <hip/hiprtc.h>
#include <hip/hip_version.h>
@@ -36,6 +39,10 @@ THE SOFTWARE.
#include "rocclr/utils/debug.hpp"
#include "rocclr/utils/flags.hpp"
#include "rocclr/utils/macros.hpp"
#include "vdi_common.hpp"
#include "device/comgrctx.hpp"
#include "../hip_comgr_helper.hpp"
#ifdef __HIP_ENABLE_RTC
extern "C" {
@@ -44,8 +51,6 @@ extern unsigned __hipRTC_header_size;
}
#endif
#include "hiprtcComgrHelper.hpp"
namespace hiprtc {
namespace internal {
template <typename T> inline std::string ToString(T v) {
@@ -112,29 +117,7 @@ struct Settings {
bool offloadArchProvided{false};
};
class RTCProgram {
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_); }
// Member Functions
bool findIsa();
static void AppendOptions(std::string app_env_var, std::vector<std::string>* options);
// Data Members
std::string name_;
std::string isa_;
std::string build_log_;
std::vector<char> executable_;
amd_comgr_data_set_t exec_input_;
};
class RTCCompileProgram : public RTCProgram {
class RTCCompileProgram : public hip::RTCProgram {
// Private Data Members
Settings settings_;
@@ -195,103 +178,6 @@ class RTCCompileProgram : public RTCProgram {
size_t getLogSize() const { return build_log_.size(); }
};
// Linker Arguments passed via hipLinkCreate
struct LinkArguments {
unsigned int max_registers_;
unsigned int threads_per_block_;
float wall_time_;
size_t info_log_size_;
char* info_log_;
size_t error_log_size_;
char* error_log_;
unsigned int optimization_level_;
unsigned int target_from_hip_context_;
unsigned int jit_target_;
unsigned int fallback_strategy_;
int generate_debug_info_;
long log_verbose_;
int generate_line_info_;
unsigned int cache_mode_;
bool sm3x_opt_;
bool fast_compile_;
const char** global_symbol_names_;
void** global_symbol_addresses_;
unsigned int global_symbol_count_;
int lto_;
int ftz_;
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_{1},
prec_sqrt_{1},
fma_{1},
linker_ir2isa_args_{nullptr},
linker_ir2isa_args_count_{0} {}
};
class RTCLinkProgram : public RTCProgram {
// Private Member Functions (forbid these function calls)
RTCLinkProgram() = delete;
RTCLinkProgram(RTCLinkProgram&) = delete;
RTCLinkProgram& operator=(RTCLinkProgram&) = delete;
amd_comgr_data_kind_t GetCOMGRDataKind(hiprtcJITInputType input_type);
// Linker Argumenets at hipLinkCreate
LinkArguments link_args_;
// Private Data Members
amd_comgr_data_set_t link_input_;
std::vector<std::string> link_options_;
static std::unordered_set<RTCLinkProgram*> linker_set_;
bool AddLinkerDataImpl(std::vector<char>& link_data, hiprtcJITInputType input_type,
std::string& link_file_name);
public:
RTCLinkProgram(std::string name);
~RTCLinkProgram() {
amd::ScopedLock lock(lock_);
linker_set_.erase(this);
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);
bool LinkComplete(void** bin_out, size_t* size_out);
void AppendLinkerOptions() { AppendOptions(HIPRTC_LINK_OPTIONS_APPEND, &link_options_); }
static bool isLinkerValid(RTCLinkProgram* link_program);
};
// Thread Local Storage Variables Aggregator Class
class TlsAggregator {
public: