[llvm] [SYCL][LLVM] Adding property set I/O library for SYCL (PR #110771)
Arvind Sudarsanam via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 9 14:23:27 PDT 2024
================
@@ -0,0 +1,217 @@
+//==- PropertySetIO.cpp - models a sequence of property sets and their I/O -==//
+//
+// 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/Support/PropertySetIO.h"
+
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/Base64.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/LineIterator.h"
+
+#include <memory>
+#include <string>
+
+using namespace llvm::util;
+using namespace llvm;
+
+namespace {
+
+using byte = std::byte;
+
+::llvm::Error makeError(const Twine &Msg) {
+ return createStringError(std::error_code{}, Msg);
+}
+
+} // anonymous namespace
+
+Expected<std::unique_ptr<PropertySetRegistry>>
+PropertySetRegistry::read(const MemoryBuffer *Buf) {
+ auto Res = std::make_unique<PropertySetRegistry>();
+ PropertySet *CurPropSet = nullptr;
+
+ for (line_iterator LI(*Buf); !LI.is_at_end(); LI++) {
+ // see if this line starts a new property set
+ if (LI->starts_with("[")) {
+ // yes - parse the category (property name)
+ auto EndPos = LI->rfind(']');
+ if (EndPos == StringRef::npos)
+ return makeError("invalid line: " + *LI);
+ StringRef Category = LI->substr(1, EndPos - 1);
+ CurPropSet = &(*Res)[Category];
+ continue;
+ }
+ if (!CurPropSet)
+ return makeError("property category missing");
+ // parse name and type+value
+ auto Parts = LI->split('=');
+
+ if (Parts.first.empty() || Parts.second.empty())
+ return makeError("invalid property line: " + *LI);
+ auto TypeVal = Parts.second.split('|');
+
+ if (TypeVal.first.empty() || TypeVal.second.empty())
+ return makeError("invalid property value: " + Parts.second);
+ APInt Tint;
+
+ // parse type
+ if (TypeVal.first.getAsInteger(10, Tint))
+ return makeError("invalid property type: " + TypeVal.first);
+ Expected<PropertyValue::Type> Ttag =
+ PropertyValue::getTypeTag(static_cast<int>(Tint.getSExtValue()));
+ StringRef Val = TypeVal.second;
+
+ if (!Ttag)
+ return Ttag.takeError();
+ PropertyValue Prop(Ttag.get());
+
+ // parse value depending on its type
+ switch (Ttag.get()) {
+ case PropertyValue::Type::UINT32: {
+ APInt ValV;
+ if (Val.getAsInteger(10, ValV))
+ return createStringError(std::error_code{},
+ "invalid property value: ", Val.data());
+ Prop.set(static_cast<uint32_t>(ValV.getZExtValue()));
+ break;
+ }
+ case PropertyValue::Type::BYTE_ARRAY: {
+ Expected<std::unique_ptr<byte[]>> DecArr =
+ Base64::decode(Val.data(), Val.size());
+ if (!DecArr)
+ return DecArr.takeError();
+ Prop.set(DecArr.get().release());
+ break;
+ }
+ default:
+ return createStringError(std::error_code{},
+ "unsupported property type: ", Ttag.get());
+ }
+ (*CurPropSet)[Parts.first] = std::move(Prop);
+ }
+ if (!CurPropSet)
+ return makeError("invalid property set registry");
+
+ return Expected<std::unique_ptr<PropertySetRegistry>>(std::move(Res));
+}
+
+namespace llvm {
+// output a property to a stream
+raw_ostream &operator<<(raw_ostream &Out, const PropertyValue &Prop) {
+ Out << static_cast<int>(Prop.getType()) << "|";
+ switch (Prop.getType()) {
+ case PropertyValue::Type::UINT32:
+ Out << Prop.asUint32();
+ break;
+ case PropertyValue::Type::BYTE_ARRAY: {
+ util::PropertyValue::SizeTy Size = Prop.getRawByteArraySize();
+ Base64::encode(Prop.asRawByteArray(), Out, (size_t)Size);
+ break;
+ }
+ default:
+ llvm_unreachable(
+ ("unsupported property type: " + utostr(Prop.getType())).c_str());
+ }
+ return Out;
+}
+} // namespace llvm
+
+void PropertySetRegistry::write(raw_ostream &Out) const {
+ for (const auto &PropSet : PropSetMap) {
+ Out << "[" << PropSet.first << "]\n";
+
+ for (const auto &Props : PropSet.second) {
+ Out << Props.first << "=" << Props.second << "\n";
+ }
----------------
asudarsa wrote:
Addressed in https://github.com/llvm/llvm-project/commit/0b4ad716189c3fcfb6e77d299d12c11a3784af0b
Thanks
https://github.com/llvm/llvm-project/pull/110771
More information about the llvm-commits
mailing list