From 8a049faccba8313f03d1cb43808bd3054f4ea9ed Mon Sep 17 00:00:00 2001 From: kjayapra-amd Date: Tue, 7 Jun 2022 09:24:47 -0700 Subject: [PATCH] SWDEV-339995 - Adding hiprtcGetBitcode and hiprtcBitcodeSize changes. Change-Id: I378f1ac98de3b11fa3f74161abd036702c7b479b [ROCm/clr commit: 2731d03f0380695866b2d43cdaf24160beb200b1] --- projects/clr/hipamd/src/hip_hcc.def.in | 2 ++ projects/clr/hipamd/src/hip_hcc.map.in | 4 ++- projects/clr/hipamd/src/hiprtc/hiprtc.cpp | 34 +++++++++++++++++-- projects/clr/hipamd/src/hiprtc/hiprtc.def | 2 ++ projects/clr/hipamd/src/hiprtc/hiprtc.map.in | 2 ++ .../clr/hipamd/src/hiprtc/hiprtcInternal.cpp | 34 ++++++++++++++++--- .../clr/hipamd/src/hiprtc/hiprtcInternal.hpp | 7 +++- 7 files changed, 76 insertions(+), 9 deletions(-) diff --git a/projects/clr/hipamd/src/hip_hcc.def.in b/projects/clr/hipamd/src/hip_hcc.def.in index 4c64e3918d..117689767c 100644 --- a/projects/clr/hipamd/src/hip_hcc.def.in +++ b/projects/clr/hipamd/src/hip_hcc.def.in @@ -267,6 +267,8 @@ hiprtcGetProgramLogSize hiprtcGetCode hiprtcGetCodeSize hiprtcGetErrorString +hiprtcGetBitcode +hiprtcGetBitcodeSize hiprtcLinkCreate hiprtcLinkAddFile hiprtcLinkAddData diff --git a/projects/clr/hipamd/src/hip_hcc.map.in b/projects/clr/hipamd/src/hip_hcc.map.in index 2717921f32..759f604ca2 100644 --- a/projects/clr/hipamd/src/hip_hcc.map.in +++ b/projects/clr/hipamd/src/hip_hcc.map.in @@ -473,6 +473,8 @@ local: hip_5.3 { global: hipDeviceSetLimit; + hiprtcGetBitcode; + hiprtcGetBitcodeSize; local: *; -} hip_5.2; \ No newline at end of file +} hip_5.2; diff --git a/projects/clr/hipamd/src/hiprtc/hiprtc.cpp b/projects/clr/hipamd/src/hiprtc/hiprtc.cpp index 4ed05d8608..ee68a82502 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtc.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtc.cpp @@ -108,13 +108,18 @@ hiprtcResult hiprtcCompileProgram(hiprtcProgram prog, int numOptions, const char auto* rtcProgram = hiprtc::RTCCompileProgram::as_RTCCompileProgram(prog); + bool fgpu_rdc = false; std::vector opt; opt.reserve(numOptions); for (int i = 0; i < numOptions; i++) { - opt.push_back(std::string(options[i])); + if(std::string(options[i]) == std::string("-fgpu-rdc")) { + fgpu_rdc = true; + } else { + opt.push_back(std::string(options[i])); + } } - if (!rtcProgram->compile(opt)) { + if (!rtcProgram->compile(opt, fgpu_rdc)) { HIPRTC_RETURN(HIPRTC_ERROR_COMPILATION); } @@ -227,6 +232,31 @@ hiprtcResult hiprtcVersion(int* major, int* minor) { HIPRTC_RETURN(HIPRTC_SUCCESS); } +hiprtcResult hiprtcGetBitcode (hiprtcProgram prog, char* bitcode) { + if (bitcode == nullptr) { + HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT); + } + + auto* rtcProgram = hiprtc::RTCCompileProgram::as_RTCCompileProgram(prog); + if (!rtcProgram->GetBitcode(bitcode)) { + HIPRTC_RETURN(HIPRTC_ERROR_INVALID_PROGRAM); + } + HIPRTC_RETURN(HIPRTC_SUCCESS); +} + +hiprtcResult hiprtcGetBitcodeSize(hiprtcProgram prog, size_t* bitcode_size) { + if (bitcode_size == nullptr) { + HIPRTC_RETURN(HIPRTC_ERROR_INVALID_INPUT); + } + + auto* rtcProgram = hiprtc::RTCCompileProgram::as_RTCCompileProgram(prog); + if (!rtcProgram->GetBitcodeSize(bitcode_size)) { + HIPRTC_RETURN(HIPRTC_ERROR_INVALID_PROGRAM); + } + + HIPRTC_RETURN(HIPRTC_SUCCESS); +} + hiprtcResult hiprtcLinkCreate(unsigned int num_options, hiprtcJIT_option* options_ptr, void** options_vals_pptr, hiprtcLinkState* hip_link_state_ptr) { HIPRTC_INIT_API(num_options, options_ptr, options_vals_pptr, hip_link_state_ptr); diff --git a/projects/clr/hipamd/src/hiprtc/hiprtc.def b/projects/clr/hipamd/src/hiprtc/hiprtc.def index 63856ac80e..9ad29c0140 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtc.def +++ b/projects/clr/hipamd/src/hiprtc/hiprtc.def @@ -14,3 +14,5 @@ hiprtcLinkAddFile hiprtcLinkAddData hiprtcLinkComplete hiprtcLinkDestroy +hiprtcGetBitcode +hiprtcGetBitcodeSize diff --git a/projects/clr/hipamd/src/hiprtc/hiprtc.map.in b/projects/clr/hipamd/src/hiprtc/hiprtc.map.in index 1b10bcd7a5..427c483340 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtc.map.in +++ b/projects/clr/hipamd/src/hiprtc/hiprtc.map.in @@ -16,6 +16,8 @@ global: hiprtcLinkAddData; hiprtcLinkComplete; hiprtcLinkDestroy; + hiprtcGetBitcode; + hiprtcGetBitcodeSize; local: *; }; diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp index 73c829bc00..be3710b8cd 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.cpp @@ -94,7 +94,7 @@ bool RTCProgram::findIsa() { } //RTC Compile Program Member Functions -RTCCompileProgram::RTCCompileProgram(std::string name_) : RTCProgram(name_) { +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)) { @@ -229,7 +229,7 @@ bool RTCCompileProgram::transformOptions() { amd::Monitor RTCProgram::lock_("HIPRTC Program", true); -bool RTCCompileProgram::compile(const std::vector& options) { +bool RTCCompileProgram::compile(const std::vector& options, bool fgpu_rdc) { amd::ScopedLock lock(lock_); // Lock, because LLVM is not multi threaded if (!addSource_impl()) { @@ -237,6 +237,8 @@ bool RTCCompileProgram::compile(const std::vector& options) { return false; } + 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()); @@ -246,14 +248,17 @@ bool RTCCompileProgram::compile(const std::vector& options) { return false; } - std::vector LLVMBitcode; - if (!compileToBitCode(compile_input_, isa_, compile_options_, build_log_, LLVMBitcode)) { + if (!compileToBitCode(compile_input_, isa_, compile_options_, build_log_, LLVMBitcode_)) { LogError("Error in hiprtc: unable to compile source to bitcode"); return false; } + if (fgpu_rdc_) { + return true; + } + std::string linkFileName = "linked"; - if (!addCodeObjData(link_input_, LLVMBitcode, linkFileName, AMD_COMGR_DATA_KIND_BC)) { + if (!addCodeObjData(link_input_, LLVMBitcode_, linkFileName, AMD_COMGR_DATA_KIND_BC)) { LogError("Error in hiprtc: unable to add linked code object"); return false; } @@ -348,6 +353,25 @@ bool RTCCompileProgram::getDemangledName(const char* name_expression, const char return false; } +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; +} + //RTC Link Program Member Functions RTCLinkProgram::RTCLinkProgram(std::string name) : RTCProgram(name) { if (amd::Comgr::create_data_set(&link_input_) != AMD_COMGR_STATUS_SUCCESS) { diff --git a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp index 763cb12695..99b4543af5 100644 --- a/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp +++ b/projects/clr/hipamd/src/hiprtc/hiprtcInternal.hpp @@ -128,6 +128,9 @@ class RTCCompileProgram : public RTCProgram { amd_comgr_data_set_t compile_input_; amd_comgr_data_set_t link_input_; + bool fgpu_rdc_; + std::vector LLVMBitcode_; + // Private Member functions bool addSource_impl(); bool addBuiltinHeader(); @@ -155,10 +158,12 @@ class RTCCompileProgram : public RTCProgram { // Public Member Functions bool addSource(const std::string& source, const std::string& name); bool addHeader(const std::string& source, const std::string& name); - bool compile(const std::vector& options); + bool compile(const std::vector& options, bool fgpu_rdc); bool getDemangledName(const char* name_expression, const char** loweredName); bool trackMangledName(std::string& name); + bool GetBitcode(char* bitcode); + bool GetBitcodeSize(size_t* bitcode_size); // Public Getter/Setters const std::vector& getExec() const { return executable_; } size_t getExecSize() const { return executable_.size(); }