13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSet.h"
18 namespace polynomial {
30 ParseResult parseMonomial(AsmParser &parser, Monomial &monomial,
31 llvm::StringRef &variable, bool &isConstantTerm,
32 bool &shouldParseMore) {
33 APInt parsedCoeff(apintBitWidth, 1);
34 auto parsedCoeffResult = parser.parseOptionalInteger(parsedCoeff);
35 monomial.coefficient = parsedCoeff;
37 isConstantTerm = false;
38 shouldParseMore = false;
40 // A + indicates it's a constant term with more to go, as in `1 + x`.
41 if (
succeeded(parser.parseOptionalPlus())) {
44 if (!parsedCoeffResult.has_value()) {
48 isConstantTerm =
true;
49 shouldParseMore =
true;
54 if (
failed(parser.parseOptionalKeyword(&variable))) {
57 if (!parsedCoeffResult.has_value()) {
62 isConstantTerm =
true;
69 if (
succeeded(parser.parseOptionalStar())) {
71 if (
failed(parser.parseStar())) {
77 if (
failed(parser.parseInteger(parsedExponent))) {
78 parser.emitError(parser.getCurrentLocation(),
79 "found invalid integer exponent");
83 monomial.exponent = parsedExponent;
88 if (
succeeded(parser.parseOptionalPlus())) {
89 shouldParseMore =
true;
95 if (
failed(parser.parseLess()))
102 Monomial parsedMonomial;
103 llvm::StringRef parsedVariableRef;
105 bool shouldParseMore;
107 isConstantTerm, shouldParseMore))) {
108 parser.emitError(parser.getCurrentLocation(),
"expected a monomial");
112 if (!isConstantTerm) {
113 std::string parsedVariable = parsedVariableRef.str();
114 variables.insert(parsedVariable);
116 monomials.push_back(parsedMonomial);
121 if (
succeeded(parser.parseOptionalGreater())) {
125 parser.getCurrentLocation(),
126 "expected + and more monomials, or > to end polynomial attribute");
130 if (variables.size() > 1) {
131 std::string vars = llvm::join(variables.keys(),
", ");
133 parser.getCurrentLocation(),
134 "polynomials must have one indeterminate, but there were multiple: " +
140 parser.emitError(parser.getCurrentLocation())
141 <<
"parsed polynomial must have unique exponents among monomials";
148 p <<
"#polynomial.ring<coefficientType=" << getCoefficientType()
149 <<
", coefficientModulus=" << getCoefficientModulus()
150 <<
", polynomialModulus=" << getPolynomialModulus() <<
'>';
154 if (
failed(parser.parseLess()))
157 if (
failed(parser.parseKeyword(
"coefficientType")))
160 if (
failed(parser.parseEqual()))
164 if (
failed(parser.parseType(ty)))
167 if (
failed(parser.parseComma()))
170 IntegerAttr coefficientModulusAttr =
nullptr;
171 if (
succeeded(parser.parseKeyword(
"coefficientModulus"))) {
172 if (
failed(parser.parseEqual()))
175 IntegerType iType = mlir::dyn_cast<IntegerType>(ty);
177 parser.emitError(parser.getCurrentLocation(),
178 "coefficientType must specify an integer type");
181 APInt coefficientModulus(iType.getWidth(), 0);
182 auto result = parser.parseInteger(coefficientModulus);
184 parser.emitError(parser.getCurrentLocation(),
185 "invalid coefficient modulus");
190 if (
failed(parser.parseComma()))
194 PolynomialAttr polyAttr =
nullptr;
195 if (
succeeded(parser.parseKeyword(
"polynomialModulus"))) {
196 if (
failed(parser.parseEqual()))
200 if (
failed(parser.parseAttribute<PolynomialAttr>(attr)))
205 Polynomial poly = polyAttr.getPolynomial();
206 APInt root(coefficientModulusAttr.getValue().getBitWidth(), 0);
207 IntegerAttr rootAttr =
nullptr;
208 if (
succeeded(parser.parseOptionalComma())) {
209 if (
failed(parser.parseKeyword(
"primitiveRoot")) ||
210 failed(parser.parseEqual()))
213 ParseResult result = parser.parseInteger(root);
215 parser.emitError(parser.getCurrentLocation(),
"invalid primitiveRoot");
221 if (
failed(parser.parseGreater()))
224 return RingAttr::get(parser.getContext(), ty, coefficientModulusAttr,
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
static FailureOr< Polynomial > fromMonomials(ArrayRef< Monomial > monomials)
@ Type
An inlay hint that for a type annotation.
ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, llvm::StringRef &variable, bool &isConstantTerm, bool &shouldParseMore)
Try to parse a monomial.
constexpr unsigned apintBitWidth
This restricts statically defined polynomials to have at most 64-bit coefficients.
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.