Skip to content

Commit 1a4649c

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

File tree

11 files changed

+682
-0
lines changed

11 files changed

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