From c9955a1cea6883ce6693c60509856ad426bba287 Mon Sep 17 00:00:00 2001 From: Ajay Date: Tue, 4 Jun 2024 12:53:52 -0700 Subject: [PATCH] SWDEV-465215 - hipFuncSetAttribute hipFuncGetAttributes fixes Change-Id: I2151e4470d63918ff6b809a8fdeaae5bea5cc899 --- hipamd/src/hip_global.cpp | 18 ++++++------- hipamd/src/hip_module.cpp | 44 ++++++++++++++++++++++++++++---- rocclr/device/devkernel.cpp | 1 + rocclr/device/devkernel.hpp | 1 + rocclr/device/pal/palkernel.cpp | 2 ++ rocclr/device/rocm/rockernel.cpp | 2 ++ 6 files changed, 54 insertions(+), 14 deletions(-) diff --git a/hipamd/src/hip_global.cpp b/hipamd/src/hip_global.cpp index 1a4a7426c7..883a7adbad 100644 --- a/hipamd/src/hip_global.cpp +++ b/hipamd/src/hip_global.cpp @@ -183,21 +183,21 @@ hipError_t Function::getStatFuncAttr(hipFuncAttributes* func_attr, int deviceId) const std::vector& devices = amd::Device::getDevices(CL_DEVICE_TYPE_GPU, false); amd::Kernel* kernel = dFunc_[deviceId]->kernel(); - const device::Kernel::WorkGroupInfo* wginfo = kernel->getDeviceKernel(*devices[deviceId])->workGroupInfo(); + auto* device_handle = devices[deviceId]; + const device::Kernel::WorkGroupInfo* wginfo = + kernel->getDeviceKernel(*device_handle)->workGroupInfo(); + int binaryVersion = device_handle->isa().versionMajor() * 10 + + device_handle->isa().versionMinor(); func_attr->sharedSizeBytes = static_cast(wginfo->localMemSize_); - func_attr->binaryVersion = static_cast(kernel->signature().version()); + func_attr->binaryVersion = binaryVersion; func_attr->cacheModeCA = 0; - func_attr->constSizeBytes = 0; + func_attr->constSizeBytes = wginfo->constMemSize_ - 1; func_attr->localSizeBytes = wginfo->privateMemSize_; - func_attr->maxDynamicSharedSizeBytes = static_cast(wginfo->availableLDSSize_ - - wginfo->localMemSize_); - + func_attr->maxDynamicSharedSizeBytes = wginfo->maxDynamicSharedSizeBytes_; func_attr->maxThreadsPerBlock = static_cast(wginfo->size_); func_attr->numRegs = static_cast(wginfo->usedVGPRs_); func_attr->preferredShmemCarveout = 0; - func_attr->ptxVersion = 30; - - + func_attr->ptxVersion = binaryVersion; return hipSuccess; } diff --git a/hipamd/src/hip_module.cpp b/hipamd/src/hip_module.cpp index 9c325d73a8..35a0120c10 100644 --- a/hipamd/src/hip_module.cpp +++ b/hipamd/src/hip_module.cpp @@ -139,7 +139,7 @@ hipError_t hipFuncGetAttribute(int* value, hipFunction_attribute attrib, hipFunc *value = static_cast(wrkGrpInfo->size_); break; case HIP_FUNC_ATTRIBUTE_CONST_SIZE_BYTES: - *value = 0; + *value = static_cast(wrkGrpInfo->constMemSize_ - 1); break; case HIP_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES: *value = static_cast(wrkGrpInfo->privateMemSize_); @@ -148,10 +148,9 @@ hipError_t hipFuncGetAttribute(int* value, hipFunction_attribute attrib, hipFunc *value = static_cast(wrkGrpInfo->usedVGPRs_); break; case HIP_FUNC_ATTRIBUTE_PTX_VERSION: - *value = 30; // Defaults to 3.0 as HCC - break; case HIP_FUNC_ATTRIBUTE_BINARY_VERSION: - *value = static_cast(kernel->signature().version()); + *value = hip::getCurrentDevice()->devices()[0]->isa().versionMajor() * 10 + + hip::getCurrentDevice()->devices()[0]->isa().versionMinor(); break; case HIP_FUNC_ATTRIBUTE_CACHE_MODE_CA: *value = 0; @@ -180,7 +179,42 @@ hipError_t hipFuncGetAttributes(hipFuncAttributes* attr, const void* func) { hipError_t hipFuncSetAttribute(const void* func, hipFuncAttribute attr, int value) { HIP_INIT_API(hipFuncSetAttribute, func, attr, value); - // No way to set function attribute yet. + if (func == nullptr) { + HIP_RETURN(hipErrorInvalidDeviceFunction); + } + if (attr < 0 || attr > hipFuncAttributeMax) { + HIP_RETURN(hipErrorInvalidValue); + } + + hipFunction_t h_func; + HIP_RETURN_ONFAIL(PlatformState::instance().getStatFunc(&h_func, func, ihipGetDevice())); + + hip::DeviceFunc* function = hip::DeviceFunc::asFunction(h_func); + if (function == nullptr) { + HIP_RETURN(hipErrorInvalidHandle); + } + amd::Kernel* kernel = reinterpret_cast(function)->kernel(); + + if (kernel == nullptr) { + HIP_RETURN(hipErrorInvalidDeviceFunction); + } + device::Kernel* d_kernel = + (device::Kernel*)(kernel->getDeviceKernel( + *(hip::getCurrentDevice()->devices()[0]))); + + if (attr == hipFuncAttributeMaxDynamicSharedMemorySize) { + if ((value < 0) || (value > (d_kernel->workGroupInfo()->availableLDSSize_ - + d_kernel->workGroupInfo()->localMemSize_))) { + HIP_RETURN(hipErrorInvalidValue); + } + d_kernel->workGroupInfo()->maxDynamicSharedSizeBytes_ = value; + } + + if (attr == hipFuncAttributePreferredSharedMemoryCarveout) { + if (value < -1 || value > 100) { + HIP_RETURN(hipErrorInvalidValue); + } + } HIP_RETURN(hipSuccess); } diff --git a/rocclr/device/devkernel.cpp b/rocclr/device/devkernel.cpp index e40b5b5946..9f91080883 100644 --- a/rocclr/device/devkernel.cpp +++ b/rocclr/device/devkernel.cpp @@ -624,6 +624,7 @@ Kernel::Kernel(const amd::Device& dev, const std::string& name, const Program& p workGroupInfo_.uniformWorkGroupSize_ = false; workGroupInfo_.wavesPerSimdHint_ = 0; workGroupInfo_.constMemSize_ = 0; + workGroupInfo_.maxDynamicSharedSizeBytes_ = 0; } // ================================================================================================ diff --git a/rocclr/device/devkernel.hpp b/rocclr/device/devkernel.hpp index 0b69f992cd..cd8709a9fa 100644 --- a/rocclr/device/devkernel.hpp +++ b/rocclr/device/devkernel.hpp @@ -387,6 +387,7 @@ class Kernel : public amd::HeapObject { size_t compileSizeHint_[3]; //!< kernel compiled workgroup size hint size_t wavesPerSimdHint_; //!< waves per simd hit size_t constMemSize_; //!< size of user-allocated constant memory + size_t maxDynamicSharedSizeBytes_; std::string compileVecTypeHint_; //!< kernel compiled vector type hint int maxOccupancyPerCu_; //!< Max occupancy per compute unit in threads diff --git a/rocclr/device/pal/palkernel.cpp b/rocclr/device/pal/palkernel.cpp index 3ea7048dd0..817b300e22 100644 --- a/rocclr/device/pal/palkernel.cpp +++ b/rocclr/device/pal/palkernel.cpp @@ -65,6 +65,8 @@ void HSAILKernel::setWorkGroupInfo(const uint32_t privateSegmentSize, workGroupInfo_.availableVGPRs_ = 256; workGroupInfo_.preferredSizeMultiple_ = workGroupInfo_.wavefrontPerSIMD_ = 64; } + workGroupInfo_.maxDynamicSharedSizeBytes_ = static_cast(workGroupInfo_.availableLDSSize_ - + workGroupInfo_.localMemSize_); } bool HSAILKernel::setKernelCode(amd::hsa::loader::Symbol* sym, amd_kernel_code_t* akc) { diff --git a/rocclr/device/rocm/rockernel.cpp b/rocclr/device/rocm/rockernel.cpp index 19c688e17e..ad55e08b85 100644 --- a/rocclr/device/rocm/rockernel.cpp +++ b/rocclr/device/rocm/rockernel.cpp @@ -188,6 +188,8 @@ bool LightningKernel::postLoad() { workGroupInfo_.wavefrontPerSIMD_ = program()->rocDevice().info().maxWorkItemSizes_[0] / wavefront_size; workGroupInfo_.wavefrontSize_ = wavefront_size; workGroupInfo_.constMemSize_ = const_size_bytes; + workGroupInfo_.maxDynamicSharedSizeBytes_ = static_cast(workGroupInfo_.availableLDSSize_ - + workGroupInfo_.localMemSize_); if (workGroupInfo_.size_ == 0) { return false; }