xref: /llvm-project/mlir/unittests/Analysis/Presburger/Parser.h (revision c1b997464bb68ae37c9ed9b5594cb440c8a13382)
1 //===- Parser.h - Parser for Presburger library -----------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines functions to parse strings into Presburger library
10 // constructs.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_UNITTESTS_ANALYSIS_PRESBURGER_PARSER_H
15 #define MLIR_UNITTESTS_ANALYSIS_PRESBURGER_PARSER_H
16 
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"
25 
26 namespace mlir {
27 namespace presburger {
28 
29 /// Parses an IntegerPolyhedron from a StringRef. It is expected that the string
30 /// represents a valid IntegerSet.
parseIntegerPolyhedron(StringRef str)31 inline IntegerPolyhedron parseIntegerPolyhedron(StringRef str) {
32   MLIRContext context(MLIRContext::Threading::DISABLED);
33   return affine::FlatAffineValueConstraints(parseIntegerSet(str, &context));
34 }
35 
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
39 /// spaces.
parsePresburgerSet(ArrayRef<StringRef> strs)40 inline PresburgerSet parsePresburgerSet(ArrayRef<StringRef> strs) {
41   assert(!strs.empty() && "strs should not be empty");
42 
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]));
47   return result;
48 }
49 
parseMultiAffineFunction(StringRef str)50 inline MultiAffineFunction parseMultiAffineFunction(StringRef str) {
51   MLIRContext context(MLIRContext::Threading::DISABLED);
52 
53   // TODO: Add default constructor for MultiAffineFunction.
54   MultiAffineFunction multiAff(PresburgerSpace::getRelationSpace(),
55                                IntMatrix(0, 1));
56   if (getMultiAffineFunctionFromMap(parseAffineMap(str, &context), multiAff)
57           .failed())
58     llvm_unreachable(
59         "Failed to parse MultiAffineFunction because of semi-affinity");
60   return multiAff;
61 }
62 
63 inline PWMAFunction
parsePWMAF(ArrayRef<std::pair<StringRef,StringRef>> pieces)64 parsePWMAF(ArrayRef<std::pair<StringRef, StringRef>> pieces) {
65   assert(!pieces.empty() && "At least one piece should be present.");
66 
67   MLIRContext context(MLIRContext::Threading::DISABLED);
68 
69   IntegerPolyhedron initDomain = parseIntegerPolyhedron(pieces[0].first);
70   MultiAffineFunction initMultiAff = parseMultiAffineFunction(pieces[0].second);
71 
72   PWMAFunction func(PresburgerSpace::getRelationSpace(
73       initMultiAff.getNumDomainVars(), initMultiAff.getNumOutputs(),
74       initMultiAff.getNumSymbolVars()));
75 
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)});
80   return func;
81 }
82 
parseRelationFromSet(StringRef set,unsigned numDomain)83 inline IntegerRelation parseRelationFromSet(StringRef set, unsigned numDomain) {
84   IntegerRelation rel = parseIntegerPolyhedron(set);
85 
86   rel.convertVarKind(VarKind::SetDim, 0, numDomain, VarKind::Domain);
87 
88   return rel;
89 }
90 
91 inline PresburgerRelation
parsePresburgerRelationFromPresburgerSet(ArrayRef<StringRef> strs,unsigned numDomain)92 parsePresburgerRelationFromPresburgerSet(ArrayRef<StringRef> strs,
93                                          unsigned numDomain) {
94   assert(!strs.empty() && "strs should not be empty");
95 
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);
101   return result;
102 }
103 
104 } // namespace presburger
105 } // namespace mlir
106 
107 #endif // MLIR_UNITTESTS_ANALYSIS_PRESBURGER_PARSER_H
108