-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SYCL] Add property set types and JSON representation #147321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| ///===- llvm/Frontend/Offloading/PropertySet.h ----------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| ///===---------------------------------------------------------------------===// | ||
| /// \file This file defines PropertySetRegistry and PropertyValue types and | ||
| /// provides helper functions to translate PropertySetRegistry from/to JSON. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/ADT/SmallVector.h" | ||
| #include "llvm/Support/Error.h" | ||
|
|
||
| #include <map> | ||
| #include <variant> | ||
|
|
||
| namespace llvm { | ||
| class raw_ostream; | ||
| class MemoryBufferRef; | ||
|
|
||
| namespace offloading { | ||
|
|
||
| using ByteArray = SmallVector<unsigned char, 0>; | ||
| using PropertyValue = std::variant<uint32_t, ByteArray>; | ||
| using PropertySet = std::map<std::string, PropertyValue>; | ||
| using PropertySetRegistry = std::map<std::string, PropertySet>; | ||
|
|
||
| void writePropertiesToJSON(const PropertySetRegistry &P, raw_ostream &O); | ||
| Expected<PropertySetRegistry> readPropertiesFromJSON(MemoryBufferRef Buf); | ||
|
|
||
| } // namespace offloading | ||
| } // namespace llvm | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| ///===- 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) { | ||
| J.attributeObject(CategoryName, [&] { | ||
| for (const auto &[PropName, PropVal] : PropSet) { | ||
| 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"); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| template <typename... Ts> auto createStringErrorV(Ts &&...Args) { | ||
| return createStringError(formatv(Args...)); | ||
jzc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Expected<PropertyValue> | ||
| readPropertyValueFromJSON(const json::Value &PropValueVal) { | ||
| if (std::optional<uint64_t> Val = PropValueVal.getAsUINT64()) { | ||
| return PropertyValue(static_cast<uint32_t>(*Val)); | ||
| } | ||
jzc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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"); | ||
| } | ||
| } | ||
| return Res; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //===- llvm/unittest/Frontend/PropertySetRegistry.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/ADT/SmallVector.h" | ||
| #include "llvm/Frontend/Offloading/PropertySet.h" | ||
| #include "llvm/Support/MemoryBuffer.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace llvm::offloading; | ||
| using namespace llvm; | ||
|
|
||
| void checkSerialization(const PropertySetRegistry &PSR) { | ||
| SmallString<0> Serialized; | ||
| raw_svector_ostream OS(Serialized); | ||
| writePropertiesToJSON(PSR, OS); | ||
| auto PSR2 = readPropertiesFromJSON({Serialized, ""}); | ||
| ASSERT_EQ("", toString(PSR2.takeError())); | ||
| EXPECT_EQ(PSR, *PSR2); | ||
| } | ||
|
|
||
| TEST(PropertySetRegistryTest, PropertySetRegistry) { | ||
| PropertySetRegistry PSR; | ||
| checkSerialization(PSR); | ||
|
|
||
| PSR["Category1"]["Prop1"] = 42U; | ||
| PSR["Category1"]["Prop2"] = ByteArray(StringRef("Hello").bytes()); | ||
| PSR["Category2"]["A"] = ByteArray{0, 4, 16, 32, 255}; | ||
| checkSerialization(PSR); | ||
|
|
||
| PSR = PropertySetRegistry(); | ||
| PSR["ABC"]["empty_array"] = ByteArray(); | ||
| PSR["ABC"]["max_val"] = std::numeric_limits<uint32_t>::max(); | ||
| checkSerialization(PSR); | ||
| } | ||
|
|
||
| TEST(PropertySetRegistryTest, IllFormedJSON) { | ||
| SmallString<0> Input; | ||
|
|
||
| // Invalid json | ||
| Input = "{ invalid }"; | ||
| auto Res = readPropertiesFromJSON({Input, ""}); | ||
| EXPECT_NE("", toString(Res.takeError())); | ||
|
|
||
| Input = ""; | ||
| Res = readPropertiesFromJSON({Input, ""}); | ||
| EXPECT_NE("", toString(Res.takeError())); | ||
|
|
||
| // Not a JSON object | ||
| Input = "[1, 2, 3]"; | ||
| Res = readPropertiesFromJSON({Input, ""}); | ||
| EXPECT_NE("", toString(Res.takeError())); | ||
|
|
||
| // Property set not an object | ||
| Input = R"({ "Category": 42 })"; | ||
| Res = readPropertiesFromJSON({Input, ""}); | ||
| EXPECT_NE("", toString(Res.takeError())); | ||
|
|
||
| // Property value has non string/non-integer type | ||
| Input = R"({ "Category": { "Prop": [1, 2, 3] } })"; | ||
| Res = readPropertiesFromJSON({Input, ""}); | ||
| EXPECT_NE("", toString(Res.takeError())); | ||
|
|
||
| // Property value is an invalid base64 string | ||
| Input = R"({ "Category": { "Prop": ";" } })"; | ||
| Res = readPropertiesFromJSON({Input, ""}); | ||
| EXPECT_NE("", toString(Res.takeError())); | ||
|
|
||
| Input = R"({ "Category": { "Prop": "!@#$" } })"; | ||
| Res = readPropertiesFromJSON({Input, ""}); | ||
| EXPECT_NE("", toString(Res.takeError())); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these go in the
.cppfile?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like remove from them from the .h? These types appear in the signatures of
readPropertiesFromJSON/writePropertiesFromJSON, so I don't think I shouldThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't see their uses, but if they're needed then it's fine.