aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Frontend/Offloading/PropertySet.cpp
blob: a70290d4e475246952222fa0e9c346f0883d6ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
///===- llvm/Frontend/Offloading/PropertySet.cpp --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Frontend/Offloading/PropertySet.h"
#include "llvm/Support/Base64.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/MemoryBufferRef.h"

using namespace llvm;
using namespace llvm::offloading;

void llvm::offloading::writePropertiesToJSON(
    const PropertySetRegistry &PSRegistry, raw_ostream &Out) {
  json::OStream J(Out);
  J.object([&] {
    for (const auto &[CategoryName, PropSet] : PSRegistry) {
      auto PropSetCapture = PropSet;
      J.attributeObject(CategoryName, [&] {
        for (const auto &[PropName, PropVal] : PropSetCapture) {
          switch (PropVal.index()) {
          case 0:
            J.attribute(PropName, std::get<uint32_t>(PropVal));
            break;
          case 1:
            J.attribute(PropName, encodeBase64(std::get<ByteArray>(PropVal)));
            break;
          default:
            llvm_unreachable("unsupported property type");
          }
        }
      });
    }
  });
}

// note: createStringError has an overload that takes a format string,
// but it uses llvm::format instead of llvm::formatv, which does
// not work with json::Value. This is a helper function to use
// llvm::formatv with createStringError.
template <typename... Ts> auto createStringErrorV(Ts &&...Args) {
  return createStringError(formatv(std::forward<Ts>(Args)...));
}

Expected<PropertyValue>
readPropertyValueFromJSON(const json::Value &PropValueVal) {
  if (std::optional<uint64_t> Val = PropValueVal.getAsUINT64())
    return PropertyValue(static_cast<uint32_t>(*Val));

  if (std::optional<StringRef> Val = PropValueVal.getAsString()) {
    std::vector<char> Decoded;
    if (Error E = decodeBase64(*Val, Decoded))
      return createStringErrorV("unable to base64 decode the string {0}: {1}",
                                Val, toString(std::move(E)));
    return PropertyValue(ByteArray(Decoded.begin(), Decoded.end()));
  }

  return createStringErrorV("expected a uint64 or a string, got {0}",
                            PropValueVal);
}

Expected<PropertySetRegistry>
llvm::offloading::readPropertiesFromJSON(MemoryBufferRef Buf) {
  PropertySetRegistry Res;
  Expected<json::Value> V = json::parse(Buf.getBuffer());
  if (Error E = V.takeError())
    return E;

  const json::Object *O = V->getAsObject();
  if (!O)
    return createStringErrorV(
        "error while deserializing property set registry: "
        "expected JSON object, got {0}",
        *V);

  for (const auto &[CategoryName, Value] : *O) {
    const json::Object *PropSetVal = Value.getAsObject();
    if (!PropSetVal)
      return createStringErrorV("error while deserializing property set {0}: "
                                "expected JSON array, got {1}",
                                CategoryName.str(), Value);

    PropertySet &PropSet = Res[CategoryName.str()];
    for (const auto &[PropName, PropValueVal] : *PropSetVal) {
      Expected<PropertyValue> Prop = readPropertyValueFromJSON(PropValueVal);
      if (Error E = Prop.takeError())
        return createStringErrorV(
            "error while deserializing property {0} in property set {1}: {2}",
            PropName.str(), CategoryName.str(), toString(std::move(E)));

      auto [It, Inserted] =
          PropSet.try_emplace(PropName.str(), std::move(*Prop));
      assert(Inserted && "Property already exists in PropertySet");
      (void)Inserted;
    }
  }
  return Res;
}