[Mlir-commits] [clang] [llvm] [mlir] [NVPTX] Convert vector function nvvm.annotations to attributes (PR #127736)

Artem Belevich llvmlistbot at llvm.org
Wed Feb 19 11:54:32 PST 2025


================
@@ -196,6 +198,36 @@ static std::optional<unsigned> getFnAttrParsedInt(const Function &F,
              : std::nullopt;
 }
 
+static SmallVector<unsigned, 3> getFnAttrParsedVector(const Function &F,
+                                                      StringRef Attr) {
+  SmallVector<unsigned, 3> V;
+  auto &Ctx = F.getContext();
+
+  if (F.hasFnAttribute(Attr)) {
+    StringRef S = F.getFnAttribute(Attr).getValueAsString();
+    for (unsigned I = 0; I < 3 && !S.empty(); I++) {
+      auto [First, Rest] = S.split(",");
+      unsigned IntVal;
+      if (First.trim().getAsInteger(0, IntVal))
+        Ctx.emitError("can't parse integer attribute " + First + " in " + Attr);
+
+      V.push_back(IntVal);
+      S = Rest;
+    }
+  }
+  return V;
+}
+
+static std::optional<unsigned> getVectorProduct(ArrayRef<unsigned> V) {
+  if (V.empty())
+    return std::nullopt;
+
+  unsigned Product = 1;
+  for (const unsigned E : V)
+    Product *= E;
+  return Product;
+}
+
----------------
Artem-B wrote:

This looks like `std::accumulate()`:
```
  unsigned sourceDimProduct = std::accumulate(
      V.begin(), V.end(), 1, std::multiplies<unsigned>{});
```

Also, I'd use a larger data type -- it's too easy to unintentionally overflow things with multiplication.

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


More information about the Mlir-commits mailing list