From 1b8b05e8e03a31d89520e21bc84ac5aea5589b3c Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Sat, 4 Nov 2023 10:05:33 -0700 Subject: [PATCH 01/46] polynomial: add dialect shell, attrs, and types --- mlir/include/mlir/Dialect/CMakeLists.txt | 1 + .../mlir/Dialect/Polynomial/CMakeLists.txt | 1 + .../mlir/Dialect/Polynomial/IR/CMakeLists.txt | 19 ++ .../mlir/Dialect/Polynomial/IR/Polynomial.h | 157 +++++++++++++ .../Polynomial/IR/PolynomialAttributes.h | 18 ++ .../Polynomial/IR/PolynomialAttributes.td | 81 +++++++ .../Dialect/Polynomial/IR/PolynomialDialect.h | 19 ++ .../Polynomial/IR/PolynomialDialect.td | 54 +++++ .../Dialect/Polynomial/IR/PolynomialOps.h | 21 ++ .../Dialect/Polynomial/IR/PolynomialOps.td | 40 ++++ .../Dialect/Polynomial/IR/PolynomialTypes.h | 17 ++ .../Dialect/Polynomial/IR/PolynomialTypes.td | 32 +++ mlir/include/mlir/InitAllDialects.h | 2 + mlir/lib/Dialect/CMakeLists.txt | 1 + mlir/lib/Dialect/Polynomial/CMakeLists.txt | 1 + mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt | 21 ++ mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 97 ++++++++ .../Polynomial/IR/PolynomialAttributes.cpp | 217 ++++++++++++++++++ .../Dialect/Polynomial/IR/PolynomialDetail.h | 65 ++++++ .../Polynomial/IR/PolynomialDialect.cpp | 47 ++++ .../Dialect/Polynomial/IR/PolynomialOps.cpp | 16 ++ mlir/test/Dialect/Polynomial/types.td | 19 ++ 22 files changed, 946 insertions(+) create mode 100644 mlir/include/mlir/Dialect/Polynomial/CMakeLists.txt create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td create mode 100644 mlir/lib/Dialect/Polynomial/CMakeLists.txt create mode 100644 mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt create mode 100644 mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp create mode 100644 mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp create mode 100644 mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h create mode 100644 mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp create mode 100644 mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp create mode 100644 mlir/test/Dialect/Polynomial/types.td diff --git a/mlir/include/mlir/Dialect/CMakeLists.txt b/mlir/include/mlir/Dialect/CMakeLists.txt index 2da79011fa26a..4bd7f12fabf7b 100644 --- a/mlir/include/mlir/Dialect/CMakeLists.txt +++ b/mlir/include/mlir/Dialect/CMakeLists.txt @@ -28,6 +28,7 @@ add_subdirectory(OpenACCMPCommon) add_subdirectory(OpenMP) add_subdirectory(PDL) add_subdirectory(PDLInterp) +add_subdirectory(Polynomial) add_subdirectory(Quant) add_subdirectory(SCF) add_subdirectory(Shape) diff --git a/mlir/include/mlir/Dialect/Polynomial/CMakeLists.txt b/mlir/include/mlir/Dialect/Polynomial/CMakeLists.txt new file mode 100644 index 0000000000000..f33061b2d87cf --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(IR) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt new file mode 100644 index 0000000000000..3719fda2aec6d --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt @@ -0,0 +1,19 @@ +set(LLVM_TARGET_DEFINITIONS PolynomialOps.td) +mlir_tablegen(PolynomialDialect.cpp.inc -gen-dialect-defs -dialect=polynomial) +mlir_tablegen(PolynomialDialect.h.inc -gen-dialect-decls -dialect=polynomial) +add_public_tablegen_target(MLIRPolynomialDialectIncGen) + +mlir_tablegen(PolynomialAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=polynomial) +mlir_tablegen(PolynomialAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=polynomial) +mlir_tablegen(PolynomialOps.cpp.inc -gen-op-defs) +mlir_tablegen(PolynomialOps.h.inc -gen-op-decls) +mlir_tablegen(PolynomialTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=polynomial) +mlir_tablegen(PolynomialTypes.h.inc -gen-typedef-decls -typedefs-dialect=polynomial) +add_public_tablegen_target(MLIRPolynomialAttributesIncGen) +add_public_tablegen_target(MLIRPolynomialOpsIncGen) +add_public_tablegen_target(MLIRPolynomialTypesIncGen) +add_dependencies(mlir-headers MLIRPolynomialOpsIncGen) + +add_mlir_doc(PolynoialOps PolynoialOps Dialects/ -gen-dialect-doc -dialect polynomial) +add_mlir_doc(PolynomialAttributes PolynomialAttributes Dialects/ -gen-attrdef-doc) +add_mlir_doc(PolynomialTypes PolynomialTypes Dialects/ -gen-typedef-doc) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h new file mode 100644 index 0000000000000..757ee61ec8c7a --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -0,0 +1,157 @@ +//===- Polynomial.h - A storage class for polynomial types --------*- 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 +// +//===----------------------------------------------------------------------===// + +#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ +#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ + +#include + +#include "mlir/Support/LLVM.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/DenseMapInfo.h" +#include "llvm/ADT/Hashing.h" + +namespace mlir { + +class MLIRContext; + +namespace polynomial { + +// This restricts statically defined polynomials to have at most 64-bit +// coefficients. This may be relaxed in the future, but it seems unlikely one +// would want to specify 128-bit polynomials statically in the source code. +constexpr unsigned apintBitWidth = 64; + +namespace detail { +struct PolynomialStorage; +} // namespace detail + +class Monomial { +public: + Monomial(int64_t coeff, uint64_t expo) + : coefficient(apintBitWidth, coeff), exponent(apintBitWidth, expo) {} + + Monomial(APInt coeff, APInt expo) + : coefficient(std::move(coeff)), exponent(std::move(expo)) {} + + Monomial() : coefficient(apintBitWidth, 0), exponent(apintBitWidth, 0) {} + + bool operator==(const Monomial &other) const { + return other.coefficient == coefficient && other.exponent == exponent; + } + bool operator!=(const Monomial &other) const { + return other.coefficient != coefficient || other.exponent != exponent; + } + + /// Monomials are ordered by exponent. + bool operator<(const Monomial &other) const { + return (exponent.ult(other.exponent)); + } + + // Prints polynomial to 'os'. + void print(raw_ostream &os) const; + + friend ::llvm::hash_code hash_value(Monomial arg); + +public: + APInt coefficient; + + // Always unsigned + APInt exponent; +}; + +/// A single-variable polynomial with integer coefficients. Polynomials are +/// immutable and uniqued. +/// +/// 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: + using ImplType = detail::PolynomialStorage; + + constexpr Polynomial() = default; + explicit Polynomial(ImplType *terms) : terms(terms) {} + + static Polynomial fromMonomials(ArrayRef monomials, + MLIRContext *context); + /// Returns a polynomial with coefficients given by `coeffs` + static Polynomial fromCoefficients(ArrayRef coeffs, + MLIRContext *context); + + MLIRContext *getContext() const; + + explicit operator bool() const { return terms != nullptr; } + bool operator==(Polynomial other) const { return other.terms == terms; } + bool operator!=(Polynomial other) const { return !(other.terms == terms); } + + // Prints polynomial to 'os'. + void print(raw_ostream &os) const; + void print(raw_ostream &os, const std::string &separator, + const std::string &exponentiation) const; + void dump() const; + + // Prints polynomial so that it can be used as a valid identifier + std::string toIdentifier() const; + + // A polynomial's terms are canonically stored in order of increasing degree. + ArrayRef getTerms() const; + + unsigned getDegree() const; + + friend ::llvm::hash_code hash_value(Polynomial arg); + +private: + ImplType *terms{nullptr}; +}; + +// Make Polynomial hashable. +inline ::llvm::hash_code hash_value(Polynomial arg) { + return ::llvm::hash_value(arg.terms); +} + +inline ::llvm::hash_code hash_value(Monomial arg) { + return ::llvm::hash_value(arg.coefficient) ^ ::llvm::hash_value(arg.exponent); +} + +inline raw_ostream &operator<<(raw_ostream &os, Polynomial polynomial) { + polynomial.print(os); + return os; +} + +} // namespace polynomial +} // namespace mlir + +namespace llvm { + +// Polynomials hash just like pointers +template <> +struct DenseMapInfo { + static mlir::polynomial::Polynomial getEmptyKey() { + auto *pointer = llvm::DenseMapInfo::getEmptyKey(); + return mlir::polynomial::Polynomial( + static_cast(pointer)); + } + static mlir::polynomial::Polynomial getTombstoneKey() { + auto *pointer = llvm::DenseMapInfo::getTombstoneKey(); + return mlir::polynomial::Polynomial( + static_cast(pointer)); + } + static unsigned getHashValue(mlir::polynomial::Polynomial val) { + return mlir::polynomial::hash_value(val); + } + static bool isEqual(mlir::polynomial::Polynomial lhs, + mlir::polynomial::Polynomial rhs) { + return lhs == rhs; + } +}; + +} // namespace llvm + +#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h new file mode 100644 index 0000000000000..6d6941cf6bdcc --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h @@ -0,0 +1,18 @@ +//===- PolynomialAttributes.h - Attributes for the Polynomial dialect -*- 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 +// +//===----------------------------------------------------------------------===// +#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ +#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ + +#include "Polynomial.h" +#include "PolynomialDialect.h" + +#define GET_ATTRDEF_CLASSES +#include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.h.inc" + +#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td new file mode 100644 index 0000000000000..0e4f2f182ede1 --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td @@ -0,0 +1,81 @@ +//===- PolynomialAttributes.td - Attribute definitions for the polynomial dialect ------*- tablegen -*-==// +// +// 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 +// +//===----------------------------------------------------------------------===// +#ifndef POLYNOMIAL_ATTRIBUTES +#define POLYNOMIAL_ATTRIBUTES + +include "PolynomialDialect.td" +include "mlir/IR/BuiltinAttributes.td" +include "mlir/IR/OpBase.td" + +class Polynomial_Attr traits = []> + : AttrDef { + let mnemonic = attrMnemonic; +} + +def Polynomial_PolynomialAttr : Polynomial_Attr<"Polynomial", "polynomial"> { + let summary = "An attribute containing a single-variable polynomial."; + let description = [{ + #poly = #polynomial.poly + }]; + + let parameters = (ins "Polynomial":$polynomial); + + let builders = [ + AttrBuilderWithInferredContext<(ins "Polynomial":$polynomial), [{ + return $_get(polynomial.getContext(), polynomial); + }]> + ]; + + let skipDefaultBuilders = 1; + let hasCustomAssemblyFormat = 1; +} + +def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { + let summary = "An attribute specifying a polynomial ring."; + let description = [{ + A ring describes the domain in which polynomial arithmetic occurs. The ring + attribute in `polynomial` represents the more specific case of polynomials + with a single indeterminate; whose coefficients can be represented by + another MLIR type (`ctype`); and, if the coefficient type is integral, + whose coefficients are taken modulo some statically known modulus (`cmod`). + + Additionally, a polynomial ring can specify an _ideal_, which converts + polynomial arithmetic to the analogue of modular integer arithmetic, where + each polynomial is represented as its remainder when dividing by the + modulus. For single-variable polynomials, an "ideal" is always specificed + via a single polynomial, which we call `polynomialModulus`. + + An expressive example is polynomials with i32 coefficients, whose + coefficients are taken modulo `2**32 - 5`, with a polynomial modulus of + `x**1024 - 1`. + + ``` + #poly_mod = #polynomial.polynomial<-1 + x**1024> + #ring = #polynomial.ring + + %0 = ... : polynomial.polynomial<#ring> + ``` + + In this case, the value of a polynomial is always ``converted'' to a + canonical form by applying repeated reductions by setting `x**1024 = 1` + and simplifying. + + The coefficient and polynomial modulus parameters are optional, and the + coefficient modulus is only allowed if the coefficient type is integral. + }]; + + let parameters = (ins + Builtin_TypeAttr: $coefficientType, + OptionalParameter<"std::optional">: $coefficientModulus, + OptionalParameter<"std::optional">: $polynomialModulus + ); + + let hasCustomAssemblyFormat = 1; +} + +#endif // POLYNOMIAL_ATTRIBUTES diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h new file mode 100644 index 0000000000000..50538126c0372 --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h @@ -0,0 +1,19 @@ +//===- PolynomialDialect.h - The Polynomial dialect -*- 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 +// +//===----------------------------------------------------------------------===// +#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ +#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ + +#include "mlir/IR/Builders.h" +#include "mlir/IR/BuiltinTypes.h" +#include "mlir/IR/Dialect.h" +#include "mlir/IR/DialectImplementation.h" + +// Generated headers (block clang-format from messing up order) +#include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h.inc" + +#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td new file mode 100644 index 0000000000000..d664a7c6159fd --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td @@ -0,0 +1,54 @@ +//===- PolynomialDialect.td - Dialect definition for the polynomial dialect ------*- tablegen -*-==// +// +// 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 +// +//===----------------------------------------------------------------------===// +#ifndef POLYNOMIAL_DIALECT +#define POLYNOMIAL_DIALECT + +include "mlir/IR/OpBase.td" + +def Polynomial_Dialect : Dialect { + let name = "polynomial"; + let cppNamespace = "::mlir::polynomial"; + let description = [{ + The Polynomial dialect defines single-variable polynomial types and + operations. + + The simplest use of `polynomial` is to represent mathematical operations in + a polynomial ring `R[x]`, where `R` is another MLIR type like `i32`. + + More generally, this dialect supports representing polynomial operations in a + quotient ring `R[X]/(f(x))` for some statically fixed polynomial `f(x)`. + Two polyomials `p(x), q(x)` are considered equal in this ring if they have the + same remainder when dividing by `f(x)`. When a modulus is given, ring operations + are performed with reductions modulo `f(x)` and relative to the coefficient ring + `R`. + + Examples: + + ```mlir + // A constant polynomial in a ring with i32 coefficients and no polynomial modulus + #ring = #polynomial.ring + %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.polynomial<1 + x**1024> + #ring = #polynomial.ring + %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.polynomial<1 + x**1024> + #ring = #polynomial.ring + %a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring> + ``` + }]; + + let useDefaultTypePrinterParser = 1; + let useDefaultAttributePrinterParser = 1; +} + +#endif // POLYNOMIAL_DIALECT diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h new file mode 100644 index 0000000000000..49491c9bdc8ad --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h @@ -0,0 +1,21 @@ +//===- PolynomialOps.h - Ops for the Polynomial dialect -*- 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 +// +//===----------------------------------------------------------------------===// +#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ +#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ + +#include "PolynomialDialect.h" +#include "PolynomialTypes.h" +#include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/BuiltinTypes.h" +#include "mlir/IR/Dialect.h" +#include "mlir/Interfaces/InferTypeOpInterface.h" + +#define GET_OP_CLASSES +#include "mlir/Dialect/Polynomial/IR/PolynomialOps.h.inc" + +#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td new file mode 100644 index 0000000000000..6ead46c953faf --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td @@ -0,0 +1,40 @@ +//===- PolynomialOps.td - Polynomial op definitions --------------------*- tablegen -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef POLYNOMIAL_OPS +#define POLYNOMIAL_OPS + +include "PolynomialDialect.td" +include "PolynomialTypes.td" +include "mlir/Interfaces/InferTypeOpInterface.td" +include "mlir/Interfaces/SideEffectInterfaces.td" + +// Base class for polynomial dialect ops. Ops in this dialect have no side +// effects. +class Polynomial_Op traits = []> : + Op; + +// Base class for unary polynomial operations. +class Polynomial_UnaryOp traits = []> : + Polynomial_Op { + let arguments = (ins Polynomial_PolynomialType:$operand); + let results = (outs Polynomial_PolynomialType:$result); + + let assemblyFormat = "$operand attr-dict `:` qualified(type($result))"; +} + +// Base class for binary polynomial operations. +class Polynomial_BinaryOp traits = []> : + Polynomial_Op { + let arguments = (ins Polynomial_PolynomialType:$lhs, Polynomial_PolynomialType:$rhs); + let results = (outs Polynomial_PolynomialType:$result); + + let assemblyFormat = "$lhs `,` $rhs attr-dict `:` qualified(type($result))"; +} + +#endif // POLYNOMIAL_OPS diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h new file mode 100644 index 0000000000000..c030de2d6c77f --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h @@ -0,0 +1,17 @@ +//===- PolynomialTypes.h - Types for the Polynomial dialect -*- 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 +// +//===----------------------------------------------------------------------===// +#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ +#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ + +#include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.h" +#include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h" + +#define GET_TYPEDEF_CLASSES +#include "mlir/Dialect/Polynomial/IR/PolynomialTypes.h.inc" + +#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td new file mode 100644 index 0000000000000..b480c9ea71447 --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td @@ -0,0 +1,32 @@ +//===- PolynomialTypes.td - Type definitions for polynomial dialect ------*- tablegen -*-==// +// +// 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 +// +//===----------------------------------------------------------------------===// +#ifndef POLYNOMIAL_TYPES +#define POLYNOMIAL_TYPES + +include "PolynomialAttributes.td" +include "PolynomialDialect.td" +include "mlir/IR/BuiltinAttributes.td" +include "mlir/IR/OpBase.td" + +class Polynomial_Type + : TypeDef { + let mnemonic = typeMnemonic; +} + +def Polynomial_PolynomialType : Polynomial_Type<"Polynomial", "polynomial"> { + let summary = "An element of a polynomial ring."; + + let description = [{ + A type for polynomials in a polynomial quotient ring. + }]; + + let parameters = (ins Polynomial_RingAttr:$ring); + let assemblyFormat = "`<` $ring `>`"; +} + +#endif // POLYNOMIAL_TYPES diff --git a/mlir/include/mlir/InitAllDialects.h b/mlir/include/mlir/InitAllDialects.h index c558dc53cc7fa..c4d788cf8ed31 100644 --- a/mlir/include/mlir/InitAllDialects.h +++ b/mlir/include/mlir/InitAllDialects.h @@ -61,6 +61,7 @@ #include "mlir/Dialect/OpenMP/OpenMPDialect.h" #include "mlir/Dialect/PDL/IR/PDL.h" #include "mlir/Dialect/PDLInterp/IR/PDLInterp.h" +#include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h" #include "mlir/Dialect/Quant/QuantOps.h" #include "mlir/Dialect/SCF/IR/SCF.h" #include "mlir/Dialect/SCF/IR/ValueBoundsOpInterfaceImpl.h" @@ -131,6 +132,7 @@ inline void registerAllDialects(DialectRegistry ®istry) { omp::OpenMPDialect, pdl::PDLDialect, pdl_interp::PDLInterpDialect, + polynomial::PolynomialDialect, quant::QuantizationDialect, ROCDL::ROCDLDialect, scf::SCFDialect, diff --git a/mlir/lib/Dialect/CMakeLists.txt b/mlir/lib/Dialect/CMakeLists.txt index b1ba5a3bc8817..a324ce7f9b19f 100644 --- a/mlir/lib/Dialect/CMakeLists.txt +++ b/mlir/lib/Dialect/CMakeLists.txt @@ -28,6 +28,7 @@ add_subdirectory(OpenACCMPCommon) add_subdirectory(OpenMP) add_subdirectory(PDL) add_subdirectory(PDLInterp) +add_subdirectory(Polynomial) add_subdirectory(Quant) add_subdirectory(SCF) add_subdirectory(Shape) diff --git a/mlir/lib/Dialect/Polynomial/CMakeLists.txt b/mlir/lib/Dialect/Polynomial/CMakeLists.txt new file mode 100644 index 0000000000000..f33061b2d87cf --- /dev/null +++ b/mlir/lib/Dialect/Polynomial/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(IR) diff --git a/mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt b/mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt new file mode 100644 index 0000000000000..695c37f7a368b --- /dev/null +++ b/mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt @@ -0,0 +1,21 @@ +add_mlir_dialect_library(MLIRPolynomialDialect + Polynomial.cpp + PolynomialAttributes.cpp + PolynomialDialect.cpp + PolynomialOps.cpp + + ADDITIONAL_HEADER_DIRS + ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Polynomial + + DEPENDS + MLIRPolynomialOpsIncGen + MLIRPolynomialTypesIncGen + MLIRPolynomialDialectIncGen + MLIRPolynomialAttributesIncGen + MLIRBuiltinAttributesIncGen + + LINK_LIBS PUBLIC + MLIRSupport + MLIRDialect + MLIRIR + ) diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp new file mode 100644 index 0000000000000..6e04691502275 --- /dev/null +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -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 Polynomial::getTerms() const { return terms->terms(); } + +Polynomial Polynomial::fromMonomials(ArrayRef 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 monomialsCopy = + llvm::OwningArrayRef(monomials); + std::sort(monomialsCopy.begin(), monomialsCopy.end()); + + StorageUniquer &uniquer = context->getAttributeUniquer(); + return Polynomial(uniquer.get( + assignCtx, monomials.size(), monomialsCopy)); +} + +Polynomial Polynomial::fromCoefficients(ArrayRef coeffs, + MLIRContext *context) { + std::vector 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; + term.coefficient.toStringSigned(coeffString); + coeffToPrint = coeffString.str(); + } + + if (term.exponent == 0) { + os << coeffToPrint; + } else if (term.exponent == 1) { + os << coeffToPrint << "x"; + } else { + llvm::SmallString<512> expString; + term.exponent.toStringSigned(expString); + os << coeffToPrint << "x" << exponentiation << expString; + } + } +} + +void Polynomial::print(raw_ostream &os) const { print(os, " + ", "**"); } + +std::string Polynomial::toIdentifier() const { + std::string result; + llvm::raw_string_ostream os(result); + print(os, "_", ""); + return os.str(); +} + +unsigned Polynomial::getDegree() const { + return terms->terms().back().exponent.getZExtValue(); +} + +} // namespace polynomial +} // namespace mlir + +MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::polynomial::detail::PolynomialStorage); diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp new file mode 100644 index 0000000000000..d3ce95e161dee --- /dev/null +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -0,0 +1,217 @@ +//===- PolynomialAttributes.cpp - Polynomial dialect attributes --*- 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/PolynomialAttributes.h" + +#include "mlir/Dialect/Polynomial/IR/Polynomial.h" +#include "mlir/Support/LLVM.h" +#include "mlir/Support/LogicalResult.h" +#include "llvm/ADT/SmallSet.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringRef.h" + +namespace mlir { +namespace polynomial { + +void PolynomialAttr::print(AsmPrinter &p) const { + p << '<'; + p << getPolynomial(); + p << '>'; +} + +/// Try to parse a monomial. If successful, populate the fields of the outparam +/// `monomial` with the results, and the `variable` outparam with the parsed +/// variable name. +ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, + llvm::StringRef *variable, bool *isConstantTerm) { + APInt parsedCoeff(apintBitWidth, 1); + auto result = parser.parseOptionalInteger(parsedCoeff); + if (result.has_value()) { + if (failed(*result)) { + parser.emitError(parser.getCurrentLocation(), + "Invalid integer coefficient."); + return failure(); + } + } + + // Variable name + result = parser.parseOptionalKeyword(variable); + if (!result.has_value() || failed(*result)) { + // we allow "failed" because it triggers when the next token is a +, + // which is allowed when the input is the constant term. + monomial.coefficient = parsedCoeff; + monomial.exponent = APInt(apintBitWidth, 0); + *isConstantTerm = true; + return success(); + } + + // Parse exponentiation symbol as ** + // We can't use caret because it's reserved for basic block identifiers + // If no star is present, it's treated as a polynomial with exponent 1 + if (failed(parser.parseOptionalStar())) { + monomial.coefficient = parsedCoeff; + monomial.exponent = APInt(apintBitWidth, 1); + return success(); + } + + // If there's one * there must be two + if (failed(parser.parseStar())) { + parser.emitError(parser.getCurrentLocation(), + "Exponents must be specified as a double-asterisk `**`."); + return failure(); + } + + // If there's a **, then the integer exponent is required. + APInt parsedExponent(apintBitWidth, 0); + if (failed(parser.parseInteger(parsedExponent))) { + parser.emitError(parser.getCurrentLocation(), + "Found invalid integer exponent."); + return failure(); + } + + monomial.coefficient = parsedCoeff; + monomial.exponent = parsedExponent; + return success(); +} + +mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, + Type type) { + if (failed(parser.parseLess())) + return {}; + + std::vector monomials; + llvm::SmallSet variables; + llvm::DenseSet exponents; + + while (true) { + Monomial parsedMonomial; + llvm::StringRef parsedVariableRef; + bool isConstantTerm = false; + if (failed(parseMonomial(parser, parsedMonomial, &parsedVariableRef, + &isConstantTerm))) { + return {}; + } + + if (!isConstantTerm) { + std::string parsedVariable = parsedVariableRef.str(); + variables.insert(parsedVariable); + } + monomials.push_back(parsedMonomial); + + if (exponents.count(parsedMonomial.exponent) > 0) { + llvm::SmallString<512> coeff_string; + parsedMonomial.exponent.toStringSigned(coeff_string); + parser.emitError(parser.getCurrentLocation(), + "At most one monomial may have exponent " + + coeff_string + ", but found multiple."); + return {}; + } + exponents.insert(parsedMonomial.exponent); + + // Parse optional +. If a + is absent, require > and break, otherwise forbid + // > and continue with the next monomial. + // ParseOptional{Plus, Greater} does not return an OptionalParseResult, so + // failed means that the token was not found. + if (failed(parser.parseOptionalPlus())) { + if (succeeded(parser.parseGreater())) { + break; + } else { + parser.emitError( + parser.getCurrentLocation(), + "Expected + and more monomials, or > to end polynomial attribute."); + return {}; + } + } else if (succeeded(parser.parseOptionalGreater())) { + parser.emitError( + parser.getCurrentLocation(), + "Expected another monomial after +, but found > ending attribute."); + return {}; + } + } + + // insert necessary constant ops to give as input to extract_slice. + if (variables.size() > 1) { + std::string vars = llvm::join(variables.begin(), variables.end(), ", "); + parser.emitError( + parser.getCurrentLocation(), + "Polynomials must have one indeterminate, but there were multiple: " + + vars); + } + + Polynomial poly = Polynomial::fromMonomials(monomials, parser.getContext()); + return PolynomialAttr::get(poly); +} + +void RingAttr::print(AsmPrinter &p) const { + p << "#polynomial.ring(typeAttr))) + return {}; + + if (failed(parser.parseComma())) + return {}; + + std::optional cmodAttr = std::nullopt; + if (succeeded(parser.parseKeyword("cmod"))) { + if (failed(parser.parseEqual())) + return {}; + + IntegerType iType = llvm::dyn_cast(typeAttr.getValue()); + if (!iType) { + parser.emitError( + parser.getCurrentLocation(), + "Invalid coefficient modulus, ctype must specify an integer type."); + return {}; + } + APInt cmod(iType.getWidth(), 0); + auto result = parser.parseInteger(cmod); + if (failed(result)) { + parser.emitError(parser.getCurrentLocation(), + "Invalid coefficient modulus."); + return {}; + } + cmodAttr = IntegerAttr::get(iType, cmod); + + if (failed(parser.parseComma())) + return {}; + } + + std::optional polyAttr = std::nullopt; + if (succeeded(parser.parseKeyword("ideal"))) { + if (failed(parser.parseEqual())) + return {}; + + PolynomialAttr attr; + if (failed(parser.parseAttribute(attr))) + return {}; + polyAttr = attr; + } + + if (failed(parser.parseGreater())) + return {}; + + return RingAttr::get(parser.getContext(), typeAttr, cmodAttr, polyAttr); +} + +} // namespace polynomial +} // namespace mlir diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h b/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h new file mode 100644 index 0000000000000..19ee6b7ec25d1 --- /dev/null +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h @@ -0,0 +1,65 @@ +//===- Polynomial.h - Storage class details for polynomial types --------*- +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDETAIL_H_ +#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDETAIL_H_ + +#include "mlir/Dialect/Polynomial/IR/Polynomial.h" +#include "mlir/Support/StorageUniquer.h" +#include "mlir/Support/TypeID.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/Support/TrailingObjects.h" + +namespace mlir { +namespace polynomial { +namespace detail { + +// A Polynomial is stored as an ordered list of monomial terms, each of which +// is a tuple of coefficient and exponent. +struct PolynomialStorage final + : public StorageUniquer::BaseStorage, + public llvm::TrailingObjects { + /// The hash key used for uniquing. + using KeyTy = std::tuple>; + + unsigned numTerms; + + MLIRContext *context; + + /// The monomial terms for this polynomial. + ArrayRef terms() const { + return {getTrailingObjects(), numTerms}; + } + + bool operator==(const KeyTy &key) const { + return std::get<0>(key) == numTerms && std::get<1>(key) == terms(); + } + + // Constructs a PolynomialStorage from a key. The context must be set by the + // caller. + static PolynomialStorage * + construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) { + auto terms = std::get<1>(key); + auto byteSize = PolynomialStorage::totalSizeToAlloc(terms.size()); + auto *rawMem = allocator.allocate(byteSize, alignof(PolynomialStorage)); + auto *res = new (rawMem) PolynomialStorage(); + res->numTerms = std::get<0>(key); + std::uninitialized_copy(terms.begin(), terms.end(), + res->getTrailingObjects()); + return res; + } +}; + +} // namespace detail +} // namespace polynomial +} // namespace mlir + +MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::polynomial::detail::PolynomialStorage) + +#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDETAIL_H_ diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp new file mode 100644 index 0000000000000..b618495b2cdcd --- /dev/null +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp @@ -0,0 +1,47 @@ +//===- PolynomialDialect.cpp - MLIR dialect for Polynomial implementation +//-------------===// +// +// 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/Dialect/Polynomial/IR/PolynomialAttributes.h" +#include "mlir/Dialect/Polynomial/IR/PolynomialOps.h" +#include "mlir/Dialect/Polynomial/IR/PolynomialTypes.h" +#include "llvm/ADT/TypeSwitch.h" + +using namespace mlir; +using namespace mlir::polynomial; + +#include "mlir/Dialect/Polynomial/IR/PolynomialDialect.cpp.inc" + +#define GET_ATTRDEF_CLASSES +#include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.cpp.inc" +#define GET_TYPEDEF_CLASSES +#include "mlir/Dialect/Polynomial/IR/PolynomialTypes.cpp.inc" +#define GET_OP_CLASSES +#include "mlir/Dialect/Polynomial/IR/PolynomialOps.cpp.inc" + +void PolynomialDialect::initialize() { + addAttributes< +#define GET_ATTRDEF_LIST +#include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.cpp.inc" + >(); + addTypes< +#define GET_TYPEDEF_LIST +#include "mlir/Dialect/Polynomial/IR/PolynomialTypes.cpp.inc" + >(); + addOperations< +#define GET_OP_LIST +#include "mlir/Dialect/Polynomial/IR/PolynomialOps.cpp.inc" + >(); + + getContext() + ->getAttributeUniquer() + .registerParametricStorageType(); +} diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp new file mode 100644 index 0000000000000..644a669205213 --- /dev/null +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp @@ -0,0 +1,16 @@ +//===- PolynomialOps.cpp - MLIR operations for polynomial implementation +//--------------===// +// +// 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" + +using namespace mlir; +using namespace mlir::polynomial; + +#define GET_OP_CLASSES +#include "mlir/Dialect/Polynomial/IR/PolynomialOps.cpp.inc" diff --git a/mlir/test/Dialect/Polynomial/types.td b/mlir/test/Dialect/Polynomial/types.td new file mode 100644 index 0000000000000..7328318002203 --- /dev/null +++ b/mlir/test/Dialect/Polynomial/types.td @@ -0,0 +1,19 @@ +// RUN: mlir-opt %s | FileCheck %s + +#my_poly = #polynomial.polynomial<1 + x**1024> +#my_poly_2 = #polynomial.polynomial<2> +#my_poly_3 = #polynomial.polynomial<3x> +#my_poly_4 = #polynomial.polynomial +#ring1 = #polynomial.ring + +!ty = !polynomial.polynomial<#ring1> + +// CHECK-LABEL: func @test_types +// CHECK-SAME: !polynomial.polynomial< +// CHECK-SAME: #polynomial.ring< +// CHECK-SAME: ctype=i32, +// CHECK-SAME: cmod=2837465 : i32, +// CHECK-SAME: ideal=#polynomial.polynomial<1 + x**1024>>> +func.func @test_types(%0: !ty) -> !ty { + return %0 : !ty +} From 8edf75a8a57823a65e4f9d16be4cfaee45b3ce9f Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 07:01:12 -0800 Subject: [PATCH 02/46] fix include guards --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 6 +++--- .../mlir/Dialect/Polynomial/IR/PolynomialAttributes.h | 6 +++--- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h | 6 +++--- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h | 6 +++--- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 757ee61ec8c7a..1ca04cf92e1ed 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ -#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ +#ifndef MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ +#define MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ #include @@ -154,4 +154,4 @@ struct DenseMapInfo { } // namespace llvm -#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ +#endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h index 6d6941cf6bdcc..3b0223e6da27a 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h @@ -6,8 +6,8 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// -#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ -#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ +#ifndef MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ +#define MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ #include "Polynomial.h" #include "PolynomialDialect.h" @@ -15,4 +15,4 @@ #define GET_ATTRDEF_CLASSES #include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.h.inc" -#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ +#endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALATTRIBUTES_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h index 50538126c0372..b1971c020b354 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h @@ -5,8 +5,8 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// -#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ -#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ +#ifndef MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ +#define MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinTypes.h" @@ -16,4 +16,4 @@ // Generated headers (block clang-format from messing up order) #include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h.inc" -#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ +#endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDIALECT_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h index 49491c9bdc8ad..8897949a182ca 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h @@ -5,8 +5,8 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// -#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ -#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ +#ifndef MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ +#define MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ #include "PolynomialDialect.h" #include "PolynomialTypes.h" @@ -18,4 +18,4 @@ #define GET_OP_CLASSES #include "mlir/Dialect/Polynomial/IR/PolynomialOps.h.inc" -#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ +#endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h index c030de2d6c77f..286b0d9653952 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h @@ -5,8 +5,8 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// -#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ -#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ +#ifndef MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ +#define MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ #include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.h" #include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h" @@ -14,4 +14,4 @@ #define GET_TYPEDEF_CLASSES #include "mlir/Dialect/Polynomial/IR/PolynomialTypes.h.inc" -#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ +#endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALTYPES_H_ From 0a515f48b0e3d40d0112a2e5c4fe7131faedf3c4 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 07:06:37 -0800 Subject: [PATCH 03/46] add top level docs to Monomial --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 1ca04cf92e1ed..945b0789733af 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -31,6 +31,8 @@ namespace detail { struct PolynomialStorage; } // namespace detail +/// A class representing a monomial of a single-variable polynomial with integer +/// coefficients. class Monomial { public: Monomial(int64_t coeff, uint64_t expo) From 06147c1b3cc0880e71f7ec2da82cb505ade107f8 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 07:08:04 -0800 Subject: [PATCH 04/46] use /// for top level comment --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 945b0789733af..b7c1fcad732aa 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -22,9 +22,9 @@ class MLIRContext; namespace polynomial { -// This restricts statically defined polynomials to have at most 64-bit -// coefficients. This may be relaxed in the future, but it seems unlikely one -// would want to specify 128-bit polynomials statically in the source code. +/// This restricts statically defined polynomials to have at most 64-bit +/// coefficients. This may be relaxed in the future, but it seems unlikely one +/// would want to specify 128-bit polynomials statically in the source code. constexpr unsigned apintBitWidth = 64; namespace detail { From 785c20460531a15ed6cb1d81582686b8ab10a216 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 07:08:48 -0800 Subject: [PATCH 05/46] make hash_value arg const --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index b7c1fcad732aa..503f187be1302 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -58,7 +58,7 @@ class Monomial { // Prints polynomial to 'os'. void print(raw_ostream &os) const; - friend ::llvm::hash_code hash_value(Monomial arg); + friend ::llvm::hash_code hash_value(const Monomial& arg); public: APInt coefficient; @@ -118,7 +118,7 @@ inline ::llvm::hash_code hash_value(Polynomial arg) { return ::llvm::hash_value(arg.terms); } -inline ::llvm::hash_code hash_value(Monomial arg) { +inline ::llvm::hash_code hash_value(const Monomial& arg) { return ::llvm::hash_value(arg.coefficient) ^ ::llvm::hash_value(arg.exponent); } From 9ec0b576302314411ac25c09dc8463961d1a100d Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 07:13:52 -0800 Subject: [PATCH 06/46] std::string -> StringRef --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 4 ++-- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 503f187be1302..063e620a1d8b3 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -95,8 +95,8 @@ class Polynomial { // Prints polynomial to 'os'. void print(raw_ostream &os) const; - void print(raw_ostream &os, const std::string &separator, - const std::string &exponentiation) 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 diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index 6e04691502275..740d1abdaa67d 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -48,8 +48,8 @@ Polynomial Polynomial::fromCoefficients(ArrayRef coeffs, return Polynomial::fromMonomials(monomials, context); } -void Polynomial::print(raw_ostream &os, const std::string &separator, - const std::string &exponentiation) const { +void Polynomial::print(raw_ostream &os, ::llvm::StringRef separator, + ::llvm::StringRef exponentiation) const { bool first = true; for (const auto &term : terms->terms()) { if (first) { From 1ec951b37e915238a885065cb01ace1d75756c3e Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 07:18:48 -0800 Subject: [PATCH 07/46] update/align header comments --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 2 +- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h | 3 +-- .../include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td | 2 +- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h | 2 +- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td | 2 +- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h | 2 +- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td | 2 +- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h | 2 +- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td | 2 +- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 3 +-- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 3 +-- mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h | 3 +-- mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp | 3 +-- mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp | 3 +-- 14 files changed, 14 insertions(+), 20 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 063e620a1d8b3..7422251bd9eb4 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -1,4 +1,4 @@ -//===- Polynomial.h - A storage class for polynomial types --------*- C++-*-==// +//===- Polynomial.h - A storage class for polynomial types ------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h index 3b0223e6da27a..b37d17bb89fb2 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.h @@ -1,5 +1,4 @@ -//===- PolynomialAttributes.h - Attributes for the Polynomial dialect -*- C++ -//-*-===// +//===- PolynomialAttributes.h - polynomial dialect attributes ---*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td index 0e4f2f182ede1..53c26e99103f1 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td @@ -1,4 +1,4 @@ -//===- PolynomialAttributes.td - Attribute definitions for the polynomial dialect ------*- tablegen -*-==// +//===- PolynomialAttributes.td - polynomial dialect attrs --*- tablegen -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h index b1971c020b354..7b7acebe7a93b 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.h @@ -1,4 +1,4 @@ -//===- PolynomialDialect.h - The Polynomial dialect -*- C++ -*-===// +//===- PolynomialDialect.h - The Polynomial dialect -------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td index d664a7c6159fd..95baafc281492 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td @@ -1,4 +1,4 @@ -//===- PolynomialDialect.td - Dialect definition for the polynomial dialect ------*- tablegen -*-==// +//===- PolynomialDialect.td - polynomial dialect -----------*- tablegen -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h index 8897949a182ca..a3ec062ac93fe 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h @@ -1,4 +1,4 @@ -//===- PolynomialOps.h - Ops for the Polynomial dialect -*- C++ -*-===// +//===- PolynomialOps.h - Ops for the Polynomial dialect ---------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td index 6ead46c953faf..06b97cdd059f8 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td @@ -1,4 +1,4 @@ -//===- PolynomialOps.td - Polynomial op definitions --------------------*- tablegen -*-===// +//===- PolynomialOps.td - Polynomial op definitions --------*- tablegen -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h index 286b0d9653952..2fc6877452547 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.h @@ -1,4 +1,4 @@ -//===- PolynomialTypes.h - Types for the Polynomial dialect -*- C++ -*-===// +//===- PolynomialTypes.h - Types for the Polynomial dialect -----*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td index b480c9ea71447..0a9be5f3f8846 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td @@ -1,4 +1,4 @@ -//===- PolynomialTypes.td - Type definitions for polynomial dialect ------*- tablegen -*-==// +//===- PolynomialTypes.td - Types for polynomial dialect ---*- tablegen -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index 740d1abdaa67d..add132f3fcde0 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -1,5 +1,4 @@ -//===- Polynomial.cpp - MLIR storage type for static Polynomial -//--------------===// +//===- 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. diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index d3ce95e161dee..d932362f76f3a 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -1,5 +1,4 @@ -//===- PolynomialAttributes.cpp - Polynomial dialect attributes --*- C++ -//-*-===// +//===- PolynomialAttributes.cpp - Polynomial dialect attrs ------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h b/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h index 19ee6b7ec25d1..f57dc50e309cd 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h @@ -1,5 +1,4 @@ -//===- Polynomial.h - Storage class details for polynomial types --------*- -// C++-*-==// +//===- Polynomial.h - Storage class details for 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. diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp index b618495b2cdcd..6b4e26e5fb03c 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp @@ -1,5 +1,4 @@ -//===- PolynomialDialect.cpp - MLIR dialect for Polynomial implementation -//-------------===// +//===- PolynomialDialect.cpp - Polynomial dialect ---------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp index 644a669205213..a33a15ca9dfe5 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp @@ -1,5 +1,4 @@ -//===- PolynomialOps.cpp - MLIR operations for polynomial implementation -//--------------===// +//===- PolynomialOps.cpp - Polynomial dialect ops ---------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. From 3b7084e75ea2f30ffcd16c9f8767876157692cd3 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 08:33:39 -0800 Subject: [PATCH 08/46] remove tex quotes --- mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td index 53c26e99103f1..d061ae26f5b80 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td @@ -61,7 +61,7 @@ def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { %0 = ... : polynomial.polynomial<#ring> ``` - In this case, the value of a polynomial is always ``converted'' to a + In this case, the value of a polynomial is always "converted" to a canonical form by applying repeated reductions by setting `x**1024 = 1` and simplifying. From b413f6a80ce8d30fe76e25df710bbb2f38e4bf15 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 08:35:09 -0800 Subject: [PATCH 09/46] use auto over OwningArrayRef --- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index add132f3fcde0..fb3d535abb6fe 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -29,8 +29,7 @@ Polynomial Polynomial::fromMonomials(ArrayRef monomials, }; // A polynomial's terms are canonically stored in order of increasing degree. - llvm::OwningArrayRef monomialsCopy = - llvm::OwningArrayRef(monomials); + auto monomialsCopy = llvm::OwningArrayRef(monomials); std::sort(monomialsCopy.begin(), monomialsCopy.end()); StorageUniquer &uniquer = context->getAttributeUniquer(); From 529064e185f53218e9d75b4f6166fef356c698a1 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 08:42:55 -0800 Subject: [PATCH 10/46] use SmallVector instead of std::vector, clarify docstring --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 4 +++- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 7422251bd9eb4..2914334fae47e 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -83,7 +83,9 @@ class Polynomial { static Polynomial fromMonomials(ArrayRef monomials, MLIRContext *context); - /// Returns a polynomial with coefficients given by `coeffs` + + /// Returns a polynomial with coefficients given by `coeffs`. The value + /// coeffs[i] is converted to a monomial with exponent i. static Polynomial fromCoefficients(ArrayRef coeffs, MLIRContext *context); diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index fb3d535abb6fe..4bc0393bcd417 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -12,6 +12,7 @@ #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" @@ -39,7 +40,8 @@ Polynomial Polynomial::fromMonomials(ArrayRef monomials, Polynomial Polynomial::fromCoefficients(ArrayRef coeffs, MLIRContext *context) { - std::vector monomials; + llvm::SmallVector monomials; + monomials.reserve(coeffs.size()); for (size_t i = 0; i < coeffs.size(); i++) { monomials.emplace_back(coeffs[i], i); } From 9e1215a3a77a13f85c7787834f05371098806b9b Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 08:45:52 -0800 Subject: [PATCH 11/46] Fix parser error string style --- .../Polynomial/IR/PolynomialAttributes.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index d932362f76f3a..8a4e75cd6cbcc 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -33,7 +33,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, if (result.has_value()) { if (failed(*result)) { parser.emitError(parser.getCurrentLocation(), - "Invalid integer coefficient."); + "invalid integer coefficient"); return failure(); } } @@ -61,7 +61,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, // If there's one * there must be two if (failed(parser.parseStar())) { parser.emitError(parser.getCurrentLocation(), - "Exponents must be specified as a double-asterisk `**`."); + "exponents must be specified as a double-asterisk `**`"); return failure(); } @@ -69,7 +69,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, APInt parsedExponent(apintBitWidth, 0); if (failed(parser.parseInteger(parsedExponent))) { parser.emitError(parser.getCurrentLocation(), - "Found invalid integer exponent."); + "found invalid integer exponent"); return failure(); } @@ -106,8 +106,8 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, llvm::SmallString<512> coeff_string; parsedMonomial.exponent.toStringSigned(coeff_string); parser.emitError(parser.getCurrentLocation(), - "At most one monomial may have exponent " + - coeff_string + ", but found multiple."); + "at most one monomial may have exponent " + + coeff_string + ", but found multiple"); return {}; } exponents.insert(parsedMonomial.exponent); @@ -122,13 +122,13 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, } else { parser.emitError( parser.getCurrentLocation(), - "Expected + and more monomials, or > to end polynomial attribute."); + "expected + and more monomials, or > to end polynomial attribute"); return {}; } } else if (succeeded(parser.parseOptionalGreater())) { parser.emitError( parser.getCurrentLocation(), - "Expected another monomial after +, but found > ending attribute."); + "expected another monomial after +, but found > ending attribute"); return {}; } } @@ -138,7 +138,7 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, std::string vars = llvm::join(variables.begin(), variables.end(), ", "); parser.emitError( parser.getCurrentLocation(), - "Polynomials must have one indeterminate, but there were multiple: " + + "polynomials must have one indeterminate, but there were multiple: " + vars); } @@ -179,14 +179,14 @@ mlir::Attribute mlir::polynomial::RingAttr::parse(AsmParser &parser, if (!iType) { parser.emitError( parser.getCurrentLocation(), - "Invalid coefficient modulus, ctype must specify an integer type."); + "invalid coefficient modulus, ctype must specify an integer type"); return {}; } APInt cmod(iType.getWidth(), 0); auto result = parser.parseInteger(cmod); if (failed(result)) { parser.emitError(parser.getCurrentLocation(), - "Invalid coefficient modulus."); + "invalid coefficient modulus"); return {}; } cmodAttr = IntegerAttr::get(iType, cmod); From b34e88e55f88c14f36e217530eedb0286968bd5e Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 08:47:02 -0800 Subject: [PATCH 12/46] small string size 512 -> 16, and snake_case to camelCase --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 8a4e75cd6cbcc..2751cd258e34f 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -103,11 +103,11 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, monomials.push_back(parsedMonomial); if (exponents.count(parsedMonomial.exponent) > 0) { - llvm::SmallString<512> coeff_string; - parsedMonomial.exponent.toStringSigned(coeff_string); + llvm::SmallString<16> coeffString; + parsedMonomial.exponent.toStringSigned(coeffString); parser.emitError(parser.getCurrentLocation(), "at most one monomial may have exponent " + - coeff_string + ", but found multiple"); + coeffString + ", but found multiple"); return {}; } exponents.insert(parsedMonomial.exponent); From 1628856ffdc11a367c93215b0a77a0abe68b083a Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 08:48:14 -0800 Subject: [PATCH 13/46] comment capitalization style --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 2751cd258e34f..470d6e8d57d7a 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -41,7 +41,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, // Variable name result = parser.parseOptionalKeyword(variable); if (!result.has_value() || failed(*result)) { - // we allow "failed" because it triggers when the next token is a +, + // We allow "failed" because it triggers when the next token is a +, // which is allowed when the input is the constant term. monomial.coefficient = parsedCoeff; monomial.exponent = APInt(apintBitWidth, 0); @@ -133,7 +133,7 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, } } - // insert necessary constant ops to give as input to extract_slice. + // Insert necessary constant ops to give as input to extract_slice. if (variables.size() > 1) { std::string vars = llvm::join(variables.begin(), variables.end(), ", "); parser.emitError( From a89c6af973046a548e4f1c376dfd10d7ec9b9603 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 09:47:25 -0800 Subject: [PATCH 14/46] use consistent verbose attribute property names --- .../Polynomial/IR/PolynomialAttributes.td | 11 ++++--- .../Polynomial/IR/PolynomialAttributes.cpp | 33 ++++++++++--------- mlir/test/Dialect/Polynomial/types.td | 8 ++--- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td index d061ae26f5b80..626350813d0dd 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td @@ -41,8 +41,9 @@ def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { A ring describes the domain in which polynomial arithmetic occurs. The ring attribute in `polynomial` represents the more specific case of polynomials with a single indeterminate; whose coefficients can be represented by - another MLIR type (`ctype`); and, if the coefficient type is integral, - whose coefficients are taken modulo some statically known modulus (`cmod`). + another MLIR type (`coefficientType`); and, if the coefficient type is + integral, whose coefficients are taken modulo some statically known modulus + (`coefficientModulus`). Additionally, a polynomial ring can specify an _ideal_, which converts polynomial arithmetic to the analogue of modular integer arithmetic, where @@ -54,9 +55,11 @@ def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { coefficients are taken modulo `2**32 - 5`, with a polynomial modulus of `x**1024 - 1`. - ``` + ```mlir #poly_mod = #polynomial.polynomial<-1 + x**1024> - #ring = #polynomial.ring + #ring = #polynomial.ring %0 = ... : polynomial.polynomial<#ring> ``` diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 470d6e8d57d7a..80678d8890ae7 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -106,8 +106,8 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, llvm::SmallString<16> coeffString; parsedMonomial.exponent.toStringSigned(coeffString); parser.emitError(parser.getCurrentLocation(), - "at most one monomial may have exponent " + - coeffString + ", but found multiple"); + "at most one monomial may have exponent " + coeffString + + ", but found multiple"); return {}; } exponents.insert(parsedMonomial.exponent); @@ -147,9 +147,9 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, } void RingAttr::print(AsmPrinter &p) const { - p << "#polynomial.ring cmodAttr = std::nullopt; - if (succeeded(parser.parseKeyword("cmod"))) { + std::optional coefficientModulusAttr = std::nullopt; + if (succeeded(parser.parseKeyword("coefficientModulus"))) { if (failed(parser.parseEqual())) return {}; IntegerType iType = llvm::dyn_cast(typeAttr.getValue()); if (!iType) { - parser.emitError( - parser.getCurrentLocation(), - "invalid coefficient modulus, ctype must specify an integer type"); + parser.emitError(parser.getCurrentLocation(), + "invalid coefficient modulus, coefficientType must " + "specify an integer type"); return {}; } - APInt cmod(iType.getWidth(), 0); - auto result = parser.parseInteger(cmod); + APInt coefficientModulus(iType.getWidth(), 0); + auto result = parser.parseInteger(coefficientModulus); if (failed(result)) { parser.emitError(parser.getCurrentLocation(), "invalid coefficient modulus"); return {}; } - cmodAttr = IntegerAttr::get(iType, cmod); + coefficientModulusAttr = IntegerAttr::get(iType, coefficientModulus); if (failed(parser.parseComma())) return {}; } std::optional polyAttr = std::nullopt; - if (succeeded(parser.parseKeyword("ideal"))) { + if (succeeded(parser.parseKeyword("polynomialModulus"))) { if (failed(parser.parseEqual())) return {}; @@ -209,7 +209,8 @@ mlir::Attribute mlir::polynomial::RingAttr::parse(AsmParser &parser, if (failed(parser.parseGreater())) return {}; - return RingAttr::get(parser.getContext(), typeAttr, cmodAttr, polyAttr); + return RingAttr::get(parser.getContext(), typeAttr, coefficientModulusAttr, + polyAttr); } } // namespace polynomial diff --git a/mlir/test/Dialect/Polynomial/types.td b/mlir/test/Dialect/Polynomial/types.td index 7328318002203..558c714e4400a 100644 --- a/mlir/test/Dialect/Polynomial/types.td +++ b/mlir/test/Dialect/Polynomial/types.td @@ -4,16 +4,16 @@ #my_poly_2 = #polynomial.polynomial<2> #my_poly_3 = #polynomial.polynomial<3x> #my_poly_4 = #polynomial.polynomial -#ring1 = #polynomial.ring +#ring1 = #polynomial.ring !ty = !polynomial.polynomial<#ring1> // CHECK-LABEL: func @test_types // CHECK-SAME: !polynomial.polynomial< // CHECK-SAME: #polynomial.ring< -// CHECK-SAME: ctype=i32, -// CHECK-SAME: cmod=2837465 : i32, -// CHECK-SAME: ideal=#polynomial.polynomial<1 + x**1024>>> +// CHECK-SAME: coefficientType=i32, +// CHECK-SAME: coefficientModulus=2837465 : i32, +// CHECK-SAME: polynomialModulus=#polynomial.polynomial<1 + x**1024>>> func.func @test_types(%0: !ty) -> !ty { return %0 : !ty } From f2a233011e4bc2fe629ac0be16e808346530f386 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 09:51:06 -0800 Subject: [PATCH 15/46] std::vector -> SmallVector --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 80678d8890ae7..7e10b6deab18c 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -83,7 +83,7 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, if (failed(parser.parseLess())) return {}; - std::vector monomials; + llvm::SmallVector monomials; llvm::SmallSet variables; llvm::DenseSet exponents; From 6803817119c3f91af6ebd33ee8841f916eb78a4a Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 10:14:06 -0800 Subject: [PATCH 16/46] pass variable stringref by reference --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 7e10b6deab18c..2847bb95ffd01 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -27,7 +27,7 @@ void PolynomialAttr::print(AsmPrinter &p) const { /// `monomial` with the results, and the `variable` outparam with the parsed /// variable name. ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, - llvm::StringRef *variable, bool *isConstantTerm) { + llvm::StringRef &variable, bool *isConstantTerm) { APInt parsedCoeff(apintBitWidth, 1); auto result = parser.parseOptionalInteger(parsedCoeff); if (result.has_value()) { @@ -39,7 +39,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, } // Variable name - result = parser.parseOptionalKeyword(variable); + result = parser.parseOptionalKeyword(&variable); if (!result.has_value() || failed(*result)) { // We allow "failed" because it triggers when the next token is a +, // which is allowed when the input is the constant term. @@ -91,7 +91,7 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, Monomial parsedMonomial; llvm::StringRef parsedVariableRef; bool isConstantTerm = false; - if (failed(parseMonomial(parser, parsedMonomial, &parsedVariableRef, + if (failed(parseMonomial(parser, parsedMonomial, parsedVariableRef, &isConstantTerm))) { return {}; } From 3e489ef868da090bd68b2102f1bf474a351f92b7 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 10:15:32 -0800 Subject: [PATCH 17/46] use llvm::is_contained --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 2847bb95ffd01..fc9abd5ab2066 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -102,7 +102,7 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, } monomials.push_back(parsedMonomial); - if (exponents.count(parsedMonomial.exponent) > 0) { + if (llvm::is_contained(exponents, parsedMonomial.exponent)) { llvm::SmallString<16> coeffString; parsedMonomial.exponent.toStringSigned(coeffString); parser.emitError(parser.getCurrentLocation(), From ca97bcd9eb534dc475bbe302f2ba5ad074d1bad7 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 10:16:24 -0800 Subject: [PATCH 18/46] fix emitError string concatenation --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index fc9abd5ab2066..20fc5871c9c32 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -105,9 +105,9 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, if (llvm::is_contained(exponents, parsedMonomial.exponent)) { llvm::SmallString<16> coeffString; parsedMonomial.exponent.toStringSigned(coeffString); - parser.emitError(parser.getCurrentLocation(), - "at most one monomial may have exponent " + coeffString + - ", but found multiple"); + parser.emitError(parser.getCurrentLocation()) + << "at most one monomial may have exponent " << coeffString + << ", but found multiple"; return {}; } exponents.insert(parsedMonomial.exponent); From ce6643372daf02bae3fd52adc231d15cb5780c1c Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 10:17:05 -0800 Subject: [PATCH 19/46] remove stray comment --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 20fc5871c9c32..a1da473af750a 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -133,7 +133,6 @@ mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, } } - // Insert necessary constant ops to give as input to extract_slice. if (variables.size() > 1) { std::string vars = llvm::join(variables.begin(), variables.end(), ", "); parser.emitError( From dc2576765dd30795cf86378ab0cf56ea551bf4ce Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 10:17:50 -0800 Subject: [PATCH 20/46] remove unnecessary llvm:: prefix --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index a1da473af750a..a4a66e1523215 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -174,7 +174,7 @@ mlir::Attribute mlir::polynomial::RingAttr::parse(AsmParser &parser, if (failed(parser.parseEqual())) return {}; - IntegerType iType = llvm::dyn_cast(typeAttr.getValue()); + IntegerType iType = dyn_cast(typeAttr.getValue()); if (!iType) { parser.emitError(parser.getCurrentLocation(), "invalid coefficient modulus, coefficientType must " From edc306bae86cb566a9ec96066b5441b39f5d336a Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 10:26:45 -0800 Subject: [PATCH 21/46] fiddle with namespaces a bit more --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index a4a66e1523215..51d761b4b8ac0 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -78,8 +78,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, return success(); } -mlir::Attribute mlir::polynomial::PolynomialAttr::parse(AsmParser &parser, - Type type) { +Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseLess())) return {}; @@ -151,8 +150,7 @@ void RingAttr::print(AsmPrinter &p) const { << ", polynomialModulus=" << getPolynomialModulus() << '>'; } -mlir::Attribute mlir::polynomial::RingAttr::parse(AsmParser &parser, - Type type) { +Attribute RingAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseLess())) return {}; @@ -174,7 +172,7 @@ mlir::Attribute mlir::polynomial::RingAttr::parse(AsmParser &parser, if (failed(parser.parseEqual())) return {}; - IntegerType iType = dyn_cast(typeAttr.getValue()); + IntegerType iType = llvm::dyn_cast(typeAttr.getValue()); if (!iType) { parser.emitError(parser.getCurrentLocation(), "invalid coefficient modulus, coefficientType must " From 0248d99fc97832bf3766d1945e1eb074d3aa59ca Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 10:29:04 -0800 Subject: [PATCH 22/46] = nullptr --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 2914334fae47e..ea61e7d68e9dd 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -112,7 +112,7 @@ class Polynomial { friend ::llvm::hash_code hash_value(Polynomial arg); private: - ImplType *terms{nullptr}; + ImplType *terms = nullptr; }; // Make Polynomial hashable. From 6703deec2b448db619f57499d55acefff7eff6f8 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Mon, 13 Nov 2023 11:03:09 -0800 Subject: [PATCH 23/46] use hash_combine --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index ea61e7d68e9dd..587070866a963 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -58,7 +58,7 @@ class Monomial { // Prints polynomial to 'os'. void print(raw_ostream &os) const; - friend ::llvm::hash_code hash_value(const Monomial& arg); + friend ::llvm::hash_code hash_value(const Monomial &arg); public: APInt coefficient; @@ -120,8 +120,9 @@ inline ::llvm::hash_code hash_value(Polynomial arg) { return ::llvm::hash_value(arg.terms); } -inline ::llvm::hash_code hash_value(const Monomial& arg) { - return ::llvm::hash_value(arg.coefficient) ^ ::llvm::hash_value(arg.exponent); +inline ::llvm::hash_code hash_value(const Monomial &arg) { + return llvm::hash_combine(::llvm::hash_value(arg.coefficient), + ::llvm::hash_value(arg.exponent)); } inline raw_ostream &operator<<(raw_ostream &os, Polynomial polynomial) { From 740430ade03d308d659a36a1f0192da29f356246 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Wed, 15 Nov 2023 11:15:53 -0800 Subject: [PATCH 24/46] add diagnostic tests --- mlir/test/Dialect/Polynomial/attributes.mlir | 44 +++++++++++++++++++ .../Polynomial/{types.td => types.mlir} | 0 2 files changed, 44 insertions(+) create mode 100644 mlir/test/Dialect/Polynomial/attributes.mlir rename mlir/test/Dialect/Polynomial/{types.td => types.mlir} (100%) diff --git a/mlir/test/Dialect/Polynomial/attributes.mlir b/mlir/test/Dialect/Polynomial/attributes.mlir new file mode 100644 index 0000000000000..6cb39b75e0791 --- /dev/null +++ b/mlir/test/Dialect/Polynomial/attributes.mlir @@ -0,0 +1,44 @@ +// RUN: mlir-opt %s --split-input-file --verify-diagnostics + +#my_poly = #polynomial.polynomial +// expected-error@below {{polynomials must have one indeterminate, but there were multiple: y, x}} +#ring1 = #polynomial.ring + +// ----- + +// expected-error@below {{expected integer value}} +// expected-error@below {{found invalid integer exponent}} +#my_poly = #polynomial.polynomial<5 + x**f> +#ring1 = #polynomial.ring + +// ----- + +// expected-error@below {{at most one monomial may have exponent 2, but found multiple}} +#my_poly = #polynomial.polynomial<5 + x**2 + 3x**2> +#ring1 = #polynomial.ring + +// ----- + +// expected-error@below {{expected '>'}} +// expected-error@below {{expected + and more monomials, or > to end polynomial attribute}} +#my_poly = #polynomial.polynomial<5 + x**2 7> +#ring1 = #polynomial.ring + +// ----- + +#my_poly = #polynomial.polynomial<5 + x**2 +> +// expected-error@below {{expected another monomial after +, but found > ending attribute}} +#ring1 = #polynomial.ring + +// ----- + +#my_poly = #polynomial.polynomial<5 + x**2> +// expected-error@below {{coefficientType must specify an integer type}} +#ring1 = #polynomial.ring + +// ----- + +#my_poly = #polynomial.polynomial<5 + x**2> +// expected-error@below {{expected integer value}} +// expected-error@below {{invalid coefficient modulus}} +#ring1 = #polynomial.ring diff --git a/mlir/test/Dialect/Polynomial/types.td b/mlir/test/Dialect/Polynomial/types.mlir similarity index 100% rename from mlir/test/Dialect/Polynomial/types.td rename to mlir/test/Dialect/Polynomial/types.mlir From 640293e4f454bbb9202c422ced46b6949e18d334 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Wed, 15 Nov 2023 11:16:16 -0800 Subject: [PATCH 25/46] cleanup minor attribute suggestions --- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 2 +- .../Polynomial/IR/PolynomialAttributes.cpp | 23 +++++++------------ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index 4bc0393bcd417..db9752e2e3c3b 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -94,4 +94,4 @@ unsigned Polynomial::getDegree() const { } // namespace polynomial } // namespace mlir -MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::polynomial::detail::PolynomialStorage); +MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::polynomial::detail::PolynomialStorage) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 51d761b4b8ac0..a4cd0a3b726db 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -30,13 +30,6 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, llvm::StringRef &variable, bool *isConstantTerm) { APInt parsedCoeff(apintBitWidth, 1); auto result = parser.parseOptionalInteger(parsedCoeff); - if (result.has_value()) { - if (failed(*result)) { - parser.emitError(parser.getCurrentLocation(), - "invalid integer coefficient"); - return failure(); - } - } // Variable name result = parser.parseOptionalKeyword(&variable); @@ -118,13 +111,14 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseOptionalPlus())) { if (succeeded(parser.parseGreater())) { break; - } else { - parser.emitError( - parser.getCurrentLocation(), - "expected + and more monomials, or > to end polynomial attribute"); - return {}; } - } else if (succeeded(parser.parseOptionalGreater())) { + parser.emitError( + parser.getCurrentLocation(), + "expected + and more monomials, or > to end polynomial attribute"); + return {}; + } + + if (succeeded(parser.parseOptionalGreater())) { parser.emitError( parser.getCurrentLocation(), "expected another monomial after +, but found > ending attribute"); @@ -175,8 +169,7 @@ Attribute RingAttr::parse(AsmParser &parser, Type type) { IntegerType iType = llvm::dyn_cast(typeAttr.getValue()); if (!iType) { parser.emitError(parser.getCurrentLocation(), - "invalid coefficient modulus, coefficientType must " - "specify an integer type"); + "coefficientType must specify an integer type"); return {}; } APInt coefficientModulus(iType.getWidth(), 0); From 019d297bfd00b62fa8f42b85a3ed540b2e1face9 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Wed, 15 Nov 2023 11:46:39 -0800 Subject: [PATCH 26/46] add more positive tests --- mlir/test/Dialect/Polynomial/types.mlir | 39 ++++++++++++++++++++----- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/mlir/test/Dialect/Polynomial/types.mlir b/mlir/test/Dialect/Polynomial/types.mlir index 558c714e4400a..64b74d9d36bb1 100644 --- a/mlir/test/Dialect/Polynomial/types.mlir +++ b/mlir/test/Dialect/Polynomial/types.mlir @@ -1,19 +1,42 @@ // RUN: mlir-opt %s | FileCheck %s -#my_poly = #polynomial.polynomial<1 + x**1024> -#my_poly_2 = #polynomial.polynomial<2> -#my_poly_3 = #polynomial.polynomial<3x> -#my_poly_4 = #polynomial.polynomial -#ring1 = #polynomial.ring - -!ty = !polynomial.polynomial<#ring1> - // CHECK-LABEL: func @test_types // CHECK-SAME: !polynomial.polynomial< // CHECK-SAME: #polynomial.ring< // CHECK-SAME: coefficientType=i32, // CHECK-SAME: coefficientModulus=2837465 : i32, // CHECK-SAME: polynomialModulus=#polynomial.polynomial<1 + x**1024>>> +#my_poly = #polynomial.polynomial<1 + x**1024> +#ring1 = #polynomial.ring +!ty = !polynomial.polynomial<#ring1> func.func @test_types(%0: !ty) -> !ty { return %0 : !ty } + + +// CHECK-LABEL: func @test_non_x_variable_64_bit +// CHECK-SAME: !polynomial.polynomial< +// CHECK-SAME: #polynomial.ring< +// CHECK-SAME: coefficientType=i64, +// CHECK-SAME: coefficientModulus=2837465 : i64, +// CHECK-SAME: polynomialModulus=#polynomial.polynomial<2 + 4x + x**3>>> +#my_poly_2 = #polynomial.polynomial +#ring2 = #polynomial.ring +!ty2 = !polynomial.polynomial<#ring2> +func.func @test_non_x_variable_64_bit(%0: !ty2) -> !ty2 { + return %0 : !ty2 +} + + +// CHECK-LABEL: func @test_linear_poly +// CHECK-SAME: !polynomial.polynomial< +// CHECK-SAME: #polynomial.ring< +// CHECK-SAME: coefficientType=i32, +// CHECK-SAME: coefficientModulus=12 : i32, +// CHECK-SAME: polynomialModulus=#polynomial.polynomial<4x>> +#my_poly_3 = #polynomial.polynomial<4x> +#ring3 = #polynomial.ring +!ty3 = !polynomial.polynomial<#ring3> +func.func @test_linear_poly(%0: !ty3) -> !ty3 { + return %0 : !ty3 +} From 5c8cfe39c40333b1eb466d277c2432c6550127b0 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Wed, 15 Nov 2023 11:58:02 -0800 Subject: [PATCH 27/46] use StringSet --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 5 +++-- mlir/test/Dialect/Polynomial/attributes.mlir | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index a4cd0a3b726db..2c1459e1bc356 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -13,6 +13,7 @@ #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" namespace mlir { namespace polynomial { @@ -76,7 +77,7 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { return {}; llvm::SmallVector monomials; - llvm::SmallSet variables; + llvm::StringSet<> variables; llvm::DenseSet exponents; while (true) { @@ -127,7 +128,7 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { } if (variables.size() > 1) { - std::string vars = llvm::join(variables.begin(), variables.end(), ", "); + std::string vars = llvm::join(variables.keys(), ", "); parser.emitError( parser.getCurrentLocation(), "polynomials must have one indeterminate, but there were multiple: " + diff --git a/mlir/test/Dialect/Polynomial/attributes.mlir b/mlir/test/Dialect/Polynomial/attributes.mlir index 6cb39b75e0791..9eeec09cbba6b 100644 --- a/mlir/test/Dialect/Polynomial/attributes.mlir +++ b/mlir/test/Dialect/Polynomial/attributes.mlir @@ -1,7 +1,7 @@ // RUN: mlir-opt %s --split-input-file --verify-diagnostics #my_poly = #polynomial.polynomial -// expected-error@below {{polynomials must have one indeterminate, but there were multiple: y, x}} +// expected-error@below {{polynomials must have one indeterminate, but there were multiple: x, y}} #ring1 = #polynomial.ring // ----- From cf382a8365e05e5e8dab937b6cbb071e672ba77d Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Wed, 15 Nov 2023 12:01:17 -0800 Subject: [PATCH 28/46] use dyn_cast member function --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 2c1459e1bc356..906c8dd3a9f35 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -167,7 +167,7 @@ Attribute RingAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseEqual())) return {}; - IntegerType iType = llvm::dyn_cast(typeAttr.getValue()); + IntegerType iType = typeAttr.getValue().dyn_cast(); if (!iType) { parser.emitError(parser.getCurrentLocation(), "coefficientType must specify an integer type"); From 27bdc5e7cca0903458aa6f886bae12d4749d8c59 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Wed, 15 Nov 2023 13:07:42 -0800 Subject: [PATCH 29/46] try refactoring monomial parser somewhat --- .../Polynomial/IR/PolynomialAttributes.cpp | 110 ++++++++++-------- mlir/test/Dialect/Polynomial/attributes.mlir | 5 +- 2 files changed, 65 insertions(+), 50 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 906c8dd3a9f35..e6f7ac47699c6 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -26,18 +26,39 @@ void PolynomialAttr::print(AsmPrinter &p) const { /// Try to parse a monomial. If successful, populate the fields of the outparam /// `monomial` with the results, and the `variable` outparam with the parsed -/// variable name. +/// variable name. Sets shouldParseMore to true if the monomial is followed by +/// a '+'. ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, - llvm::StringRef &variable, bool *isConstantTerm) { + llvm::StringRef &variable, bool *isConstantTerm, + bool *shouldParseMore) { APInt parsedCoeff(apintBitWidth, 1); - auto result = parser.parseOptionalInteger(parsedCoeff); - - // Variable name - result = parser.parseOptionalKeyword(&variable); - if (!result.has_value() || failed(*result)) { - // We allow "failed" because it triggers when the next token is a +, - // which is allowed when the input is the constant term. - monomial.coefficient = parsedCoeff; + auto parsedCoeffResult = parser.parseOptionalInteger(parsedCoeff); + monomial.coefficient = parsedCoeff; + + *isConstantTerm = false; + *shouldParseMore = false; + + // A + indicates it's a constant term with more to go, as in `1 + x`. + if (succeeded(parser.parseOptionalPlus())) { + // If no coefficient was parsed, and there's a +, then it's effectively + // parsing an empty string. + if (!parsedCoeffResult.has_value()) { + return failure(); + } + monomial.exponent = APInt(apintBitWidth, 0); + *isConstantTerm = true; + *shouldParseMore = true; + return success(); + } + + // A monomial can be a trailing constant term, as in `x + 1` + if (failed(parser.parseOptionalKeyword(&variable))) { + // If neither a coefficient nor a variable was found, then it's effectively + // parsing an empty string. + if (!parsedCoeffResult.has_value()) { + return failure(); + } + monomial.exponent = APInt(apintBitWidth, 0); *isConstantTerm = true; return success(); @@ -46,29 +67,30 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, // Parse exponentiation symbol as ** // We can't use caret because it's reserved for basic block identifiers // If no star is present, it's treated as a polynomial with exponent 1 - if (failed(parser.parseOptionalStar())) { - monomial.coefficient = parsedCoeff; - monomial.exponent = APInt(apintBitWidth, 1); - return success(); - } + if (succeeded(parser.parseOptionalStar())) { + // If there's one * there must be two + if (failed(parser.parseStar())) { + parser.emitError(parser.getCurrentLocation(), + "exponents must be specified as a double-asterisk `**`"); + return failure(); + } - // If there's one * there must be two - if (failed(parser.parseStar())) { - parser.emitError(parser.getCurrentLocation(), - "exponents must be specified as a double-asterisk `**`"); - return failure(); - } + // If there's a **, then the integer exponent is required. + APInt parsedExponent(apintBitWidth, 0); + if (failed(parser.parseInteger(parsedExponent))) { + parser.emitError(parser.getCurrentLocation(), + "found invalid integer exponent"); + return failure(); + } - // If there's a **, then the integer exponent is required. - APInt parsedExponent(apintBitWidth, 0); - if (failed(parser.parseInteger(parsedExponent))) { - parser.emitError(parser.getCurrentLocation(), - "found invalid integer exponent"); - return failure(); + monomial.exponent = parsedExponent; + } else { + monomial.exponent = APInt(apintBitWidth, 1); } - monomial.coefficient = parsedCoeff; - monomial.exponent = parsedExponent; + if (succeeded(parser.parseOptionalPlus())) { + *shouldParseMore = true; + } return success(); } @@ -83,9 +105,11 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { while (true) { Monomial parsedMonomial; llvm::StringRef parsedVariableRef; - bool isConstantTerm = false; + bool isConstantTerm; + bool shouldParseMore; if (failed(parseMonomial(parser, parsedMonomial, parsedVariableRef, - &isConstantTerm))) { + &isConstantTerm, &shouldParseMore))) { + parser.emitError(parser.getCurrentLocation(), "expected a monomial"); return {}; } @@ -105,26 +129,16 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { } exponents.insert(parsedMonomial.exponent); - // Parse optional +. If a + is absent, require > and break, otherwise forbid - // > and continue with the next monomial. - // ParseOptional{Plus, Greater} does not return an OptionalParseResult, so - // failed means that the token was not found. - if (failed(parser.parseOptionalPlus())) { - if (succeeded(parser.parseGreater())) { - break; - } - parser.emitError( - parser.getCurrentLocation(), - "expected + and more monomials, or > to end polynomial attribute"); - return {}; - } + if (shouldParseMore) + continue; if (succeeded(parser.parseOptionalGreater())) { - parser.emitError( - parser.getCurrentLocation(), - "expected another monomial after +, but found > ending attribute"); - return {}; + break; } + parser.emitError( + parser.getCurrentLocation(), + "expected + and more monomials, or > to end polynomial attribute"); + return {}; } if (variables.size() > 1) { diff --git a/mlir/test/Dialect/Polynomial/attributes.mlir b/mlir/test/Dialect/Polynomial/attributes.mlir index 9eeec09cbba6b..16e385c520079 100644 --- a/mlir/test/Dialect/Polynomial/attributes.mlir +++ b/mlir/test/Dialect/Polynomial/attributes.mlir @@ -7,6 +7,7 @@ // ----- // expected-error@below {{expected integer value}} +// expected-error@below {{expected a monomial}} // expected-error@below {{found invalid integer exponent}} #my_poly = #polynomial.polynomial<5 + x**f> #ring1 = #polynomial.ring @@ -19,17 +20,17 @@ // ----- -// expected-error@below {{expected '>'}} // expected-error@below {{expected + and more monomials, or > to end polynomial attribute}} #my_poly = #polynomial.polynomial<5 + x**2 7> #ring1 = #polynomial.ring // ----- +// expected-error@below {{expected a monomial}} #my_poly = #polynomial.polynomial<5 + x**2 +> -// expected-error@below {{expected another monomial after +, but found > ending attribute}} #ring1 = #polynomial.ring + // ----- #my_poly = #polynomial.polynomial<5 + x**2> From 156e3b38e513853deb2e8e2069c65ce3aa884ca7 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Sun, 31 Mar 2024 15:36:33 -0700 Subject: [PATCH 30/46] don't unique Polynomial, remove std::optional from OptionalParameter --- .../mlir/Dialect/Polynomial/IR/Polynomial.h | 63 ++++-------------- .../Polynomial/IR/PolynomialAttributes.td | 13 +--- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 30 ++------- .../Polynomial/IR/PolynomialAttributes.cpp | 8 +-- .../Dialect/Polynomial/IR/PolynomialDetail.h | 64 ------------------- .../Polynomial/IR/PolynomialDialect.cpp | 5 -- 6 files changed, 26 insertions(+), 157 deletions(-) delete mode 100644 mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 587070866a963..56b066b1e9440 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -15,6 +15,7 @@ #include "llvm/ADT/APInt.h" #include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/Hashing.h" +#include "llvm/ADT/SmallVector.h" namespace mlir { @@ -27,10 +28,6 @@ namespace polynomial { /// would want to specify 128-bit polynomials statically in the source code. constexpr unsigned apintBitWidth = 64; -namespace detail { -struct PolynomialStorage; -} // namespace detail - /// A class representing a monomial of a single-variable polynomial with integer /// coefficients. class Monomial { @@ -67,8 +64,7 @@ class Monomial { APInt exponent; }; -/// A single-variable polynomial with integer coefficients. Polynomials are -/// immutable and uniqued. +/// A single-variable polynomial with integer coefficients. /// /// Eg: x^1024 + x + 1 /// @@ -76,24 +72,19 @@ class Monomial { /// it is used consistently throughout the polynomial. class Polynomial { public: - using ImplType = detail::PolynomialStorage; + Polynomial() = delete; - constexpr Polynomial() = default; - explicit Polynomial(ImplType *terms) : terms(terms) {} + Polynomial(const llvm::SmallVector& terms) : terms(terms) {}; - static Polynomial fromMonomials(ArrayRef monomials, - MLIRContext *context); + static Polynomial fromMonomials(ArrayRef monomials); /// Returns a polynomial with coefficients given by `coeffs`. The value /// coeffs[i] is converted to a monomial with exponent i. - static Polynomial fromCoefficients(ArrayRef coeffs, - MLIRContext *context); - - MLIRContext *getContext() const; + static Polynomial fromCoefficients(ArrayRef coeffs); - explicit operator bool() const { return terms != nullptr; } - bool operator==(Polynomial other) const { return other.terms == terms; } - bool operator!=(Polynomial other) const { return !(other.terms == terms); } + explicit operator bool() const { return !terms.empty(); } + bool operator==(const Polynomial& other) const { return other.terms == terms; } + bool operator!=(const Polynomial& other) const { return !(other.terms == terms); } // Prints polynomial to 'os'. void print(raw_ostream &os) const; @@ -104,20 +95,18 @@ class Polynomial { // Prints polynomial so that it can be used as a valid identifier std::string toIdentifier() const; - // A polynomial's terms are canonically stored in order of increasing degree. - ArrayRef getTerms() const; - unsigned getDegree() const; friend ::llvm::hash_code hash_value(Polynomial arg); private: - ImplType *terms = nullptr; + // The monomial terms for this polynomial. + SmallVector terms; }; // Make Polynomial hashable. inline ::llvm::hash_code hash_value(Polynomial arg) { - return ::llvm::hash_value(arg.terms); + return ::llvm::hash_combine_range(arg.terms.begin(), arg.terms.end()); } inline ::llvm::hash_code hash_value(const Monomial &arg) { @@ -125,7 +114,7 @@ inline ::llvm::hash_code hash_value(const Monomial &arg) { ::llvm::hash_value(arg.exponent)); } -inline raw_ostream &operator<<(raw_ostream &os, Polynomial polynomial) { +inline raw_ostream &operator<<(raw_ostream &os, const Polynomial& polynomial) { polynomial.print(os); return os; } @@ -133,30 +122,4 @@ inline raw_ostream &operator<<(raw_ostream &os, Polynomial polynomial) { } // namespace polynomial } // namespace mlir -namespace llvm { - -// Polynomials hash just like pointers -template <> -struct DenseMapInfo { - static mlir::polynomial::Polynomial getEmptyKey() { - auto *pointer = llvm::DenseMapInfo::getEmptyKey(); - return mlir::polynomial::Polynomial( - static_cast(pointer)); - } - static mlir::polynomial::Polynomial getTombstoneKey() { - auto *pointer = llvm::DenseMapInfo::getTombstoneKey(); - return mlir::polynomial::Polynomial( - static_cast(pointer)); - } - static unsigned getHashValue(mlir::polynomial::Polynomial val) { - return mlir::polynomial::hash_value(val); - } - static bool isEqual(mlir::polynomial::Polynomial lhs, - mlir::polynomial::Polynomial rhs) { - return lhs == rhs; - } -}; - -} // namespace llvm - #endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td index 626350813d0dd..723f6b7c2006b 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td @@ -22,16 +22,7 @@ def Polynomial_PolynomialAttr : Polynomial_Attr<"Polynomial", "polynomial"> { let description = [{ #poly = #polynomial.poly }]; - let parameters = (ins "Polynomial":$polynomial); - - let builders = [ - AttrBuilderWithInferredContext<(ins "Polynomial":$polynomial), [{ - return $_get(polynomial.getContext(), polynomial); - }]> - ]; - - let skipDefaultBuilders = 1; let hasCustomAssemblyFormat = 1; } @@ -74,8 +65,8 @@ def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { let parameters = (ins Builtin_TypeAttr: $coefficientType, - OptionalParameter<"std::optional">: $coefficientModulus, - OptionalParameter<"std::optional">: $polynomialModulus + OptionalParameter<"IntegerAttr">: $coefficientModulus, + OptionalParameter<"PolynomialAttr">: $polynomialModulus ); let hasCustomAssemblyFormat = 1; diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index db9752e2e3c3b..0097e540a3a75 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -8,7 +8,6 @@ #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" @@ -19,39 +18,26 @@ namespace mlir { namespace polynomial { -MLIRContext *Polynomial::getContext() const { return terms->context; } - -ArrayRef Polynomial::getTerms() const { return terms->terms(); } - -Polynomial Polynomial::fromMonomials(ArrayRef monomials, - MLIRContext *context) { - auto assignCtx = [context](detail::PolynomialStorage *storage) { - storage->context = context; - }; - +Polynomial Polynomial::fromMonomials(ArrayRef monomials) { // A polynomial's terms are canonically stored in order of increasing degree. - auto monomialsCopy = llvm::OwningArrayRef(monomials); + auto monomialsCopy = llvm::SmallVector(monomials); std::sort(monomialsCopy.begin(), monomialsCopy.end()); - - StorageUniquer &uniquer = context->getAttributeUniquer(); - return Polynomial(uniquer.get( - assignCtx, monomials.size(), monomialsCopy)); + return Polynomial(monomialsCopy); } -Polynomial Polynomial::fromCoefficients(ArrayRef coeffs, - MLIRContext *context) { +Polynomial Polynomial::fromCoefficients(ArrayRef coeffs) { llvm::SmallVector monomials; monomials.reserve(coeffs.size()); for (size_t i = 0; i < coeffs.size(); i++) { monomials.emplace_back(coeffs[i], i); } - return Polynomial::fromMonomials(monomials, context); + 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->terms()) { + for (const auto &term : terms) { if (first) { first = false; } else { @@ -88,10 +74,8 @@ std::string Polynomial::toIdentifier() const { } unsigned Polynomial::getDegree() const { - return terms->terms().back().exponent.getZExtValue(); + return terms.back().exponent.getZExtValue(); } } // namespace polynomial } // namespace mlir - -MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::polynomial::detail::PolynomialStorage) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index e6f7ac47699c6..4810e79f1c101 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -149,8 +149,8 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { vars); } - Polynomial poly = Polynomial::fromMonomials(monomials, parser.getContext()); - return PolynomialAttr::get(poly); + Polynomial poly = Polynomial::fromMonomials(monomials); + return PolynomialAttr::get(parser.getContext(), poly); } void RingAttr::print(AsmPrinter &p) const { @@ -176,7 +176,7 @@ Attribute RingAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseComma())) return {}; - std::optional coefficientModulusAttr = std::nullopt; + IntegerAttr coefficientModulusAttr = nullptr; if (succeeded(parser.parseKeyword("coefficientModulus"))) { if (failed(parser.parseEqual())) return {}; @@ -200,7 +200,7 @@ Attribute RingAttr::parse(AsmParser &parser, Type type) { return {}; } - std::optional polyAttr = std::nullopt; + PolynomialAttr polyAttr = nullptr; if (succeeded(parser.parseKeyword("polynomialModulus"))) { if (failed(parser.parseEqual())) return {}; diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h b/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h deleted file mode 100644 index f57dc50e309cd..0000000000000 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialDetail.h +++ /dev/null @@ -1,64 +0,0 @@ -//===- Polynomial.h - Storage class details for 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 -// -//===----------------------------------------------------------------------===// - -#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDETAIL_H_ -#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDETAIL_H_ - -#include "mlir/Dialect/Polynomial/IR/Polynomial.h" -#include "mlir/Support/StorageUniquer.h" -#include "mlir/Support/TypeID.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/Support/TrailingObjects.h" - -namespace mlir { -namespace polynomial { -namespace detail { - -// A Polynomial is stored as an ordered list of monomial terms, each of which -// is a tuple of coefficient and exponent. -struct PolynomialStorage final - : public StorageUniquer::BaseStorage, - public llvm::TrailingObjects { - /// The hash key used for uniquing. - using KeyTy = std::tuple>; - - unsigned numTerms; - - MLIRContext *context; - - /// The monomial terms for this polynomial. - ArrayRef terms() const { - return {getTrailingObjects(), numTerms}; - } - - bool operator==(const KeyTy &key) const { - return std::get<0>(key) == numTerms && std::get<1>(key) == terms(); - } - - // Constructs a PolynomialStorage from a key. The context must be set by the - // caller. - static PolynomialStorage * - construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) { - auto terms = std::get<1>(key); - auto byteSize = PolynomialStorage::totalSizeToAlloc(terms.size()); - auto *rawMem = allocator.allocate(byteSize, alignof(PolynomialStorage)); - auto *res = new (rawMem) PolynomialStorage(); - res->numTerms = std::get<0>(key); - std::uninitialized_copy(terms.begin(), terms.end(), - res->getTrailingObjects()); - return res; - } -}; - -} // namespace detail -} // namespace polynomial -} // namespace mlir - -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::polynomial::detail::PolynomialStorage) - -#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALDETAIL_H_ diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp index 6b4e26e5fb03c..f16aa2f55b71a 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp @@ -8,7 +8,6 @@ #include "mlir/Dialect/Polynomial/IR/Polynomial.h" -#include "PolynomialDetail.h" #include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.h" #include "mlir/Dialect/Polynomial/IR/PolynomialOps.h" #include "mlir/Dialect/Polynomial/IR/PolynomialTypes.h" @@ -39,8 +38,4 @@ void PolynomialDialect::initialize() { #define GET_OP_LIST #include "mlir/Dialect/Polynomial/IR/PolynomialOps.cpp.inc" >(); - - getContext() - ->getAttributeUniquer() - .registerParametricStorageType(); } From ca4413429e6517c7c53c583c02d4571f47ebeb20 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Sun, 31 Mar 2024 15:40:48 -0700 Subject: [PATCH 31/46] clang-format --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 56b066b1e9440..8388bdd8ebacb 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -74,7 +74,7 @@ class Polynomial { public: Polynomial() = delete; - Polynomial(const llvm::SmallVector& terms) : terms(terms) {}; + Polynomial(const llvm::SmallVector &terms) : terms(terms){}; static Polynomial fromMonomials(ArrayRef monomials); @@ -83,8 +83,12 @@ class Polynomial { static Polynomial fromCoefficients(ArrayRef coeffs); explicit operator bool() const { return !terms.empty(); } - bool operator==(const Polynomial& other) const { return other.terms == terms; } - bool operator!=(const Polynomial& other) const { return !(other.terms == terms); } + bool operator==(const Polynomial &other) const { + return other.terms == terms; + } + bool operator!=(const Polynomial &other) const { + return !(other.terms == terms); + } // Prints polynomial to 'os'. void print(raw_ostream &os) const; @@ -114,7 +118,7 @@ inline ::llvm::hash_code hash_value(const Monomial &arg) { ::llvm::hash_value(arg.exponent)); } -inline raw_ostream &operator<<(raw_ostream &os, const Polynomial& polynomial) { +inline raw_ostream &operator<<(raw_ostream &os, const Polynomial &polynomial) { polynomial.print(os); return os; } From e1d4b42a16f158c3bde27886cda9deca26f580b0 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:00:25 -0700 Subject: [PATCH 32/46] address comments about cmake config --- .../mlir/Dialect/Polynomial/IR/CMakeLists.txt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt index 3719fda2aec6d..b51daf5d616e9 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt +++ b/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt @@ -1,19 +1,26 @@ -set(LLVM_TARGET_DEFINITIONS PolynomialOps.td) +set(LLVM_TARGET_DEFINITIONS PolynomialDialect.td) mlir_tablegen(PolynomialDialect.cpp.inc -gen-dialect-defs -dialect=polynomial) mlir_tablegen(PolynomialDialect.h.inc -gen-dialect-decls -dialect=polynomial) add_public_tablegen_target(MLIRPolynomialDialectIncGen) +add_dependencies(mlir-headers MLIRPolynomialDialectIncGen) +set(LLVM_TARGET_DEFINITIONS PolynomialAttributes.td) mlir_tablegen(PolynomialAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=polynomial) mlir_tablegen(PolynomialAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=polynomial) +add_public_tablegen_target(MLIRPolynomialAttributesIncGen) +add_dependencies(mlir-headers MLIRPolynomialAttributesIncGen) +add_mlir_doc(PolynomialAttributes PolynomialAttributes Dialects/ -gen-attrdef-doc) + +set(LLVM_TARGET_DEFINITIONS PolynomialOps.td) mlir_tablegen(PolynomialOps.cpp.inc -gen-op-defs) mlir_tablegen(PolynomialOps.h.inc -gen-op-decls) -mlir_tablegen(PolynomialTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=polynomial) -mlir_tablegen(PolynomialTypes.h.inc -gen-typedef-decls -typedefs-dialect=polynomial) -add_public_tablegen_target(MLIRPolynomialAttributesIncGen) add_public_tablegen_target(MLIRPolynomialOpsIncGen) -add_public_tablegen_target(MLIRPolynomialTypesIncGen) add_dependencies(mlir-headers MLIRPolynomialOpsIncGen) - add_mlir_doc(PolynoialOps PolynoialOps Dialects/ -gen-dialect-doc -dialect polynomial) -add_mlir_doc(PolynomialAttributes PolynomialAttributes Dialects/ -gen-attrdef-doc) + +set(LLVM_TARGET_DEFINITIONS PolynomialTypes.td) +mlir_tablegen(PolynomialTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=polynomial) +mlir_tablegen(PolynomialTypes.h.inc -gen-typedef-decls -typedefs-dialect=polynomial) +add_public_tablegen_target(MLIRPolynomialTypesIncGen) +add_dependencies(mlir-headers MLIRPolynomialTypesIncGen) add_mlir_doc(PolynomialTypes PolynomialTypes Dialects/ -gen-typedef-doc) From bfee480e96c178c30895f1fd55ef38f165859b28 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:03:28 -0700 Subject: [PATCH 33/46] fix include guard --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 8388bdd8ebacb..cde196e3485eb 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ -#define MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ +#ifndef MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ +#define MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ #include @@ -126,4 +126,4 @@ inline raw_ostream &operator<<(raw_ostream &os, const Polynomial &polynomial) { } // namespace polynomial } // namespace mlir -#endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ +#endif // MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ From 7ed17127387e77d3df8eaccbf90bb0e2dcf0e42a Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:04:13 -0700 Subject: [PATCH 34/46] remove std::move --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index cde196e3485eb..2313ab00ec082 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -35,8 +35,8 @@ class Monomial { Monomial(int64_t coeff, uint64_t expo) : coefficient(apintBitWidth, coeff), exponent(apintBitWidth, expo) {} - Monomial(APInt coeff, APInt expo) - : coefficient(std::move(coeff)), exponent(std::move(expo)) {} + Monomial(const APInt &coeff, const APInt &expo) + : coefficient(coeff), exponent(expo) {} Monomial() : coefficient(apintBitWidth, 0), exponent(apintBitWidth, 0) {} From a8243185e6d2ab94f5cd4e08088f9273bedb0726 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:13:06 -0700 Subject: [PATCH 35/46] use explicit and ArrayRef in polynomial constructor --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 2313ab00ec082..b4bfc9ca8a61b 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -13,7 +13,7 @@ #include "mlir/Support/LLVM.h" #include "llvm/ADT/APInt.h" -#include "llvm/ADT/DenseMapInfo.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/SmallVector.h" @@ -74,7 +74,7 @@ class Polynomial { public: Polynomial() = delete; - Polynomial(const llvm::SmallVector &terms) : terms(terms){}; + explicit Polynomial(ArrayRef terms) : terms(terms) {}; static Polynomial fromMonomials(ArrayRef monomials); From 59984d49dfd6a1bf17a1f395f5ec502ef3e438c4 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:13:58 -0700 Subject: [PATCH 36/46] const hash_value arg --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index b4bfc9ca8a61b..6a71da6089d20 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -101,7 +101,7 @@ class Polynomial { unsigned getDegree() const; - friend ::llvm::hash_code hash_value(Polynomial arg); + friend ::llvm::hash_code hash_value(const Polynomial &arg); private: // The monomial terms for this polynomial. @@ -109,7 +109,7 @@ class Polynomial { }; // Make Polynomial hashable. -inline ::llvm::hash_code hash_value(Polynomial arg) { +inline ::llvm::hash_code hash_value(const Polynomial &arg) { return ::llvm::hash_combine_range(arg.terms.begin(), arg.terms.end()); } From 2ccec4b70faeeaa9b98a97bcc52d4b2f6123f7d7 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:15:53 -0700 Subject: [PATCH 37/46] don't evaluate size in ever loop iteration --- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index 0097e540a3a75..0a76d55ca5853 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -27,8 +27,9 @@ Polynomial Polynomial::fromMonomials(ArrayRef monomials) { Polynomial Polynomial::fromCoefficients(ArrayRef coeffs) { llvm::SmallVector monomials; - monomials.reserve(coeffs.size()); - for (size_t i = 0; i < coeffs.size(); i++) { + auto size = coeffs.size(); + monomials.reserve(size); + for (size_t i = 0; i < size; i++) { monomials.emplace_back(coeffs[i], i); } return Polynomial::fromMonomials(monomials); From f47c22f2a4a2d6ca6a0bb6c950e2ed3d74193632 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:20:25 -0700 Subject: [PATCH 38/46] expand auto --- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index 0a76d55ca5853..00c503720942b 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -38,7 +38,7 @@ Polynomial Polynomial::fromCoefficients(ArrayRef coeffs) { void Polynomial::print(raw_ostream &os, ::llvm::StringRef separator, ::llvm::StringRef exponentiation) const { bool first = true; - for (const auto &term : terms) { + for (const Monomial &term : terms) { if (first) { first = false; } else { From 2ce3cd97d9f3e0731c97f5493d8df07943dff302 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:21:09 -0700 Subject: [PATCH 39/46] 512 -> 16 chars smallstring --- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index 00c503720942b..b18df98f86824 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -48,7 +48,7 @@ void Polynomial::print(raw_ostream &os, ::llvm::StringRef separator, if (term.coefficient == 1 && term.exponent.uge(1)) { coeffToPrint = ""; } else { - llvm::SmallString<512> coeffString; + llvm::SmallString<16> coeffString; term.coefficient.toStringSigned(coeffString); coeffToPrint = coeffString.str(); } @@ -58,7 +58,7 @@ void Polynomial::print(raw_ostream &os, ::llvm::StringRef separator, } else if (term.exponent == 1) { os << coeffToPrint << "x"; } else { - llvm::SmallString<512> expString; + llvm::SmallString<16> expString; term.exponent.toStringSigned(expString); os << coeffToPrint << "x" << exponentiation << expString; } From a1b682a27a12cccb9870ab40b8604be2b6371841 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:22:36 -0700 Subject: [PATCH 40/46] add period to end of comments, remove SmallSet include --- .../Dialect/Polynomial/IR/PolynomialAttributes.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 4810e79f1c101..e8ea4439f6b5b 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -10,7 +10,6 @@ #include "mlir/Dialect/Polynomial/IR/Polynomial.h" #include "mlir/Support/LLVM.h" #include "mlir/Support/LogicalResult.h" -#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" @@ -51,7 +50,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, return success(); } - // A monomial can be a trailing constant term, as in `x + 1` + // A monomial can be a trailing constant term, as in `x + 1`. if (failed(parser.parseOptionalKeyword(&variable))) { // If neither a coefficient nor a variable was found, then it's effectively // parsing an empty string. @@ -64,11 +63,11 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, return success(); } - // Parse exponentiation symbol as ** - // We can't use caret because it's reserved for basic block identifiers - // If no star is present, it's treated as a polynomial with exponent 1 + // Parse exponentiation symbol as `**`. We can't use caret because it's + // reserved for basic block identifiers If no star is present, it's treated + // as a polynomial with exponent 1. if (succeeded(parser.parseOptionalStar())) { - // If there's one * there must be two + // If there's one * there must be two. if (failed(parser.parseStar())) { parser.emitError(parser.getCurrentLocation(), "exponents must be specified as a double-asterisk `**`"); From c4487119cc94d55a8ff759c179ebaeaac52c3dcb Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:23:26 -0700 Subject: [PATCH 41/46] remove parseStar error message --- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index e8ea4439f6b5b..1362c1b343fab 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -69,8 +69,6 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, if (succeeded(parser.parseOptionalStar())) { // If there's one * there must be two. if (failed(parser.parseStar())) { - parser.emitError(parser.getCurrentLocation(), - "exponents must be specified as a double-asterisk `**`"); return failure(); } From b86febc7ff1974fec4f951dc35431414542539ce Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:24:14 -0700 Subject: [PATCH 42/46] pass mutated bools by reference --- .../Polynomial/IR/PolynomialAttributes.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 1362c1b343fab..0e1e686191f21 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -28,14 +28,14 @@ void PolynomialAttr::print(AsmPrinter &p) const { /// variable name. Sets shouldParseMore to true if the monomial is followed by /// a '+'. ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, - llvm::StringRef &variable, bool *isConstantTerm, - bool *shouldParseMore) { + llvm::StringRef &variable, bool &isConstantTerm, + bool &shouldParseMore) { APInt parsedCoeff(apintBitWidth, 1); auto parsedCoeffResult = parser.parseOptionalInteger(parsedCoeff); monomial.coefficient = parsedCoeff; - *isConstantTerm = false; - *shouldParseMore = false; + isConstantTerm = false; + shouldParseMore = false; // A + indicates it's a constant term with more to go, as in `1 + x`. if (succeeded(parser.parseOptionalPlus())) { @@ -45,8 +45,8 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, return failure(); } monomial.exponent = APInt(apintBitWidth, 0); - *isConstantTerm = true; - *shouldParseMore = true; + isConstantTerm = true; + shouldParseMore = true; return success(); } @@ -59,7 +59,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, } monomial.exponent = APInt(apintBitWidth, 0); - *isConstantTerm = true; + isConstantTerm = true; return success(); } @@ -86,7 +86,7 @@ ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, } if (succeeded(parser.parseOptionalPlus())) { - *shouldParseMore = true; + shouldParseMore = true; } return success(); } @@ -105,7 +105,7 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { bool isConstantTerm; bool shouldParseMore; if (failed(parseMonomial(parser, parsedMonomial, parsedVariableRef, - &isConstantTerm, &shouldParseMore))) { + isConstantTerm, shouldParseMore))) { parser.emitError(parser.getCurrentLocation(), "expected a monomial"); return {}; } From 6f0ff78e1d1c31478f44c03a3ab67aaaccacb8f0 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:36:12 -0700 Subject: [PATCH 43/46] move duplicate exponent check to fromMonomials --- .../mlir/Dialect/Polynomial/IR/Polynomial.h | 7 ++++--- mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp | 20 ++++++++++++++++--- .../Polynomial/IR/PolynomialAttributes.cpp | 20 +++++++------------ mlir/test/Dialect/Polynomial/attributes.mlir | 2 +- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 6a71da6089d20..adfb275d70223 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -9,8 +9,7 @@ #ifndef MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ #define MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ -#include - +#include "mlir/Support/LogicalResult.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" @@ -76,7 +75,9 @@ class Polynomial { explicit Polynomial(ArrayRef terms) : terms(terms) {}; - static Polynomial fromMonomials(ArrayRef monomials); + // Returns a Polynomial from a list of monomials. + // Fails if two monomials have the same exponent. + static FailureOr fromMonomials(ArrayRef monomials); /// Returns a polynomial with coefficients given by `coeffs`. The value /// coeffs[i] is converted to a monomial with exponent i. diff --git a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp index b18df98f86824..5916ffba78e24 100644 --- a/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/Polynomial.cpp @@ -8,7 +8,7 @@ #include "mlir/Dialect/Polynomial/IR/Polynomial.h" -#include "mlir/IR/MLIRContext.h" +#include "mlir/Support/LogicalResult.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" @@ -18,10 +18,20 @@ namespace mlir { namespace polynomial { -Polynomial Polynomial::fromMonomials(ArrayRef monomials) { +FailureOr Polynomial::fromMonomials(ArrayRef monomials) { // A polynomial's terms are canonically stored in order of increasing degree. auto monomialsCopy = llvm::SmallVector(monomials); std::sort(monomialsCopy.begin(), monomialsCopy.end()); + + // Ensure non-unique exponents are not present. Since we sorted the list by + // exponent, a linear scan of adjancent monomials suffices. + if (std::adjacent_find(monomialsCopy.begin(), monomialsCopy.end(), + [](const Monomial &lhs, const Monomial &rhs) { + return lhs.exponent == rhs.exponent; + }) != monomialsCopy.end()) { + return failure(); + } + return Polynomial(monomialsCopy); } @@ -32,7 +42,11 @@ Polynomial Polynomial::fromCoefficients(ArrayRef coeffs) { for (size_t i = 0; i < size; i++) { monomials.emplace_back(coeffs[i], i); } - return Polynomial::fromMonomials(monomials); + auto result = Polynomial::fromMonomials(monomials); + // Construction guarantees unique exponents, so the failure mode of + // fromMonomials can be bypassed. + assert(succeeded(result)); + return result.value(); } void Polynomial::print(raw_ostream &os, ::llvm::StringRef separator, diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 0e1e686191f21..7730e110f71dc 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -97,7 +97,6 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { llvm::SmallVector monomials; llvm::StringSet<> variables; - llvm::DenseSet exponents; while (true) { Monomial parsedMonomial; @@ -116,16 +115,6 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { } monomials.push_back(parsedMonomial); - if (llvm::is_contained(exponents, parsedMonomial.exponent)) { - llvm::SmallString<16> coeffString; - parsedMonomial.exponent.toStringSigned(coeffString); - parser.emitError(parser.getCurrentLocation()) - << "at most one monomial may have exponent " << coeffString - << ", but found multiple"; - return {}; - } - exponents.insert(parsedMonomial.exponent); - if (shouldParseMore) continue; @@ -146,8 +135,13 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { vars); } - Polynomial poly = Polynomial::fromMonomials(monomials); - return PolynomialAttr::get(parser.getContext(), poly); + auto result = Polynomial::fromMonomials(monomials); + if (failed(result)) { + parser.emitError(parser.getCurrentLocation()) + << "parsed polynomial must have unique exponents among monomials"; + return {}; + } + return PolynomialAttr::get(parser.getContext(), result.value()); } void RingAttr::print(AsmPrinter &p) const { diff --git a/mlir/test/Dialect/Polynomial/attributes.mlir b/mlir/test/Dialect/Polynomial/attributes.mlir index 16e385c520079..3973ae3944335 100644 --- a/mlir/test/Dialect/Polynomial/attributes.mlir +++ b/mlir/test/Dialect/Polynomial/attributes.mlir @@ -14,8 +14,8 @@ // ----- -// expected-error@below {{at most one monomial may have exponent 2, but found multiple}} #my_poly = #polynomial.polynomial<5 + x**2 + 3x**2> +// expected-error@below {{parsed polynomial must have unique exponents among monomials}} #ring1 = #polynomial.ring // ----- From 3c0a064fcce47c96fe473a9888ab4ad0fbe4f25a Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 14:52:36 -0700 Subject: [PATCH 44/46] use Type instead of TypeAttr --- .../mlir/Dialect/Polynomial/IR/PolynomialAttributes.td | 2 +- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td index 723f6b7c2006b..726f2ea8d3b7b 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td @@ -64,7 +64,7 @@ def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { }]; let parameters = (ins - Builtin_TypeAttr: $coefficientType, + "Type": $coefficientType, OptionalParameter<"IntegerAttr">: $coefficientModulus, OptionalParameter<"PolynomialAttr">: $polynomialModulus ); diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 7730e110f71dc..445cb0bb91ea0 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -160,8 +160,8 @@ Attribute RingAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseEqual())) return {}; - TypeAttr typeAttr; - if (failed(parser.parseAttribute(typeAttr))) + Type ty; + if (failed(parser.parseType(ty))) return {}; if (failed(parser.parseComma())) @@ -172,7 +172,7 @@ Attribute RingAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseEqual())) return {}; - IntegerType iType = typeAttr.getValue().dyn_cast(); + IntegerType iType = ty.dyn_cast(); if (!iType) { parser.emitError(parser.getCurrentLocation(), "coefficientType must specify an integer type"); @@ -205,7 +205,7 @@ Attribute RingAttr::parse(AsmParser &parser, Type type) { if (failed(parser.parseGreater())) return {}; - return RingAttr::get(parser.getContext(), typeAttr, coefficientModulusAttr, + return RingAttr::get(parser.getContext(), ty, coefficientModulusAttr, polyAttr); } From aebf3645442d1f3a59bf870fb60b38229116fda0 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Fri, 12 Apr 2024 16:47:42 -0700 Subject: [PATCH 45/46] clang-format --- mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h | 4 ++-- mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index adfb275d70223..6c79f553d9a7c 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -9,8 +9,8 @@ #ifndef MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ #define MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ -#include "mlir/Support/LogicalResult.h" #include "mlir/Support/LLVM.h" +#include "mlir/Support/LogicalResult.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/Hashing.h" @@ -73,7 +73,7 @@ class Polynomial { public: Polynomial() = delete; - explicit Polynomial(ArrayRef terms) : terms(terms) {}; + explicit Polynomial(ArrayRef terms) : terms(terms){}; // Returns a Polynomial from a list of monomials. // Fails if two monomials have the same exponent. diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp index 445cb0bb91ea0..ee09c73bb3c4a 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialAttributes.cpp @@ -137,9 +137,9 @@ Attribute PolynomialAttr::parse(AsmParser &parser, Type type) { auto result = Polynomial::fromMonomials(monomials); if (failed(result)) { - parser.emitError(parser.getCurrentLocation()) - << "parsed polynomial must have unique exponents among monomials"; - return {}; + parser.emitError(parser.getCurrentLocation()) + << "parsed polynomial must have unique exponents among monomials"; + return {}; } return PolynomialAttr::get(parser.getContext(), result.value()); } From e7bb769593bbb76677d920b12aaa78439113c40e Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Sun, 14 Apr 2024 22:18:04 -0700 Subject: [PATCH 46/46] use the add_mlir_dialect macro --- .../mlir/Dialect/Polynomial/IR/CMakeLists.txt | 28 +--- .../mlir/Dialect/Polynomial/IR/Polynomial.h | 2 +- .../mlir/Dialect/Polynomial/IR/Polynomial.td | 153 ++++++++++++++++++ .../Polynomial/IR/PolynomialAttributes.td | 75 --------- .../Polynomial/IR/PolynomialDialect.td | 54 ------- .../Dialect/Polynomial/IR/PolynomialOps.h | 2 +- .../Dialect/Polynomial/IR/PolynomialOps.td | 40 ----- .../Dialect/Polynomial/IR/PolynomialTypes.td | 32 ---- mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt | 4 +- .../Polynomial/IR/PolynomialDialect.cpp | 4 +- .../Dialect/Polynomial/IR/PolynomialOps.cpp | 2 +- 11 files changed, 165 insertions(+), 231 deletions(-) create mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td delete mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td delete mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td delete mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td delete mode 100644 mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt index b51daf5d616e9..d8039deb5ee21 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt +++ b/mlir/include/mlir/Dialect/Polynomial/IR/CMakeLists.txt @@ -1,26 +1,10 @@ -set(LLVM_TARGET_DEFINITIONS PolynomialDialect.td) -mlir_tablegen(PolynomialDialect.cpp.inc -gen-dialect-defs -dialect=polynomial) -mlir_tablegen(PolynomialDialect.h.inc -gen-dialect-decls -dialect=polynomial) -add_public_tablegen_target(MLIRPolynomialDialectIncGen) -add_dependencies(mlir-headers MLIRPolynomialDialectIncGen) +add_mlir_dialect(Polynomial polynomial) +add_mlir_doc(PolynomialDialect PolynomialDialect Polynomial/ -gen-dialect-doc) +add_mlir_doc(PolynomialOps PolynomialOps Polynomial/ -gen-op-doc) +add_mlir_doc(PolynomialAttributes PolynomialAttributes Dialects/ -gen-attrdef-doc) +add_mlir_doc(PolynomialTypes PolynomialTypes Dialects/ -gen-typedef-doc) -set(LLVM_TARGET_DEFINITIONS PolynomialAttributes.td) +set(LLVM_TARGET_DEFINITIONS Polynomial.td) mlir_tablegen(PolynomialAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=polynomial) mlir_tablegen(PolynomialAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=polynomial) add_public_tablegen_target(MLIRPolynomialAttributesIncGen) -add_dependencies(mlir-headers MLIRPolynomialAttributesIncGen) -add_mlir_doc(PolynomialAttributes PolynomialAttributes Dialects/ -gen-attrdef-doc) - -set(LLVM_TARGET_DEFINITIONS PolynomialOps.td) -mlir_tablegen(PolynomialOps.cpp.inc -gen-op-defs) -mlir_tablegen(PolynomialOps.h.inc -gen-op-decls) -add_public_tablegen_target(MLIRPolynomialOpsIncGen) -add_dependencies(mlir-headers MLIRPolynomialOpsIncGen) -add_mlir_doc(PolynoialOps PolynoialOps Dialects/ -gen-dialect-doc -dialect polynomial) - -set(LLVM_TARGET_DEFINITIONS PolynomialTypes.td) -mlir_tablegen(PolynomialTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=polynomial) -mlir_tablegen(PolynomialTypes.h.inc -gen-typedef-decls -typedefs-dialect=polynomial) -add_public_tablegen_target(MLIRPolynomialTypesIncGen) -add_dependencies(mlir-headers MLIRPolynomialTypesIncGen) -add_mlir_doc(PolynomialTypes PolynomialTypes Dialects/ -gen-typedef-doc) diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h index 6c79f553d9a7c..39b05b9d3ad14 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h @@ -1,4 +1,4 @@ -//===- Polynomial.h - A storage class for polynomial types ------*- C++ -*-===// +//===- Polynomial.h - A data class for polynomials --------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td new file mode 100644 index 0000000000000..5d8da8399b01b --- /dev/null +++ b/mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td @@ -0,0 +1,153 @@ +//===- PolynomialOps.td - Polynomial dialect ---------------*- tablegen -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef POLYNOMIAL_OPS +#define POLYNOMIAL_OPS + +include "mlir/IR/BuiltinAttributes.td" +include "mlir/IR/OpBase.td" +include "mlir/Interfaces/InferTypeOpInterface.td" +include "mlir/Interfaces/SideEffectInterfaces.td" + +def Polynomial_Dialect : Dialect { + let name = "polynomial"; + let cppNamespace = "::mlir::polynomial"; + let description = [{ + The Polynomial dialect defines single-variable polynomial types and + operations. + + The simplest use of `polynomial` is to represent mathematical operations in + a polynomial ring `R[x]`, where `R` is another MLIR type like `i32`. + + More generally, this dialect supports representing polynomial operations in a + quotient ring `R[X]/(f(x))` for some statically fixed polynomial `f(x)`. + Two polyomials `p(x), q(x)` are considered equal in this ring if they have the + same remainder when dividing by `f(x)`. When a modulus is given, ring operations + are performed with reductions modulo `f(x)` and relative to the coefficient ring + `R`. + + Examples: + + ```mlir + // A constant polynomial in a ring with i32 coefficients and no polynomial modulus + #ring = #polynomial.ring + %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.polynomial<1 + x**1024> + #ring = #polynomial.ring + %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.polynomial<1 + x**1024> + #ring = #polynomial.ring + %a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring> + ``` + }]; + + let useDefaultTypePrinterParser = 1; + let useDefaultAttributePrinterParser = 1; +} + +class Polynomial_Attr traits = []> + : AttrDef { + let mnemonic = attrMnemonic; +} + +def Polynomial_PolynomialAttr : Polynomial_Attr<"Polynomial", "polynomial"> { + let summary = "An attribute containing a single-variable polynomial."; + let description = [{ + #poly = #polynomial.poly + }]; + let parameters = (ins "Polynomial":$polynomial); + let hasCustomAssemblyFormat = 1; +} + +def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { + let summary = "An attribute specifying a polynomial ring."; + let description = [{ + A ring describes the domain in which polynomial arithmetic occurs. The ring + attribute in `polynomial` represents the more specific case of polynomials + with a single indeterminate; whose coefficients can be represented by + another MLIR type (`coefficientType`); and, if the coefficient type is + integral, whose coefficients are taken modulo some statically known modulus + (`coefficientModulus`). + + Additionally, a polynomial ring can specify an _ideal_, which converts + polynomial arithmetic to the analogue of modular integer arithmetic, where + each polynomial is represented as its remainder when dividing by the + modulus. For single-variable polynomials, an "ideal" is always specificed + via a single polynomial, which we call `polynomialModulus`. + + An expressive example is polynomials with i32 coefficients, whose + coefficients are taken modulo `2**32 - 5`, with a polynomial modulus of + `x**1024 - 1`. + + ```mlir + #poly_mod = #polynomial.polynomial<-1 + x**1024> + #ring = #polynomial.ring + + %0 = ... : polynomial.polynomial<#ring> + ``` + + In this case, the value of a polynomial is always "converted" to a + canonical form by applying repeated reductions by setting `x**1024 = 1` + and simplifying. + + The coefficient and polynomial modulus parameters are optional, and the + coefficient modulus is only allowed if the coefficient type is integral. + }]; + + let parameters = (ins + "Type": $coefficientType, + OptionalParameter<"IntegerAttr">: $coefficientModulus, + OptionalParameter<"PolynomialAttr">: $polynomialModulus + ); + + let hasCustomAssemblyFormat = 1; +} + +class Polynomial_Type + : TypeDef { + let mnemonic = typeMnemonic; +} + +def Polynomial_PolynomialType : Polynomial_Type<"Polynomial", "polynomial"> { + let summary = "An element of a polynomial ring."; + + let description = [{ + A type for polynomials in a polynomial quotient ring. + }]; + + let parameters = (ins Polynomial_RingAttr:$ring); + let assemblyFormat = "`<` $ring `>`"; +} + +class Polynomial_Op traits = []> : + Op; + +class Polynomial_UnaryOp traits = []> : + Polynomial_Op { + let arguments = (ins Polynomial_PolynomialType:$operand); + let results = (outs Polynomial_PolynomialType:$result); + + let assemblyFormat = "$operand attr-dict `:` qualified(type($result))"; +} + +class Polynomial_BinaryOp traits = []> : + Polynomial_Op { + let arguments = (ins Polynomial_PolynomialType:$lhs, Polynomial_PolynomialType:$rhs); + let results = (outs Polynomial_PolynomialType:$result); + + let assemblyFormat = "$lhs `,` $rhs attr-dict `:` qualified(type($result))"; +} + +#endif // POLYNOMIAL_OPS diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td deleted file mode 100644 index 726f2ea8d3b7b..0000000000000 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialAttributes.td +++ /dev/null @@ -1,75 +0,0 @@ -//===- PolynomialAttributes.td - polynomial dialect attrs --*- tablegen -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -#ifndef POLYNOMIAL_ATTRIBUTES -#define POLYNOMIAL_ATTRIBUTES - -include "PolynomialDialect.td" -include "mlir/IR/BuiltinAttributes.td" -include "mlir/IR/OpBase.td" - -class Polynomial_Attr traits = []> - : AttrDef { - let mnemonic = attrMnemonic; -} - -def Polynomial_PolynomialAttr : Polynomial_Attr<"Polynomial", "polynomial"> { - let summary = "An attribute containing a single-variable polynomial."; - let description = [{ - #poly = #polynomial.poly - }]; - let parameters = (ins "Polynomial":$polynomial); - let hasCustomAssemblyFormat = 1; -} - -def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { - let summary = "An attribute specifying a polynomial ring."; - let description = [{ - A ring describes the domain in which polynomial arithmetic occurs. The ring - attribute in `polynomial` represents the more specific case of polynomials - with a single indeterminate; whose coefficients can be represented by - another MLIR type (`coefficientType`); and, if the coefficient type is - integral, whose coefficients are taken modulo some statically known modulus - (`coefficientModulus`). - - Additionally, a polynomial ring can specify an _ideal_, which converts - polynomial arithmetic to the analogue of modular integer arithmetic, where - each polynomial is represented as its remainder when dividing by the - modulus. For single-variable polynomials, an "ideal" is always specificed - via a single polynomial, which we call `polynomialModulus`. - - An expressive example is polynomials with i32 coefficients, whose - coefficients are taken modulo `2**32 - 5`, with a polynomial modulus of - `x**1024 - 1`. - - ```mlir - #poly_mod = #polynomial.polynomial<-1 + x**1024> - #ring = #polynomial.ring - - %0 = ... : polynomial.polynomial<#ring> - ``` - - In this case, the value of a polynomial is always "converted" to a - canonical form by applying repeated reductions by setting `x**1024 = 1` - and simplifying. - - The coefficient and polynomial modulus parameters are optional, and the - coefficient modulus is only allowed if the coefficient type is integral. - }]; - - let parameters = (ins - "Type": $coefficientType, - OptionalParameter<"IntegerAttr">: $coefficientModulus, - OptionalParameter<"PolynomialAttr">: $polynomialModulus - ); - - let hasCustomAssemblyFormat = 1; -} - -#endif // POLYNOMIAL_ATTRIBUTES diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td deleted file mode 100644 index 95baafc281492..0000000000000 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialDialect.td +++ /dev/null @@ -1,54 +0,0 @@ -//===- PolynomialDialect.td - polynomial dialect -----------*- tablegen -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -#ifndef POLYNOMIAL_DIALECT -#define POLYNOMIAL_DIALECT - -include "mlir/IR/OpBase.td" - -def Polynomial_Dialect : Dialect { - let name = "polynomial"; - let cppNamespace = "::mlir::polynomial"; - let description = [{ - The Polynomial dialect defines single-variable polynomial types and - operations. - - The simplest use of `polynomial` is to represent mathematical operations in - a polynomial ring `R[x]`, where `R` is another MLIR type like `i32`. - - More generally, this dialect supports representing polynomial operations in a - quotient ring `R[X]/(f(x))` for some statically fixed polynomial `f(x)`. - Two polyomials `p(x), q(x)` are considered equal in this ring if they have the - same remainder when dividing by `f(x)`. When a modulus is given, ring operations - are performed with reductions modulo `f(x)` and relative to the coefficient ring - `R`. - - Examples: - - ```mlir - // A constant polynomial in a ring with i32 coefficients and no polynomial modulus - #ring = #polynomial.ring - %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.polynomial<1 + x**1024> - #ring = #polynomial.ring - %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.polynomial<1 + x**1024> - #ring = #polynomial.ring - %a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring> - ``` - }]; - - let useDefaultTypePrinterParser = 1; - let useDefaultAttributePrinterParser = 1; -} - -#endif // POLYNOMIAL_DIALECT diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h index a3ec062ac93fe..bacaad81ce8e5 100644 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h +++ b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h @@ -16,6 +16,6 @@ #include "mlir/Interfaces/InferTypeOpInterface.h" #define GET_OP_CLASSES -#include "mlir/Dialect/Polynomial/IR/PolynomialOps.h.inc" +#include "mlir/Dialect/Polynomial/IR/Polynomial.h.inc" #endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_ diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td deleted file mode 100644 index 06b97cdd059f8..0000000000000 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.td +++ /dev/null @@ -1,40 +0,0 @@ -//===- PolynomialOps.td - Polynomial op definitions --------*- tablegen -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#ifndef POLYNOMIAL_OPS -#define POLYNOMIAL_OPS - -include "PolynomialDialect.td" -include "PolynomialTypes.td" -include "mlir/Interfaces/InferTypeOpInterface.td" -include "mlir/Interfaces/SideEffectInterfaces.td" - -// Base class for polynomial dialect ops. Ops in this dialect have no side -// effects. -class Polynomial_Op traits = []> : - Op; - -// Base class for unary polynomial operations. -class Polynomial_UnaryOp traits = []> : - Polynomial_Op { - let arguments = (ins Polynomial_PolynomialType:$operand); - let results = (outs Polynomial_PolynomialType:$result); - - let assemblyFormat = "$operand attr-dict `:` qualified(type($result))"; -} - -// Base class for binary polynomial operations. -class Polynomial_BinaryOp traits = []> : - Polynomial_Op { - let arguments = (ins Polynomial_PolynomialType:$lhs, Polynomial_PolynomialType:$rhs); - let results = (outs Polynomial_PolynomialType:$result); - - let assemblyFormat = "$lhs `,` $rhs attr-dict `:` qualified(type($result))"; -} - -#endif // POLYNOMIAL_OPS diff --git a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td b/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td deleted file mode 100644 index 0a9be5f3f8846..0000000000000 --- a/mlir/include/mlir/Dialect/Polynomial/IR/PolynomialTypes.td +++ /dev/null @@ -1,32 +0,0 @@ -//===- PolynomialTypes.td - Types for polynomial dialect ---*- tablegen -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -#ifndef POLYNOMIAL_TYPES -#define POLYNOMIAL_TYPES - -include "PolynomialAttributes.td" -include "PolynomialDialect.td" -include "mlir/IR/BuiltinAttributes.td" -include "mlir/IR/OpBase.td" - -class Polynomial_Type - : TypeDef { - let mnemonic = typeMnemonic; -} - -def Polynomial_PolynomialType : Polynomial_Type<"Polynomial", "polynomial"> { - let summary = "An element of a polynomial ring."; - - let description = [{ - A type for polynomials in a polynomial quotient ring. - }]; - - let parameters = (ins Polynomial_RingAttr:$ring); - let assemblyFormat = "`<` $ring `>`"; -} - -#endif // POLYNOMIAL_TYPES diff --git a/mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt b/mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt index 695c37f7a368b..7f5b3255d5d90 100644 --- a/mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt +++ b/mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt @@ -8,9 +8,7 @@ add_mlir_dialect_library(MLIRPolynomialDialect ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Polynomial DEPENDS - MLIRPolynomialOpsIncGen - MLIRPolynomialTypesIncGen - MLIRPolynomialDialectIncGen + MLIRPolynomialIncGen MLIRPolynomialAttributesIncGen MLIRBuiltinAttributesIncGen diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp index f16aa2f55b71a..a672a59b8a465 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialDialect.cpp @@ -23,7 +23,7 @@ using namespace mlir::polynomial; #define GET_TYPEDEF_CLASSES #include "mlir/Dialect/Polynomial/IR/PolynomialTypes.cpp.inc" #define GET_OP_CLASSES -#include "mlir/Dialect/Polynomial/IR/PolynomialOps.cpp.inc" +#include "mlir/Dialect/Polynomial/IR/Polynomial.cpp.inc" void PolynomialDialect::initialize() { addAttributes< @@ -36,6 +36,6 @@ void PolynomialDialect::initialize() { >(); addOperations< #define GET_OP_LIST -#include "mlir/Dialect/Polynomial/IR/PolynomialOps.cpp.inc" +#include "mlir/Dialect/Polynomial/IR/Polynomial.cpp.inc" >(); } diff --git a/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp b/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp index a33a15ca9dfe5..96c59a28b8fdc 100644 --- a/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp +++ b/mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp @@ -12,4 +12,4 @@ using namespace mlir; using namespace mlir::polynomial; #define GET_OP_CLASSES -#include "mlir/Dialect/Polynomial/IR/PolynomialOps.cpp.inc" +#include "mlir/Dialect/Polynomial/IR/Polynomial.cpp.inc"