Python bindings: support __floordiv__ for index types

The original reimplementation of EDSC as declarative builders and the
    subsequent rework of Python bindings forbade to use the (true) division
    operator for values of the index types without providing an alternative.  Index
    types only support floor and ceil division through affine maps.  Expose this to
    Python bindings through a `__floordiv__` function on `ValueHandle`s.

--

PiperOrigin-RevId: 241713093
This commit is contained in:
Alex Zinenko 2019-04-03 05:39:02 -07:00 committed by Mehdi Amini
parent 1e021cfbd7
commit 509619829d
2 changed files with 15 additions and 0 deletions

View File

@ -1234,6 +1234,9 @@ PYBIND11_MODULE(pybind, m) {
.def("__truediv__",
[](PythonValueHandle lhs, PythonValueHandle rhs)
-> PythonValueHandle { return lhs.value / rhs.value; })
.def("__floordiv__",
[](PythonValueHandle lhs, PythonValueHandle rhs)
-> PythonValueHandle { return floorDiv(lhs, rhs); })
.def("__mod__",
[](PythonValueHandle lhs, PythonValueHandle rhs)
-> PythonValueHandle { return lhs.value % rhs.value; })

View File

@ -308,6 +308,18 @@ class EdscTest(unittest.TestCase):
self.assertIn("%true_2 = constant 1 : i1", code)
self.assertIn("%8 = subi %true_2, %7 : i1", code)
def testDivisions(self):
with self.module.function_context(
"division", [self.indexType, self.i32Type, self.i32Type], []) as fun:
# indices only support floor division
fun.arg(0) // E.constant_index(42)
# regular values only support regular division
fun.arg(1) / fun.arg(2)
code = str(self.module)
self.assertIn("floordiv 42", code)
self.assertIn("divis %arg1, %arg2 : i32", code)
def testCustom(self):
with self.module.function_context("custom", [self.indexType, self.f32Type],
[]) as fun: