[Mlir-commits] [mlir] Revert "Support polynomial attributes with floating point coefficient… (PR #92001)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon May 13 10:19:38 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Jeremy Kun (j2kun)
<details>
<summary>Changes</summary>
…s (#<!-- -->91137)"
This reverts commit 91a14dbf825b79ff143d1b16124763a4a80facab.
Not sure how to fix the build error this introduced, so reverting until I can figure it out
https://lab.llvm.org/buildbot/#/builders/264/builds/10468
---
Patch is 56.98 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/92001.diff
8 Files Affected:
- (modified) mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h (+43-150)
- (modified) mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td (+52-87)
- (modified) mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp (+51-27)
- (modified) mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp (+96-76)
- (modified) mlir/test/Dialect/Polynomial/attributes.mlir (+14-8)
- (modified) mlir/test/Dialect/Polynomial/ops.mlir (+31-33)
- (modified) mlir/test/Dialect/Polynomial/ops_errors.mlir (+33-33)
- (modified) mlir/test/Dialect/Polynomial/types.mlir (+28-37)
``````````diff
diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h
index 2b3f0e105c6c5..3325a6fa3f9fc 100644
--- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h
+++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h
@@ -11,13 +11,10 @@
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
-#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Hashing.h"
-#include "llvm/ADT/SmallString.h"
-#include "llvm/ADT/Twine.h"
-#include "llvm/Support/raw_ostream.h"
+#include "llvm/ADT/SmallVector.h"
namespace mlir {
@@ -30,202 +27,98 @@ namespace polynomial {
/// would want to specify 128-bit polynomials statically in the source code.
constexpr unsigned apintBitWidth = 64;
-template <typename CoefficientType>
-class MonomialBase {
+/// A class representing a monomial of a single-variable polynomial with integer
+/// coefficients.
+class Monomial {
public:
- MonomialBase(const CoefficientType &coeff, const APInt &expo)
+ Monomial(int64_t coeff, uint64_t expo)
+ : coefficient(apintBitWidth, coeff), exponent(apintBitWidth, expo) {}
+
+ Monomial(const APInt &coeff, const APInt &expo)
: coefficient(coeff), exponent(expo) {}
- virtual ~MonomialBase() = 0;
- const CoefficientType &getCoefficient() const { return coefficient; }
- CoefficientType &getMutableCoefficient() { return coefficient; }
- const APInt &getExponent() const { return exponent; }
- void setCoefficient(const CoefficientType &coeff) { coefficient = coeff; }
- void setExponent(const APInt &exp) { exponent = exp; }
+ Monomial() : coefficient(apintBitWidth, 0), exponent(apintBitWidth, 0) {}
- bool operator==(const MonomialBase &other) const {
+ bool operator==(const Monomial &other) const {
return other.coefficient == coefficient && other.exponent == exponent;
}
- bool operator!=(const MonomialBase &other) const {
+ bool operator!=(const Monomial &other) const {
return other.coefficient != coefficient || other.exponent != exponent;
}
/// Monomials are ordered by exponent.
- bool operator<(const MonomialBase &other) const {
+ bool operator<(const Monomial &other) const {
return (exponent.ult(other.exponent));
}
- virtual bool isMonic() const = 0;
- virtual void
- coefficientToString(llvm::SmallString<16> &coeffString) const = 0;
-
- template <typename T>
- friend ::llvm::hash_code hash_value(const MonomialBase<T> &arg);
+ friend ::llvm::hash_code hash_value(const Monomial &arg);
-protected:
- CoefficientType coefficient;
- APInt exponent;
-};
-
-/// A class representing a monomial of a single-variable polynomial with integer
-/// coefficients.
-class IntMonomial : public MonomialBase<APInt> {
public:
- IntMonomial(int64_t coeff, uint64_t expo)
- : MonomialBase(APInt(apintBitWidth, coeff), APInt(apintBitWidth, expo)) {}
-
- IntMonomial()
- : MonomialBase(APInt(apintBitWidth, 0), APInt(apintBitWidth, 0)) {}
-
- ~IntMonomial() = default;
-
- bool isMonic() const override { return coefficient == 1; }
+ APInt coefficient;
- void coefficientToString(llvm::SmallString<16> &coeffString) const override {
- coefficient.toStringSigned(coeffString);
- }
+ // Always unsigned
+ APInt exponent;
};
-/// A class representing a monomial of a single-variable polynomial with integer
-/// coefficients.
-class FloatMonomial : public MonomialBase<APFloat> {
+/// A single-variable polynomial with integer coefficients.
+///
+/// Eg: x^1024 + x + 1
+///
+/// The symbols used as the polynomial's indeterminate don't matter, so long as
+/// it is used consistently throughout the polynomial.
+class Polynomial {
public:
- FloatMonomial(double coeff, uint64_t expo)
- : MonomialBase(APFloat(coeff), APInt(apintBitWidth, expo)) {}
-
- FloatMonomial() : MonomialBase(APFloat((double)0), APInt(apintBitWidth, 0)) {}
+ Polynomial() = delete;
- ~FloatMonomial() = default;
+ explicit Polynomial(ArrayRef<Monomial> terms) : terms(terms){};
- bool isMonic() const override { return coefficient == APFloat(1.0); }
-
- void coefficientToString(llvm::SmallString<16> &coeffString) const override {
- coefficient.toString(coeffString);
- }
-};
-
-template <typename Monomial>
-class PolynomialBase {
-public:
- PolynomialBase() = delete;
+ // Returns a Polynomial from a list of monomials.
+ // Fails if two monomials have the same exponent.
+ static FailureOr<Polynomial> fromMonomials(ArrayRef<Monomial> monomials);
- explicit PolynomialBase(ArrayRef<Monomial> terms) : terms(terms){};
+ /// Returns a polynomial with coefficients given by `coeffs`. The value
+ /// coeffs[i] is converted to a monomial with exponent i.
+ static Polynomial fromCoefficients(ArrayRef<int64_t> coeffs);
explicit operator bool() const { return !terms.empty(); }
- bool operator==(const PolynomialBase &other) const {
+ bool operator==(const Polynomial &other) const {
return other.terms == terms;
}
- bool operator!=(const PolynomialBase &other) const {
+ bool operator!=(const Polynomial &other) const {
return !(other.terms == terms);
}
- void print(raw_ostream &os, ::llvm::StringRef separator,
- ::llvm::StringRef exponentiation) const {
- bool first = true;
- for (const Monomial &term : getTerms()) {
- if (first) {
- first = false;
- } else {
- os << separator;
- }
- std::string coeffToPrint;
- if (term.isMonic() && term.getExponent().uge(1)) {
- coeffToPrint = "";
- } else {
- llvm::SmallString<16> coeffString;
- term.coefficientToString(coeffString);
- coeffToPrint = coeffString.str();
- }
-
- if (term.getExponent() == 0) {
- os << coeffToPrint;
- } else if (term.getExponent() == 1) {
- os << coeffToPrint << "x";
- } else {
- llvm::SmallString<16> expString;
- term.getExponent().toStringSigned(expString);
- os << coeffToPrint << "x" << exponentiation << expString;
- }
- }
- }
-
// Prints polynomial to 'os'.
- void print(raw_ostream &os) const { print(os, " + ", "**"); }
-
+ void print(raw_ostream &os) const;
+ void print(raw_ostream &os, ::llvm::StringRef separator,
+ ::llvm::StringRef exponentiation) const;
void dump() const;
// Prints polynomial so that it can be used as a valid identifier
- std::string toIdentifier() const {
- std::string result;
- llvm::raw_string_ostream os(result);
- print(os, "_", "");
- return os.str();
- }
+ std::string toIdentifier() const;
- unsigned getDegree() const {
- return terms.back().getExponent().getZExtValue();
- }
+ unsigned getDegree() const;
ArrayRef<Monomial> getTerms() const { return terms; }
- template <typename T>
- friend ::llvm::hash_code hash_value(const PolynomialBase<T> &arg);
+ friend ::llvm::hash_code hash_value(const Polynomial &arg);
private:
// The monomial terms for this polynomial.
SmallVector<Monomial> terms;
};
-/// A single-variable polynomial with integer coefficients.
-///
-/// Eg: x^1024 + x + 1
-class IntPolynomial : public PolynomialBase<IntMonomial> {
-public:
- explicit IntPolynomial(ArrayRef<IntMonomial> terms) : PolynomialBase(terms) {}
-
- // Returns a Polynomial from a list of monomials.
- // Fails if two monomials have the same exponent.
- static FailureOr<IntPolynomial>
- fromMonomials(ArrayRef<IntMonomial> monomials);
-
- /// Returns a polynomial with coefficients given by `coeffs`. The value
- /// coeffs[i] is converted to a monomial with exponent i.
- static IntPolynomial fromCoefficients(ArrayRef<int64_t> coeffs);
-};
-
-/// A single-variable polynomial with double coefficients.
-///
-/// Eg: 1.0 x^1024 + 3.5 x + 1e-05
-class FloatPolynomial : public PolynomialBase<FloatMonomial> {
-public:
- explicit FloatPolynomial(ArrayRef<FloatMonomial> terms)
- : PolynomialBase(terms) {}
-
- // Returns a Polynomial from a list of monomials.
- // Fails if two monomials have the same exponent.
- static FailureOr<FloatPolynomial>
- fromMonomials(ArrayRef<FloatMonomial> monomials);
-
- /// Returns a polynomial with coefficients given by `coeffs`. The value
- /// coeffs[i] is converted to a monomial with exponent i.
- static FloatPolynomial fromCoefficients(ArrayRef<double> coeffs);
-};
-
-// Make Polynomials hashable.
-template <typename T>
-inline ::llvm::hash_code hash_value(const PolynomialBase<T> &arg) {
+// Make Polynomial hashable.
+inline ::llvm::hash_code hash_value(const Polynomial &arg) {
return ::llvm::hash_combine_range(arg.terms.begin(), arg.terms.end());
}
-template <typename T>
-inline ::llvm::hash_code hash_value(const MonomialBase<T> &arg) {
+inline ::llvm::hash_code hash_value(const Monomial &arg) {
return llvm::hash_combine(::llvm::hash_value(arg.coefficient),
::llvm::hash_value(arg.exponent));
}
-template <typename T>
-inline raw_ostream &operator<<(raw_ostream &os,
- const PolynomialBase<T> &polynomial) {
+inline raw_ostream &operator<<(raw_ostream &os, const Polynomial &polynomial) {
polynomial.print(os);
return os;
}
diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td
index ae8484501a50d..ed1f4ce8b7e59 100644
--- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td
+++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td
@@ -39,14 +39,14 @@ def Polynomial_Dialect : Dialect {
%a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring>
// A constant polynomial in a ring with i32 coefficients, modulo (x^1024 + 1)
- #modulus = #polynomial.int_polynomial<1 + x**1024>
+ #modulus = #polynomial.polynomial<1 + x**1024>
#ring = #polynomial.ring<coefficientType=i32, polynomialModulus=#modulus>
%a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring>
// A constant polynomial in a ring with i32 coefficients, with a polynomial
// modulus of (x^1024 + 1) and a coefficient modulus of 17.
- #modulus = #polynomial.int_polynomial<1 + x**1024>
- #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=17:i32, polynomialModulus=#modulus>
+ #modulus = #polynomial.polynomial<1 + x**1024>
+ #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=17, polynomialModulus=#modulus>
%a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring>
```
}];
@@ -60,12 +60,12 @@ class Polynomial_Attr<string name, string attrMnemonic, list<Trait> traits = []>
let mnemonic = attrMnemonic;
}
-def Polynomial_IntPolynomialAttr : Polynomial_Attr<"IntPolynomial", "int_polynomial"> {
- let summary = "An attribute containing a single-variable polynomial with integer coefficients.";
+def Polynomial_PolynomialAttr : Polynomial_Attr<"Polynomial", "polynomial"> {
+ let summary = "An attribute containing a single-variable polynomial.";
let description = [{
- A polynomial attribute represents a single-variable polynomial with integer
- coefficients, which is used to define the modulus of a `RingAttr`, as well
- as to define constants and perform constant folding for `polynomial` ops.
+ A polynomial attribute represents a single-variable polynomial, which
+ is used to define the modulus of a `RingAttr`, as well as to define constants
+ and perform constant folding for `polynomial` ops.
The polynomial must be expressed as a list of monomial terms, with addition
or subtraction between them. The choice of variable name is arbitrary, but
@@ -76,32 +76,10 @@ def Polynomial_IntPolynomialAttr : Polynomial_Attr<"IntPolynomial", "int_polynom
Example:
```mlir
- #poly = #polynomial.int_polynomial<x**1024 + 1>
+ #poly = #polynomial.polynomial<x**1024 + 1>
```
}];
- let parameters = (ins "::mlir::polynomial::IntPolynomial":$polynomial);
- let hasCustomAssemblyFormat = 1;
-}
-
-def Polynomial_FloatPolynomialAttr : Polynomial_Attr<"FloatPolynomial", "float_polynomial"> {
- let summary = "An attribute containing a single-variable polynomial with double precision floating point coefficients.";
- let description = [{
- A polynomial attribute represents a single-variable polynomial with double
- precision floating point coefficients.
-
- The polynomial must be expressed as a list of monomial terms, with addition
- or subtraction between them. The choice of variable name is arbitrary, but
- must be consistent across all the monomials used to define a single
- attribute. The order of monomial terms is arbitrary, each monomial degree
- must occur at most once.
-
- Example:
-
- ```mlir
- #poly = #polynomial.float_polynomial<0.5 x**7 + 1.5>
- ```
- }];
- let parameters = (ins "FloatPolynomial":$polynomial);
+ let parameters = (ins "::mlir::polynomial::Polynomial":$polynomial);
let hasCustomAssemblyFormat = 1;
}
@@ -126,9 +104,9 @@ def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> {
`x**1024 - 1`.
```mlir
- #poly_mod = #polynomial.int_polynomial<-1 + x**1024>
+ #poly_mod = #polynomial.polynomial<-1 + x**1024>
#ring = #polynomial.ring<coefficientType=i32,
- coefficientModulus=4294967291:i32,
+ coefficientModulus=4294967291,
polynomialModulus=#poly_mod>
%0 = ... : polynomial.polynomial<#ring>
@@ -145,24 +123,19 @@ def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> {
let parameters = (ins
"Type": $coefficientType,
OptionalParameter<"::mlir::IntegerAttr">: $coefficientModulus,
- OptionalParameter<"::mlir::polynomial::IntPolynomialAttr">: $polynomialModulus,
+ OptionalParameter<"::mlir::polynomial::PolynomialAttr">: $polynomialModulus,
OptionalParameter<"::mlir::IntegerAttr">: $primitiveRoot
);
- let assemblyFormat = "`<` struct(params) `>`";
+
let builders = [
- AttrBuilderWithInferredContext<
+ AttrBuilder<
(ins "::mlir::Type":$coefficientTy,
- CArg<"::mlir::IntegerAttr", "nullptr"> :$coefficientModulusAttr,
- CArg<"::mlir::polynomial::IntPolynomialAttr", "nullptr"> :$polynomialModulusAttr,
- CArg<"::mlir::IntegerAttr", "nullptr"> :$primitiveRootAttr), [{
- return $_get(
- coefficientTy.getContext(),
- coefficientTy,
- coefficientModulusAttr,
- polynomialModulusAttr,
- primitiveRootAttr);
- }]>,
+ "::mlir::IntegerAttr":$coefficientModulusAttr,
+ "::mlir::polynomial::PolynomialAttr":$polynomialModulusAttr), [{
+ return $_get($_ctxt, coefficientTy, coefficientModulusAttr, polynomialModulusAttr, nullptr);
+ }]>
];
+ let hasCustomAssemblyFormat = 1;
}
class Polynomial_Type<string name, string typeMnemonic>
@@ -176,7 +149,7 @@ def Polynomial_PolynomialType : Polynomial_Type<"Polynomial", "polynomial"> {
A type for polynomials in a polynomial quotient ring.
}];
let parameters = (ins Polynomial_RingAttr:$ring);
- let assemblyFormat = "`<` struct(params) `>`";
+ let assemblyFormat = "`<` $ring `>`";
}
def PolynomialLike: TypeOrContainer<Polynomial_PolynomialType, "polynomial-like">;
@@ -214,10 +187,10 @@ def Polynomial_AddOp : Polynomial_BinaryOp<"add", [Commutative]> {
```mlir
// add two polynomials modulo x^1024 - 1
- #poly = #polynomial.int_polynomial<x**1024 - 1>
- #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536:i32, polynomialModulus=#poly>
- %0 = polynomial.constant #polynomial.int_polynomial<1 + x**2> : !polynomial.polynomial<#ring>
- %1 = polynomial.constant #polynomial.int_polynomial<x**5 - x + 1> : !polynomial.polynomial<#ring>
+ #poly = #polynomial.polynomial<x**1024 - 1>
+ #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536, polynomialModulus=#poly>
+ %0 = polynomial.constant #polynomial.polynomial<1 + x**2> : !polynomial.polynomial<#ring>
+ %1 = polynomial.constant #polynomial.polynomial<x**5 - x + 1> : !polynomial.polynomial<#ring>
%2 = polynomial.add %0, %1 : !polynomial.polynomial<#ring>
```
}];
@@ -238,10 +211,10 @@ def Polynomial_SubOp : Polynomial_BinaryOp<"sub"> {
```mlir
// subtract two polynomials modulo x^1024 - 1
- #poly = #polynomial.int_polynomial<x**1024 - 1>
- #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536:i32, polynomialModulus=#poly>
- %0 = polynomial.constant #polynomial.int_polynomial<1 + x**2> : !polynomial.polynomial<#ring>
- %1 = polynomial.constant #polynomial.int_polynomial<x**5 - x + 1> : !polynomial.polynomial<#ring>
+ #poly = #polynomial.polynomial<x**1024 - 1>
+ #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536, polynomialModulus=#poly>
+ %0 = polynomial.constant #polynomial.polynomial<1 + x**2> : !polynomial.polynomial<#ring>
+ %1 = polynomial.constant #polynomial.polynomial<x**5 - x + 1> : !polynomial.polynomial<#ring>
%2 = polynomial.sub %0, %1 : !polynomial.polynomial<#ring>
```
}];
@@ -262,10 +235,10 @@ def Polynomial_MulOp : Polynomial_BinaryOp<"mul", [Commutative]> {
```mlir
// multiply two polynomials modulo x^1024 - 1
- #poly = #polynomial.int_polynomial<x**1024 - 1>
- #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536:i32, polynomialModulus=#poly>
- %0 = polynomial.constant #polynomial.int_polynomial<1 + x**2> : !polynomial.polynomial<#ring>
- %1 = polynomial.constant #polynomial.int_polynomial<x**5 - x + 1> : !polynomial.polynomial<#ring>
+ #poly = #polynomial.polynomial<x**1024 - 1>
+ #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536, polynomialModulus=#poly>
+ %0 = polynomial.constant #polynomial.polynomial<1 + x**2> : !polynomial.polynomial<#ring>
+ %1 = polynomial.constant #polynomial.polynomial<x**5 - x + 1> : !polynomial.polynomial<#ring>
%2 = polynomial.mul %0, %1 : !polynomial.polynomial<#ring>
```
}];
@@ -287,9 +260,9 @@ def Polynomial_MulScalarOp : Polynomial_Op<"mul_scalar", [
```mlir
// multiply two polynomials modulo x^1024 - 1
- #poly = #polynomial.int_polynomial<x**1024 - 1>
- #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536:i32, polynomialModulus=#poly>
- %0 = polynomial.constant #polynomial.int_polynomial<1 + x**2> : !polynomial.polynomial<#ring>
+ #poly = #polynomial.polynomial<x**1024 - 1>
+ #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536, polynomialModulus=#poly>
+ %0 = polynomial.constant #polynomial.polynomial<1 + x**2> : !polynomial.polynomial<#ring>
%1 = arith.constant 3 : i32
%2 = polynomial.mul_scalar %0, %1 : !polynomial.polynomial<#ring>, i32
```
@@ -318,9 +291,9 @@ def Polynomial_LeadingTermOp: Polynomial_Op<"leading_term"> {
Example:
```mlir
- #poly = #polynomial.int_polynomial<x**1024 - 1>
- #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536:i32, polynomialModulus=#poly>
- %0 = polynomial.constant #polynomial.int_polynomial<1 + x**2> : !polynomial.polynomial<#ring>
+ #poly = #polynomial.polynomial<x**1024 - 1>
+ #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536, polynomialModulus=#poly>
+ %0 = polynomial.constant #polynomial.polynomial<1 + x**2> : !polynomial.polynomial<#ring>
%1, %2 = polynomial.leading_term %0 : !polynomial.polynomial<#ring> -> (index, i32)
```
}];
@@ -341,8 +314,8 @@ def Polynomial_MonomialOp: Polynomial_Op<"monomial"> {
Example:
```mlir
- #poly = #polynomial.int_polynomial<x**1024 - 1>
- #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536:i32, polynomialModulus=#poly>
+ #poly = #polynomial.polynomial<x**1024 - 1>
+ #ring = #polynomial.ring<coefficientType=i32, coefficientModulus=65536, polynomialModulus=#poly>
%deg = arith.constant 1023 : index
%five = arith.constant 5 : i32
%0 = polynomial.monomial %five, %deg : (i32, index) -> !polynomial.polynomial<#ring>
@@ -381,8 +354,8 @@ def Polynomial_FromTensorOp : Polynomial_Op<"from_tensor", [Pure]> {
Example:
```mlir
- #poly = #polynomial.int_polynomial<x**1024 - 1>
- #ring = #polynomial.ring<coefficientType=i32, coeffi...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/92001
More information about the Mlir-commits
mailing list