diff --git a/hipamd/src/hip_clang.cpp b/hipamd/src/hip_clang.cpp index 635aa8391f..e8f3e86881 100644 --- a/hipamd/src/hip_clang.cpp +++ b/hipamd/src/hip_clang.cpp @@ -28,6 +28,7 @@ THE SOFTWARE. #include "hip_hcc_internal.h" #include "hip_fatbin.h" #include "trace_helper.h" +#include "program_state.inl" #ifdef __GNUC__ #pragma GCC visibility push (default) @@ -94,8 +95,10 @@ __hipRegisterFatBinary(const void* data) agent); if (module->executable.handle) { - modules->at(deviceId) = module; - tprintf(DB_FB, "Loaded code object for %s\n", name); + hip_impl::program_state_impl::read_kernarg_metadata(image, module->kernargs); + modules->at(deviceId) = module; + + tprintf(DB_FB, "Loaded code object for %s, args size=%ld\n", name, module->kernargs.size()); } else { fprintf(stderr, "Failed to load code object for %s\n", name); abort(); @@ -232,7 +235,7 @@ static DeviceVar* findVar(std::string hostVar, int deviceId, hipModule_t hmod) { } hipError_t ihipGetGlobalVar(hipDeviceptr_t* dev_ptr, size_t* size_ptr, - const char* hostVar, hipModule_t hmod) { + const char* hostVar, hipModule_t hmod) { GET_TLS(); auto ctx = ihipGetTlsDefaultCtx(); @@ -425,6 +428,41 @@ extern "C" hipError_t __hipPopCallConfiguration( return hipSuccess; } +int getCurrentDeviceId() +{ + GET_TLS(); + + int deviceId = 0; + auto ctx = ihipGetTlsDefaultCtx(); + + if(!ctx) return deviceId; + + LockedAccessor_CtxCrit_t crit(ctx->criticalData()); + + if(crit->_execStack.size() != 0) + { + auto &exec = crit->_execStack.top(); + + if (exec._hStream) { + deviceId = exec._hStream->getDevice()->_deviceId; + } else if (ctx->getDevice()) { + deviceId = ctx->getDevice()->_deviceId; + } + } else if (ctx->getDevice()) { + deviceId = ctx->getDevice()->_deviceId; + } + return deviceId; +} + +hipFunction_t ihipGetDeviceFunction(const void *hostFunction) +{ + int deviceId = getCurrentDeviceId(); + auto it = g_functions.find(hostFunction); + if (it == g_functions.end() || !it->second[deviceId]) { + return nullptr; + } + return it->second[deviceId]; +} hipError_t hipSetupArgument( const void *arg, diff --git a/hipamd/src/hip_hcc_internal.h b/hipamd/src/hip_hcc_internal.h index 8eec7292c2..104fd910a8 100644 --- a/hipamd/src/hip_hcc_internal.h +++ b/hipamd/src/hip_hcc_internal.h @@ -33,7 +33,7 @@ THE SOFTWARE. #include "hip_prof_api.h" #include "hip_util.h" #include "env.h" - +#include #if (__hcc_workweek__ < 16354) #error("This version of HIP requires a newer version of HCC."); @@ -1079,4 +1079,14 @@ static inline ihipCtx_t* iihipGetTlsDefaultCtx(TlsData* tls) { return tls->defaultCtx; } +/** + * @brief Get device function from host kernel function pointer + * Needed only for clang + HIP-HCC RT + * + * @param [in] hostFunction host kernel function pointer + * + * @returns hipFuntion_t, nullptr + */ +hipFunction_t ihipGetDeviceFunction(const void *hostFunction); + #endif diff --git a/hipamd/src/hip_module.cpp b/hipamd/src/hip_module.cpp index b6fbc45866..0a7348a3a2 100644 --- a/hipamd/src/hip_module.cpp +++ b/hipamd/src/hip_module.cpp @@ -344,6 +344,8 @@ hipError_t ihipExtLaunchMultiKernelMultiDevice(hipLaunchParams* launchParamsList free(kds); return hipErrorInvalidValue; } + if (!kds[i]->_kernarg_layout.empty()) continue; + hip_impl::kernargs_size_align kargs = ps.get_kernargs_size_align( reinterpret_cast(lp.func)); kds[i]->_kernarg_layout = *reinterpret_cast>*>( diff --git a/hipamd/src/program_state.inl b/hipamd/src/program_state.inl index d77abd9674..548a56795f 100644 --- a/hipamd/src/program_state.inl +++ b/hipamd/src/program_state.inl @@ -19,6 +19,8 @@ #include #include #include "hc.hpp" +#include "hip_hcc_internal.h" +#include "trace_helper.h" #include @@ -944,14 +946,16 @@ public: auto it0 = get_functions(agent).find(function_address); - if (it0 == get_functions(agent).cend()) { - hip_throw(std::runtime_error{ + if (it0 != get_functions(agent).cend()) return it0->second; + + // For hip-clang compiler + Hcc RT + hipFunction_t f = ihipGetDeviceFunction((const void*)function_address); + if (f) return reinterpret_cast(*f); + + hip_throw(std::runtime_error{ "No device code available for function: " + std::string(name(function_address)) + ", for agent: " + name(agent)}); - } - - return it0->second; } const std::vector>& diff --git a/hipamd/tests/src/gcc/LaunchKernel.c b/hipamd/tests/src/gcc/LaunchKernel.c index ef910a423e..d2fc854510 100644 --- a/hipamd/tests/src/gcc/LaunchKernel.c +++ b/hipamd/tests/src/gcc/LaunchKernel.c @@ -19,10 +19,10 @@ /* HIT_START - * BUILD_CMD: gpu.o %hc -I%hip-path/include -g -c %S/gpu.cpp -o %T/gpu.o EXCLUDE_HIP_PLATFORM nvcc vdi EXCLUDE_HIP_COMPILER clang - * BUILD_CMD: launchkernel.o %hc -D__HIP_PLATFORM_HCC__ -g -I%hip-path/include -c %S/LaunchKernel.c -o %T/launchkernel.o EXCLUDE_HIP_PLATFORM nvcc vdi EXCLUDE_HIP_COMPILER clang - * BUILD_CMD: LaunchKernel %hc %T/launchkernel.o %T/gpu.o -g -Wl,--rpath=%hip-path/lib %hip-path/lib/libhip_hcc.so -o %T/%t DEPENDS gpu.o launchkernel.o EXCLUDE_HIP_PLATFORM nvcc vdi EXCLUDE_HIP_COMPILER clang - * TEST: %t EXCLUDE_HIP_PLATFORM nvcc vdi EXCLUDE_HIP_COMPILER clang + * BUILD_CMD: gpu.o %hc -I%hip-path/include -g -c %S/gpu.cpp -o %T/gpu.o EXCLUDE_HIP_PLATFORM nvcc vdi + * BUILD_CMD: launchkernel.o %hc -D__HIP_PLATFORM_HCC__ -g -I%hip-path/include -c %S/LaunchKernel.c -o %T/launchkernel.o EXCLUDE_HIP_PLATFORM nvcc vdi + * BUILD_CMD: LaunchKernel %hc %T/launchkernel.o %T/gpu.o -g -Wl,--rpath=%hip-path/lib %hip-path/lib/libhip_hcc.so -o %T/%t DEPENDS gpu.o launchkernel.o EXCLUDE_HIP_PLATFORM nvcc vdi + * TEST: %t EXCLUDE_HIP_PLATFORM nvcc vdi * HIT_END */