From f6a1b0247ddb7ee571c35c7c15be5d31695ee697 Mon Sep 17 00:00:00 2001 From: Ross Brunton Date: Mon, 7 Jul 2025 16:10:19 +0100 Subject: [PATCH 1/3] [Offload] Allow "tagging" device info entries with offload keys When generating the device info tree, nodes can be marked with an offload Device Info value. The nodes can also look up children based on this value. --- offload/plugins-nextgen/amdgpu/src/rtl.cpp | 11 +++++--- .../common/include/PluginInterface.h | 27 ++++++++++++++++--- offload/plugins-nextgen/cuda/src/rtl.cpp | 8 +++--- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/offload/plugins-nextgen/amdgpu/src/rtl.cpp b/offload/plugins-nextgen/amdgpu/src/rtl.cpp index 832c31c43b5d2..52ea3283b24ef 100644 --- a/offload/plugins-nextgen/amdgpu/src/rtl.cpp +++ b/offload/plugins-nextgen/amdgpu/src/rtl.cpp @@ -2562,7 +2562,8 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy { Status2 = hsa_system_get_info(HSA_SYSTEM_INFO_VERSION_MINOR, &Minor); if (Status == HSA_STATUS_SUCCESS && Status2 == HSA_STATUS_SUCCESS) Info.add("HSA Runtime Version", - std::to_string(Major) + "." + std::to_string(Minor)); + std::to_string(Major) + "." + std::to_string(Minor), "", + DeviceInfo::DRIVER_VERSION); Info.add("HSA OpenMP Device Number", DeviceId); @@ -2572,11 +2573,11 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy { Status = getDeviceAttrRaw(HSA_AGENT_INFO_NAME, TmpChar); if (Status == HSA_STATUS_SUCCESS) - Info.add("Device Name", TmpChar); + Info.add("Device Name", TmpChar, "", DeviceInfo::NAME); Status = getDeviceAttrRaw(HSA_AGENT_INFO_VENDOR_NAME, TmpChar); if (Status == HSA_STATUS_SUCCESS) - Info.add("Vendor Name", TmpChar); + Info.add("Vendor Name", TmpChar, "", DeviceInfo::VENDOR); hsa_device_type_t DevType; Status = getDeviceAttrRaw(HSA_AGENT_INFO_DEVICE, DevType); @@ -2652,7 +2653,9 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy { Status = getDeviceAttrRaw(HSA_AGENT_INFO_WORKGROUP_MAX_DIM, WorkgrpMaxDim); if (Status == HSA_STATUS_SUCCESS) { - auto &MaxSize = *Info.add("Workgroup Max Size per Dimension"); + auto &MaxSize = + *Info.add("Workgroup Max Size per Dimension", std::monostate{}, "", + DeviceInfo::MAX_WORK_GROUP_SIZE); MaxSize.add("x", WorkgrpMaxDim[0]); MaxSize.add("y", WorkgrpMaxDim[1]); MaxSize.add("z", WorkgrpMaxDim[2]); diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h index 162b149ab483e..331f74d564eeb 100644 --- a/offload/plugins-nextgen/common/include/PluginInterface.h +++ b/offload/plugins-nextgen/common/include/PluginInterface.h @@ -113,6 +113,12 @@ struct AsyncInfoWrapperTy { __tgt_async_info *AsyncInfoPtr; }; +enum class DeviceInfo { +#define OFFLOAD_DEVINFO(Name, _, Value) Name = Value, +#include "OffloadInfo.inc" +#undef OFFLOAD_DEVINFO +}; + /// Tree node for device information /// /// This information is either printed or used by liboffload to extract certain @@ -133,6 +139,8 @@ struct InfoTreeNode { // * The same key can appear multiple times std::unique_ptr> Children; + std::map DeviceInfoMap; + InfoTreeNode() : InfoTreeNode("", std::monostate{}, "") {} InfoTreeNode(std::string Key, VariantType Value, std::string Units) : Key(Key), Value(Value), Units(Units) {} @@ -140,10 +148,12 @@ struct InfoTreeNode { /// Add a new info entry as a child of this node. The entry requires at least /// a key string in \p Key. The value in \p Value is optional and can be any /// type that is representable as a string. The units in \p Units is optional - /// and must be a string. + /// and must be a string. Providing a device info key allows liboffload to + /// use that value for an appropriate olGetDeviceInfo query template InfoTreeNode *add(std::string Key, T Value = T(), - const std::string &Units = std::string()) { + const std::string &Units = std::string(), + std::optional DeviceInfoKey = std::nullopt) { assert(!Key.empty() && "Invalid info key"); if (!Children) @@ -157,7 +167,12 @@ struct InfoTreeNode { else ValueVariant = std::string{Value}; - return &Children->emplace_back(Key, ValueVariant, Units); + auto Ptr = &Children->emplace_back(Key, ValueVariant, Units); + + if (DeviceInfoKey) + DeviceInfoMap[*DeviceInfoKey] = Children->size() - 1; + + return Ptr; } std::optional get(StringRef Key) { @@ -171,6 +186,12 @@ struct InfoTreeNode { return It; } + std::optional get(DeviceInfo Info) { + if (DeviceInfoMap.count(Info)) + return &(*Children)[DeviceInfoMap[Info]]; + return std::nullopt; + } + /// Print all info entries in the tree void print() const { // Fake an additional indent so that values are offset from the keys diff --git a/offload/plugins-nextgen/cuda/src/rtl.cpp b/offload/plugins-nextgen/cuda/src/rtl.cpp index 53089df2d0f0d..df56c2bfa8824 100644 --- a/offload/plugins-nextgen/cuda/src/rtl.cpp +++ b/offload/plugins-nextgen/cuda/src/rtl.cpp @@ -935,13 +935,14 @@ struct CUDADeviceTy : public GenericDeviceTy { if (Res == CUDA_SUCCESS) // For consistency with other drivers, store the version as a string // rather than an integer - Info.add("CUDA Driver Version", std::to_string(TmpInt)); + Info.add("CUDA Driver Version", std::to_string(TmpInt), "", + DeviceInfo::DRIVER_VERSION); Info.add("CUDA OpenMP Device Number", DeviceId); Res = cuDeviceGetName(TmpChar, 1000, Device); if (Res == CUDA_SUCCESS) - Info.add("Device Name", TmpChar); + Info.add("Device Name", TmpChar, "", DeviceInfo::NAME); Info.add("Vendor Name", "NVIDIA"); @@ -978,7 +979,8 @@ struct CUDADeviceTy : public GenericDeviceTy { if (Res == CUDA_SUCCESS) Info.add("Maximum Threads per Block", TmpInt); - auto &MaxBlock = *Info.add("Maximum Block Dimensions", ""); + auto &MaxBlock = *Info.add("Maximum Block Dimensions", std::monostate{}, "", + DeviceInfo::MAX_WORK_GROUP_SIZE); Res = getDeviceAttrRaw(CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, TmpInt); if (Res == CUDA_SUCCESS) MaxBlock.add("x", TmpInt); From 0319cd2f05289796a95deeb65372708a4c692ec5 Mon Sep 17 00:00:00 2001 From: Ross Brunton Date: Tue, 8 Jul 2025 10:11:57 +0100 Subject: [PATCH 2/3] Change map type --- offload/plugins-nextgen/common/include/PluginInterface.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h index 331f74d564eeb..8c17a2ee07047 100644 --- a/offload/plugins-nextgen/common/include/PluginInterface.h +++ b/offload/plugins-nextgen/common/include/PluginInterface.h @@ -139,7 +139,7 @@ struct InfoTreeNode { // * The same key can appear multiple times std::unique_ptr> Children; - std::map DeviceInfoMap; + llvm::DenseMap DeviceInfoMap; InfoTreeNode() : InfoTreeNode("", std::monostate{}, "") {} InfoTreeNode(std::string Key, VariantType Value, std::string Units) @@ -187,8 +187,9 @@ struct InfoTreeNode { } std::optional get(DeviceInfo Info) { - if (DeviceInfoMap.count(Info)) - return &(*Children)[DeviceInfoMap[Info]]; + auto Result = DeviceInfoMap.find(Info); + if (Result != DeviceInfoMap.end()) + return &(*Children)[Result->second]; return std::nullopt; } From 00aab9f48e9313ef48c4178e6e2d1fa3c1013a16 Mon Sep 17 00:00:00 2001 From: Ross Brunton Date: Wed, 9 Jul 2025 11:37:08 +0100 Subject: [PATCH 3/3] Tag nvidia vendor name --- offload/plugins-nextgen/cuda/src/rtl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/offload/plugins-nextgen/cuda/src/rtl.cpp b/offload/plugins-nextgen/cuda/src/rtl.cpp index df56c2bfa8824..bf8272925a811 100644 --- a/offload/plugins-nextgen/cuda/src/rtl.cpp +++ b/offload/plugins-nextgen/cuda/src/rtl.cpp @@ -944,7 +944,7 @@ struct CUDADeviceTy : public GenericDeviceTy { if (Res == CUDA_SUCCESS) Info.add("Device Name", TmpChar, "", DeviceInfo::NAME); - Info.add("Vendor Name", "NVIDIA"); + Info.add("Vendor Name", "NVIDIA", "", DeviceInfo::VENDOR); Res = cuDeviceTotalMem(&TmpSt, Device); if (Res == CUDA_SUCCESS)