From: John Demme Date: Wed, 6 Apr 2022 00:10:20 +0000 (-0700) Subject: [MLIR] [Python] Pybind adaptors: coerce None to default MlirLocation X-Git-Tag: upstream/15.0.7~11364 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=94d96c2a6201e0927e835b5c0e5b4cb8f95eeadf;p=platform%2Fupstream%2Fllvm.git [MLIR] [Python] Pybind adaptors: coerce None to default MlirLocation Add default source location coercion to enable location elision in Python code. --- diff --git a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h index 661ed48..582855a 100644 --- a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h +++ b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h @@ -125,11 +125,16 @@ struct type_caster { }; /// Casts object <-> MlirLocation. -// TODO: Coerce None to default MlirLocation. template <> struct type_caster { PYBIND11_TYPE_CASTER(MlirLocation, _("MlirLocation")); bool load(handle src, bool) { + if (src.is_none()) { + // Gets the current thread-bound context. + src = py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir")) + .attr("Location") + .attr("current"); + } py::object capsule = mlirApiObjectToCapsule(src); value = mlirPythonCapsuleToLocation(capsule.ptr()); return !mlirLocationIsNull(value);