|
| 1 | +//===- PolynomialOps.td - Polynomial dialect ---------------*- tablegen -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef POLYNOMIAL_OPS |
| 10 | +#define POLYNOMIAL_OPS |
| 11 | + |
| 12 | +include "mlir/IR/BuiltinAttributes.td" |
| 13 | +include "mlir/IR/OpBase.td" |
| 14 | +include "mlir/Interfaces/InferTypeOpInterface.td" |
| 15 | +include "mlir/Interfaces/SideEffectInterfaces.td" |
| 16 | + |
| 17 | +def Polynomial_Dialect : Dialect { |
| 18 | + let name = "polynomial"; |
| 19 | + let cppNamespace = "::mlir::polynomial"; |
| 20 | + let description = [{ |
| 21 | + The Polynomial dialect defines single-variable polynomial types and |
| 22 | + operations. |
| 23 | + |
| 24 | + The simplest use of `polynomial` is to represent mathematical operations in |
| 25 | + a polynomial ring `R[x]`, where `R` is another MLIR type like `i32`. |
| 26 | + |
| 27 | + More generally, this dialect supports representing polynomial operations in a |
| 28 | + quotient ring `R[X]/(f(x))` for some statically fixed polynomial `f(x)`. |
| 29 | + Two polyomials `p(x), q(x)` are considered equal in this ring if they have the |
| 30 | + same remainder when dividing by `f(x)`. When a modulus is given, ring operations |
| 31 | + are performed with reductions modulo `f(x)` and relative to the coefficient ring |
| 32 | + `R`. |
| 33 | + |
| 34 | + Examples: |
| 35 | + |
| 36 | + ```mlir |
| 37 | + // A constant polynomial in a ring with i32 coefficients and no polynomial modulus |
| 38 | + #ring = #polynomial.ring<ctype=i32> |
| 39 | + %a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring> |
| 40 | + |
| 41 | + // A constant polynomial in a ring with i32 coefficients, modulo (x^1024 + 1) |
| 42 | + #modulus = #polynomial.polynomial<1 + x**1024> |
| 43 | + #ring = #polynomial.ring<ctype=i32, ideal=#modulus> |
| 44 | + %a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring> |
| 45 | + |
| 46 | + // A constant polynomial in a ring with i32 coefficients, with a polynomial |
| 47 | + // modulus of (x^1024 + 1) and a coefficient modulus of 17. |
| 48 | + #modulus = #polynomial.polynomial<1 + x**1024> |
| 49 | + #ring = #polynomial.ring<ctype=i32, cmod=17, ideal=#modulus> |
| 50 | + %a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring> |
| 51 | + ``` |
| 52 | + }]; |
| 53 | + |
| 54 | + let useDefaultTypePrinterParser = 1; |
| 55 | + let useDefaultAttributePrinterParser = 1; |
| 56 | +} |
| 57 | + |
| 58 | +class Polynomial_Attr<string name, string attrMnemonic, list<Trait> traits = []> |
| 59 | + : AttrDef<Polynomial_Dialect, name, traits> { |
| 60 | + let mnemonic = attrMnemonic; |
| 61 | +} |
| 62 | + |
| 63 | +def Polynomial_PolynomialAttr : Polynomial_Attr<"Polynomial", "polynomial"> { |
| 64 | + let summary = "An attribute containing a single-variable polynomial."; |
| 65 | + let description = [{ |
| 66 | + #poly = #polynomial.poly<x**1024 + 1> |
| 67 | + }]; |
| 68 | + let parameters = (ins "Polynomial":$polynomial); |
| 69 | + let hasCustomAssemblyFormat = 1; |
| 70 | +} |
| 71 | + |
| 72 | +def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> { |
| 73 | + let summary = "An attribute specifying a polynomial ring."; |
| 74 | + let description = [{ |
| 75 | + A ring describes the domain in which polynomial arithmetic occurs. The ring |
| 76 | + attribute in `polynomial` represents the more specific case of polynomials |
| 77 | + with a single indeterminate; whose coefficients can be represented by |
| 78 | + another MLIR type (`coefficientType`); and, if the coefficient type is |
| 79 | + integral, whose coefficients are taken modulo some statically known modulus |
| 80 | + (`coefficientModulus`). |
| 81 | + |
| 82 | + Additionally, a polynomial ring can specify an _ideal_, which converts |
| 83 | + polynomial arithmetic to the analogue of modular integer arithmetic, where |
| 84 | + each polynomial is represented as its remainder when dividing by the |
| 85 | + modulus. For single-variable polynomials, an "ideal" is always specificed |
| 86 | + via a single polynomial, which we call `polynomialModulus`. |
| 87 | + |
| 88 | + An expressive example is polynomials with i32 coefficients, whose |
| 89 | + coefficients are taken modulo `2**32 - 5`, with a polynomial modulus of |
| 90 | + `x**1024 - 1`. |
| 91 | + |
| 92 | + ```mlir |
| 93 | + #poly_mod = #polynomial.polynomial<-1 + x**1024> |
| 94 | + #ring = #polynomial.ring<coefficientType=i32, |
| 95 | + coefficientModulus=4294967291, |
| 96 | + polynomialModulus=#poly_mod> |
| 97 | + |
| 98 | + %0 = ... : polynomial.polynomial<#ring> |
| 99 | + ``` |
| 100 | + |
| 101 | + In this case, the value of a polynomial is always "converted" to a |
| 102 | + canonical form by applying repeated reductions by setting `x**1024 = 1` |
| 103 | + and simplifying. |
| 104 | + |
| 105 | + The coefficient and polynomial modulus parameters are optional, and the |
| 106 | + coefficient modulus is only allowed if the coefficient type is integral. |
| 107 | + }]; |
| 108 | + |
| 109 | + let parameters = (ins |
| 110 | + "Type": $coefficientType, |
| 111 | + OptionalParameter<"IntegerAttr">: $coefficientModulus, |
| 112 | + OptionalParameter<"PolynomialAttr">: $polynomialModulus |
| 113 | + ); |
| 114 | + |
| 115 | + let hasCustomAssemblyFormat = 1; |
| 116 | +} |
| 117 | + |
| 118 | +class Polynomial_Type<string name, string typeMnemonic> |
| 119 | + : TypeDef<Polynomial_Dialect, name> { |
| 120 | + let mnemonic = typeMnemonic; |
| 121 | +} |
| 122 | + |
| 123 | +def Polynomial_PolynomialType : Polynomial_Type<"Polynomial", "polynomial"> { |
| 124 | + let summary = "An element of a polynomial ring."; |
| 125 | + |
| 126 | + let description = [{ |
| 127 | + A type for polynomials in a polynomial quotient ring. |
| 128 | + }]; |
| 129 | + |
| 130 | + let parameters = (ins Polynomial_RingAttr:$ring); |
| 131 | + let assemblyFormat = "`<` $ring `>`"; |
| 132 | +} |
| 133 | + |
| 134 | +class Polynomial_Op<string mnemonic, list<Trait> traits = []> : |
| 135 | + Op<Polynomial_Dialect, mnemonic, traits # [Pure]>; |
| 136 | + |
| 137 | +class Polynomial_UnaryOp<string mnemonic, list<Trait> traits = []> : |
| 138 | + Polynomial_Op<mnemonic, traits # [SameOperandsAndResultType]> { |
| 139 | + let arguments = (ins Polynomial_PolynomialType:$operand); |
| 140 | + let results = (outs Polynomial_PolynomialType:$result); |
| 141 | + |
| 142 | + let assemblyFormat = "$operand attr-dict `:` qualified(type($result))"; |
| 143 | +} |
| 144 | + |
| 145 | +class Polynomial_BinaryOp<string mnemonic, list<Trait> traits = []> : |
| 146 | + Polynomial_Op<mnemonic, traits # [SameOperandsAndResultType]> { |
| 147 | + let arguments = (ins Polynomial_PolynomialType:$lhs, Polynomial_PolynomialType:$rhs); |
| 148 | + let results = (outs Polynomial_PolynomialType:$result); |
| 149 | + |
| 150 | + let assemblyFormat = "$lhs `,` $rhs attr-dict `:` qualified(type($result))"; |
| 151 | +} |
| 152 | + |
| 153 | +#endif // POLYNOMIAL_OPS |
0 commit comments