|
| 1 | +///===- llvm/Frontend/Offloading/PropertySet.cpp --------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Frontend/Offloading/PropertySet.h" |
| 10 | +#include "llvm/Support/Base64.h" |
| 11 | +#include "llvm/Support/JSON.h" |
| 12 | +#include "llvm/Support/MemoryBufferRef.h" |
| 13 | + |
| 14 | +using namespace llvm; |
| 15 | +using namespace llvm::offloading; |
| 16 | + |
| 17 | +void llvm::offloading::writePropertiesToJSON( |
| 18 | + const PropertySetRegistry &PSRegistry, raw_ostream &Out) { |
| 19 | + json::OStream J(Out); |
| 20 | + J.object([&] { |
| 21 | + for (const auto &[CategoryName, PropSet] : PSRegistry) { |
| 22 | + J.attributeObject(CategoryName, [&] { |
| 23 | + for (const auto &[PropName, PropVal] : PropSet) { |
| 24 | + switch (PropVal.index()) { |
| 25 | + case 0: |
| 26 | + J.attribute(PropName, std::get<uint32_t>(PropVal)); |
| 27 | + break; |
| 28 | + case 1: |
| 29 | + J.attribute(PropName, encodeBase64(std::get<ByteArray>(PropVal))); |
| 30 | + break; |
| 31 | + default: |
| 32 | + llvm_unreachable("unsupported property type"); |
| 33 | + } |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +// note: createStringError has an overload that takes a format string, |
| 41 | +// but it uses llvm::format instead of llvm::formatv, which does |
| 42 | +// not work with json::Value. This is a helper function to use |
| 43 | +// llvm::formatv with createStringError. |
| 44 | +template <typename... Ts> auto createStringErrorV(Ts &&...Args) { |
| 45 | + return createStringError(formatv(std::forward<Ts>(Args)...)); |
| 46 | +} |
| 47 | + |
| 48 | +Expected<PropertyValue> |
| 49 | +readPropertyValueFromJSON(const json::Value &PropValueVal) { |
| 50 | + if (std::optional<uint64_t> Val = PropValueVal.getAsUINT64()) |
| 51 | + return PropertyValue(static_cast<uint32_t>(*Val)); |
| 52 | + |
| 53 | + if (std::optional<StringRef> Val = PropValueVal.getAsString()) { |
| 54 | + std::vector<char> Decoded; |
| 55 | + if (Error E = decodeBase64(*Val, Decoded)) |
| 56 | + return createStringErrorV("unable to base64 decode the string {0}: {1}", |
| 57 | + Val, toString(std::move(E))); |
| 58 | + return PropertyValue(ByteArray(Decoded.begin(), Decoded.end())); |
| 59 | + } |
| 60 | + |
| 61 | + return createStringErrorV("expected a uint64 or a string, got {0}", |
| 62 | + PropValueVal); |
| 63 | +} |
| 64 | + |
| 65 | +Expected<PropertySetRegistry> |
| 66 | +llvm::offloading::readPropertiesFromJSON(MemoryBufferRef Buf) { |
| 67 | + PropertySetRegistry Res; |
| 68 | + Expected<json::Value> V = json::parse(Buf.getBuffer()); |
| 69 | + if (Error E = V.takeError()) |
| 70 | + return E; |
| 71 | + |
| 72 | + const json::Object *O = V->getAsObject(); |
| 73 | + if (!O) |
| 74 | + return createStringErrorV( |
| 75 | + "error while deserializing property set registry: " |
| 76 | + "expected JSON object, got {0}", |
| 77 | + *V); |
| 78 | + |
| 79 | + for (const auto &[CategoryName, Value] : *O) { |
| 80 | + const json::Object *PropSetVal = Value.getAsObject(); |
| 81 | + if (!PropSetVal) |
| 82 | + return createStringErrorV("error while deserializing property set {0}: " |
| 83 | + "expected JSON array, got {1}", |
| 84 | + CategoryName.str(), Value); |
| 85 | + |
| 86 | + PropertySet &PropSet = Res[CategoryName.str()]; |
| 87 | + for (const auto &[PropName, PropValueVal] : *PropSetVal) { |
| 88 | + Expected<PropertyValue> Prop = readPropertyValueFromJSON(PropValueVal); |
| 89 | + if (Error E = Prop.takeError()) |
| 90 | + return createStringErrorV( |
| 91 | + "error while deserializing property {0} in property set {1}: {2}", |
| 92 | + PropName.str(), CategoryName.str(), toString(std::move(E))); |
| 93 | + |
| 94 | + auto [It, Inserted] = |
| 95 | + PropSet.try_emplace(PropName.str(), std::move(*Prop)); |
| 96 | + assert(Inserted && "Property already exists in PropertySet"); |
| 97 | + } |
| 98 | + } |
| 99 | + return Res; |
| 100 | +} |
0 commit comments