[llvm] [SYCL][LLVM] Adding property set I/O library for SYCL (PR #110771)

Chris B via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 14:12:44 PDT 2024


================
@@ -0,0 +1,207 @@
+//= SYCLPropertySetIO.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/SYCLPropertySetIO.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 {
+
+::llvm::Error makeError(const Twine &Msg) {
+  return createStringError(std::error_code{}, Msg);
+}
+
+} // anonymous namespace
+
+Expected<std::unique_ptr<SYCLPropertySetRegistry>>
+SYCLPropertySetRegistry::read(const MemoryBuffer *Buf) {
+  auto Res = std::make_unique<SYCLPropertySetRegistry>();
+  SYCLPropertySet *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("[")) {
+      // 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<SYCLPropertyValue::Type> Ttag =
+        SYCLPropertyValue::getTypeTag(static_cast<int>(Tint.getSExtValue()));
+    StringRef Val = TypeVal.second;
+
+    if (!Ttag)
+      return Ttag.takeError();
+    SYCLPropertyValue Prop(Ttag.get());
+
+    // Parse value depending on its type
+    switch (Ttag.get()) {
+    case SYCLPropertyValue::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 SYCLPropertyValue::Type::BYTE_ARRAY: {
+      std::vector<char> Output;
----------------
llvm-beanz wrote:

```suggestion
      SmallVector<char> Output;
```
Are there common sizes or upper bounds for the base64 data?

If not, you should be able to pre-compute the worst-case size for the data from the base64 string's size, so you can resize the array accordingly to avoid intermediate resizes?

https://github.com/llvm/llvm-project/pull/110771


More information about the llvm-commits mailing list