Skip to content

Commit f5acf65

Browse files
committed
[presburger] Develope python bindings for presburger
c++ library This MR is work in progress.
1 parent 9bf68c2 commit f5acf65

File tree

11 files changed

+1103
-0
lines changed

11 files changed

+1103
-0
lines changed

llvm/include/llvm/ADT/DynamicAPInt.h

+7
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ class DynamicAPInt {
217217

218218
raw_ostream &print(raw_ostream &OS) const;
219219
LLVM_DUMP_METHOD void dump() const;
220+
221+
void *getAsOpaquePointer() const { return const_cast<DynamicAPInt *>(this); }
222+
223+
static DynamicAPInt *getFromOpaquePointer(const void *Pointer) {
224+
return const_cast<DynamicAPInt *>(
225+
reinterpret_cast<const DynamicAPInt *>(Pointer));
226+
}
220227
};
221228

222229
inline raw_ostream &operator<<(raw_ostream &OS, const DynamicAPInt &X) {

mlir/include/mlir-c/Presburger.h

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
#ifndef MLIR_C_PRESBURGER_H
2+
#define MLIR_C_PRESBURGER_H
3+
#include "mlir-c/AffineExpr.h"
4+
#include "mlir-c/Support.h"
5+
#include <vector>
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
enum MlirPresburgerVariableKind {
12+
Symbol,
13+
Local,
14+
Domain,
15+
Range,
16+
SetDim = Range
17+
};
18+
19+
#define DEFINE_C_API_STRUCT(name, storage) \
20+
struct name { \
21+
storage *ptr; \
22+
}; \
23+
typedef struct name name
24+
DEFINE_C_API_STRUCT(MlirPresburgerIntegerRelation, void);
25+
DEFINE_C_API_STRUCT(MlirPresburgerDynamicAPInt, const void);
26+
#undef DEFINE_C_API_STRUCT
27+
28+
//===----------------------------------------------------------------------===//
29+
// IntegerRelation creation/destruction and basic metadata operations
30+
//===----------------------------------------------------------------------===//
31+
32+
/// Constructs a relation reserving memory for the specified number
33+
/// of constraints and variables.
34+
MLIR_CAPI_EXPORTED MlirPresburgerIntegerRelation
35+
mlirPresburgerIntegerRelationCreate(unsigned numReservedInequalities,
36+
unsigned numReservedEqualities,
37+
unsigned numReservedCols);
38+
39+
/// Constructs an IntegerRelation from a packed 2D matrix of tableau
40+
/// coefficients in row-major order. The first `numDomainVars` columns are
41+
/// considered domain and the remaining `numRangeVars` columns are domain
42+
/// variables.
43+
MLIR_CAPI_EXPORTED MlirPresburgerIntegerRelation
44+
mlirPresburgerIntegerRelationCreateFromCoefficients(
45+
const int64_t *inequalityCoefficients, unsigned numInequalities,
46+
const int64_t *equalityCoefficients, unsigned numEqualities,
47+
unsigned numDomainVars, unsigned numRangeVars,
48+
unsigned numExtraReservedInequalities = 0,
49+
unsigned numExtraReservedEqualities = 0, unsigned numExtraReservedCols = 0);
50+
51+
/// Destroys an IntegerRelation.
52+
MLIR_CAPI_EXPORTED void
53+
mlirPresburgerIntegerRelationDestroy(MlirPresburgerIntegerRelation relation);
54+
55+
//===----------------------------------------------------------------------===//
56+
// IntegerRelation binary operations
57+
//===----------------------------------------------------------------------===//
58+
59+
MLIR_CAPI_EXPORTED void
60+
mlirPresburgerIntegerRelationAppend(MlirPresburgerIntegerRelation lhs,
61+
MlirPresburgerIntegerRelation rhs);
62+
63+
/// Return the intersection of the two relations.
64+
/// If there are locals, they will be merged.
65+
MLIR_CAPI_EXPORTED MlirPresburgerIntegerRelation
66+
mlirPresburgerIntegerRelationIntersect(MlirPresburgerIntegerRelation lhs,
67+
MlirPresburgerIntegerRelation rhs);
68+
69+
/// Return whether `lhs` and `rhs` are equal. This is integer-exact
70+
/// and somewhat expensive, since it uses the integer emptiness check
71+
/// (see IntegerRelation::findIntegerSample()).
72+
MLIR_CAPI_EXPORTED bool
73+
mlirPresburgerIntegerRelationIsEqual(MlirPresburgerIntegerRelation lhs,
74+
MlirPresburgerIntegerRelation rhs);
75+
76+
MLIR_CAPI_EXPORTED bool mlirPresburgerIntegerRelationIsObviouslyEqual(
77+
MlirPresburgerIntegerRelation lhs, MlirPresburgerIntegerRelation rhs);
78+
79+
MLIR_CAPI_EXPORTED bool
80+
mlirPresburgerIntegerRelationIsSubsetOf(MlirPresburgerIntegerRelation lhs,
81+
MlirPresburgerIntegerRelation rhs);
82+
83+
//===----------------------------------------------------------------------===//
84+
// IntegerRelation Tableau Inspection
85+
//===----------------------------------------------------------------------===//
86+
87+
/// Returns the value at the specified equality row and column.
88+
MLIR_CAPI_EXPORTED MlirPresburgerDynamicAPInt
89+
mlirPresburgerIntegerRelationAtEq(unsigned i, unsigned j);
90+
91+
/// The same, but casts to int64_t. This is unsafe and will assert-fail if the
92+
/// value does not fit in an int64_t.
93+
MLIR_CAPI_EXPORTED int64_t mlirPresburgerIntegerRelationAtEq64(
94+
MlirPresburgerIntegerRelation relation, unsigned row, unsigned col);
95+
96+
/// Returns the value at the specified inequality row and column.
97+
MLIR_CAPI_EXPORTED MlirPresburgerDynamicAPInt
98+
mlirPresburgerIntegerRelationAtIneq(MlirPresburgerIntegerRelation relation,
99+
unsigned row, unsigned col);
100+
101+
MLIR_CAPI_EXPORTED int64_t mlirPresburgerIntegerRelationAtIneq64(
102+
MlirPresburgerIntegerRelation relation, unsigned row, unsigned col);
103+
104+
/// Returns the number of inequalities and equalities.
105+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumConstraints(
106+
MlirPresburgerIntegerRelation relation);
107+
108+
/// Returns the number of columns classified as domain variables.
109+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumDomainVars(
110+
MlirPresburgerIntegerRelation relation);
111+
112+
/// Returns the number of columns classified as range variables.
113+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumRangeVars(
114+
MlirPresburgerIntegerRelation relation);
115+
116+
/// Returns the number of columns classified as symbol variables.
117+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumSymbolVars(
118+
MlirPresburgerIntegerRelation relation);
119+
120+
/// Returns the number of columns classified as local variables.
121+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumLocalVars(
122+
MlirPresburgerIntegerRelation relation);
123+
124+
MLIR_CAPI_EXPORTED unsigned
125+
mlirPresburgerIntegerRelationNumDimVars(MlirPresburgerIntegerRelation relation);
126+
127+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumDimAndSymbolVars(
128+
MlirPresburgerIntegerRelation relation);
129+
130+
MLIR_CAPI_EXPORTED unsigned
131+
mlirPresburgerIntegerRelationNumVars(MlirPresburgerIntegerRelation relation);
132+
133+
MLIR_CAPI_EXPORTED unsigned
134+
mlirPresburgerIntegerRelationNumCols(MlirPresburgerIntegerRelation relation);
135+
136+
/// Returns the number of equality constraints.
137+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumEqualities(
138+
MlirPresburgerIntegerRelation relation);
139+
140+
/// Returns the number of inequality constraints.
141+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumInequalities(
142+
MlirPresburgerIntegerRelation relation);
143+
144+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumReservedEqualities(
145+
MlirPresburgerIntegerRelation relation);
146+
147+
MLIR_CAPI_EXPORTED unsigned
148+
mlirPresburgerIntegerRelationNumReservedInequalities(
149+
MlirPresburgerIntegerRelation relation);
150+
151+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationGetNumVarKind(
152+
MlirPresburgerIntegerRelation relation, MlirPresburgerVariableKind kind);
153+
154+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationGetVarKindOffset(
155+
MlirPresburgerIntegerRelation relation, MlirPresburgerVariableKind kind);
156+
157+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationGetVarKindEnd(
158+
MlirPresburgerIntegerRelation relation, MlirPresburgerVariableKind kind);
159+
160+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationGetVarKindOverLap(
161+
MlirPresburgerIntegerRelation relation, MlirPresburgerVariableKind kind,
162+
unsigned varStart, unsigned varLimit);
163+
164+
/// Return the VarKind of the var at the specified position.
165+
MLIR_CAPI_EXPORTED MlirPresburgerVariableKind
166+
mlirPresburgerIntegerRelationGetVarKindAt(
167+
MlirPresburgerIntegerRelation relation, unsigned pos);
168+
169+
//===----------------------------------------------------------------------===//
170+
// IntegerRelation Tableau Manipulation
171+
//===----------------------------------------------------------------------===//
172+
173+
MLIR_CAPI_EXPORTED unsigned
174+
mlirPresburgerIntegerRelationInsertVar(MlirPresburgerIntegerRelation relation,
175+
MlirPresburgerVariableKind kind,
176+
unsigned pos, unsigned num = 1);
177+
178+
MLIR_CAPI_EXPORTED unsigned
179+
mlirPresburgerIntegerRelationAppendVar(MlirPresburgerIntegerRelation relation,
180+
MlirPresburgerVariableKind kind,
181+
unsigned num = 1);
182+
183+
/// Adds an equality with the given coefficients.
184+
MLIR_CAPI_EXPORTED void
185+
mlirPresburgerIntegerRelationAddEquality(MlirPresburgerIntegerRelation relation,
186+
const std::vector<int64_t> &eq);
187+
188+
/// Adds an inequality with the given coefficients.
189+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationAddInequality(
190+
MlirPresburgerIntegerRelation relation, const std::vector<int64_t> &inEq);
191+
192+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationEliminateRedundantLocalVar(
193+
MlirPresburgerIntegerRelation relation, unsigned posA, unsigned posB);
194+
195+
/// Removes variables of the specified kind with the specified pos (or
196+
/// within the specified range) from the system. The specified location is
197+
/// relative to the first variable of the specified kind.
198+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationRemoveVarKind(
199+
MlirPresburgerIntegerRelation relation, MlirPresburgerVariableKind kind,
200+
unsigned pos);
201+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationRemoveVarRangeKind(
202+
MlirPresburgerIntegerRelation relation, MlirPresburgerVariableKind kind,
203+
unsigned varStart, unsigned varLimit);
204+
205+
/// Removes the specified variable from the system.
206+
MLIR_CAPI_EXPORTED void
207+
mlirPresburgerIntegerRelationRemoveVar(MlirPresburgerIntegerRelation relation,
208+
unsigned pos);
209+
210+
/// Remove the (in)equalities at specified position.
211+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationRemoveEquality(
212+
MlirPresburgerIntegerRelation relation, unsigned pos);
213+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationRemoveInequality(
214+
MlirPresburgerIntegerRelation relation, unsigned pos);
215+
216+
/// Remove the (in)equalities at positions [start, end).
217+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationRemoveEqualityRange(
218+
MlirPresburgerIntegerRelation relation, unsigned start, unsigned end);
219+
MLIR_CAPI_EXPORTED void mlirPresburgerIntegerRelationRemoveInequalityRange(
220+
MlirPresburgerIntegerRelation relation, unsigned start, unsigned end);
221+
222+
//===----------------------------------------------------------------------===//
223+
// IntegerRelation Dump
224+
//===----------------------------------------------------------------------===//
225+
MLIR_CAPI_EXPORTED void
226+
mlirPresburgerIntegerRelationDump(MlirPresburgerIntegerRelation relation);
227+
#ifdef __cplusplus
228+
}
229+
#endif
230+
#endif // MLIR_C_PRESBURGER_H

mlir/include/mlir/Analysis/Presburger/IntegerRelation.h

+9
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,15 @@ class IntegerRelation {
753753
// false.
754754
bool isFullDim();
755755

756+
void *getAsOpaquePointer() const {
757+
return const_cast<IntegerRelation *>(this);
758+
}
759+
760+
static IntegerRelation *getFromOpaquePointer(const void *pointer) {
761+
return const_cast<IntegerRelation *>(
762+
reinterpret_cast<const IntegerRelation *>(pointer));
763+
}
764+
756765
void print(raw_ostream &os) const;
757766
void dump() const;
758767

mlir/include/mlir/CAPI/Presburger.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef MLIR_CAPI_PRESBURGER_H
2+
#define MLIR_CAPI_PRESBURGER_H
3+
4+
#include "mlir-c/Presburger.h"
5+
#include "mlir/Analysis/Presburger/IntegerRelation.h"
6+
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
7+
#include "mlir/CAPI/Wrap.h"
8+
#include "llvm/ADT/DynamicAPInt.h"
9+
10+
DEFINE_C_API_PTR_METHODS(MlirPresburgerIntegerRelation,
11+
mlir::presburger::IntegerRelation)
12+
13+
static inline MlirPresburgerDynamicAPInt wrap(llvm::DynamicAPInt *cpp) {
14+
return MlirPresburgerDynamicAPInt{cpp->getAsOpaquePointer()};
15+
}
16+
17+
static inline llvm::DynamicAPInt *unwrap(MlirPresburgerDynamicAPInt c) {
18+
return llvm::DynamicAPInt::getFromOpaquePointer(c.ptr);
19+
}
20+
21+
#endif /* MLIR_CAPI_PRESBURGER_H */

0 commit comments

Comments
 (0)