This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Move PyConcreteType to header. NFC.
ClosedPublic

Authored by jdd on Apr 28 2021, 3:40 PM.

Details

Summary

This allows out-of-tree users to derive PyConcreteType to bind custom
types.

The Type version of https://reviews.llvm.org/D101063/new/

Diff Detail

Event Timeline

jdd created this revision.Apr 28 2021, 3:40 PM
jdd requested review of this revision.Apr 28 2021, 3:40 PM
jdd edited the summary of this revision. (Show Details)Apr 28 2021, 3:41 PM
jdd added reviewers: stellaraccident, mikeurbach.
stellaraccident accepted this revision.Apr 28 2021, 4:13 PM

Works for me, but I will make the PSA that by expanding the public API in this way, we are heading in to really deep waters when it comes to linkage issues and bugs. We can run this forward a bit and see, but know that actually shipping a shared mlir python package in combination with other projects has a lot of sharp edges that will need resolution at some point.

This revision is now accepted and ready to land.Apr 28 2021, 4:13 PM

PSA noted. When I mentioned this on discourse, it seemed like this and the previous change for attributes were expansions we could get some mileage out of without totally going off the deep end. Are there immediate linkage concerns with deriving PyConcreteType out of tree, or just the general concerns we have been discussing?

This revision was landed with ongoing or failed builds.Apr 28 2021, 4:46 PM
This revision was automatically updated to reflect the committed changes.
jdd added a comment.Apr 28 2021, 4:49 PM

Thanks @stellaraccident. So I assume you'd be opposed to making this a real public API by moving some of the python headers into 'mlir/include'? I was going to do that next since as of now, it's quite awkward to #include in out-of-tree code.

PSA noted. When I mentioned this on discourse, it seemed like this and the previous change for attributes were expansions we could get some mileage out of without totally going off the deep end. Are there immediate linkage concerns with deriving PyConcreteType out of tree, or just the general concerns we have been discussing?

Generic concerns from me:

  • This does not scale beyond a single out of tree project controlling the entire Python environment by providing the mlir native libraries and its project libraries (i.e. circt). If another MLIR project wanted to co-exist, it would need to be built against the same installed MLIR libraries.
  • The compatibility rules for pybind11 to interop at the C++ level between multiple shared-library extensions are arcane and hard to satisfy for things not intimately built together (i.e. basically the tuple of (compiler, standard library, compile options, etc) all have to match and are checked specifically. Penalty for violation is that the types at the Python level are actually forked (the binding is actually stored in a dict in the Python builtin module with a key hard-coded to the above parameters). This usually causes chaos to ensue and is not completely obvious.
  • We still have the lingering TypeID linkage issue (I've been hunting it down for 6 months and haven't run it to ground completely yet). This will work one day and not the next, based on past experience. This bug unfortunately can happen when you obey all of the other rules. I have to assume we'll track it down eventually...
  • Other projects who have gone down this path have eventually had to grow a platform specific loader stub which can adapt things at runtime and work around issues that come from distributing binaries.
  • Those other projects have typically also had to embrace a deployment strategy that lets them pip install headers/dev libraries and rework end projects to build against that (which provides a bit of a source of truth to make sure everything is at the same revision/config options/etc). LLVM's source instability will hurt here since we have very limited and unpredictable compatibility windows between projects.

An answer that neatly side-steps this for projects like circt and npcomp, which merely want to use MLIR for their own ends, and have limited benefit from joining on a single shared installation, is to move the MLIR Python binding in a direction so that it can be *privately embedded* in such a project (i.e. as an circt.mlir private module vs a shared dependency on mlir). I've tried to keep the source tree in a rough shape to make such an evolution practical at some point, but it would be definite work.

Thanks @stellaraccident. So I assume you'd be opposed to making this a real public API by moving some of the python headers into 'mlir/include'? I was going to do that next since as of now, it's quite awkward to #include in out-of-tree code.

I'm not opposed, but I am advising caution: the distance between doing this and real, unsolvable problems, is really short.

ftynse added a subscriber: ftynse.Apr 29 2021, 4:23 AM

@stellaraccident do you mind replicating your comments in https://llvm.discourse.group/t/python-bindings-for-out-of-tree-projects/3339 so we don't spread the discussion?