12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSet.h"
18 namespace polynomial {
21 p <<
'<' << getPolynomial() <<
'>';
25 p <<
'<' << getPolynomial() <<
'>';
31 template <
typename MonomialType>
39 template <
typename Monomial>
42 bool &isConstantTerm,
bool &shouldParseMore,
46 isConstantTerm =
false;
47 shouldParseMore =
false;
57 isConstantTerm =
true;
58 shouldParseMore =
true;
71 isConstantTerm =
true;
88 "found invalid integer exponent");
92 monomial.setExponent(parsedExponent);
98 shouldParseMore =
true;
103 template <
typename Monomial>
109 Monomial parsedMonomial;
110 llvm::StringRef parsedVariableRef;
112 bool shouldParseMore;
113 if (failed(parseMonomial<Monomial>(
114 parser, parsedMonomial, parsedVariableRef, isConstantTerm,
115 shouldParseMore, parseAndStoreCoefficient))) {
120 if (!isConstantTerm) {
121 std::string parsedVariable = parsedVariableRef.str();
122 variables.insert(parsedVariable);
124 monomials.push_back(parsedMonomial);
134 "expected + and more monomials, or > to end polynomial attribute");
138 if (variables.size() > 1) {
139 std::string vars = llvm::join(variables.keys(),
", ");
142 "polynomials must have one indeterminate, but there were multiple: " +
157 if (failed(parsePolynomialAttr<IntMonomial>(
158 parser, monomials, variables,
159 [&](IntMonomial &monomial) -> OptionalParseResult {
161 OptionalParseResult result =
163 monomial.setCoefficient(parsedCoeff);
170 if (failed(result)) {
172 <<
"parsed polynomial must have unique exponents among monomials";
178 if (failed(parser.parseLess()))
184 ParseCoefficientFn<FloatMonomial> parseAndStoreCoefficient =
185 [&](FloatMonomial &monomial) -> OptionalParseResult {
186 double coeffValue = 1.0;
187 ParseResult result = parser.parseFloat(coeffValue);
188 monomial.setCoefficient(APFloat(coeffValue));
189 return OptionalParseResult(result);
192 if (failed(parsePolynomialAttr<FloatMonomial>(parser, monomials, variables,
193 parseAndStoreCoefficient))) {
198 if (failed(result)) {
199 parser.emitError(parser.getCurrentLocation())
200 <<
"parsed polynomial must have unique exponents among monomials";
208 Type coefficientType, IntegerAttr coefficientModulus,
209 IntPolynomialAttr polynomialModulus) {
210 if (coefficientModulus) {
211 auto coeffIntType = llvm::dyn_cast<IntegerType>(coefficientType);
213 return emitError() <<
"coefficientModulus specified but coefficientType "
216 APInt coeffModValue = coefficientModulus.getValue();
217 if (coeffModValue == 0) {
218 return emitError() <<
"coefficientModulus should not be 0";
220 if (coeffModValue.slt(0)) {
221 return emitError() <<
"coefficientModulus should be positive";
223 auto coeffModWidth = (coeffModValue - 1).getActiveBits();
224 auto coeffWidth = coeffIntType.getWidth();
225 if (coeffModWidth > coeffWidth) {
226 return emitError() <<
"coefficientModulus needs bit width of "
228 <<
" but coefficientType can only contain "
229 << coeffWidth <<
" bits";
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual OptionalParseResult parseOptionalInteger(APInt &result)=0
Parse an optional integer value from the stream.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseOptionalPlus()=0
Parse a '+' token if present.
virtual ParseResult parseOptionalGreater()=0
Parse a '>' token if present.
virtual ParseResult parseStar()=0
Parse a '*' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseOptionalStar()=0
Parse a '*' token if present.
Attributes are known-constant values of operations.
This class represents a diagnostic that is inflight and set to be reported.
This class implements Optional functionality for ParseResult.
bool has_value() const
Returns true if we contain a valid ParseResult value.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
static FailureOr< FloatPolynomial > fromMonomials(ArrayRef< FloatMonomial > monomials)
static FailureOr< IntPolynomial > fromMonomials(ArrayRef< IntMonomial > monomials)
LogicalResult parsePolynomialAttr(AsmParser &parser, llvm::SmallVector< Monomial > &monomials, llvm::StringSet<> &variables, ParseCoefficientFn< Monomial > parseAndStoreCoefficient)
ParseResult parseMonomial(AsmParser &parser, Monomial &monomial, llvm::StringRef &variable, bool &isConstantTerm, bool &shouldParseMore, ParseCoefficientFn< Monomial > parseAndStoreCoefficient)
Try to parse a monomial.
std::function< OptionalParseResult(MonomialType &)> ParseCoefficientFn
A callable that parses the coefficient using the appropriate method for the given monomial type,...
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.
llvm::function_ref< Fn > function_ref
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...