Skip to content

Revert "Support polynomial attributes with floating point coefficients (#91137)" #92001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 43 additions & 150 deletions mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
Expand Down
Loading
Loading