[Mlir-commits] [mlir] Add a polynomial dialect shell, attributes, and types (PR #72081)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Sun Apr 7 23:59:56 PDT 2024


================
@@ -0,0 +1,81 @@
+//===- Polynomial.cpp - MLIR storage type for static Polynomial -*- C++ -*-===//
+//
+// 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 "mlir/Dialect/Polynomial/IR/Polynomial.h"
+
+#include "mlir/IR/MLIRContext.h"
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace mlir {
+namespace polynomial {
+
+Polynomial Polynomial::fromMonomials(ArrayRef<Monomial> monomials) {
+  // A polynomial's terms are canonically stored in order of increasing degree.
+  auto monomialsCopy = llvm::SmallVector<Monomial>(monomials);
+  std::sort(monomialsCopy.begin(), monomialsCopy.end());
+  return Polynomial(monomialsCopy);
+}
+
+Polynomial Polynomial::fromCoefficients(ArrayRef<int64_t> coeffs) {
+  llvm::SmallVector<Monomial> monomials;
+  monomials.reserve(coeffs.size());
+  for (size_t i = 0; i < coeffs.size(); i++) {
+    monomials.emplace_back(coeffs[i], i);
+  }
+  return Polynomial::fromMonomials(monomials);
+}
+
+void Polynomial::print(raw_ostream &os, ::llvm::StringRef separator,
+                       ::llvm::StringRef exponentiation) const {
+  bool first = true;
+  for (const auto &term : terms) {
----------------
ftynse wrote:

Nit: please expand `auto` unless the type is obvious from line context.

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


More information about the Mlir-commits mailing list