Skip to content

Commit 0744372

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

File tree

11 files changed

+801
-0
lines changed

11 files changed

+801
-0
lines changed

llvm/include/llvm/ADT/DynamicAPInt.h

Lines changed: 7 additions & 0 deletions
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

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
enum MlirPresburgerVariableKind {
11+
Symbol,
12+
Local,
13+
Domain,
14+
Range,
15+
SetDim = Range
16+
};
17+
18+
#define DEFINE_C_API_STRUCT(name, storage) \
19+
struct name { \
20+
storage *ptr; \
21+
}; \
22+
typedef struct name name
23+
DEFINE_C_API_STRUCT(MlirPresburgerIntegerRelation, void);
24+
DEFINE_C_API_STRUCT(MlirPresburgerDynamicAPInt, const void);
25+
#undef DEFINE_C_API_STRUCT
26+
27+
//===----------------------------------------------------------------------===//
28+
// IntegerRelation creation/destruction and basic metadata operations
29+
//===----------------------------------------------------------------------===//
30+
31+
/// Constructs a relation reserving memory for the specified number
32+
/// of constraints and variables.
33+
MLIR_CAPI_EXPORTED MlirPresburgerIntegerRelation
34+
mlirPresburgerIntegerRelationCreate(unsigned numReservedInequalities,
35+
unsigned numReservedEqualities,
36+
unsigned numReservedCols);
37+
38+
/// Constructs an IntegerRelation from a packed 2D matrix of tableau
39+
/// coefficients in row-major order. The first `numDomainVars` columns are
40+
/// considered domain and the remaining `numRangeVars` columns are domain
41+
/// variables.
42+
MLIR_CAPI_EXPORTED MlirPresburgerIntegerRelation
43+
mlirPresburgerIntegerRelationCreateFromCoefficients(
44+
const int64_t *inequalityCoefficients, unsigned numInequalities,
45+
const int64_t *equalityCoefficients, unsigned numEqualities,
46+
unsigned numDomainVars, unsigned numRangeVars,
47+
unsigned numExtraReservedInequalities = 0,
48+
unsigned numExtraReservedEqualities = 0, unsigned numExtraReservedCols = 0);
49+
50+
/// Destroys an IntegerRelation.
51+
MLIR_CAPI_EXPORTED void
52+
mlirPresburgerIntegerRelationDestroy(MlirPresburgerIntegerRelation relation);
53+
54+
//===----------------------------------------------------------------------===//
55+
// IntegerRelation binary operations
56+
//===----------------------------------------------------------------------===//
57+
58+
MLIR_CAPI_EXPORTED void
59+
mlirPresburgerIntegerRelationAppend(MlirPresburgerIntegerRelation lhs,
60+
MlirPresburgerIntegerRelation rhs);
61+
62+
/// Return the intersection of the two relations.
63+
/// If there are locals, they will be merged.
64+
MLIR_CAPI_EXPORTED MlirPresburgerIntegerRelation
65+
mlirPresburgerIntegerRelationIntersect(MlirPresburgerIntegerRelation lhs,
66+
MlirPresburgerIntegerRelation rhs);
67+
68+
/// Return whether `lhs` and `rhs` are equal. This is integer-exact
69+
/// and somewhat expensive, since it uses the integer emptiness check
70+
/// (see IntegerRelation::findIntegerSample()).
71+
MLIR_CAPI_EXPORTED bool
72+
mlirPresburgerIntegerRelationIsEqual(MlirPresburgerIntegerRelation lhs,
73+
MlirPresburgerIntegerRelation rhs);
74+
75+
MLIR_CAPI_EXPORTED bool mlirPresburgerIntegerRelationIsObviouslyEqual(
76+
MlirPresburgerIntegerRelation lhs, MlirPresburgerIntegerRelation rhs);
77+
78+
MLIR_CAPI_EXPORTED bool
79+
mlirPresburgerIntegerRelationIsSubsetOf(MlirPresburgerIntegerRelation lhs,
80+
MlirPresburgerIntegerRelation rhs);
81+
82+
//===----------------------------------------------------------------------===//
83+
// IntegerRelation Tableau Inspection
84+
//===----------------------------------------------------------------------===//
85+
86+
/// Returns the value at the specified equality row and column.
87+
MLIR_CAPI_EXPORTED MlirPresburgerDynamicAPInt
88+
mlirPresburgerIntegerRelationAtEq(unsigned i, unsigned j);
89+
90+
/// The same, but casts to int64_t. This is unsafe and will assert-fail if the
91+
/// value does not fit in an int64_t.
92+
MLIR_CAPI_EXPORTED int64_t mlirPresburgerIntegerRelationAtEq64(
93+
MlirPresburgerIntegerRelation relation, unsigned row, unsigned col);
94+
95+
/// Returns the value at the specified inequality row and column.
96+
MLIR_CAPI_EXPORTED MlirPresburgerDynamicAPInt
97+
mlirPresburgerIntegerRelationAtIneq(MlirPresburgerIntegerRelation relation,
98+
unsigned row, unsigned col);
99+
100+
MLIR_CAPI_EXPORTED int64_t mlirPresburgerIntegerRelationAtIneq64(
101+
MlirPresburgerIntegerRelation relation, unsigned row, unsigned col);
102+
103+
/// Returns the number of inequalities and equalities.
104+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumConstraints(
105+
MlirPresburgerIntegerRelation relation);
106+
107+
/// Returns the number of columns classified as domain variables.
108+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumDomainVars(
109+
MlirPresburgerIntegerRelation relation);
110+
111+
/// Returns the number of columns classified as range variables.
112+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumRangeVars(
113+
MlirPresburgerIntegerRelation relation);
114+
115+
/// Returns the number of columns classified as symbol variables.
116+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumSymbolVars(
117+
MlirPresburgerIntegerRelation relation);
118+
119+
/// Returns the number of columns classified as local variables.
120+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumLocalVars(
121+
MlirPresburgerIntegerRelation relation);
122+
123+
MLIR_CAPI_EXPORTED unsigned
124+
mlirPresburgerIntegerRelationNumDimVars(MlirPresburgerIntegerRelation relation);
125+
126+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumDimAndSymbolVars(
127+
MlirPresburgerIntegerRelation relation);
128+
129+
MLIR_CAPI_EXPORTED unsigned
130+
mlirPresburgerIntegerRelationNumVars(MlirPresburgerIntegerRelation relation);
131+
132+
MLIR_CAPI_EXPORTED unsigned
133+
mlirPresburgerIntegerRelationNumCols(MlirPresburgerIntegerRelation relation);
134+
135+
/// Returns the number of equality constraints.
136+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumEqualities(
137+
MlirPresburgerIntegerRelation relation);
138+
139+
/// Returns the number of inequality constraints.
140+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumInequalities(
141+
MlirPresburgerIntegerRelation relation);
142+
143+
MLIR_CAPI_EXPORTED unsigned mlirPresburgerIntegerRelationNumReservedEqualities(
144+
MlirPresburgerIntegerRelation relation);
145+
146+
MLIR_CAPI_EXPORTED unsigned
147+
mlirPresburgerIntegerRelationNumReservedInequalities(
148+
MlirPresburgerIntegerRelation relation);
149+
150+
/// Return the VarKind of the var at the specified position.
151+
MLIR_CAPI_EXPORTED MlirPresburgerVariableKind
152+
mlirPresburgerIntegerRelationGetVarKindAt(unsigned pos);
153+
154+
//===----------------------------------------------------------------------===//
155+
// IntegerRelation Tableau Manipulation
156+
//===----------------------------------------------------------------------===//
157+
/// Adds an equality with the given coefficients.
158+
MLIR_CAPI_EXPORTED void
159+
mlirPresburgerIntegerRelationAddEquality(const int64_t *coefficients,
160+
size_t coefficientsSize);
161+
162+
/// Adds an inequality with the given coefficients.
163+
MLIR_CAPI_EXPORTED void
164+
mlirPresburgerIntegerRelationAddInequality(const int64_t *coefficients,
165+
size_t coefficientsSize);
166+
167+
MLIR_CAPI_EXPORTED void
168+
mlirPresburgerIntegerRelationDump(MlirPresburgerIntegerRelation relation);
169+
#ifdef __cplusplus
170+
}
171+
#endif
172+
#endif // MLIR_C_PRESBURGER_H

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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
static inline MlirPresburgerVariableKind wrap(mlir::presburger::VarKind var) {
22+
return static_cast<MlirPresburgerVariableKind>(var);
23+
}
24+
25+
static inline mlir::presburger::VarKind unwarp(MlirPresburgerVariableKind var) {
26+
return static_cast<mlir::presburger::VarKind>(var);
27+
}
28+
29+
#endif /* MLIR_CAPI_PRESBURGER_H */

0 commit comments

Comments
 (0)