Skip to content

Commit 84d07d0

Browse files
committed
[MLIR][Presburger] Improve unittest parsing
This patch adds better functions for parsing MultiAffineFunctions and PWMAFunctions in Presburger unittests. A PWMAFunction can now be parsed as: ``` PWMAFunction result = parsePWMAF({ {"(x, y) : (x >= 10, x <= 20, y >= 1)", "(x, y) -> (x + y)"}, {"(x, y) : (x >= 21)", "(x, y) -> (x + y)"}, {"(x, y) : (x <= 9)", "(x, y) -> (x - y)"}, {"(x, y) : (x >= 10, x <= 20, y <= 0)", "(x, y) -> (x - y)"}, }); ``` which is much more readable than the old format since the output can be described as an AffineMap, instead of coefficients. This patch also adds support for parsing divisions in MultiAffineFunctions and PWMAFunctions which was previously not possible. Reviewed By: arjunp Differential Revision: https://reviews.llvm.org/D133654
1 parent 6e0e926 commit 84d07d0

File tree

17 files changed

+903
-1028
lines changed

17 files changed

+903
-1028
lines changed

mlir/include/mlir/AsmParser/AsmParser.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,13 @@ Type parseType(llvm::StringRef typeStr, MLIRContext *context);
7676
/// returned in `numRead`.
7777
Type parseType(llvm::StringRef typeStr, MLIRContext *context, size_t &numRead);
7878

79-
/// This parses a single IntegerSet to an MLIR context if it was valid. If not,
80-
/// an error message is emitted through a new SourceMgrDiagnosticHandler
81-
/// constructed from a new SourceMgr with a single MemoryBuffer wrapping
82-
/// `str`. If the passed `str` has additional tokens that were not part of the
83-
/// IntegerSet, a failure is returned. Diagnostics are printed on failure if
84-
/// `printDiagnosticInfo` is true.
85-
IntegerSet parseIntegerSet(llvm::StringRef str, MLIRContext *context,
86-
bool printDiagnosticInfo = true);
79+
/// This parses a single IntegerSet/AffineMap to an MLIR context if it was
80+
/// valid. If not, an error message is emitted through a new
81+
/// SourceMgrDiagnosticHandler constructed from a new SourceMgr with a single
82+
/// MemoryBuffer wrapping `str`. If the passed `str` has additional tokens that
83+
/// were not part of the IntegerSet/AffineMap, a failure is returned.
84+
AffineMap parseAffineMap(llvm::StringRef str, MLIRContext *context);
85+
IntegerSet parseIntegerSet(llvm::StringRef str, MLIRContext *context);
8786

8887
} // namespace mlir
8988

mlir/include/mlir/Dialect/Affine/Analysis/AffineStructures.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Value;
3232
class MemRefType;
3333
struct MutableAffineMap;
3434

35+
namespace presburger {
36+
class MultiAffineFunction;
37+
} // namespace presburger
38+
3539
/// FlatAffineValueConstraints represents an extension of IntegerPolyhedron
3640
/// where each non-local variable can have an SSA Value attached to it.
3741
class FlatAffineValueConstraints : public presburger::IntegerPolyhedron {
@@ -615,6 +619,10 @@ getFlattenedAffineExprs(IntegerSet set,
615619
std::vector<SmallVector<int64_t, 8>> *flattenedExprs,
616620
FlatAffineValueConstraints *cst = nullptr);
617621

622+
LogicalResult
623+
getMultiAffineFunctionFromMap(AffineMap map,
624+
presburger::MultiAffineFunction &multiAff);
625+
618626
/// Re-indexes the dimensions and symbols of an affine map with given `operands`
619627
/// values to align with `dims` and `syms` values.
620628
///

mlir/lib/AsmParser/AffineParser.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,8 @@ Parser::parseAffineExprOfSSAIds(AffineExpr &expr,
734734
.parseAffineExprOfSSAIds(expr);
735735
}
736736

737-
IntegerSet mlir::parseIntegerSet(StringRef inputStr, MLIRContext *context,
738-
bool printDiagnosticInfo) {
737+
static void parseAffineMapOrIntegerSet(StringRef inputStr, MLIRContext *context,
738+
AffineMap &map, IntegerSet &set) {
739739
llvm::SourceMgr sourceMgr;
740740
auto memBuffer = llvm::MemoryBuffer::getMemBuffer(
741741
inputStr, /*BufferName=*/"<mlir_parser_buffer>",
@@ -747,17 +747,31 @@ IntegerSet mlir::parseIntegerSet(StringRef inputStr, MLIRContext *context,
747747
/*codeCompleteContext=*/nullptr);
748748
Parser parser(state);
749749

750-
raw_ostream &os = printDiagnosticInfo ? llvm::errs() : llvm::nulls();
751-
SourceMgrDiagnosticHandler handler(sourceMgr, context, os);
752-
IntegerSet set;
753-
if (parser.parseIntegerSetReference(set))
754-
return IntegerSet();
750+
SourceMgrDiagnosticHandler handler(sourceMgr, context, llvm::errs());
751+
if (parser.parseAffineMapOrIntegerSetReference(map, set))
752+
return;
755753

756754
Token endTok = parser.getToken();
757755
if (endTok.isNot(Token::eof)) {
758756
parser.emitError(endTok.getLoc(), "encountered unexpected token");
759-
return IntegerSet();
757+
return;
760758
}
759+
}
760+
761+
AffineMap mlir::parseAffineMap(StringRef inputStr, MLIRContext *context) {
762+
AffineMap map;
763+
IntegerSet set;
764+
parseAffineMapOrIntegerSet(inputStr, context, map, set);
765+
assert(!set &&
766+
"expected string to represent AffineMap, but got IntegerSet instead");
767+
return map;
768+
}
761769

770+
IntegerSet mlir::parseIntegerSet(StringRef inputStr, MLIRContext *context) {
771+
AffineMap map;
772+
IntegerSet set;
773+
parseAffineMapOrIntegerSet(inputStr, context, map, set);
774+
assert(!map &&
775+
"expected string to represent IntegerSet, but got AffineMap instead");
762776
return set;
763777
}

mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,3 +1801,31 @@ LogicalResult mlir::getRelationFromMap(const AffineValueMap &map,
18011801

18021802
return success();
18031803
}
1804+
1805+
LogicalResult
1806+
mlir::getMultiAffineFunctionFromMap(AffineMap map,
1807+
MultiAffineFunction &multiAff) {
1808+
FlatAffineValueConstraints cst;
1809+
std::vector<SmallVector<int64_t, 8>> flattenedExprs;
1810+
LogicalResult result = getFlattenedAffineExprs(map, &flattenedExprs, &cst);
1811+
1812+
if (result.failed())
1813+
return failure();
1814+
1815+
DivisionRepr divs = cst.getLocalReprs();
1816+
assert(divs.hasAllReprs() &&
1817+
"AffineMap cannot produce divs without local representation");
1818+
1819+
// TODO: We shouldn't have to do this conversion.
1820+
Matrix mat(map.getNumResults(), map.getNumInputs() + divs.getNumDivs() + 1);
1821+
for (unsigned i = 0, e = flattenedExprs.size(); i < e; ++i)
1822+
for (unsigned j = 0, f = flattenedExprs[i].size(); j < f; ++j)
1823+
mat(i, j) = flattenedExprs[i][j];
1824+
1825+
multiAff = MultiAffineFunction(
1826+
PresburgerSpace::getRelationSpace(map.getNumDims(), map.getNumResults(),
1827+
map.getNumSymbols(), divs.getNumDivs()),
1828+
mat, divs);
1829+
1830+
return success();
1831+
}

mlir/unittests/Analysis/Presburger/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ add_mlir_unittest(MLIRPresburgerTests
44
LinearTransformTest.cpp
55
MatrixTest.cpp
66
MPIntTest.cpp
7+
Parser.h
8+
ParserTest.cpp
79
PresburgerSetTest.cpp
810
PresburgerSpaceTest.cpp
911
PWMAFunctionTest.cpp
1012
SimplexTest.cpp
11-
../../Dialect/Affine/Analysis/AffineStructuresParser.cpp
1213
)
1314

1415
target_link_libraries(MLIRPresburgerTests

0 commit comments

Comments
 (0)