Repository: llvm/llvm-project
File: mlir/include/mlir/Bindings/Python/IRCore.h (C/C++)

Code before:

namespace PyDynamicOpTraits {

class MLIR_PYTHON_API_EXPORTED IsTerminator : public PyDynamicOpTrait {
public:
  static bool attach(const nanobind::object &opName, PyMlirContext &context);
  static void bind(nanobind::module_ &m);
};

class MLIR_PYTHON_API_EXPORTED NoTerminator : public PyDynamicOpTrait {
public:
  static bool attach(const nanobind::object &opName, PyMlirContext &context);
  static void bind(nanobind::module_ &m);
};

} // namespace PyDynamicOpTraits

MLIR_PYTHON_API_EXPORTED MlirValue getUniqueResult(MlirOperation operation);
MLIR_PYTHON_API_EXPORTED void populateIRCore(nanobind::module_ &m);
MLIR_PYTHON_API_EXPORTED void populateRoot(nanobind::module_ &m);

/// Helper for creating an @classmethod.
template <class Func, typename... Args>
inline nanobind::object classmethod(Func f, Args... args) {
  nanobind::object cf = nanobind::cpp_function(f, args...);
  nanobind::object builtins = nanobind::module_::import_("builtins");
  return builtins.attr("classmethod")(cf);
}

} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
} // namespace python
} // namespace mlir

namespace nanobind {
namespace detail {
template <>
struct type_caster<
    mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyMlirContext>
    : MlirDefaultingCaster<
          mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyMlirContext> {
};
template <>
struct type_caster<
    mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyLocation>
    : MlirDefaultingCaster<
          mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyLocation> {};

} // namespace detail
} // namespace nanobind

#endif // MLIR_BINDINGS_PYTHON_IRCORE_H

Review comment (suggestion):
In nanobindadaptors we have a way of caching these imports 

https://github.com/llvm/llvm-project/blob/2430410b7d879fce3db76c21bb8c60ed22abd0b5/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h#L74

Can you use it for this import

Code after:

namespace PyDynamicOpTraits {

class MLIR_PYTHON_API_EXPORTED IsTerminator : public PyDynamicOpTrait {
public:
  static bool attach(const nanobind::object &opName, PyMlirContext &context);
  static void bind(nanobind::module_ &m);
};

class MLIR_PYTHON_API_EXPORTED NoTerminator : public PyDynamicOpTrait {
public:
  static bool attach(const nanobind::object &opName, PyMlirContext &context);
  static void bind(nanobind::module_ &m);
};

} // namespace PyDynamicOpTraits

MLIR_PYTHON_API_EXPORTED MlirValue getUniqueResult(MlirOperation operation);
MLIR_PYTHON_API_EXPORTED void populateIRCore(nanobind::module_ &m);
MLIR_PYTHON_API_EXPORTED void populateRoot(nanobind::module_ &m);

/// Helper for creating an @classmethod.
template <class Func, typename... Args>
inline nanobind::object classmethod(Func f, Args... args) {
  nanobind::object cf = nanobind::cpp_function(f, args...);
  static SafeInit<nanobind::object> classmethodFn([]() {
    return std::make_unique<nanobind::object>(
        nanobind::module_::import_("builtins").attr("classmethod"));
  });
  return classmethodFn.get()(cf);
}

} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
} // namespace python
} // namespace mlir

namespace nanobind {
namespace detail {
template <>
struct type_caster<
    mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyMlirContext>
    : MlirDefaultingCaster<
          mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyMlirContext> {
};
template <>
struct type_caster<
    mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyLocation>
    : MlirDefaultingCaster<
          mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN::DefaultingPyLocation> {};

} // namespace detail