This is an archive of the discontinued LLVM Phabricator instance.

Customize exception thrown from mlir.Operation.create() python bindings
ClosedPublic

Authored by mehdi_amini on Dec 4 2020, 5:30 PM.

Details

Summary

The default exception handling isn't very user friendly and does not
point accurately to the issue. Instead we can indicate which of the
operands isn't valid and provide contextual information in the error
message.

Diff Detail

Event Timeline

mehdi_amini created this revision.Dec 4 2020, 5:30 PM
mehdi_amini requested review of this revision.Dec 4 2020, 5:30 PM
mehdi_amini added inline comments.Dec 4 2020, 5:31 PM
mlir/lib/Bindings/Python/IRModules.cpp
939

This is a lot of boilerplate, happy to take a suggestion on how to improve it!

Add one more test

mehdi_amini added inline comments.Dec 4 2020, 5:48 PM
mlir/lib/Bindings/Python/IRModules.cpp
922

I also don't quite understand why casting None to a PyAttribute requires handling this other exception here, while above for the cast to std::string it does not... (maybe something about std::string itself...)

stellaraccident accepted this revision.Dec 4 2020, 10:52 PM
stellaraccident added inline comments.
mlir/lib/Bindings/Python/IRModules.cpp
912

This and the py::reference_cast_error are deeper than I have gone. If you see PybindUtils.h, you'll see that I punted back to catching std::exception and just tightly scoping the body because I had trouble with the corners.

922

I've never hit this before but probably because I usually explicitly check for none().

939

This is the version that I improved, believe it or not. There were prior iterations that were more convoluted with respect to checking the failure conditions in a way that wouldn't leak. But agreed. I have a feeling that this method may turn into a performance hotspot and need reworking anyway, so might make sense to save any major uplift to be based on some measurements. There are just a lot of mismatched types and forms that need to be bridged: not sure there is going to be a really elegant way to do it.

mlir/test/Bindings/Python/ir_operation.py
562

Can you make sure these are ValueError (not Exception -- ValueError is most commonly associated with this kind of mismatch)? You may need to throw them as std::invalid_argument for that to be the case.

This revision is now accepted and ready to land.Dec 4 2020, 10:52 PM
mehdi_amini added inline comments.Dec 5 2020, 9:25 PM
mlir/test/Bindings/Python/ir_operation.py
562

I can, but PyBind issue a RuntimeError for cast conversion: https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/common.h#L724

Can you confirm we should stick with ValueError?

stellaraccident added inline comments.Dec 6 2020, 4:43 PM
mlir/test/Bindings/Python/ir_operation.py
562

That's annoying/inconsistent with typical usage, but better to be internally self consistent I suppose. Leaving it as is sgtm.

This revision was landed with ongoing or failed builds.Dec 7 2020, 3:07 PM
This revision was automatically updated to reflect the committed changes.