[Mlir-commits] [mlir] [mlir][python] value casting (PR #69644)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Tue Oct 31 02:48:53 PDT 2023


================
@@ -88,6 +88,20 @@ void PyGlobals::registerTypeCaster(MlirTypeID mlirTypeID,
   }
 }
 
+void PyGlobals::registerValueCaster(MlirTypeID mlirTypeID,
+                                    pybind11::function valueCaster,
+                                    bool replace) {
+  pybind11::object &found = valueCasterMap[mlirTypeID];
+  if (found && !found.is_none() && !replace)
+    throw std::runtime_error("Value caster is already registered: " +
+                             py::repr(found).cast<std::string>());
+  found = std::move(valueCaster);
+  const auto foundIt = valueCasterMapCache.find(mlirTypeID);
+  if (foundIt != valueCasterMapCache.end() && !foundIt->second.is_none()) {
----------------
ftynse wrote:

It looks strange that we don't update the cache if the cached value is `none`. If the caster is registered after somebody tried to look it up, it will be ignored AFAICS. Feels surprising, e.g., for interactive usage. I see this is copied from above, but do you know/understand why it was done this way?

We may indeed not want to allow registering casters if we already looked up one (maybe to force casters to be provided with dialects and added when the dialect is loaded), but then I'd expect us to (1) not add the new caster to the main, non-cache map and (2) potentially raise an error rather than silently ignoring the new caster.

https://github.com/llvm/llvm-project/pull/69644


More information about the Mlir-commits mailing list