1 //===- Parser.h - Parser for Presburger library -----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines functions to parse strings into Presburger library
12 //===----------------------------------------------------------------------===//
14 #ifndef MLIR_UNITTESTS_ANALYSIS_PRESBURGER_PARSER_H
15 #define MLIR_UNITTESTS_ANALYSIS_PRESBURGER_PARSER_H
17 #include "mlir/Analysis/Presburger/IntegerRelation.h"
18 #include "mlir/Analysis/Presburger/PWMAFunction.h"
19 #include "mlir/Analysis/Presburger/PresburgerRelation.h"
20 #include "mlir/AsmParser/AsmParser.h"
21 #include "mlir/Dialect/Affine/Analysis/AffineStructures.h"
22 #include "mlir/IR/AffineExpr.h"
23 #include "mlir/IR/AffineMap.h"
24 #include "mlir/IR/IntegerSet.h"
27 namespace presburger
{
29 /// Parses an IntegerPolyhedron from a StringRef. It is expected that the string
30 /// represents a valid IntegerSet.
31 inline IntegerPolyhedron
parseIntegerPolyhedron(StringRef str
) {
32 MLIRContext
context(MLIRContext::Threading::DISABLED
);
33 return affine::FlatAffineValueConstraints(parseIntegerSet(str
, &context
));
36 /// Parse a list of StringRefs to IntegerRelation and combine them into a
37 /// PresburgerSet by using the union operation. It is expected that the strings
38 /// are all valid IntegerSet representation and that all of them have compatible
40 inline PresburgerSet
parsePresburgerSet(ArrayRef
<StringRef
> strs
) {
41 assert(!strs
.empty() && "strs should not be empty");
43 IntegerPolyhedron initPoly
= parseIntegerPolyhedron(strs
[0]);
44 PresburgerSet
result(initPoly
);
45 for (unsigned i
= 1, e
= strs
.size(); i
< e
; ++i
)
46 result
.unionInPlace(parseIntegerPolyhedron(strs
[i
]));
50 inline MultiAffineFunction
parseMultiAffineFunction(StringRef str
) {
51 MLIRContext
context(MLIRContext::Threading::DISABLED
);
53 // TODO: Add default constructor for MultiAffineFunction.
54 MultiAffineFunction
multiAff(PresburgerSpace::getRelationSpace(),
56 if (getMultiAffineFunctionFromMap(parseAffineMap(str
, &context
), multiAff
)
59 "Failed to parse MultiAffineFunction because of semi-affinity");
64 parsePWMAF(ArrayRef
<std::pair
<StringRef
, StringRef
>> pieces
) {
65 assert(!pieces
.empty() && "At least one piece should be present.");
67 MLIRContext
context(MLIRContext::Threading::DISABLED
);
69 IntegerPolyhedron initDomain
= parseIntegerPolyhedron(pieces
[0].first
);
70 MultiAffineFunction initMultiAff
= parseMultiAffineFunction(pieces
[0].second
);
72 PWMAFunction
func(PresburgerSpace::getRelationSpace(
73 initMultiAff
.getNumDomainVars(), initMultiAff
.getNumOutputs(),
74 initMultiAff
.getNumSymbolVars()));
76 func
.addPiece({PresburgerSet(initDomain
), initMultiAff
});
77 for (unsigned i
= 1, e
= pieces
.size(); i
< e
; ++i
)
78 func
.addPiece({PresburgerSet(parseIntegerPolyhedron(pieces
[i
].first
)),
79 parseMultiAffineFunction(pieces
[i
].second
)});
83 inline IntegerRelation
parseRelationFromSet(StringRef set
, unsigned numDomain
) {
84 IntegerRelation rel
= parseIntegerPolyhedron(set
);
86 rel
.convertVarKind(VarKind::SetDim
, 0, numDomain
, VarKind::Domain
);
91 inline PresburgerRelation
92 parsePresburgerRelationFromPresburgerSet(ArrayRef
<StringRef
> strs
,
94 assert(!strs
.empty() && "strs should not be empty");
96 IntegerRelation rel
= parseIntegerPolyhedron(strs
[0]);
97 PresburgerRelation
result(rel
);
98 for (unsigned i
= 1, e
= strs
.size(); i
< e
; ++i
)
99 result
.unionInPlace(parseIntegerPolyhedron(strs
[i
]));
100 result
.convertVarKind(VarKind::SetDim
, 0, numDomain
, VarKind::Domain
, 0);
104 } // namespace presburger
107 #endif // MLIR_UNITTESTS_ANALYSIS_PRESBURGER_PARSER_H