Skip to content

Commit 9b3a080

Browse files
committed
[mlir][affinexpr] add parseAffineExpr to parser API
Similar to AffineMap and IntegerSet parsing, this change makes the more fine-grained AffineExpr available for general parsing, using a preset symbol set to recognize variables. Motivation: The AffineExpr parser will be used by the new sparse tensor encoding surface syntax. Originally, we planned to duplicate the affine parser completely, but that would be a terrible waste of a good thing. With this minor API change, we prepare the way for the sparse tensor dialect (and others) to reuse the AffineExpr parser outside the context of a more restricted AffineMap parser. Reviewed By: Peiming Differential Revision: https://reviews.llvm.org/D154177
1 parent 0a7ff09 commit 9b3a080

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,12 @@ class AsmParser {
10321032
/// Parse an affine map instance into 'map'.
10331033
virtual ParseResult parseAffineMap(AffineMap &map) = 0;
10341034

1035+
/// Parse an affine expr instance into 'expr' using the already computed
1036+
/// mapping from symbols to affine expressions in 'symbolSet'.
1037+
virtual ParseResult
1038+
parseAffineExpr(SmallVectorImpl<std::pair<StringRef, AffineExpr>> &symbolSet,
1039+
AffineExpr &expr) = 0;
1040+
10351041
/// Parse an integer set instance into 'set'.
10361042
virtual ParseResult parseIntegerSet(IntegerSet &set) = 0;
10371043

mlir/lib/AsmParser/AffineParser.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class AffineParser : public Parser {
5353
ParseResult parseAffineMapRange(unsigned numDims, unsigned numSymbols,
5454
AffineMap &result);
5555
ParseResult parseAffineMapOrIntegerSetInline(AffineMap &map, IntegerSet &set);
56+
ParseResult parseAffineExprInline(
57+
SmallVectorImpl<std::pair<StringRef, AffineExpr>> &symbolSet,
58+
AffineExpr &expr);
5659
ParseResult parseIntegerSetConstraints(unsigned numDims, unsigned numSymbols,
5760
IntegerSet &result);
5861
ParseResult parseAffineMapOfSSAIds(AffineMap &map,
@@ -533,6 +536,15 @@ ParseResult AffineParser::parseAffineMapOrIntegerSetInline(AffineMap &map,
533536
return parseIntegerSetConstraints(numDims, numSymbols, set);
534537
}
535538

539+
/// Parse an affine expresion definition inline, with given symbols.
540+
ParseResult AffineParser::parseAffineExprInline(
541+
SmallVectorImpl<std::pair<StringRef, AffineExpr>> &symbolSet,
542+
AffineExpr &expr) {
543+
dimsAndSymbols.assign(symbolSet);
544+
expr = parseAffineExpr();
545+
return success(expr != nullptr);
546+
}
547+
536548
/// Parse an AffineMap where the dim and symbol identifiers are SSA ids.
537549
ParseResult
538550
AffineParser::parseAffineMapOfSSAIds(AffineMap &map,
@@ -703,6 +715,11 @@ ParseResult Parser::parseAffineMapReference(AffineMap &map) {
703715
return emitError(curLoc, "expected AffineMap, but got IntegerSet");
704716
return success();
705717
}
718+
ParseResult Parser::parseAffineExprReference(
719+
SmallVectorImpl<std::pair<StringRef, AffineExpr>> &symbolSet,
720+
AffineExpr &expr) {
721+
return AffineParser(state).parseAffineExprInline(symbolSet, expr);
722+
}
706723
ParseResult Parser::parseIntegerSetReference(IntegerSet &set) {
707724
SMLoc curLoc = getToken().getLoc();
708725
AffineMap map;

mlir/lib/AsmParser/AsmParserImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,14 @@ class AsmParserImpl : public BaseT {
456456
return parser.parseAffineMapReference(map);
457457
}
458458

459+
/// Parse an affine expr instance into 'expr' using the already computed
460+
/// mapping from symbols to affine expressions in 'symbolSet'.
461+
ParseResult
462+
parseAffineExpr(SmallVectorImpl<std::pair<StringRef, AffineExpr>> &symbolSet,
463+
AffineExpr &expr) override {
464+
return parser.parseAffineExprReference(symbolSet, expr);
465+
}
466+
459467
/// Parse an integer set instance into 'set'.
460468
ParseResult parseIntegerSet(IntegerSet &set) override {
461469
return parser.parseIntegerSetReference(set);

mlir/lib/AsmParser/Parser.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,13 @@ class Parser {
296296
// Affine Parsing
297297
//===--------------------------------------------------------------------===//
298298

299-
/// Parse a reference to either an affine map, or an integer set.
299+
/// Parse a reference to either an affine map, expr, or an integer set.
300300
ParseResult parseAffineMapOrIntegerSetReference(AffineMap &map,
301301
IntegerSet &set);
302302
ParseResult parseAffineMapReference(AffineMap &map);
303+
ParseResult parseAffineExprReference(
304+
SmallVectorImpl<std::pair<StringRef, AffineExpr>> &symbolSet,
305+
AffineExpr &expr);
303306
ParseResult parseIntegerSetReference(IntegerSet &set);
304307

305308
/// Parse an AffineMap where the dim and symbol identifiers are SSA ids.

0 commit comments

Comments
 (0)