Skip to content

Commit 54259a8

Browse files
committed
[Offload] Add type information to device info nodes
Rather than being "stringly typed", store values as a std::variant that can hold various types. This means that liboffload doesn't have to do any string parsing for integer/bool device info keys.
1 parent de33390 commit 54259a8

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,23 +228,23 @@ Error olGetDeviceInfoImplDetail(ol_device_handle_t Device,
228228
ReturnHelper ReturnValue(PropSize, PropValue, PropSizeRet);
229229

230230
// Find the info if it exists under any of the given names
231-
auto GetInfo = [&](std::vector<std::string> Names) {
231+
auto GetInfoString = [&](std::vector<std::string> Names) {
232232
if (Device == HostDevice())
233-
return std::string("Host");
233+
return "Host";
234234

235235
if (!Device->Device)
236-
return std::string("");
236+
return "";
237237

238238
auto Info = Device->Device->obtainInfoImpl();
239239
if (auto Err = Info.takeError())
240-
return std::string("");
240+
return "";
241241

242242
for (auto Name : Names) {
243243
if (auto Entry = Info->get(Name))
244-
return (*Entry)->Value;
244+
return std::get<std::string>((*Entry)->Value).c_str();
245245
}
246246

247-
return std::string("");
247+
return "";
248248
};
249249

250250
switch (PropName) {
@@ -254,12 +254,12 @@ Error olGetDeviceInfoImplDetail(ol_device_handle_t Device,
254254
return Device == HostDevice() ? ReturnValue(OL_DEVICE_TYPE_HOST)
255255
: ReturnValue(OL_DEVICE_TYPE_GPU);
256256
case OL_DEVICE_INFO_NAME:
257-
return ReturnValue(GetInfo({"Device Name"}).c_str());
257+
return ReturnValue(GetInfoString({"Device Name"}));
258258
case OL_DEVICE_INFO_VENDOR:
259-
return ReturnValue(GetInfo({"Vendor Name"}).c_str());
259+
return ReturnValue(GetInfoString({"Vendor Name"}));
260260
case OL_DEVICE_INFO_DRIVER_VERSION:
261261
return ReturnValue(
262-
GetInfo({"CUDA Driver Version", "HSA Runtime Version"}).c_str());
262+
GetInfoString({"CUDA Driver Version", "HSA Runtime Version"}));
263263
default:
264264
return createOffloadError(ErrorCode::INVALID_ENUMERATION,
265265
"getDeviceInfo enum '%i' is invalid", PropName);

offload/plugins-nextgen/common/include/PluginInterface.h

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <list>
1818
#include <map>
1919
#include <shared_mutex>
20+
#include <variant>
2021
#include <vector>
2122

2223
#include "ExclusiveAccess.h"
@@ -122,7 +123,9 @@ struct InfoTreeNode {
122123
static constexpr uint64_t IndentSize = 4;
123124

124125
std::string Key;
125-
std::string Value;
126+
struct None {};
127+
using VariantType = std::variant<uint64_t, std::string, bool, None>;
128+
VariantType Value;
126129
std::string Units;
127130
// Need to specify a default value number of elements here as `InfoTreeNode`'s
128131
// size is unknown. This is a vector (rather than a Key->Value map) since:
@@ -131,31 +134,31 @@ struct InfoTreeNode {
131134
// * The same key can appear multiple times
132135
std::unique_ptr<llvm::SmallVector<InfoTreeNode, 8>> Children;
133136

134-
InfoTreeNode() : InfoTreeNode("", "", "") {}
135-
InfoTreeNode(std::string Key, std::string Value, std::string Units)
137+
InfoTreeNode() : InfoTreeNode("", None{}, "") {}
138+
InfoTreeNode(std::string Key, VariantType Value, std::string Units)
136139
: Key(Key), Value(Value), Units(Units) {}
137140

138141
/// Add a new info entry as a child of this node. The entry requires at least
139142
/// a key string in \p Key. The value in \p Value is optional and can be any
140143
/// type that is representable as a string. The units in \p Units is optional
141144
/// and must be a string.
142-
template <typename T = std::string>
145+
template <typename T = None>
143146
InfoTreeNode *add(std::string Key, T Value = T(),
144147
const std::string &Units = std::string()) {
145148
assert(!Key.empty() && "Invalid info key");
146149

147150
if (!Children)
148151
Children = std::make_unique<llvm::SmallVector<InfoTreeNode, 8>>();
149152

150-
std::string ValueStr;
151-
if constexpr (std::is_same_v<T, bool>)
152-
ValueStr = Value ? "Yes" : "No";
153+
VariantType ValueVariant;
154+
if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, None>)
155+
ValueVariant = Value;
153156
else if constexpr (std::is_arithmetic_v<T>)
154-
ValueStr = std::to_string(Value);
157+
ValueVariant = static_cast<uint64_t>(Value);
155158
else
156-
ValueStr = Value;
159+
ValueVariant = std::string{Value};
157160

158-
return &Children->emplace_back(Key, ValueStr, Units);
161+
return &Children->emplace_back(Key, ValueVariant, Units);
159162
}
160163

161164
std::optional<InfoTreeNode *> get(StringRef Key) {
@@ -184,8 +187,23 @@ struct InfoTreeNode {
184187
MaxKeySize - (Key.size() + KeyIndentSize) + IndentSize;
185188

186189
llvm::outs() << std::string(KeyIndentSize, ' ') << Key
187-
<< std::string(ValIndentSize, ' ') << Value
188-
<< (Units.empty() ? "" : " ") << Units << "\n";
190+
<< std::string(ValIndentSize, ' ');
191+
std::visit(
192+
[](auto &&V) {
193+
using T = std::decay_t<decltype(V)>;
194+
if constexpr (std::is_same_v<T, std::string>)
195+
llvm::outs() << V;
196+
else if constexpr (std::is_same_v<T, bool>)
197+
llvm::outs() << (V ? "Yes" : "No");
198+
else if constexpr (std::is_same_v<T, uint64_t>)
199+
llvm::outs() << V;
200+
else if constexpr (std::is_same_v<T, None>) {
201+
// Do nothing
202+
} else
203+
static_assert(false, "doPrint visit not exhaustive");
204+
},
205+
Value);
206+
llvm::outs() << (Units.empty() ? "" : " ") << Units << "\n";
189207
}
190208

191209
// Print children

0 commit comments

Comments
 (0)