[mlir][python] stop initialization on ImportError

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.

Reviewed By: stellaraccident

Differential Revision: https://reviews.llvm.org/D133450
This commit is contained in:
Ashay Rane 2022-09-28 14:53:36 +00:00
parent 9e6840ccba
commit b947e15a57
1 changed files with 5 additions and 0 deletions

View File

@ -62,6 +62,11 @@ def _site_initialize():
m = importlib.import_module(f".{module_name}", __name__)
except ModuleNotFoundError:
return False
except ImportError:
message = (f"Error importing mlir initializer {module_name}. This may "
"happen in unclean incremental builds but is likely a real bug if "
"encountered otherwise and the MLIR Python API may not function.")
logging.warning(message, exc_info=True)
logging.debug("Initializing MLIR with module: %s", module_name)
if hasattr(m, "register_dialects"):