[Mlir-commits] [mlir] Add a polynomial dialect shell, attributes, and types (PR #72081)
Jeremy Kun
llvmlistbot at llvm.org
Mon Nov 13 10:21:19 PST 2023
================
@@ -0,0 +1,97 @@
+//===- Polynomial.cpp - MLIR storage type for static Polynomial
+//--------------===//
+//
+// 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 "PolynomialDetail.h"
+#include "mlir/IR/MLIRContext.h"
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace mlir {
+namespace polynomial {
+
+MLIRContext *Polynomial::getContext() const { return terms->context; }
+
+ArrayRef<Monomial> Polynomial::getTerms() const { return terms->terms(); }
+
+Polynomial Polynomial::fromMonomials(ArrayRef<Monomial> monomials,
+ MLIRContext *context) {
+ auto assignCtx = [context](detail::PolynomialStorage *storage) {
+ storage->context = context;
+ };
+
+ // A polynomial's terms are canonically stored in order of increasing degree.
+ llvm::OwningArrayRef<Monomial> monomialsCopy =
+ llvm::OwningArrayRef<Monomial>(monomials);
+ std::sort(monomialsCopy.begin(), monomialsCopy.end());
+
+ StorageUniquer &uniquer = context->getAttributeUniquer();
+ return Polynomial(uniquer.get<detail::PolynomialStorage>(
+ assignCtx, monomials.size(), monomialsCopy));
+}
+
+Polynomial Polynomial::fromCoefficients(ArrayRef<int64_t> coeffs,
+ MLIRContext *context) {
+ std::vector<Monomial> monomials;
+ for (size_t i = 0; i < coeffs.size(); i++) {
+ monomials.emplace_back(coeffs[i], i);
+ }
+ return Polynomial::fromMonomials(monomials, context);
+}
+
+void Polynomial::print(raw_ostream &os, const std::string &separator,
+ const std::string &exponentiation) const {
+ bool first = true;
+ for (const auto &term : terms->terms()) {
+ if (first) {
+ first = false;
+ } else {
+ os << separator;
+ }
+ std::string coeffToPrint;
+ if (term.coefficient == 1 && term.exponent.uge(1)) {
+ coeffToPrint = "";
+ } else {
+ llvm::SmallString<512> coeffString;
----------------
j2kun wrote:
Changed to 16 characters in 758d4f098c91af75c5a8b019a070cf8bc14b5262
https://github.com/llvm/llvm-project/pull/72081
More information about the Mlir-commits
mailing list