Skip to content

Commit e7bb769

Browse files
committed
use the add_mlir_dialect macro
1 parent aebf364 commit e7bb769

File tree

11 files changed

+165
-231
lines changed

11 files changed

+165
-231
lines changed
Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
1-
set(LLVM_TARGET_DEFINITIONS PolynomialDialect.td)
2-
mlir_tablegen(PolynomialDialect.cpp.inc -gen-dialect-defs -dialect=polynomial)
3-
mlir_tablegen(PolynomialDialect.h.inc -gen-dialect-decls -dialect=polynomial)
4-
add_public_tablegen_target(MLIRPolynomialDialectIncGen)
5-
add_dependencies(mlir-headers MLIRPolynomialDialectIncGen)
1+
add_mlir_dialect(Polynomial polynomial)
2+
add_mlir_doc(PolynomialDialect PolynomialDialect Polynomial/ -gen-dialect-doc)
3+
add_mlir_doc(PolynomialOps PolynomialOps Polynomial/ -gen-op-doc)
4+
add_mlir_doc(PolynomialAttributes PolynomialAttributes Dialects/ -gen-attrdef-doc)
5+
add_mlir_doc(PolynomialTypes PolynomialTypes Dialects/ -gen-typedef-doc)
66

7-
set(LLVM_TARGET_DEFINITIONS PolynomialAttributes.td)
7+
set(LLVM_TARGET_DEFINITIONS Polynomial.td)
88
mlir_tablegen(PolynomialAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=polynomial)
99
mlir_tablegen(PolynomialAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=polynomial)
1010
add_public_tablegen_target(MLIRPolynomialAttributesIncGen)
11-
add_dependencies(mlir-headers MLIRPolynomialAttributesIncGen)
12-
add_mlir_doc(PolynomialAttributes PolynomialAttributes Dialects/ -gen-attrdef-doc)
13-
14-
set(LLVM_TARGET_DEFINITIONS PolynomialOps.td)
15-
mlir_tablegen(PolynomialOps.cpp.inc -gen-op-defs)
16-
mlir_tablegen(PolynomialOps.h.inc -gen-op-decls)
17-
add_public_tablegen_target(MLIRPolynomialOpsIncGen)
18-
add_dependencies(mlir-headers MLIRPolynomialOpsIncGen)
19-
add_mlir_doc(PolynoialOps PolynoialOps Dialects/ -gen-dialect-doc -dialect polynomial)
20-
21-
set(LLVM_TARGET_DEFINITIONS PolynomialTypes.td)
22-
mlir_tablegen(PolynomialTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=polynomial)
23-
mlir_tablegen(PolynomialTypes.h.inc -gen-typedef-decls -typedefs-dialect=polynomial)
24-
add_public_tablegen_target(MLIRPolynomialTypesIncGen)
25-
add_dependencies(mlir-headers MLIRPolynomialTypesIncGen)
26-
add_mlir_doc(PolynomialTypes PolynomialTypes Dialects/ -gen-typedef-doc)

mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- Polynomial.h - A storage class for polynomial types ------*- C++ -*-===//
1+
//===- Polynomial.h - A data class for polynomials --------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

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

Lines changed: 0 additions & 75 deletions
This file was deleted.

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

Lines changed: 0 additions & 54 deletions
This file was deleted.

mlir/include/mlir/Dialect/Polynomial/IR/PolynomialOps.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
#include "mlir/Interfaces/InferTypeOpInterface.h"
1717

1818
#define GET_OP_CLASSES
19-
#include "mlir/Dialect/Polynomial/IR/PolynomialOps.h.inc"
19+
#include "mlir/Dialect/Polynomial/IR/Polynomial.h.inc"
2020

2121
#endif // MLIR_INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIALOPS_H_

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

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)