This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][python bindings] implement `PyValue` subclassing to enable operator overloading
ClosedPublic

Authored by makslevental on Apr 6 2023, 7:29 PM.

Details

Summary

Refactor python bindings to enable subclassing values and overloading operators on both python and cpp side. The test shows one use-case (tensor wrapper indexing maps to tensor.extract).

There are two ways to make this work (both on display in the sequence of commits);

  1. Move PyConcreteValue to a public header(https://reviews.llvm.org/D147758?vs=on&id=511599#toc)
  2. (Current) Add mlir_value_subclass but also add a python side constructor to py::class_<PyValue>

I can't comment on the relative merits/tradeoffs; first one "breaks symmetry" with mlir_type_subclass and mlir_attribute_subclass, while second potentially does a bad thing in adding a constructor to py::class_<PyValue> where I believe the intent is to not have one.

Note in the diff demonstrating the first approach there are some stray PYBIND11_EXPORTs (i.e., they are not necessary).

Diff Detail

Event Timeline

makslevental created this revision.Apr 6 2023, 7:29 PM
Herald added a project: Restricted Project. · View Herald Transcript
makslevental requested review of this revision.Apr 6 2023, 7:29 PM
makslevental retitled this revision from [MLIR][DRAFT] WIP refactor python bindings to [MLIR][DRAFT] refactor python bindings.Apr 6 2023, 7:30 PM

cleaner approach

makslevental retitled this revision from [MLIR][DRAFT] refactor python bindings to [MLIR][python bindings] implement `PyValue` subclassing to enable operator overloading.Apr 6 2023, 10:17 PM
makslevental edited the summary of this revision. (Show Details)

update cmake

makslevental added inline comments.Apr 6 2023, 10:28 PM
mlir/lib/Bindings/Python/IRCore.cpp
3263

I'll be honest I'm not 100% sure why this works (cf the Value(t) in the test) since there is no single argument constructor for PyValue. But it does indeed pass on in all my envs.

makslevental edited the summary of this revision. (Show Details)Apr 7 2023, 8:29 AM
makslevental edited the summary of this revision. (Show Details)Apr 7 2023, 8:48 AM
makslevental edited the summary of this revision. (Show Details)

Using pure_subclass is the right way, the PyConcrete* doesn't scale properly to out-of-tree projects. We didn't need it for values because value kinds are not user-extensible, there are only block arguments and operation results. That being said, I see the value of letting Python subclass those similarly to how C++ now provides TypedValue wrappers.

Why does this need to move PybindUtils.h? It feels like an implementation detail that may not be reusable.

mlir/lib/Bindings/Python/IRCore.cpp
3263

Implicit copy constructor?

mlir/test/python/dialects/python_test.py
306–344

I'd rather keep this as a separate test for "extensions", I don't think we are ready for IR-building operator overloads being perceived as part of main bindings.

ftynse added a reviewer: jpienaar.

undo PybindUtils.h move

makslevental added inline comments.Apr 11 2023, 11:06 AM
mlir/test/python/lib/PythonTestModule.cpp
45

just a small (trivial) illustration for how the subclassing can be used to implement something on the cpp side as well.

ftynse accepted this revision.Apr 14 2023, 2:39 AM
ftynse added inline comments.
mlir/test/python/dialects/python_test.py
333

Nit: please add the newline.

This revision is now accepted and ready to land.Apr 14 2023, 2:39 AM
makslevental marked an inline comment as done.

fix nits

makslevental marked an inline comment as done.Apr 14 2023, 11:13 AM