This is an archive of the discontinued LLVM Phabricator instance.

Initial boiler-plate for python bindings.
ClosedPublic

Authored by stellaraccident on Jul 6 2020, 11:06 PM.

Details

Summary
  • Native '_mlir' extension module.
  • Python mlir/__init__.py trampoline module.
  • Lit test that checks a message.
  • Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.

This is a draft to back up a more formal RFC to iron out naming, location, etc.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJul 6 2020, 11:06 PM

Fantastic! LGTM overall :)

mlir/CMakeLists.txt
85

I don't know CMake best practices, but is this something we could put in

mlir/lib/Bindings/CMakeLists.txt

101

Can you move binding_test as test/bindings ?

mlir/lib/Bindings/Python/CMakeLists.txt
63

I am not sure this will work on MacOS?

70

Thanks for the extensive doc in the Cmake files!

stellaraccident marked 4 inline comments as done.Jul 7 2020, 5:05 PM
stellaraccident added inline comments.
mlir/CMakeLists.txt
85

Open to it but I usually see things like this done at the lowest ancestor directory in a big project (if not in the global cmake config). In this case, setting up the dependency in a leaf directory would create a fragile ordering between the Bindings subdirectory and the test directory (which needs a couple of config items from here).

101

Afaict, the entire 'test' tree is covered by a single lit test suite. I'm not a lit config expert, but I think this can be done by:

  • Having something like (in the leaf directory):
if not config.enable_bindings_python:
  config.unsupported = True
  • Doing some conditional setup in the test/lit.cfg.py

I'd have to fiddle with it as I've not conditionally included parts of the test tree like this yet.

mlir/lib/Bindings/Python/CMakeLists.txt
63

Probably not now that I think of it. My experience with MacOS ended 10 years ago. I can find an incantation, but I'll need a collaborator who has a Mac to actually make it work.

Do you know if we have a CMake variable we can condition on for MacOS?

70

Yw - some of this was hard-fought in previous iterations :) We probably want to refactor some of it to an include file, but I think it is useful to write out long-hand first.

stellaraccident marked 3 inline comments as not done.Jul 7 2020, 5:06 PM
mehdi_amini added inline comments.Jul 7 2020, 5:27 PM
mlir/CMakeLists.txt
101

Yes a lit.local.cfg with the snippet you mention seems like the way to go. I can help looking into this if needed.

mlir/lib/Bindings/Python/CMakeLists.txt
63

The MacOS linker to not support linker scripts. What is the requirement on the the visibility? Why don't we build the source with -fvisibility=hidden?

The rest of LLVM CMake seems to check with:

if(APPLE)
66

Your basic flow does not depend on MLIR yet, are we gonna depend on libMLIR.so, or will it be configurable to link the static lib or the .so?

Move test suite, add condition to linker script, and add a trivial dep on MLIR to demonstrate.

stellaraccident marked 6 inline comments as done.Jul 7 2020, 6:13 PM

PTAL

mlir/lib/Bindings/Python/CMakeLists.txt
63

Added the conditional. I suspect that this will work as-is on MacOS but without some additional linker config may error at runtime if loaded into a process that exposes a different version of LLVM symbols (but I'm running off of some very old knowledge about the vagaries of linkage issues on that platform).

The requirement on visibility imposed by pybind is that any sources that use pybind must be compiled with -fvisibility=hidden, which is what the CXX_VISIBILITY_PRESET property is setting (in a platform agnostic way).

The additional thing I'm doing here is using a linker script to force the non-pybind *dependencies* to be hidden, which could otherwise only be controlled by setting an LLVM level CXX flag. This makes it so that "static" shared-library extension modules are self contained and do not leak LLVM exports to/from other shared-libraries that may be resident in the same process.

If building LLVM to only produce a python extension (i.e. as part of publishing to a pip archive or something), we could get somewhat better/smaller binaries by forcing all of LLVM to build with -fvisibility=hidden, but forcing it with linker scripts/flags is a nice middle ground that gives us the isolation while not leaking necessary compiler flags to the whole project.

66

I just added a trivial dep on MLIR to make sure it works.

I'm anticipating needing an option to control this behavior as all combinations are necessary in the limit. Currently this is building a self-contained ("static") shared library, which seems like a good starting point (and is what I generally prefer for development). I'd like to follow-up with the knobs for a true shared build (probably in conjunction with rules to copy the sources and built artifact to a dedicated directory in the build tree). Getting the shared linkage right can be a pain across platforms, so I'd like to make sure the simple static case works first/by default.

stellaraccident marked an inline comment as done.
mehdi_amini accepted this revision.Jul 7 2020, 8:10 PM
mehdi_amini added inline comments.
mlir/lib/Bindings/Python/CMakeLists.txt
63

OK now I get it, originally I thought you'd like against libMLIR.so and so the binding shared lib would be a "thin" object with naturally only the binding code.

It is likely not an issue with the two-levels namespace on MacOS in general (even though you can still hide things in the linker using an export list, which LLVM does for the LTO plugin: https://github.com/llvm/llvm-project/blob/master/llvm/tools/lto/CMakeLists.txt#L22 )

mlir/test/lit.cfg.py
45

Do you need these additions?

This revision is now accepted and ready to land.Jul 7 2020, 8:10 PM
stellaraccident marked 3 inline comments as done.Jul 7 2020, 9:06 PM
stellaraccident added inline comments.
mlir/lib/Bindings/Python/CMakeLists.txt
63

We definitely want to support the thin case but I'd like to iterate to get there and make sure we support the fat case, since that is important for various distribution scenarios (and has fewer moving parts to cause sharp edges -- which is why I prefer it for dev and cross builds).

mlir/test/lit.cfg.py
45

Yes - otherwise, adding the .py extension to the test suite advice causes lit to match its own config file and try to run it as a test :/

stellaraccident marked an inline comment as done.

Rebase to head.

stephenneuendorffer added inline comments.
mlir/test/CMakeLists.txt
9–13

I assume these were just never needed in the past? might be nice to have tests that depend on them? Is the rest dependent on this or should it maybe be separate?

stellaraccident marked an inline comment as done.Jul 8 2020, 7:57 PM
stellaraccident added inline comments.
mlir/test/CMakeLists.txt
9–13

Yes, I found them missing when trying to figure out why -DMY_FEATURE_ENABLED=ON caused parse errors on the site.cfg. It looks like instead of adding them here, someone just started using the convention of defining them as 0/1 in the main file (and requiring a numeric flag on the command line). Adding them, makes it robust to saying -DMLIR_VULKAN_RUNNER_ENABLED=ON (versus '1') -- so I think people were just getting by before: there are tests for each of these and if you pass a non 0/1 bool on the command line you'll get a parse error without this addition.

This was fun to find. Let me bust this out and send separately.

This revision was automatically updated to reflect the committed changes.