This is an archive of the discontinued LLVM Phabricator instance.

[mlir][python] stop initialization on ImportError
ClosedPublic

Authored by ashay-github on Sep 7 2022, 1:28 PM.

Details

Summary

An _mlirRegisterEverything.*.so file from an old build that referenced
MLIRPythonExtension.RegisterEverything, but which no longer references
that extension in a new build, causes runtime errors in the new build
like:

ImportError: _mlirRegisterEverything.cpython-38-x86_64-linux-gnu.so: undefined symbol: mlirRegisterAllPasses

The error occurs because the MLIR Python binding tries to dynamically
import the _mlirRegisterEverything module but the dynamic importer
fails since the new build no longer references
MLIRPythonExtension.RegisterEverything.

One possible solution is for the user to manually remove the
_mlirRegisterEverything.*.so file. This patch instead resolves the
problem in code by printing a waning if the module cannot be
imported.

Diff Detail

Event Timeline

ashay-github created this revision.Sep 7 2022, 1:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 7 2022, 1:28 PM
ashay-github requested review of this revision.Sep 7 2022, 1:28 PM
mlir/python/mlir/_mlir_libs/__init__.py
63

The downside listed is a big one, in my opinion. While I am sympathetic to the upgrade plight, I've found the silently swallowing errors love this is a bigger foot gun.

Can you split the excpetion handler? Keep ModuleNotFound first, then add another one for ImportError. In that one, log an error with exc_info=True with a very draconian message like "error importing mlir initializer {module_name}. This may happen in unclean incremental builds but is likely a real bug if encountered elsewhere and the MLIR Python API may not function."

ashay-github edited the summary of this revision. (Show Details)

Now printing the exception to the console along with a message about possibly unclean incremental build upon encountering an ImportError exception.

ashay-github added inline comments.Sep 28 2022, 7:32 AM
mlir/python/mlir/_mlir_libs/__init__.py
63

I've found the silently swallowing errors love this is a bigger foot gun

I agree, and thanks for catching that. I updated the exception handler to print the message and abort execution.

mlir/python/mlir/_mlir_libs/__init__.py
71

Let's not do the sys.exit. that is a really mean thing to do in a library. I think it is fine to return false here and let things go with the error.

I would also downgrade the message to warning.

ashay-github edited the summary of this revision. (Show Details)

Downgraded to a warning, also removed sys.exit(1).

Thanks for the heads up! I'll wait until it lands and rebase this patch (and update it accordingly).

Do you need help committing? The other one does - so if you both do, I can do final fix-ups as I'm landing both.

I have commit privilege, but feel free to commit both patches if that makes things easier! :)

stellaraccident accepted this revision.Sep 28 2022, 5:36 PM

Go ahead and land at your convenience. I'll land the other one once you do (busy and not going to get to it today)

This revision is now accepted and ready to land.Sep 28 2022, 5:36 PM
This revision was automatically updated to reflect the committed changes.