Skip to content

Commit b80e0fb

Browse files
authored
[mlir][polynomial] split attributes into its own tablegen (#92613)
Out of tree we have other dialects that use the ring attribute, but we get compilation errors when generating ops while pulling in all the Polynomial tablegen ops (there's no `-dialect` flag in `mlir-tblgen` for op generation like there is for attributes and types). This PR simply moves the attributes into its own file, so it can be included separately, and this also requires moving the dialect declaration into its own file.
1 parent ab7930b commit b80e0fb

File tree

5 files changed

+184
-153
lines changed

5 files changed

+184
-153
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
add_mlir_dialect(Polynomial polynomial)
22
add_mlir_doc(Polynomial PolynomialDialect Dialects/ -gen-dialect-doc -dialect=polynomial)
33

4-
set(LLVM_TARGET_DEFINITIONS Polynomial.td)
4+
set(LLVM_TARGET_DEFINITIONS PolynomialAttributes.td)
55
mlir_tablegen(PolynomialAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=polynomial)
66
mlir_tablegen(PolynomialAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=polynomial)
77
add_public_tablegen_target(MLIRPolynomialAttributesIncGen)

mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td

Lines changed: 2 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -13,157 +13,8 @@ include "mlir/IR/BuiltinAttributes.td"
1313
include "mlir/IR/OpBase.td"
1414
include "mlir/Interfaces/InferTypeOpInterface.td"
1515
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<coefficientType=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.int_polynomial<1 + x**1024>
43-
#ring = #polynomial.ring<coefficientType=i32, polynomialModulus=#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.int_polynomial<1 + x**1024>
49-
#ring = #polynomial.ring<coefficientType=i32, coefficientModulus=17:i32, polynomialModulus=#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_IntPolynomialAttr : Polynomial_Attr<"IntPolynomial", "int_polynomial"> {
64-
let summary = "An attribute containing a single-variable polynomial with integer coefficients.";
65-
let description = [{
66-
A polynomial attribute represents a single-variable polynomial with integer
67-
coefficients, which is used to define the modulus of a `RingAttr`, as well
68-
as to define constants and perform constant folding for `polynomial` ops.
69-
70-
The polynomial must be expressed as a list of monomial terms, with addition
71-
or subtraction between them. The choice of variable name is arbitrary, but
72-
must be consistent across all the monomials used to define a single
73-
attribute. The order of monomial terms is arbitrary, each monomial degree
74-
must occur at most once.
75-
76-
Example:
77-
78-
```mlir
79-
#poly = #polynomial.int_polynomial<x**1024 + 1>
80-
```
81-
}];
82-
let parameters = (ins "::mlir::polynomial::IntPolynomial":$polynomial);
83-
let hasCustomAssemblyFormat = 1;
84-
}
85-
86-
def Polynomial_FloatPolynomialAttr : Polynomial_Attr<"FloatPolynomial", "float_polynomial"> {
87-
let summary = "An attribute containing a single-variable polynomial with double precision floating point coefficients.";
88-
let description = [{
89-
A polynomial attribute represents a single-variable polynomial with double
90-
precision floating point coefficients.
91-
92-
The polynomial must be expressed as a list of monomial terms, with addition
93-
or subtraction between them. The choice of variable name is arbitrary, but
94-
must be consistent across all the monomials used to define a single
95-
attribute. The order of monomial terms is arbitrary, each monomial degree
96-
must occur at most once.
97-
98-
Example:
99-
100-
```mlir
101-
#poly = #polynomial.float_polynomial<0.5 x**7 + 1.5>
102-
```
103-
}];
104-
let parameters = (ins "FloatPolynomial":$polynomial);
105-
let hasCustomAssemblyFormat = 1;
106-
}
107-
108-
def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> {
109-
let summary = "An attribute specifying a polynomial ring.";
110-
let description = [{
111-
A ring describes the domain in which polynomial arithmetic occurs. The ring
112-
attribute in `polynomial` represents the more specific case of polynomials
113-
with a single indeterminate; whose coefficients can be represented by
114-
another MLIR type (`coefficientType`); and, if the coefficient type is
115-
integral, whose coefficients are taken modulo some statically known modulus
116-
(`coefficientModulus`).
117-
118-
Additionally, a polynomial ring can specify a _polynomialModulus_, which converts
119-
polynomial arithmetic to the analogue of modular integer arithmetic, where
120-
each polynomial is represented as its remainder when dividing by the
121-
modulus. For single-variable polynomials, an "polynomialModulus" is always specificed
122-
via a single polynomial, which we call `polynomialModulus`.
123-
124-
An expressive example is polynomials with i32 coefficients, whose
125-
coefficients are taken modulo `2**32 - 5`, with a polynomial modulus of
126-
`x**1024 - 1`.
127-
128-
```mlir
129-
#poly_mod = #polynomial.int_polynomial<-1 + x**1024>
130-
#ring = #polynomial.ring<coefficientType=i32,
131-
coefficientModulus=4294967291:i32,
132-
polynomialModulus=#poly_mod>
133-
134-
%0 = ... : polynomial.polynomial<#ring>
135-
```
136-
137-
In this case, the value of a polynomial is always "converted" to a
138-
canonical form by applying repeated reductions by setting `x**1024 = 1`
139-
and simplifying.
140-
141-
The coefficient and polynomial modulus parameters are optional, and the
142-
coefficient modulus is only allowed if the coefficient type is integral.
143-
}];
144-
145-
let parameters = (ins
146-
"Type": $coefficientType,
147-
OptionalParameter<"::mlir::IntegerAttr">: $coefficientModulus,
148-
OptionalParameter<"::mlir::polynomial::IntPolynomialAttr">: $polynomialModulus,
149-
OptionalParameter<"::mlir::IntegerAttr">: $primitiveRoot
150-
);
151-
let assemblyFormat = "`<` struct(params) `>`";
152-
let builders = [
153-
AttrBuilderWithInferredContext<
154-
(ins "::mlir::Type":$coefficientTy,
155-
CArg<"::mlir::IntegerAttr", "nullptr"> :$coefficientModulusAttr,
156-
CArg<"::mlir::polynomial::IntPolynomialAttr", "nullptr"> :$polynomialModulusAttr,
157-
CArg<"::mlir::IntegerAttr", "nullptr"> :$primitiveRootAttr), [{
158-
return $_get(
159-
coefficientTy.getContext(),
160-
coefficientTy,
161-
coefficientModulusAttr,
162-
polynomialModulusAttr,
163-
primitiveRootAttr);
164-
}]>,
165-
];
166-
}
16+
include "mlir/Dialect/Polynomial/IR/PolynomialDialect.td"
17+
include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.td"
16718

16819
class Polynomial_Type<string name, string typeMnemonic>
16920
: TypeDef<Polynomial_Dialect, name> {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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_ATTRIBUTES
10+
#define POLYNOMIAL_ATTRIBUTES
11+
12+
include "mlir/IR/BuiltinAttributes.td"
13+
include "mlir/Dialect/Polynomial/IR/PolynomialDialect.td"
14+
15+
class Polynomial_Attr<string name, string attrMnemonic, list<Trait> traits = []>
16+
: AttrDef<Polynomial_Dialect, name, traits> {
17+
let mnemonic = attrMnemonic;
18+
}
19+
20+
def Polynomial_IntPolynomialAttr : Polynomial_Attr<"IntPolynomial", "int_polynomial"> {
21+
let summary = "An attribute containing a single-variable polynomial with integer coefficients.";
22+
let description = [{
23+
A polynomial attribute represents a single-variable polynomial with integer
24+
coefficients, which is used to define the modulus of a `RingAttr`, as well
25+
as to define constants and perform constant folding for `polynomial` ops.
26+
27+
The polynomial must be expressed as a list of monomial terms, with addition
28+
or subtraction between them. The choice of variable name is arbitrary, but
29+
must be consistent across all the monomials used to define a single
30+
attribute. The order of monomial terms is arbitrary, each monomial degree
31+
must occur at most once.
32+
33+
Example:
34+
35+
```mlir
36+
#poly = #polynomial.int_polynomial<x**1024 + 1>
37+
```
38+
}];
39+
let parameters = (ins "::mlir::polynomial::IntPolynomial":$polynomial);
40+
let hasCustomAssemblyFormat = 1;
41+
}
42+
43+
def Polynomial_FloatPolynomialAttr : Polynomial_Attr<"FloatPolynomial", "float_polynomial"> {
44+
let summary = "An attribute containing a single-variable polynomial with double precision floating point coefficients.";
45+
let description = [{
46+
A polynomial attribute represents a single-variable polynomial with double
47+
precision floating point coefficients.
48+
49+
The polynomial must be expressed as a list of monomial terms, with addition
50+
or subtraction between them. The choice of variable name is arbitrary, but
51+
must be consistent across all the monomials used to define a single
52+
attribute. The order of monomial terms is arbitrary, each monomial degree
53+
must occur at most once.
54+
55+
Example:
56+
57+
```mlir
58+
#poly = #polynomial.float_polynomial<0.5 x**7 + 1.5>
59+
```
60+
}];
61+
let parameters = (ins "FloatPolynomial":$polynomial);
62+
let hasCustomAssemblyFormat = 1;
63+
}
64+
65+
def Polynomial_RingAttr : Polynomial_Attr<"Ring", "ring"> {
66+
let summary = "An attribute specifying a polynomial ring.";
67+
let description = [{
68+
A ring describes the domain in which polynomial arithmetic occurs. The ring
69+
attribute in `polynomial` represents the more specific case of polynomials
70+
with a single indeterminate; whose coefficients can be represented by
71+
another MLIR type (`coefficientType`); and, if the coefficient type is
72+
integral, whose coefficients are taken modulo some statically known modulus
73+
(`coefficientModulus`).
74+
75+
Additionally, a polynomial ring can specify a _polynomialModulus_, which converts
76+
polynomial arithmetic to the analogue of modular integer arithmetic, where
77+
each polynomial is represented as its remainder when dividing by the
78+
modulus. For single-variable polynomials, an "polynomialModulus" is always specificed
79+
via a single polynomial, which we call `polynomialModulus`.
80+
81+
An expressive example is polynomials with i32 coefficients, whose
82+
coefficients are taken modulo `2**32 - 5`, with a polynomial modulus of
83+
`x**1024 - 1`.
84+
85+
```mlir
86+
#poly_mod = #polynomial.int_polynomial<-1 + x**1024>
87+
#ring = #polynomial.ring<coefficientType=i32,
88+
coefficientModulus=4294967291:i32,
89+
polynomialModulus=#poly_mod>
90+
91+
%0 = ... : polynomial.polynomial<#ring>
92+
```
93+
94+
In this case, the value of a polynomial is always "converted" to a
95+
canonical form by applying repeated reductions by setting `x**1024 = 1`
96+
and simplifying.
97+
98+
The coefficient and polynomial modulus parameters are optional, and the
99+
coefficient modulus is only allowed if the coefficient type is integral.
100+
}];
101+
102+
let parameters = (ins
103+
"Type": $coefficientType,
104+
OptionalParameter<"::mlir::IntegerAttr">: $coefficientModulus,
105+
OptionalParameter<"::mlir::polynomial::IntPolynomialAttr">: $polynomialModulus,
106+
OptionalParameter<"::mlir::IntegerAttr">: $primitiveRoot
107+
);
108+
let assemblyFormat = "`<` struct(params) `>`";
109+
let builders = [
110+
AttrBuilderWithInferredContext<
111+
(ins "::mlir::Type":$coefficientTy,
112+
CArg<"::mlir::IntegerAttr", "nullptr"> :$coefficientModulusAttr,
113+
CArg<"::mlir::polynomial::IntPolynomialAttr", "nullptr"> :$polynomialModulusAttr,
114+
CArg<"::mlir::IntegerAttr", "nullptr"> :$primitiveRootAttr), [{
115+
return $_get(
116+
coefficientTy.getContext(),
117+
coefficientTy,
118+
coefficientModulusAttr,
119+
polynomialModulusAttr,
120+
primitiveRootAttr);
121+
}]>,
122+
];
123+
}
124+
125+
#endif // POLYNOMIAL_ATTRIBUTES
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===- PolynomialDialect.td - Polynomial dialect base ------*- 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_DIALECT
10+
#define POLYNOMIAL_DIALECT
11+
12+
include "mlir/IR/OpBase.td"
13+
14+
def Polynomial_Dialect : Dialect {
15+
let name = "polynomial";
16+
let cppNamespace = "::mlir::polynomial";
17+
let description = [{
18+
The Polynomial dialect defines single-variable polynomial types and
19+
operations.
20+
21+
The simplest use of `polynomial` is to represent mathematical operations in
22+
a polynomial ring `R[x]`, where `R` is another MLIR type like `i32`.
23+
24+
More generally, this dialect supports representing polynomial operations in a
25+
quotient ring `R[X]/(f(x))` for some statically fixed polynomial `f(x)`.
26+
Two polyomials `p(x), q(x)` are considered equal in this ring if they have the
27+
same remainder when dividing by `f(x)`. When a modulus is given, ring operations
28+
are performed with reductions modulo `f(x)` and relative to the coefficient ring
29+
`R`.
30+
31+
Examples:
32+
33+
```mlir
34+
// A constant polynomial in a ring with i32 coefficients and no polynomial modulus
35+
#ring = #polynomial.ring<coefficientType=i32>
36+
%a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring>
37+
38+
// A constant polynomial in a ring with i32 coefficients, modulo (x^1024 + 1)
39+
#modulus = #polynomial.int_polynomial<1 + x**1024>
40+
#ring = #polynomial.ring<coefficientType=i32, polynomialModulus=#modulus>
41+
%a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring>
42+
43+
// A constant polynomial in a ring with i32 coefficients, with a polynomial
44+
// modulus of (x^1024 + 1) and a coefficient modulus of 17.
45+
#modulus = #polynomial.int_polynomial<1 + x**1024>
46+
#ring = #polynomial.ring<coefficientType=i32, coefficientModulus=17:i32, polynomialModulus=#modulus>
47+
%a = polynomial.constant <1 + x**2 - 3x**3> : polynomial.polynomial<#ring>
48+
```
49+
}];
50+
51+
let useDefaultTypePrinterParser = 1;
52+
let useDefaultAttributePrinterParser = 1;
53+
}
54+
55+
#endif // POLYNOMIAL_OPS

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6737,7 +6737,7 @@ cc_library(
67376737

67386738
td_library(
67396739
name = "PolynomialTdFiles",
6740-
srcs = ["include/mlir/Dialect/Polynomial/IR/Polynomial.td"],
6740+
srcs = glob(["include/mlir/Dialect/Polynomial/IR/*.td"]),
67416741
includes = ["include"],
67426742
deps = [
67436743
":BuiltinDialectTdFiles",

0 commit comments

Comments
 (0)