Skip to content

Commit 96de816

Browse files
committed
[SYCL] Add property set types and JSON representation
1 parent 543f948 commit 96de816

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
///===- llvm/Frontend/Offloading/PropertySet.h ----------------------------===//
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+
/// \file This file defines PropertySetRegistry and PropertyValue types and
9+
/// provides helper functions to translate PropertySetRegistry from/to JSON.
10+
//===----------------------------------------------------------------------===//
11+
12+
#include "llvm/ADT/SmallVector.h"
13+
#include "llvm/Support/Error.h"
14+
15+
#include <map>
16+
#include <variant>
17+
18+
namespace llvm {
19+
class raw_ostream;
20+
class MemoryBufferRef;
21+
22+
namespace offloading {
23+
24+
using ByteArray = SmallVector<unsigned char, 0>;
25+
using PropertyValue = std::variant<uint32_t, ByteArray>;
26+
using PropertySet = std::map<std::string, PropertyValue>;
27+
using PropertySetRegistry = std::map<std::string, PropertySet>;
28+
29+
void writePropertiesToJSON(const PropertySetRegistry &P, raw_ostream &O);
30+
Expected<PropertySetRegistry> readPropertiesFromJSON(MemoryBufferRef Buf);
31+
32+
} // namespace offloading
33+
} // namespace llvm

llvm/lib/Frontend/Offloading/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_llvm_component_library(LLVMFrontendOffloading
22
Utility.cpp
33
OffloadWrapper.cpp
4+
PropertySet.cpp
45

56
ADDITIONAL_HEADER_DIRS
67
${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
template <typename... Ts> auto createStringErrorV(Ts &&...Args) {
41+
return createStringError(formatv(Args...));
42+
}
43+
44+
Expected<PropertyValue>
45+
readPropertyValueFromJSON(const json::Value &PropValueVal) {
46+
if (std::optional<uint64_t> Val = PropValueVal.getAsUINT64()) {
47+
return PropertyValue(static_cast<uint32_t>(*Val));
48+
}
49+
50+
if (std::optional<StringRef> Val = PropValueVal.getAsString()) {
51+
std::vector<char> Decoded;
52+
if (Error E = decodeBase64(*Val, Decoded))
53+
return createStringErrorV("unable to base64 decode the string {0}: {1}",
54+
Val, toString(std::move(E)));
55+
return PropertyValue(ByteArray(Decoded.begin(), Decoded.end()));
56+
}
57+
58+
return createStringErrorV("expected a uint64 or a string, got {0}",
59+
PropValueVal);
60+
}
61+
62+
Expected<PropertySetRegistry>
63+
llvm::offloading::readPropertiesFromJSON(MemoryBufferRef Buf) {
64+
PropertySetRegistry Res;
65+
Expected<json::Value> V = json::parse(Buf.getBuffer());
66+
if (Error E = V.takeError())
67+
return E;
68+
69+
const json::Object *O = V->getAsObject();
70+
if (!O)
71+
return createStringErrorV(
72+
"error while deserializing property set registry: "
73+
"expected JSON object, got {0}",
74+
*V);
75+
76+
for (const auto &[CategoryName, Value] : *O) {
77+
const json::Object *PropSetVal = Value.getAsObject();
78+
if (!PropSetVal)
79+
return createStringErrorV("error while deserializing property set {0}: "
80+
"expected JSON array, got {1}",
81+
CategoryName.str(), Value);
82+
83+
PropertySet &PropSet = Res[CategoryName.str()];
84+
for (const auto &[PropName, PropValueVal] : *PropSetVal) {
85+
Expected<PropertyValue> Prop = readPropertyValueFromJSON(PropValueVal);
86+
if (Error E = Prop.takeError())
87+
return createStringErrorV(
88+
"error while deserializing property {0} in property set {1}: {2}",
89+
PropName.str(), CategoryName.str(), toString(std::move(E)));
90+
91+
auto [It, Inserted] =
92+
PropSet.try_emplace(PropName.str(), std::move(*Prop));
93+
assert(Inserted && "Property already exists in PropertySet");
94+
}
95+
}
96+
return Res;
97+
}

llvm/unittests/Frontend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_llvm_unittest(LLVMFrontendTests
2121
OpenMPDecompositionTest.cpp
2222
OpenMPDirectiveNameTest.cpp
2323
OpenMPDirectiveNameParserTest.cpp
24+
PropertySetRegistryTest.cpp
2425

2526
DEPENDS
2627
acc_gen
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===- llvm/unittest/Frontend/PropertySetRegistry.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/ADT/SmallVector.h"
10+
#include "llvm/Frontend/Offloading/PropertySet.h"
11+
#include "llvm/Support/MemoryBuffer.h"
12+
#include "gtest/gtest.h"
13+
14+
using namespace llvm::offloading;
15+
using namespace llvm;
16+
17+
void checkSerialization(const PropertySetRegistry &PSR) {
18+
SmallString<0> Serialized;
19+
raw_svector_ostream OS(Serialized);
20+
writePropertiesToJSON(PSR, OS);
21+
auto PSR2 = readPropertiesFromJSON({Serialized, ""});
22+
ASSERT_EQ("", toString(PSR2.takeError()));
23+
EXPECT_EQ(PSR, *PSR2);
24+
}
25+
26+
TEST(PropertySetRegistryTest, PropertySetRegistry) {
27+
PropertySetRegistry PSR;
28+
checkSerialization(PSR);
29+
30+
PSR["Category1"]["Prop1"] = 42U;
31+
PSR["Category1"]["Prop2"] = ByteArray(StringRef("Hello").bytes());
32+
PSR["Category2"]["A"] = ByteArray{0, 4, 16, 32, 255};
33+
checkSerialization(PSR);
34+
35+
PSR = PropertySetRegistry();
36+
PSR["ABC"]["empty_array"] = ByteArray();
37+
PSR["ABC"]["max_val"] = std::numeric_limits<uint32_t>::max();
38+
checkSerialization(PSR);
39+
}
40+
41+
TEST(PropertySetRegistryTest, IllFormedJSON) {
42+
SmallString<0> Input;
43+
44+
// Invalid json
45+
Input = "{ invalid }";
46+
auto Res = readPropertiesFromJSON({Input, ""});
47+
EXPECT_NE("", toString(Res.takeError()));
48+
49+
Input = "";
50+
Res = readPropertiesFromJSON({Input, ""});
51+
EXPECT_NE("", toString(Res.takeError()));
52+
53+
// Not a JSON object
54+
Input = "[1, 2, 3]";
55+
Res = readPropertiesFromJSON({Input, ""});
56+
EXPECT_NE("", toString(Res.takeError()));
57+
58+
// Property set not an object
59+
Input = R"({ "Category": 42 })";
60+
Res = readPropertiesFromJSON({Input, ""});
61+
EXPECT_NE("", toString(Res.takeError()));
62+
63+
// Property value has non string/non-integer type
64+
Input = R"({ "Category": { "Prop": [1, 2, 3] } })";
65+
Res = readPropertiesFromJSON({Input, ""});
66+
EXPECT_NE("", toString(Res.takeError()));
67+
68+
// Property value is an invalid base64 string
69+
Input = R"({ "Category": { "Prop": ";" } })";
70+
Res = readPropertiesFromJSON({Input, ""});
71+
EXPECT_NE("", toString(Res.takeError()));
72+
73+
Input = R"({ "Category": { "Prop": "!@#$" } })";
74+
Res = readPropertiesFromJSON({Input, ""});
75+
EXPECT_NE("", toString(Res.takeError()));
76+
}

0 commit comments

Comments
 (0)