| 1 |
//===- IRAffine.cpp - Exports 'ir' module affine related bindings ---------===// |
| 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 |
#include <utility> |
| 10 |
|
| 11 |
#include "IRModule.h" |
| 12 |
|
| 13 |
#include "PybindUtils.h" |
| 14 |
|
| 15 |
#include "mlir-c/AffineMap.h" |
| 16 |
#include "mlir-c/Bindings/Python/Interop.h" |
| 17 |
#include "mlir-c/IntegerSet.h" |
| 18 |
#include "llvm/ADT/Hashing.h" |
| 19 |
|
| 20 |
namespace py = pybind11; |
| 21 |
using namespace mlir; |
| 22 |
using namespace mlir::python; |
| 23 |
|
| 24 |
using llvm::SmallVector; |
| 25 |
using llvm::StringRef; |
| 26 |
using llvm::Twine; |
| 27 |
|
| 28 |
static const char kDumpDocstring[] = |
| 29 |
R"(Dumps a debug representation of the object to stderr.)"; |
| 30 |
|
| 31 |
/// Attempts to populate `result` with the content of `list` casted to the |
| 32 |
/// appropriate type (Python and C types are provided as template arguments). |
| 33 |
/// Throws errors in case of failure, using "action" to describe what the caller |
| 34 |
/// was attempting to do. |
| 35 |
template <typename PyType, typename CType> |
| 36 |
static void pyListToVector(const py::list &list, |
| 37 |
llvm::SmallVectorImpl<CType> &result, |
| 38 |
StringRef action) { |
| 39 |
result.reserve(py::len(list)); |
| 40 |
for (py::handle item : list) { |
| 41 |
try { |
| 42 |
result.push_back(item.cast<PyType>()); |
| 43 |
} catch (py::cast_error &err) { |
| 44 |
std::string msg = (llvm::Twine("Invalid expression when ") + action + |
| 45 |
" (" + err.what() + ")") |
| 46 |
.str(); |
| 47 |
throw py::cast_error(msg); |
| 48 |
} catch (py::reference_cast_error &err) { |
| 49 |
std::string msg = (llvm::Twine("Invalid expression (None?) when ") + |
| 50 |
action + " (" + err.what() + ")") |
| 51 |
.str(); |
| 52 |
throw py::cast_error(msg); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
template <typename PermutationTy> |
| 58 |
static bool isPermutation(std::vector<PermutationTy> permutation) { |
| 59 |
llvm::SmallVector<bool, 8> seen(permutation.size(), false); |
| 60 |
for (auto val : permutation) { |
| 61 |
if (val < permutation.size()) { |
| 62 |
if (seen[val]) |
| 63 |
return false; |
| 64 |
seen[val] = true; |
| 65 |
continue; |
| 66 |
} |
| 67 |
return false; |
| 68 |
} |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
namespace { |
| 73 |
|
| 74 |
/// CRTP base class for Python MLIR affine expressions that subclass AffineExpr |
| 75 |
/// and should be castable from it. Intermediate hierarchy classes can be |
| 76 |
/// modeled by specifying BaseTy. |
| 77 |
template <typename DerivedTy, typename BaseTy = PyAffineExpr> |
| 78 |
class PyConcreteAffineExpr : public BaseTy { |
| 79 |
public: |
| 80 |
// Derived classes must define statics for: |
| 81 |
// IsAFunctionTy isaFunction |
| 82 |
// const char *pyClassName |
| 83 |
// and redefine bindDerived. |
| 84 |
using ClassTy = py::class_<DerivedTy, BaseTy>; |
| 85 |
using IsAFunctionTy = bool (*)(MlirAffineExpr); |
| 86 |
|
| 87 |
PyConcreteAffineExpr() = default; |
| 88 |
PyConcreteAffineExpr(PyMlirContextRef contextRef, MlirAffineExpr affineExpr) |
| 89 |
: BaseTy(std::move(contextRef), affineExpr) {} |
| 90 |
PyConcreteAffineExpr(PyAffineExpr &orig) |
| 91 |
: PyConcreteAffineExpr(orig.getContext(), castFrom(orig)) {} |
| 92 |
|
| 93 |
static MlirAffineExpr castFrom(PyAffineExpr &orig) { |
| 94 |
if (!DerivedTy::isaFunction(orig)) { |
| 95 |
auto origRepr = py::repr(py::cast(orig)).cast<std::string>(); |
| 96 |
throw SetPyError(PyExc_ValueError, |
| 97 |
Twine("Cannot cast affine expression to ") + |
| 98 |
DerivedTy::pyClassName + " (from " + origRepr + ")"); |
| 99 |
} |
| 100 |
return orig; |
| 101 |
} |
| 102 |
|
| 103 |
static void bind(py::module &m) { |
| 104 |
auto cls = ClassTy(m, DerivedTy::pyClassName, py::module_local()); |
| 105 |
cls.def(py::init<PyAffineExpr &>(), py::arg("expr")); |
| 106 |
cls.def_static( |
| 107 |
"isinstance", |
| 108 |
[](PyAffineExpr &otherAffineExpr) -> bool { |
| 109 |
return DerivedTy::isaFunction(otherAffineExpr); |
| 110 |
}, |
| 111 |
py::arg("other")); |
| 112 |
DerivedTy::bindDerived(cls); |
| 113 |
} |
| 114 |
|
| 115 |
/// Implemented by derived classes to add methods to the Python subclass. |
| 116 |
static void bindDerived(ClassTy &m) {} |
| 117 |
}; |
| 118 |
|
| 119 |
class PyAffineConstantExpr : public PyConcreteAffineExpr<PyAffineConstantExpr> { |
| 120 |
public: |
| 121 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsAConstant; |
| 122 |
static constexpr const char *pyClassName = "AffineConstantExpr"; |
| 123 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 124 |
|
| 125 |
static PyAffineConstantExpr get(intptr_t value, |
| 126 |
DefaultingPyMlirContext context) { |
| 127 |
MlirAffineExpr affineExpr = |
| 128 |
mlirAffineConstantExprGet(context->get(), static_cast<int64_t>(value)); |
| 129 |
return PyAffineConstantExpr(context->getRef(), affineExpr); |
| 130 |
} |
| 131 |
|
| 132 |
static void bindDerived(ClassTy &c) { |
| 133 |
c.def_static("get", &PyAffineConstantExpr::get, py::arg("value"), |
| 134 |
py::arg("context") = py::none()); |
| 135 |
c.def_property_readonly("value", [](PyAffineConstantExpr &self) { |
| 136 |
return mlirAffineConstantExprGetValue(self); |
| 137 |
}); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
class PyAffineDimExpr : public PyConcreteAffineExpr<PyAffineDimExpr> { |
| 142 |
public: |
| 143 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsADim; |
| 144 |
static constexpr const char *pyClassName = "AffineDimExpr"; |
| 145 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 146 |
|
| 147 |
static PyAffineDimExpr get(intptr_t pos, DefaultingPyMlirContext context) { |
| 148 |
MlirAffineExpr affineExpr = mlirAffineDimExprGet(context->get(), pos); |
| 149 |
return PyAffineDimExpr(context->getRef(), affineExpr); |
| 150 |
} |
| 151 |
|
| 152 |
static void bindDerived(ClassTy &c) { |
| 153 |
c.def_static("get", &PyAffineDimExpr::get, py::arg("position"), |
| 154 |
py::arg("context") = py::none()); |
| 155 |
c.def_property_readonly("position", [](PyAffineDimExpr &self) { |
| 156 |
return mlirAffineDimExprGetPosition(self); |
| 157 |
}); |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
class PyAffineSymbolExpr : public PyConcreteAffineExpr<PyAffineSymbolExpr> { |
| 162 |
public: |
| 163 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsASymbol; |
| 164 |
static constexpr const char *pyClassName = "AffineSymbolExpr"; |
| 165 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 166 |
|
| 167 |
static PyAffineSymbolExpr get(intptr_t pos, DefaultingPyMlirContext context) { |
| 168 |
MlirAffineExpr affineExpr = mlirAffineSymbolExprGet(context->get(), pos); |
| 169 |
return PyAffineSymbolExpr(context->getRef(), affineExpr); |
| 170 |
} |
| 171 |
|
| 172 |
static void bindDerived(ClassTy &c) { |
| 173 |
c.def_static("get", &PyAffineSymbolExpr::get, py::arg("position"), |
| 174 |
py::arg("context") = py::none()); |
| 175 |
c.def_property_readonly("position", [](PyAffineSymbolExpr &self) { |
| 176 |
return mlirAffineSymbolExprGetPosition(self); |
| 177 |
}); |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
class PyAffineBinaryExpr : public PyConcreteAffineExpr<PyAffineBinaryExpr> { |
| 182 |
public: |
| 183 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsABinary; |
| 184 |
static constexpr const char *pyClassName = "AffineBinaryExpr"; |
| 185 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 186 |
|
| 187 |
PyAffineExpr lhs() { |
| 188 |
MlirAffineExpr lhsExpr = mlirAffineBinaryOpExprGetLHS(get()); |
| 189 |
return PyAffineExpr(getContext(), lhsExpr); |
| 190 |
} |
| 191 |
|
| 192 |
PyAffineExpr rhs() { |
| 193 |
MlirAffineExpr rhsExpr = mlirAffineBinaryOpExprGetRHS(get()); |
| 194 |
return PyAffineExpr(getContext(), rhsExpr); |
| 195 |
} |
| 196 |
|
| 197 |
static void bindDerived(ClassTy &c) { |
| 198 |
c.def_property_readonly("lhs", &PyAffineBinaryExpr::lhs); |
| 199 |
c.def_property_readonly("rhs", &PyAffineBinaryExpr::rhs); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
class PyAffineAddExpr |
| 204 |
: public PyConcreteAffineExpr<PyAffineAddExpr, PyAffineBinaryExpr> { |
| 205 |
public: |
| 206 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsAAdd; |
| 207 |
static constexpr const char *pyClassName = "AffineAddExpr"; |
| 208 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 209 |
|
| 210 |
static PyAffineAddExpr get(PyAffineExpr lhs, const PyAffineExpr &rhs) { |
| 211 |
MlirAffineExpr expr = mlirAffineAddExprGet(lhs, rhs); |
| 212 |
return PyAffineAddExpr(lhs.getContext(), expr); |
| 213 |
} |
| 214 |
|
| 215 |
static PyAffineAddExpr getRHSConstant(PyAffineExpr lhs, intptr_t rhs) { |
| 216 |
MlirAffineExpr expr = mlirAffineAddExprGet( |
| 217 |
lhs, mlirAffineConstantExprGet(mlirAffineExprGetContext(lhs), rhs)); |
| 218 |
return PyAffineAddExpr(lhs.getContext(), expr); |
| 219 |
} |
| 220 |
|
| 221 |
static PyAffineAddExpr getLHSConstant(intptr_t lhs, PyAffineExpr rhs) { |
| 222 |
MlirAffineExpr expr = mlirAffineAddExprGet( |
| 223 |
mlirAffineConstantExprGet(mlirAffineExprGetContext(rhs), lhs), rhs); |
| 224 |
return PyAffineAddExpr(rhs.getContext(), expr); |
| 225 |
} |
| 226 |
|
| 227 |
static void bindDerived(ClassTy &c) { |
| 228 |
c.def_static("get", &PyAffineAddExpr::get); |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
class PyAffineMulExpr |
| 233 |
: public PyConcreteAffineExpr<PyAffineMulExpr, PyAffineBinaryExpr> { |
| 234 |
public: |
| 235 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsAMul; |
| 236 |
static constexpr const char *pyClassName = "AffineMulExpr"; |
| 237 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 238 |
|
| 239 |
static PyAffineMulExpr get(PyAffineExpr lhs, const PyAffineExpr &rhs) { |
| 240 |
MlirAffineExpr expr = mlirAffineMulExprGet(lhs, rhs); |
| 241 |
return PyAffineMulExpr(lhs.getContext(), expr); |
| 242 |
} |
| 243 |
|
| 244 |
static PyAffineMulExpr getRHSConstant(PyAffineExpr lhs, intptr_t rhs) { |
| 245 |
MlirAffineExpr expr = mlirAffineMulExprGet( |
| 246 |
lhs, mlirAffineConstantExprGet(mlirAffineExprGetContext(lhs), rhs)); |
| 247 |
return PyAffineMulExpr(lhs.getContext(), expr); |
| 248 |
} |
| 249 |
|
| 250 |
static PyAffineMulExpr getLHSConstant(intptr_t lhs, PyAffineExpr rhs) { |
| 251 |
MlirAffineExpr expr = mlirAffineMulExprGet( |
| 252 |
mlirAffineConstantExprGet(mlirAffineExprGetContext(rhs), lhs), rhs); |
| 253 |
return PyAffineMulExpr(rhs.getContext(), expr); |
| 254 |
} |
| 255 |
|
| 256 |
static void bindDerived(ClassTy &c) { |
| 257 |
c.def_static("get", &PyAffineMulExpr::get); |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
class PyAffineModExpr |
| 262 |
: public PyConcreteAffineExpr<PyAffineModExpr, PyAffineBinaryExpr> { |
| 263 |
public: |
| 264 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsAMod; |
| 265 |
static constexpr const char *pyClassName = "AffineModExpr"; |
| 266 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 267 |
|
| 268 |
static PyAffineModExpr get(PyAffineExpr lhs, const PyAffineExpr &rhs) { |
| 269 |
MlirAffineExpr expr = mlirAffineModExprGet(lhs, rhs); |
| 270 |
return PyAffineModExpr(lhs.getContext(), expr); |
| 271 |
} |
| 272 |
|
| 273 |
static PyAffineModExpr getRHSConstant(PyAffineExpr lhs, intptr_t rhs) { |
| 274 |
MlirAffineExpr expr = mlirAffineModExprGet( |
| 275 |
lhs, mlirAffineConstantExprGet(mlirAffineExprGetContext(lhs), rhs)); |
| 276 |
return PyAffineModExpr(lhs.getContext(), expr); |
| 277 |
} |
| 278 |
|
| 279 |
static PyAffineModExpr getLHSConstant(intptr_t lhs, PyAffineExpr rhs) { |
| 280 |
MlirAffineExpr expr = mlirAffineModExprGet( |
| 281 |
mlirAffineConstantExprGet(mlirAffineExprGetContext(rhs), lhs), rhs); |
| 282 |
return PyAffineModExpr(rhs.getContext(), expr); |
| 283 |
} |
| 284 |
|
| 285 |
static void bindDerived(ClassTy &c) { |
| 286 |
c.def_static("get", &PyAffineModExpr::get); |
| 287 |
} |
| 288 |
}; |
| 289 |
|
| 290 |
class PyAffineFloorDivExpr |
| 291 |
: public PyConcreteAffineExpr<PyAffineFloorDivExpr, PyAffineBinaryExpr> { |
| 292 |
public: |
| 293 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsAFloorDiv; |
| 294 |
static constexpr const char *pyClassName = "AffineFloorDivExpr"; |
| 295 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 296 |
|
| 297 |
static PyAffineFloorDivExpr get(PyAffineExpr lhs, const PyAffineExpr &rhs) { |
| 298 |
MlirAffineExpr expr = mlirAffineFloorDivExprGet(lhs, rhs); |
| 299 |
return PyAffineFloorDivExpr(lhs.getContext(), expr); |
| 300 |
} |
| 301 |
|
| 302 |
static PyAffineFloorDivExpr getRHSConstant(PyAffineExpr lhs, intptr_t rhs) { |
| 303 |
MlirAffineExpr expr = mlirAffineFloorDivExprGet( |
| 304 |
lhs, mlirAffineConstantExprGet(mlirAffineExprGetContext(lhs), rhs)); |
| 305 |
return PyAffineFloorDivExpr(lhs.getContext(), expr); |
| 306 |
} |
| 307 |
|
| 308 |
static PyAffineFloorDivExpr getLHSConstant(intptr_t lhs, PyAffineExpr rhs) { |
| 309 |
MlirAffineExpr expr = mlirAffineFloorDivExprGet( |
| 310 |
mlirAffineConstantExprGet(mlirAffineExprGetContext(rhs), lhs), rhs); |
| 311 |
return PyAffineFloorDivExpr(rhs.getContext(), expr); |
| 312 |
} |
| 313 |
|
| 314 |
static void bindDerived(ClassTy &c) { |
| 315 |
c.def_static("get", &PyAffineFloorDivExpr::get); |
| 316 |
} |
| 317 |
}; |
| 318 |
|
| 319 |
class PyAffineCeilDivExpr |
| 320 |
: public PyConcreteAffineExpr<PyAffineCeilDivExpr, PyAffineBinaryExpr> { |
| 321 |
public: |
| 322 |
static constexpr IsAFunctionTy isaFunction = mlirAffineExprIsACeilDiv; |
| 323 |
static constexpr const char *pyClassName = "AffineCeilDivExpr"; |
| 324 |
using PyConcreteAffineExpr::PyConcreteAffineExpr; |
| 325 |
|
| 326 |
static PyAffineCeilDivExpr get(PyAffineExpr lhs, const PyAffineExpr &rhs) { |
| 327 |
MlirAffineExpr expr = mlirAffineCeilDivExprGet(lhs, rhs); |
| 328 |
return PyAffineCeilDivExpr(lhs.getContext(), expr); |
| 329 |
} |
| 330 |
|
| 331 |
static PyAffineCeilDivExpr getRHSConstant(PyAffineExpr lhs, intptr_t rhs) { |
| 332 |
MlirAffineExpr expr = mlirAffineCeilDivExprGet( |
| 333 |
lhs, mlirAffineConstantExprGet(mlirAffineExprGetContext(lhs), rhs)); |
| 334 |
return PyAffineCeilDivExpr(lhs.getContext(), expr); |
| 335 |
} |
| 336 |
|
| 337 |
static PyAffineCeilDivExpr getLHSConstant(intptr_t lhs, PyAffineExpr rhs) { |
| 338 |
MlirAffineExpr expr = mlirAffineCeilDivExprGet( |
| 339 |
mlirAffineConstantExprGet(mlirAffineExprGetContext(rhs), lhs), rhs); |
| 340 |
return PyAffineCeilDivExpr(rhs.getContext(), expr); |
| 341 |
} |
| 342 |
|
| 343 |
static void bindDerived(ClassTy &c) { |
| 344 |
c.def_static("get", &PyAffineCeilDivExpr::get); |
| 345 |
} |
| 346 |
}; |
| 347 |
|
| 348 |
} // namespace |
| 349 |
|
| 350 |
bool PyAffineExpr::operator==(const PyAffineExpr &other) { |
| 351 |
return mlirAffineExprEqual(affineExpr, other.affineExpr); |
| 352 |
} |
| 353 |
|
| 354 |
py::object PyAffineExpr::getCapsule() { |
| 355 |
return py::reinterpret_steal<py::object>( |
| 356 |
mlirPythonAffineExprToCapsule(*this)); |
| 357 |
} |
| 358 |
|
| 359 |
PyAffineExpr PyAffineExpr::createFromCapsule(py::object capsule) { |
| 360 |
MlirAffineExpr rawAffineExpr = mlirPythonCapsuleToAffineExpr(capsule.ptr()); |
| 361 |
if (mlirAffineExprIsNull(rawAffineExpr)) |
| 362 |
throw py::error_already_set(); |
| 363 |
return PyAffineExpr( |
| 364 |
PyMlirContext::forContext(mlirAffineExprGetContext(rawAffineExpr)), |
| 365 |
rawAffineExpr); |
| 366 |
} |
| 367 |
|
| 368 |
//------------------------------------------------------------------------------ |
| 369 |
// PyAffineMap and utilities. |
| 370 |
//------------------------------------------------------------------------------ |
| 371 |
namespace { |
| 372 |
|
| 373 |
/// A list of expressions contained in an affine map. Internally these are |
| 374 |
/// stored as a consecutive array leading to inexpensive random access. Both |
| 375 |
/// the map and the expression are owned by the context so we need not bother |
| 376 |
/// with lifetime extension. |
| 377 |
class PyAffineMapExprList |
| 378 |
: public Sliceable<PyAffineMapExprList, PyAffineExpr> { |
| 379 |
public: |
| 380 |
static constexpr const char *pyClassName = "AffineExprList"; |
| 381 |
|
| 382 |
PyAffineMapExprList(const PyAffineMap &map, intptr_t startIndex = 0, |
| 383 |
intptr_t length = -1, intptr_t step = 1) |
| 384 |
: Sliceable(startIndex, |
| 385 |
length == -1 ? mlirAffineMapGetNumResults(map) : length, |
| 386 |
step), |
| 387 |
affineMap(map) {} |
| 388 |
|
| 389 |
private: |
| 390 |
/// Give the parent CRTP class access to hook implementations below. |
| 391 |
friend class Sliceable<PyAffineMapExprList, PyAffineExpr>; |
| 392 |
|
| 393 |
intptr_t getRawNumElements() { return mlirAffineMapGetNumResults(affineMap); } |
| 394 |
|
| 395 |
PyAffineExpr getRawElement(intptr_t pos) { |
| 396 |
return PyAffineExpr(affineMap.getContext(), |
| 397 |
mlirAffineMapGetResult(affineMap, pos)); |
| 398 |
} |
| 399 |
|
| 400 |
PyAffineMapExprList slice(intptr_t startIndex, intptr_t length, |
| 401 |
intptr_t step) { |
| 402 |
return PyAffineMapExprList(affineMap, startIndex, length, step); |
| 403 |
} |
| 404 |
|
| 405 |
PyAffineMap affineMap; |
| 406 |
}; |
| 407 |
} // namespace |
| 408 |
|
| 409 |
bool PyAffineMap::operator==(const PyAffineMap &other) { |
| 410 |
return mlirAffineMapEqual(affineMap, other.affineMap); |
| 411 |
} |
| 412 |
|
| 413 |
py::object PyAffineMap::getCapsule() { |
| 414 |
return py::reinterpret_steal<py::object>(mlirPythonAffineMapToCapsule(*this)); |
| 415 |
} |
| 416 |
|
| 417 |
PyAffineMap PyAffineMap::createFromCapsule(py::object capsule) { |
| 418 |
MlirAffineMap rawAffineMap = mlirPythonCapsuleToAffineMap(capsule.ptr()); |
| 419 |
if (mlirAffineMapIsNull(rawAffineMap)) |
| 420 |
throw py::error_already_set(); |
| 421 |
return PyAffineMap( |
| 422 |
PyMlirContext::forContext(mlirAffineMapGetContext(rawAffineMap)), |
| 423 |
rawAffineMap); |
| 424 |
} |
| 425 |
|
| 426 |
//------------------------------------------------------------------------------ |
| 427 |
// PyIntegerSet and utilities. |
| 428 |
//------------------------------------------------------------------------------ |
| 429 |
namespace { |
| 430 |
|
| 431 |
class PyIntegerSetConstraint { |
| 432 |
public: |
| 433 |
PyIntegerSetConstraint(PyIntegerSet set, intptr_t pos) |
| 434 |
: set(std::move(set)), pos(pos) {} |
| 435 |
|
| 436 |
PyAffineExpr getExpr() { |
| 437 |
return PyAffineExpr(set.getContext(), |
| 438 |
mlirIntegerSetGetConstraint(set, pos)); |
| 439 |
} |
| 440 |
|
| 441 |
bool isEq() { return mlirIntegerSetIsConstraintEq(set, pos); } |
| 442 |
|
| 443 |
static void bind(py::module &m) { |
| 444 |
py::class_<PyIntegerSetConstraint>(m, "IntegerSetConstraint", |
| 445 |
py::module_local()) |
| 446 |
.def_property_readonly("expr", &PyIntegerSetConstraint::getExpr) |
| 447 |
.def_property_readonly("is_eq", &PyIntegerSetConstraint::isEq); |
| 448 |
} |
| 449 |
|
| 450 |
private: |
| 451 |
PyIntegerSet set; |
| 452 |
intptr_t pos; |
| 453 |
}; |
| 454 |
|
| 455 |
class PyIntegerSetConstraintList |
| 456 |
: public Sliceable<PyIntegerSetConstraintList, PyIntegerSetConstraint> { |
| 457 |
public: |
| 458 |
static constexpr const char *pyClassName = "IntegerSetConstraintList"; |
| 459 |
|
| 460 |
PyIntegerSetConstraintList(const PyIntegerSet &set, intptr_t startIndex = 0, |
| 461 |
intptr_t length = -1, intptr_t step = 1) |
| 462 |
: Sliceable(startIndex, |
| 463 |
length == -1 ? mlirIntegerSetGetNumConstraints(set) : length, |
| 464 |
step), |
| 465 |
set(set) {} |
| 466 |
|
| 467 |
private: |
| 468 |
/// Give the parent CRTP class access to hook implementations below. |
| 469 |
friend class Sliceable<PyIntegerSetConstraintList, PyIntegerSetConstraint>; |
| 470 |
|
| 471 |
intptr_t getRawNumElements() { return mlirIntegerSetGetNumConstraints(set); } |
| 472 |
|
| 473 |
PyIntegerSetConstraint getRawElement(intptr_t pos) { |
| 474 |
return PyIntegerSetConstraint(set, pos); |
| 475 |
} |
| 476 |
|
| 477 |
PyIntegerSetConstraintList slice(intptr_t startIndex, intptr_t length, |
| 478 |
intptr_t step) { |
| 479 |
return PyIntegerSetConstraintList(set, startIndex, length, step); |
| 480 |
} |
| 481 |
|
| 482 |
PyIntegerSet set; |
| 483 |
}; |
| 484 |
} // namespace |
| 485 |
|
| 486 |
bool PyIntegerSet::operator==(const PyIntegerSet &other) { |
| 487 |
return mlirIntegerSetEqual(integerSet, other.integerSet); |
| 488 |
} |
| 489 |
|
| 490 |
py::object PyIntegerSet::getCapsule() { |
| 491 |
return py::reinterpret_steal<py::object>( |
| 492 |
mlirPythonIntegerSetToCapsule(*this)); |
| 493 |
} |
| 494 |
|
| 495 |
PyIntegerSet PyIntegerSet::createFromCapsule(py::object capsule) { |
| 496 |
MlirIntegerSet rawIntegerSet = mlirPythonCapsuleToIntegerSet(capsule.ptr()); |
| 497 |
if (mlirIntegerSetIsNull(rawIntegerSet)) |
| 498 |
throw py::error_already_set(); |
| 499 |
return PyIntegerSet( |
| 500 |
PyMlirContext::forContext(mlirIntegerSetGetContext(rawIntegerSet)), |
| 501 |
rawIntegerSet); |
| 502 |
} |
| 503 |
|
| 504 |
void mlir::python::populateIRAffine(py::module &m) { |
| 505 |
//---------------------------------------------------------------------------- |
| 506 |
// Mapping of PyAffineExpr and derived classes. |
| 507 |
//---------------------------------------------------------------------------- |
| 508 |
py::class_<PyAffineExpr>(m, "AffineExpr", py::module_local()) |
| 509 |
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, |
| 510 |
&PyAffineExpr::getCapsule) |
| 511 |
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyAffineExpr::createFromCapsule) |
| 512 |
.def("__add__", &PyAffineAddExpr::get) |
| 513 |
.def("__add__", &PyAffineAddExpr::getRHSConstant) |
| 514 |
.def("__radd__", &PyAffineAddExpr::getRHSConstant) |
| 515 |
.def("__mul__", &PyAffineMulExpr::get) |
| 516 |
.def("__mul__", &PyAffineMulExpr::getRHSConstant) |
| 517 |
.def("__rmul__", &PyAffineMulExpr::getRHSConstant) |
| 518 |
.def("__mod__", &PyAffineModExpr::get) |
| 519 |
.def("__mod__", &PyAffineModExpr::getRHSConstant) |
| 520 |
.def("__rmod__", |
| 521 |
[](PyAffineExpr &self, intptr_t other) { |
| 522 |
return PyAffineModExpr::get( |
| 523 |
PyAffineConstantExpr::get(other, *self.getContext().get()), |
| 524 |
self); |
| 525 |
}) |
| 526 |
.def("__sub__", |
| 527 |
[](PyAffineExpr &self, PyAffineExpr &other) { |
| 528 |
auto negOne = |
| 529 |
PyAffineConstantExpr::get(-1, *self.getContext().get()); |
| 530 |
return PyAffineAddExpr::get(self, |
| 531 |
PyAffineMulExpr::get(negOne, other)); |
| 532 |
}) |
| 533 |
.def("__sub__", |
| 534 |
[](PyAffineExpr &self, intptr_t other) { |
| 535 |
return PyAffineAddExpr::get( |
| 536 |
self, |
| 537 |
PyAffineConstantExpr::get(-other, *self.getContext().get())); |
| 538 |
}) |
| 539 |
.def("__rsub__", |
| 540 |
[](PyAffineExpr &self, intptr_t other) { |
| 541 |
return PyAffineAddExpr::getLHSConstant( |
| 542 |
other, PyAffineMulExpr::getLHSConstant(-1, self)); |
| 543 |
}) |
| 544 |
.def("__eq__", [](PyAffineExpr &self, |
| 545 |
PyAffineExpr &other) { return self == other; }) |
| 546 |
.def("__eq__", |
| 547 |
[](PyAffineExpr &self, py::object &other) { return false; }) |
| 548 |
.def("__str__", |
| 549 |
[](PyAffineExpr &self) { |
| 550 |
PyPrintAccumulator printAccum; |
| 551 |
mlirAffineExprPrint(self, printAccum.getCallback(), |
| 552 |
printAccum.getUserData()); |
| 553 |
return printAccum.join(); |
| 554 |
}) |
| 555 |
.def("__repr__", |
| 556 |
[](PyAffineExpr &self) { |
| 557 |
PyPrintAccumulator printAccum; |
| 558 |
printAccum.parts.append("AffineExpr("); |
| 559 |
mlirAffineExprPrint(self, printAccum.getCallback(), |
| 560 |
printAccum.getUserData()); |
| 561 |
printAccum.parts.append(")"); |
| 562 |
return printAccum.join(); |
| 563 |
}) |
| 564 |
.def("__hash__", |
| 565 |
[](PyAffineExpr &self) { |
| 566 |
return static_cast<size_t>(llvm::hash_value(self.get().ptr)); |
| 567 |
}) |
| 568 |
.def_property_readonly( |
| 569 |
"context", |
| 570 |
[](PyAffineExpr &self) { return self.getContext().getObject(); }) |
| 571 |
.def("compose", |
| 572 |
[](PyAffineExpr &self, PyAffineMap &other) { |
| 573 |
return PyAffineExpr(self.getContext(), |
| 574 |
mlirAffineExprCompose(self, other)); |
| 575 |
}) |
| 576 |
.def_static( |
| 577 |
"get_add", &PyAffineAddExpr::get, |
| 578 |
"Gets an affine expression containing a sum of two expressions.") |
| 579 |
.def_static("get_add", &PyAffineAddExpr::getLHSConstant, |
| 580 |
"Gets an affine expression containing a sum of a constant " |
| 581 |
"and another expression.") |
| 582 |
.def_static("get_add", &PyAffineAddExpr::getRHSConstant, |
| 583 |
"Gets an affine expression containing a sum of an expression " |
| 584 |
"and a constant.") |
| 585 |
.def_static( |
| 586 |
"get_mul", &PyAffineMulExpr::get, |
| 587 |
"Gets an affine expression containing a product of two expressions.") |
| 588 |
.def_static("get_mul", &PyAffineMulExpr::getLHSConstant, |
| 589 |
"Gets an affine expression containing a product of a " |
| 590 |
"constant and another expression.") |
| 591 |
.def_static("get_mul", &PyAffineMulExpr::getRHSConstant, |
| 592 |
"Gets an affine expression containing a product of an " |
| 593 |
"expression and a constant.") |
| 594 |
.def_static("get_mod", &PyAffineModExpr::get, |
| 595 |
"Gets an affine expression containing the modulo of dividing " |
| 596 |
"one expression by another.") |
| 597 |
.def_static("get_mod", &PyAffineModExpr::getLHSConstant, |
| 598 |
"Gets a semi-affine expression containing the modulo of " |
| 599 |
"dividing a constant by an expression.") |
| 600 |
.def_static("get_mod", &PyAffineModExpr::getRHSConstant, |
| 601 |
"Gets an affine expression containing the module of dividing" |
| 602 |
"an expression by a constant.") |
| 603 |
.def_static("get_floor_div", &PyAffineFloorDivExpr::get, |
| 604 |
"Gets an affine expression containing the rounded-down " |
| 605 |
"result of dividing one expression by another.") |
| 606 |
.def_static("get_floor_div", &PyAffineFloorDivExpr::getLHSConstant, |
| 607 |
"Gets a semi-affine expression containing the rounded-down " |
| 608 |
"result of dividing a constant by an expression.") |
| 609 |
.def_static("get_floor_div", &PyAffineFloorDivExpr::getRHSConstant, |
| 610 |
"Gets an affine expression containing the rounded-down " |
| 611 |
"result of dividing an expression by a constant.") |
| 612 |
.def_static("get_ceil_div", &PyAffineCeilDivExpr::get, |
| 613 |
"Gets an affine expression containing the rounded-up result " |
| 614 |
"of dividing one expression by another.") |
| 615 |
.def_static("get_ceil_div", &PyAffineCeilDivExpr::getLHSConstant, |
| 616 |
"Gets a semi-affine expression containing the rounded-up " |
| 617 |
"result of dividing a constant by an expression.") |
| 618 |
.def_static("get_ceil_div", &PyAffineCeilDivExpr::getRHSConstant, |
| 619 |
"Gets an affine expression containing the rounded-up result " |
| 620 |
"of dividing an expression by a constant.") |
| 621 |
.def_static("get_constant", &PyAffineConstantExpr::get, py::arg("value"), |
| 622 |
py::arg("context") = py::none(), |
| 623 |
"Gets a constant affine expression with the given value.") |
| 624 |
.def_static( |
| 625 |
"get_dim", &PyAffineDimExpr::get, py::arg("position"), |
| 626 |
py::arg("context") = py::none(), |
| 627 |
"Gets an affine expression of a dimension at the given position.") |
| 628 |
.def_static( |
| 629 |
"get_symbol", &PyAffineSymbolExpr::get, py::arg("position"), |
| 630 |
py::arg("context") = py::none(), |
| 631 |
"Gets an affine expression of a symbol at the given position.") |
| 632 |
.def( |
| 633 |
"dump", [](PyAffineExpr &self) { mlirAffineExprDump(self); }, |
| 634 |
kDumpDocstring); |
| 635 |
PyAffineConstantExpr::bind(m); |
| 636 |
PyAffineDimExpr::bind(m); |
| 637 |
PyAffineSymbolExpr::bind(m); |
| 638 |
PyAffineBinaryExpr::bind(m); |
| 639 |
PyAffineAddExpr::bind(m); |
| 640 |
PyAffineMulExpr::bind(m); |
| 641 |
PyAffineModExpr::bind(m); |
| 642 |
PyAffineFloorDivExpr::bind(m); |
| 643 |
PyAffineCeilDivExpr::bind(m); |
| 644 |
|
| 645 |
//---------------------------------------------------------------------------- |
| 646 |
// Mapping of PyAffineMap. |
| 647 |
//---------------------------------------------------------------------------- |
| 648 |
py::class_<PyAffineMap>(m, "AffineMap", py::module_local()) |
| 649 |
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, |
| 650 |
&PyAffineMap::getCapsule) |
| 651 |
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyAffineMap::createFromCapsule) |
| 652 |
.def("__eq__", |
| 653 |
[](PyAffineMap &self, PyAffineMap &other) { return self == other; }) |
| 654 |
.def("__eq__", [](PyAffineMap &self, py::object &other) { return false; }) |
| 655 |
.def("__str__", |
| 656 |
[](PyAffineMap &self) { |
| 657 |
PyPrintAccumulator printAccum; |
| 658 |
mlirAffineMapPrint(self, printAccum.getCallback(), |
| 659 |
printAccum.getUserData()); |
| 660 |
return printAccum.join(); |
| 661 |
}) |
| 662 |
.def("__repr__", |
| 663 |
[](PyAffineMap &self) { |
| 664 |
PyPrintAccumulator printAccum; |
| 665 |
printAccum.parts.append("AffineMap("); |
| 666 |
mlirAffineMapPrint(self, printAccum.getCallback(), |
| 667 |
printAccum.getUserData()); |
| 668 |
printAccum.parts.append(")"); |
| 669 |
return printAccum.join(); |
| 670 |
}) |
| 671 |
.def("__hash__", |
| 672 |
[](PyAffineMap &self) { |
| 673 |
return static_cast<size_t>(llvm::hash_value(self.get().ptr)); |
| 674 |
}) |
| 675 |
.def_static("compress_unused_symbols", |
| 676 |
[](py::list affineMaps, DefaultingPyMlirContext context) { |
| 677 |
SmallVector<MlirAffineMap> maps; |
| 678 |
pyListToVector<PyAffineMap, MlirAffineMap>( |
| 679 |
affineMaps, maps, "attempting to create an AffineMap"); |
| 680 |
std::vector<MlirAffineMap> compressed(affineMaps.size()); |
| 681 |
auto populate = [](void *result, intptr_t idx, |
| 682 |
MlirAffineMap m) { |
| 683 |
static_cast<MlirAffineMap *>(result)[idx] = (m); |
| 684 |
}; |
| 685 |
mlirAffineMapCompressUnusedSymbols( |
| 686 |
maps.data(), maps.size(), compressed.data(), populate); |
| 687 |
std::vector<PyAffineMap> res; |
| 688 |
res.reserve(compressed.size()); |
| 689 |
for (auto m : compressed) |
| 690 |
res.emplace_back(context->getRef(), m); |
| 691 |
return res; |
| 692 |
}) |
| 693 |
.def_property_readonly( |
| 694 |
"context", |
| 695 |
[](PyAffineMap &self) { return self.getContext().getObject(); }, |
| 696 |
"Context that owns the Affine Map") |
| 697 |
.def( |
| 698 |
"dump", [](PyAffineMap &self) { mlirAffineMapDump(self); }, |
| 699 |
kDumpDocstring) |
| 700 |
.def_static( |
| 701 |
"get", |
| 702 |
[](intptr_t dimCount, intptr_t symbolCount, py::list exprs, |
| 703 |
DefaultingPyMlirContext context) { |
| 704 |
SmallVector<MlirAffineExpr> affineExprs; |
| 705 |
pyListToVector<PyAffineExpr, MlirAffineExpr>( |
| 706 |
exprs, affineExprs, "attempting to create an AffineMap"); |
| 707 |
MlirAffineMap map = |
| 708 |
mlirAffineMapGet(context->get(), dimCount, symbolCount, |
| 709 |
affineExprs.size(), affineExprs.data()); |
| 710 |
return PyAffineMap(context->getRef(), map); |
| 711 |
}, |
| 712 |
py::arg("dim_count"), py::arg("symbol_count"), py::arg("exprs"), |
| 713 |
py::arg("context") = py::none(), |
| 714 |
"Gets a map with the given expressions as results.") |
| 715 |
.def_static( |
| 716 |
"get_constant", |
| 717 |
[](intptr_t value, DefaultingPyMlirContext context) { |
| 718 |
MlirAffineMap affineMap = |
| 719 |
mlirAffineMapConstantGet(context->get(), value); |
| 720 |
return PyAffineMap(context->getRef(), affineMap); |
| 721 |
}, |
| 722 |
py::arg("value"), py::arg("context") = py::none(), |
| 723 |
"Gets an affine map with a single constant result") |
| 724 |
.def_static( |
| 725 |
"get_empty", |
| 726 |
[](DefaultingPyMlirContext context) { |
| 727 |
MlirAffineMap affineMap = mlirAffineMapEmptyGet(context->get()); |
| 728 |
return PyAffineMap(context->getRef(), affineMap); |
| 729 |
}, |
| 730 |
py::arg("context") = py::none(), "Gets an empty affine map.") |
| 731 |
.def_static( |
| 732 |
"get_identity", |
| 733 |
[](intptr_t nDims, DefaultingPyMlirContext context) { |
| 734 |
MlirAffineMap affineMap = |
| 735 |
mlirAffineMapMultiDimIdentityGet(context->get(), nDims); |
| 736 |
return PyAffineMap(context->getRef(), affineMap); |
| 737 |
}, |
| 738 |
py::arg("n_dims"), py::arg("context") = py::none(), |
| 739 |
"Gets an identity map with the given number of dimensions.") |
| 740 |
.def_static( |
| 741 |
"get_minor_identity", |
| 742 |
[](intptr_t nDims, intptr_t nResults, |
| 743 |
DefaultingPyMlirContext context) { |
| 744 |
MlirAffineMap affineMap = |
| 745 |
mlirAffineMapMinorIdentityGet(context->get(), nDims, nResults); |
| 746 |
return PyAffineMap(context->getRef(), affineMap); |
| 747 |
}, |
| 748 |
py::arg("n_dims"), py::arg("n_results"), |
| 749 |
py::arg("context") = py::none(), |
| 750 |
"Gets a minor identity map with the given number of dimensions and " |
| 751 |
"results.") |
| 752 |
.def_static( |
| 753 |
"get_permutation", |
| 754 |
[](std::vector<unsigned> permutation, |
| 755 |
DefaultingPyMlirContext context) { |
| 756 |
if (!isPermutation(permutation)) |
| 757 |
throw py::cast_error("Invalid permutation when attempting to " |
| 758 |
"create an AffineMap"); |
| 759 |
MlirAffineMap affineMap = mlirAffineMapPermutationGet( |
| 760 |
context->get(), permutation.size(), permutation.data()); |
| 761 |
return PyAffineMap(context->getRef(), affineMap); |
| 762 |
}, |
| 763 |
py::arg("permutation"), py::arg("context") = py::none(), |
| 764 |
"Gets an affine map that permutes its inputs.") |
| 765 |
.def( |
| 766 |
"get_submap", |
| 767 |
[](PyAffineMap &self, std::vector<intptr_t> &resultPos) { |
| 768 |
intptr_t numResults = mlirAffineMapGetNumResults(self); |
| 769 |
for (intptr_t pos : resultPos) { |
| 770 |
if (pos < 0 || pos >= numResults) |
| 771 |
throw py::value_error("result position out of bounds"); |
| 772 |
} |
| 773 |
MlirAffineMap affineMap = mlirAffineMapGetSubMap( |
| 774 |
self, resultPos.size(), resultPos.data()); |
| 775 |
return PyAffineMap(self.getContext(), affineMap); |
| 776 |
}, |
| 777 |
py::arg("result_positions")) |
| 778 |
.def( |
| 779 |
"get_major_submap", |
| 780 |
[](PyAffineMap &self, intptr_t nResults) { |
| 781 |
if (nResults >= mlirAffineMapGetNumResults(self)) |
| 782 |
throw py::value_error("number of results out of bounds"); |
| 783 |
MlirAffineMap affineMap = |
| 784 |
mlirAffineMapGetMajorSubMap(self, nResults); |
| 785 |
return PyAffineMap(self.getContext(), affineMap); |
| 786 |
}, |
| 787 |
py::arg("n_results")) |
| 788 |
.def( |
| 789 |
"get_minor_submap", |
| 790 |
[](PyAffineMap &self, intptr_t nResults) { |
| 791 |
if (nResults >= mlirAffineMapGetNumResults(self)) |
| 792 |
throw py::value_error("number of results out of bounds"); |
| 793 |
MlirAffineMap affineMap = |
| 794 |
mlirAffineMapGetMinorSubMap(self, nResults); |
| 795 |
return PyAffineMap(self.getContext(), affineMap); |
| 796 |
}, |
| 797 |
py::arg("n_results")) |
| 798 |
.def( |
| 799 |
"replace", |
| 800 |
[](PyAffineMap &self, PyAffineExpr &expression, |
| 801 |
PyAffineExpr &replacement, intptr_t numResultDims, |
| 802 |
intptr_t numResultSyms) { |
| 803 |
MlirAffineMap affineMap = mlirAffineMapReplace( |
| 804 |
self, expression, replacement, numResultDims, numResultSyms); |
| 805 |
return PyAffineMap(self.getContext(), affineMap); |
| 806 |
}, |
| 807 |
py::arg("expr"), py::arg("replacement"), py::arg("n_result_dims"), |
| 808 |
py::arg("n_result_syms")) |
| 809 |
.def_property_readonly( |
| 810 |
"is_permutation", |
| 811 |
[](PyAffineMap &self) { return mlirAffineMapIsPermutation(self); }) |
| 812 |
.def_property_readonly("is_projected_permutation", |
| 813 |
[](PyAffineMap &self) { |
| 814 |
return mlirAffineMapIsProjectedPermutation(self); |
| 815 |
}) |
| 816 |
.def_property_readonly( |
| 817 |
"n_dims", |
| 818 |
[](PyAffineMap &self) { return mlirAffineMapGetNumDims(self); }) |
| 819 |
.def_property_readonly( |
| 820 |
"n_inputs", |
| 821 |
[](PyAffineMap &self) { return mlirAffineMapGetNumInputs(self); }) |
| 822 |
.def_property_readonly( |
| 823 |
"n_symbols", |
| 824 |
[](PyAffineMap &self) { return mlirAffineMapGetNumSymbols(self); }) |
| 825 |
.def_property_readonly("results", [](PyAffineMap &self) { |
| 826 |
return PyAffineMapExprList(self); |
| 827 |
}); |
| 828 |
PyAffineMapExprList::bind(m); |
| 829 |
|
| 830 |
//---------------------------------------------------------------------------- |
| 831 |
// Mapping of PyIntegerSet. |
| 832 |
//---------------------------------------------------------------------------- |
| 833 |
py::class_<PyIntegerSet>(m, "IntegerSet", py::module_local()) |
| 834 |
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, |
| 835 |
&PyIntegerSet::getCapsule) |
| 836 |
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyIntegerSet::createFromCapsule) |
| 837 |
.def("__eq__", [](PyIntegerSet &self, |
| 838 |
PyIntegerSet &other) { return self == other; }) |
| 839 |
.def("__eq__", [](PyIntegerSet &self, py::object other) { return false; }) |
| 840 |
.def("__str__", |
| 841 |
[](PyIntegerSet &self) { |
| 842 |
PyPrintAccumulator printAccum; |
| 843 |
mlirIntegerSetPrint(self, printAccum.getCallback(), |
| 844 |
printAccum.getUserData()); |
| 845 |
return printAccum.join(); |
| 846 |
}) |
| 847 |
.def("__repr__", |
| 848 |
[](PyIntegerSet &self) { |
| 849 |
PyPrintAccumulator printAccum; |
| 850 |
printAccum.parts.append("IntegerSet("); |
| 851 |
mlirIntegerSetPrint(self, printAccum.getCallback(), |
| 852 |
printAccum.getUserData()); |
| 853 |
printAccum.parts.append(")"); |
| 854 |
return printAccum.join(); |
| 855 |
}) |
| 856 |
.def("__hash__", |
| 857 |
[](PyIntegerSet &self) { |
| 858 |
return static_cast<size_t>(llvm::hash_value(self.get().ptr)); |
| 859 |
}) |
| 860 |
.def_property_readonly( |
| 861 |
"context", |
| 862 |
[](PyIntegerSet &self) { return self.getContext().getObject(); }) |
| 863 |
.def( |
| 864 |
"dump", [](PyIntegerSet &self) { mlirIntegerSetDump(self); }, |
| 865 |
kDumpDocstring) |
| 866 |
.def_static( |
| 867 |
"get", |
| 868 |
[](intptr_t numDims, intptr_t numSymbols, py::list exprs, |
| 869 |
std::vector<bool> eqFlags, DefaultingPyMlirContext context) { |
| 870 |
if (exprs.size() != eqFlags.size()) |
| 871 |
throw py::value_error( |
| 872 |
"Expected the number of constraints to match " |
| 873 |
"that of equality flags"); |
| 874 |
if (exprs.empty()) |
| 875 |
throw py::value_error("Expected non-empty list of constraints"); |
| 876 |
|
| 877 |
// Copy over to a SmallVector because std::vector has a |
| 878 |
// specialization for booleans that packs data and does not |
| 879 |
// expose a `bool *`. |
| 880 |
SmallVector<bool, 8> flags(eqFlags.begin(), eqFlags.end()); |
| 881 |
|
| 882 |
SmallVector<MlirAffineExpr> affineExprs; |
| 883 |
pyListToVector<PyAffineExpr>(exprs, affineExprs, |
| 884 |
"attempting to create an IntegerSet"); |
| 885 |
MlirIntegerSet set = mlirIntegerSetGet( |
| 886 |
context->get(), numDims, numSymbols, exprs.size(), |
| 887 |
affineExprs.data(), flags.data()); |
| 888 |
return PyIntegerSet(context->getRef(), set); |
| 889 |
}, |
| 890 |
py::arg("num_dims"), py::arg("num_symbols"), py::arg("exprs"), |
| 891 |
py::arg("eq_flags"), py::arg("context") = py::none()) |
| 892 |
.def_static( |
| 893 |
"get_empty", |
| 894 |
[](intptr_t numDims, intptr_t numSymbols, |
| 895 |
DefaultingPyMlirContext context) { |
| 896 |
MlirIntegerSet set = |
| 897 |
mlirIntegerSetEmptyGet(context->get(), numDims, numSymbols); |
| 898 |
return PyIntegerSet(context->getRef(), set); |
| 899 |
}, |
| 900 |
py::arg("num_dims"), py::arg("num_symbols"), |
| 901 |
py::arg("context") = py::none()) |
| 902 |
.def( |
| 903 |
"get_replaced", |
| 904 |
[](PyIntegerSet &self, py::list dimExprs, py::list symbolExprs, |
| 905 |
intptr_t numResultDims, intptr_t numResultSymbols) { |
| 906 |
if (static_cast<intptr_t>(dimExprs.size()) != |
| 907 |
mlirIntegerSetGetNumDims(self)) |
| 908 |
throw py::value_error( |
| 909 |
"Expected the number of dimension replacement expressions " |
| 910 |
"to match that of dimensions"); |
| 911 |
if (static_cast<intptr_t>(symbolExprs.size()) != |
| 912 |
mlirIntegerSetGetNumSymbols(self)) |
| 913 |
throw py::value_error( |
| 914 |
"Expected the number of symbol replacement expressions " |
| 915 |
"to match that of symbols"); |
| 916 |
|
| 917 |
SmallVector<MlirAffineExpr> dimAffineExprs, symbolAffineExprs; |
| 918 |
pyListToVector<PyAffineExpr>( |
| 919 |
dimExprs, dimAffineExprs, |
| 920 |
"attempting to create an IntegerSet by replacing dimensions"); |
| 921 |
pyListToVector<PyAffineExpr>( |
| 922 |
symbolExprs, symbolAffineExprs, |
| 923 |
"attempting to create an IntegerSet by replacing symbols"); |
| 924 |
MlirIntegerSet set = mlirIntegerSetReplaceGet( |
| 925 |
self, dimAffineExprs.data(), symbolAffineExprs.data(), |
| 926 |
numResultDims, numResultSymbols); |
| 927 |
return PyIntegerSet(self.getContext(), set); |
| 928 |
}, |
| 929 |
py::arg("dim_exprs"), py::arg("symbol_exprs"), |
| 930 |
py::arg("num_result_dims"), py::arg("num_result_symbols")) |
| 931 |
.def_property_readonly("is_canonical_empty", |
| 932 |
[](PyIntegerSet &self) { |
| 933 |
return mlirIntegerSetIsCanonicalEmpty(self); |
| 934 |
}) |
| 935 |
.def_property_readonly( |
| 936 |
"n_dims", |
| 937 |
[](PyIntegerSet &self) { return mlirIntegerSetGetNumDims(self); }) |
| 938 |
.def_property_readonly( |
| 939 |
"n_symbols", |
| 940 |
[](PyIntegerSet &self) { return mlirIntegerSetGetNumSymbols(self); }) |
| 941 |
.def_property_readonly( |
| 942 |
"n_inputs", |
| 943 |
[](PyIntegerSet &self) { return mlirIntegerSetGetNumInputs(self); }) |
| 944 |
.def_property_readonly("n_equalities", |
| 945 |
[](PyIntegerSet &self) { |
| 946 |
return mlirIntegerSetGetNumEqualities(self); |
| 947 |
}) |
| 948 |
.def_property_readonly("n_inequalities", |
| 949 |
[](PyIntegerSet &self) { |
| 950 |
return mlirIntegerSetGetNumInequalities(self); |
| 951 |
}) |
| 952 |
.def_property_readonly("constraints", [](PyIntegerSet &self) { |
| 953 |
return PyIntegerSetConstraintList(self); |
| 954 |
}); |
| 955 |
PyIntegerSetConstraint::bind(m); |
| 956 |
PyIntegerSetConstraintList::bind(m); |
| 957 |
} |