LLVM 22.0.0git
PropertySet.cpp
Go to the documentation of this file.
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
10#include "llvm/Support/Base64.h"
11#include "llvm/Support/JSON.h"
13
14using namespace llvm;
15using namespace llvm::offloading;
16
18 const PropertySetRegistry &PSRegistry, raw_ostream &Out) {
19 json::OStream J(Out);
20 J.object([&] {
21 for (const auto &[CategoryName, PropSet] : PSRegistry) {
22 auto PropSetCapture = PropSet;
23 J.attributeObject(CategoryName, [&] {
24 for (const auto &[PropName, PropVal] : PropSetCapture) {
25 switch (PropVal.index()) {
26 case 0:
27 J.attribute(PropName, std::get<uint32_t>(PropVal));
28 break;
29 case 1:
30 J.attribute(PropName, encodeBase64(std::get<ByteArray>(PropVal)));
31 break;
32 default:
33 llvm_unreachable("unsupported property type");
34 }
35 }
36 });
37 }
38 });
39}
40
41// note: createStringError has an overload that takes a format string,
42// but it uses llvm::format instead of llvm::formatv, which does
43// not work with json::Value. This is a helper function to use
44// llvm::formatv with createStringError.
45template <typename... Ts> auto createStringErrorV(Ts &&...Args) {
46 return createStringError(formatv(std::forward<Ts>(Args)...));
47}
48
51 if (std::optional<uint64_t> Val = PropValueVal.getAsUINT64())
52 return PropertyValue(static_cast<uint32_t>(*Val));
53
54 if (std::optional<StringRef> Val = PropValueVal.getAsString()) {
55 std::vector<char> Decoded;
56 if (Error E = decodeBase64(*Val, Decoded))
57 return createStringErrorV("unable to base64 decode the string {0}: {1}",
58 Val, toString(std::move(E)));
59 return PropertyValue(ByteArray(Decoded.begin(), Decoded.end()));
60 }
61
62 return createStringErrorV("expected a uint64 or a string, got {0}",
63 PropValueVal);
64}
65
70 if (Error E = V.takeError())
71 return E;
72
73 const json::Object *O = V->getAsObject();
74 if (!O)
75 return createStringErrorV(
76 "error while deserializing property set registry: "
77 "expected JSON object, got {0}",
78 *V);
79
80 for (const auto &[CategoryName, Value] : *O) {
81 const json::Object *PropSetVal = Value.getAsObject();
82 if (!PropSetVal)
83 return createStringErrorV("error while deserializing property set {0}: "
84 "expected JSON array, got {1}",
85 CategoryName.str(), Value);
86
87 PropertySet &PropSet = Res[CategoryName.str()];
88 for (const auto &[PropName, PropValueVal] : *PropSetVal) {
90 if (Error E = Prop.takeError())
91 return createStringErrorV(
92 "error while deserializing property {0} in property set {1}: {2}",
93 PropName.str(), CategoryName.str(), toString(std::move(E)));
94
95 auto [It, Inserted] =
96 PropSet.try_emplace(PropName.str(), std::move(*Prop));
97 assert(Inserted && "Property already exists in PropertySet");
98 (void)Inserted;
99 }
100 }
101 return Res;
102}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file supports working with JSON data.
Expected< PropertyValue > readPropertyValueFromJSON(const json::Value &PropValueVal)
Definition: PropertySet.cpp:50
auto createStringErrorV(Ts &&...Args)
Definition: PropertySet.cpp:45
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
Error takeError()
Take ownership of the stored error.
Definition: Error.h:612
StringRef getBuffer() const
LLVM Value Representation.
Definition: Value.h:75
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition: JSON.h:996
void object(Block Contents)
Emit an object whose elements are emitted in the provided Block.
Definition: JSON.h:1026
void attributeObject(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an object with attributes from the Block.
Definition: JSON.h:1059
An Object is a JSON object, which maps strings to heterogenous JSON values.
Definition: JSON.h:98
A Value is an JSON value of unknown type.
Definition: JSON.h:288
std::optional< uint64_t > getAsUINT64() const
Definition: JSON.h:445
std::optional< llvm::StringRef > getAsString() const
Definition: JSON.h:455
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
LLVM_ABI llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition: JSON.cpp:684
LLVM_ABI Expected< PropertySetRegistry > readPropertiesFromJSON(MemoryBufferRef Buf)
Definition: PropertySet.cpp:67
std::map< std::string, PropertyValue > PropertySet
Definition: PropertySet.h:27
LLVM_ABI void writePropertiesToJSON(const PropertySetRegistry &P, raw_ostream &O)
Definition: PropertySet.cpp:17
std::map< std::string, PropertySet > PropertySetRegistry
Definition: PropertySet.h:28
std::variant< uint32_t, ByteArray > PropertyValue
Definition: PropertySet.h:26
SmallVector< unsigned char, 0 > ByteArray
Definition: PropertySet.h:25
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1305
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI llvm::Error decodeBase64(llvm::StringRef Input, std::vector< char > &Output)
Definition: Base64.cpp:37
const char * toString(DWARFSectionKind Kind)