[Mlir-commits] [mlir] [MLIR][Python] Remove partial LLVM APIs in python bindings (4/n) (PR #180256)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Feb 6 10:26:13 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: RattataKing (RattataKing)

<details>
<summary>Changes</summary>

This PR continues work from #<!-- -->178290 
It replaces some LLVM utilities with straightforward `std::` equivalents.

---

Patch is 27.88 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/180256.diff


9 Files Affected:

- (modified) mlir/include/mlir/Bindings/Python/Globals.h (+1-2) 
- (modified) mlir/include/mlir/Bindings/Python/IRAttributes.h (+6-5) 
- (modified) mlir/include/mlir/Bindings/Python/NanobindUtils.h (-4) 
- (modified) mlir/lib/Bindings/Python/ExecutionEngineModule.cpp (+4-1) 
- (modified) mlir/lib/Bindings/Python/Globals.cpp (+20-17) 
- (modified) mlir/lib/Bindings/Python/IRAffine.cpp (+31-35) 
- (modified) mlir/lib/Bindings/Python/IRAttributes.cpp (+20-23) 
- (modified) mlir/lib/Bindings/Python/IRInterfaces.cpp (+26-28) 
- (modified) mlir/lib/Bindings/Python/IRTypes.cpp (+4-5) 


``````````diff
diff --git a/mlir/include/mlir/Bindings/Python/Globals.h b/mlir/include/mlir/Bindings/Python/Globals.h
index bdcabea76cd3c..23cccdd36279a 100644
--- a/mlir/include/mlir/Bindings/Python/Globals.h
+++ b/mlir/include/mlir/Bindings/Python/Globals.h
@@ -23,7 +23,6 @@
 #include "mlir/CAPI/Support.h"
 
 #include "llvm/ADT/StringExtras.h"
-#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Regex.h"
 
 namespace mlir {
@@ -127,7 +126,7 @@ class MLIR_PYTHON_API_EXPORTED PyGlobals {
   /// name. Note that this may trigger a load of the dialect, which can
   /// arbitrarily re-enter.
   std::optional<nanobind::object>
-  lookupOpAdaptorClass(llvm::StringRef operationName);
+  lookupOpAdaptorClass(std::string_view operationName);
 
   class MLIR_PYTHON_API_EXPORTED TracebackLoc {
   public:
diff --git a/mlir/include/mlir/Bindings/Python/IRAttributes.h b/mlir/include/mlir/Bindings/Python/IRAttributes.h
index 5ff9afd0875f1..d5d5548602114 100644
--- a/mlir/include/mlir/Bindings/Python/IRAttributes.h
+++ b/mlir/include/mlir/Bindings/Python/IRAttributes.h
@@ -13,6 +13,7 @@
 #include <string>
 #include <string_view>
 #include <utility>
+#include <vector>
 
 #include "mlir-c/BuiltinAttributes.h"
 #include "mlir-c/BuiltinTypes.h"
@@ -31,13 +32,13 @@ struct nb_buffer_info {
   ssize_t size = 0;
   const char *format = nullptr;
   ssize_t ndim = 0;
-  SmallVector<ssize_t, 4> shape;
-  SmallVector<ssize_t, 4> strides;
+  std::vector<ssize_t> shape;
+  std::vector<ssize_t> strides;
   bool readonly = false;
 
   nb_buffer_info(
       void *ptr, ssize_t itemsize, const char *format, ssize_t ndim,
-      SmallVector<ssize_t, 4> shape_in, SmallVector<ssize_t, 4> strides_in,
+      std::vector<ssize_t> shape_in, std::vector<ssize_t> strides_in,
       bool readonly = false,
       std::unique_ptr<Py_buffer, void (*)(Py_buffer *)> owned_view_in =
           std::unique_ptr<Py_buffer, void (*)(Py_buffer *)>(nullptr, nullptr));
@@ -462,11 +463,11 @@ class MLIR_PYTHON_API_EXPORTED PyDenseElementsAttribute
     Type *data = static_cast<Type *>(
         const_cast<void *>(mlirDenseElementsAttrGetRawData(*this)));
     // Prepare the shape for the buffer_info.
-    SmallVector<intptr_t, 4> shape;
+    std::vector<ssize_t> shape;
     for (intptr_t i = 0; i < rank; ++i)
       shape.push_back(mlirShapedTypeGetDimSize(shapedType, i));
     // Prepare the strides for the buffer_info.
-    SmallVector<intptr_t, 4> strides;
+    std::vector<ssize_t> strides;
     if (mlirDenseElementsAttrIsSplat(*this)) {
       // Splats are special, only the single value is stored.
       strides.assign(rank, 0);
diff --git a/mlir/include/mlir/Bindings/Python/NanobindUtils.h b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
index b72ed31a4a4e5..69b0c6f0f51ad 100644
--- a/mlir/include/mlir/Bindings/Python/NanobindUtils.h
+++ b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
@@ -12,10 +12,6 @@
 
 #include "mlir-c/Support.h"
 #include "mlir/Bindings/Python/Nanobind.h"
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/Twine.h"
-#include "llvm/Support/DataTypes.h"
 
 #include <fstream>
 #include <sstream>
diff --git a/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp b/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp
index 01b7930deffd2..76dbd54ef0467 100644
--- a/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp
+++ b/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <vector>
+
 #include "mlir-c/ExecutionEngine.h"
 #include "mlir/Bindings/Python/IRCore.h"
 #include "mlir/Bindings/Python/Nanobind.h"
@@ -83,7 +85,8 @@ NB_MODULE(_mlirExecutionEngine, m) {
           [](PyExecutionEngine &self, PyModule &module, int optLevel,
              const std::vector<std::string> &sharedLibPaths,
              bool enableObjectDump, bool enablePIC) {
-            llvm::SmallVector<MlirStringRef, 4> libPaths;
+            std::vector<MlirStringRef> libPaths;
+            libPaths.reserve(sharedLibPaths.size());
             for (const std::string &path : sharedLibPaths)
               libPaths.push_back({path.c_str(), path.length()});
             MlirExecutionEngine executionEngine = mlirExecutionEngineCreate(
diff --git a/mlir/lib/Bindings/Python/Globals.cpp b/mlir/lib/Bindings/Python/Globals.cpp
index 1fcd83c15c6ce..b3b7619ad34e6 100644
--- a/mlir/lib/Bindings/Python/Globals.cpp
+++ b/mlir/lib/Bindings/Python/Globals.cpp
@@ -86,11 +86,11 @@ void PyGlobals::registerAttributeBuilder(const std::string &attributeKind,
   nb::ft_lock_guard lock(mutex);
   nb::object &found = attributeBuilderMap[attributeKind];
   if (found && !replace) {
-    throw std::runtime_error((llvm::Twine("Attribute builder for '") +
-                              attributeKind +
-                              "' is already registered with func: " +
-                              nb::cast<std::string>(nb::str(found)))
-                                 .str());
+    throw std::runtime_error(
+        nanobind::detail::join("Attribute builder for '", attributeKind,
+                               "' is already registered with func: ",
+                               nb::cast<std::string>(nb::str(found)))
+            .c_str());
   }
   found = std::move(pyFunc);
 }
@@ -120,9 +120,10 @@ void PyGlobals::registerDialectImpl(const std::string &dialectNamespace,
   nb::ft_lock_guard lock(mutex);
   nb::object &found = dialectClassMap[dialectNamespace];
   if (found) {
-    throw std::runtime_error((llvm::Twine("Dialect namespace '") +
-                              dialectNamespace + "' is already registered.")
-                                 .str());
+    throw std::runtime_error(nanobind::detail::join("Dialect namespace '",
+                                                    dialectNamespace,
+                                                    "' is already registered.")
+                                 .c_str());
   }
   found = std::move(pyClass);
 }
@@ -132,9 +133,10 @@ void PyGlobals::registerOperationImpl(const std::string &operationName,
   nb::ft_lock_guard lock(mutex);
   nb::object &found = operationClassMap[operationName];
   if (found && !replace) {
-    throw std::runtime_error((llvm::Twine("Operation '") + operationName +
-                              "' is already registered.")
-                                 .str());
+    throw std::runtime_error(nanobind::detail::join("Operation '",
+                                                    operationName,
+                                                    "' is already registered.")
+                                 .c_str());
   }
   found = std::move(pyClass);
 }
@@ -144,9 +146,10 @@ void PyGlobals::registerOpAdaptorImpl(const std::string &operationName,
   nb::ft_lock_guard lock(mutex);
   nb::object &found = opAdaptorClassMap[operationName];
   if (found && !replace) {
-    throw std::runtime_error((llvm::Twine("Operation adaptor of '") +
-                              operationName + "' is already registered.")
-                                 .str());
+    throw std::runtime_error(nanobind::detail::join("Operation adaptor of '",
+                                                    operationName,
+                                                    "' is already registered.")
+                                 .c_str());
   }
   found = std::move(pyClass);
 }
@@ -222,10 +225,10 @@ PyGlobals::lookupOperationClass(std::string_view operationName) {
 }
 
 std::optional<nb::object>
-PyGlobals::lookupOpAdaptorClass(llvm::StringRef operationName) {
+PyGlobals::lookupOpAdaptorClass(std::string_view operationName) {
   // Make sure dialect module is loaded.
-  auto split = operationName.split('.');
-  llvm::StringRef dialectNamespace = split.first;
+  std::string_view dialectNamespace =
+      operationName.substr(0, operationName.find('.'));
   (void)loadDialectModule(dialectNamespace);
 
   nb::ft_lock_guard lock(mutex);
diff --git a/mlir/lib/Bindings/Python/IRAffine.cpp b/mlir/lib/Bindings/Python/IRAffine.cpp
index 2e760e6e6f830..9fb2cfde2d340 100644
--- a/mlir/lib/Bindings/Python/IRAffine.cpp
+++ b/mlir/lib/Bindings/Python/IRAffine.cpp
@@ -8,8 +8,10 @@
 
 #include <cstddef>
 #include <cstdint>
+#include <memory>
 #include <stdexcept>
 #include <string>
+#include <string_view>
 #include <utility>
 #include <vector>
 
@@ -23,19 +25,11 @@
 #include "mlir-c/IntegerSet.h"
 #include "mlir/Bindings/Python/Nanobind.h"
 #include "mlir/Support/LLVM.h"
-#include "llvm/ADT/Hashing.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/Twine.h"
 
 namespace nb = nanobind;
 using namespace mlir;
 using namespace mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN;
 
-using llvm::SmallVector;
-using llvm::StringRef;
-using llvm::Twine;
-
 static const char kDumpDocstring[] =
     R"(Dumps a debug representation of the object to stderr.)";
 
@@ -44,22 +38,22 @@ static const char kDumpDocstring[] =
 /// Throws errors in case of failure, using "action" to describe what the caller
 /// was attempting to do.
 template <typename PyType, typename CType>
-static void pyListToVector(const nb::list &list,
-                           llvm::SmallVectorImpl<CType> &result,
-                           StringRef action) {
+static void pyListToVector(const nb::list &list, std::vector<CType> &result,
+                           std::string_view action) {
   result.reserve(nb::len(list));
   for (nb::handle item : list) {
     try {
       result.push_back(nb::cast<PyType>(item));
     } catch (nb::cast_error &err) {
-      std::string msg = (llvm::Twine("Invalid expression when ") + action +
-                         " (" + err.what() + ")")
-                            .str();
+      std::string msg = nanobind::detail::join("Invalid expression when ",
+                                               action, " (", err.what(), ")")
+                            .c_str();
       throw std::runtime_error(msg.c_str());
     } catch (std::runtime_error &err) {
-      std::string msg = (llvm::Twine("Invalid expression (None?) when ") +
-                         action + " (" + err.what() + ")")
-                            .str();
+      std::string msg =
+          nanobind::detail::join("Invalid expression (None?) when ", action,
+                                 " (", err.what(), ")")
+              .c_str();
       throw std::runtime_error(msg.c_str());
     }
   }
@@ -67,7 +61,7 @@ static void pyListToVector(const nb::list &list,
 
 template <typename PermutationTy>
 static bool isPermutation(const std::vector<PermutationTy> &permutation) {
-  llvm::SmallVector<bool, 8> seen(permutation.size(), false);
+  std::vector<bool> seen(permutation.size(), false);
   for (auto val : permutation) {
     if (val < permutation.size()) {
       if (seen[val])
@@ -106,11 +100,11 @@ class PyConcreteAffineExpr : public BaseTy {
   static MlirAffineExpr castFrom(PyAffineExpr &orig) {
     if (!DerivedTy::isaFunction(orig)) {
       auto origRepr = nb::cast<std::string>(nb::repr(nb::cast(orig)));
-      throw nb::value_error((Twine("Cannot cast affine expression to ") +
-                             DerivedTy::pyClassName + " (from " + origRepr +
-                             ")")
-                                .str()
-                                .c_str());
+      throw nb::value_error(
+          nanobind::detail::join("Cannot cast affine expression to ",
+                                 DerivedTy::pyClassName, " (from ", origRepr,
+                                 ")")
+              .c_str());
     }
     return orig;
   }
@@ -602,7 +596,7 @@ void populateIRAffine(nb::module_ &m) {
            })
       .def("__hash__",
            [](PyAffineExpr &self) {
-             return static_cast<size_t>(llvm::hash_value(self.get().ptr));
+             return std::hash<const void *>{}(self.get().ptr);
            })
       .def_prop_ro(
           "context",
@@ -739,12 +733,12 @@ void populateIRAffine(nb::module_ &m) {
            })
       .def("__hash__",
            [](PyAffineMap &self) {
-             return static_cast<size_t>(llvm::hash_value(self.get().ptr));
+             return std::hash<const void *>{}(self.get().ptr);
            })
       .def_static(
           "compress_unused_symbols",
           [](const nb::list &affineMaps, DefaultingPyMlirContext context) {
-            SmallVector<MlirAffineMap> maps;
+            std::vector<MlirAffineMap> maps;
             pyListToVector<PyAffineMap, MlirAffineMap>(
                 affineMaps, maps, "attempting to create an AffineMap");
             std::vector<MlirAffineMap> compressed(affineMaps.size());
@@ -772,7 +766,7 @@ void populateIRAffine(nb::module_ &m) {
           "get",
           [](intptr_t dimCount, intptr_t symbolCount, const nb::list &exprs,
              DefaultingPyMlirContext context) {
-            SmallVector<MlirAffineExpr> affineExprs;
+            std::vector<MlirAffineExpr> affineExprs;
             pyListToVector<PyAffineExpr, MlirAffineExpr>(
                 exprs, affineExprs, "attempting to create an AffineMap");
             MlirAffineMap map =
@@ -925,7 +919,7 @@ void populateIRAffine(nb::module_ &m) {
            })
       .def("__hash__",
            [](PyIntegerSet &self) {
-             return static_cast<size_t>(llvm::hash_value(self.get().ptr));
+             return std::hash<const void *>{}(self.get().ptr);
            })
       .def_prop_ro(
           "context",
@@ -946,17 +940,18 @@ void populateIRAffine(nb::module_ &m) {
             if (exprs.size() == 0)
               throw nb::value_error("Expected non-empty list of constraints");
 
-            // Copy over to a SmallVector because std::vector has a
-            // specialization for booleans that packs data and does not
-            // expose a `bool *`.
-            SmallVector<bool, 8> flags(eqFlags.begin(), eqFlags.end());
+            // std::vector<bool> does not expose a bool* data pointer.
+            std::unique_ptr<bool[]> flags =
+                std::make_unique<bool[]>(eqFlags.size());
+            for (size_t i = 0, e = eqFlags.size(); i < e; ++i)
+              flags[i] = eqFlags[i];
 
-            SmallVector<MlirAffineExpr> affineExprs;
+            std::vector<MlirAffineExpr> affineExprs;
             pyListToVector<PyAffineExpr>(exprs, affineExprs,
                                          "attempting to create an IntegerSet");
             MlirIntegerSet set = mlirIntegerSetGet(
                 context->get(), numDims, numSymbols, exprs.size(),
-                affineExprs.data(), flags.data());
+                affineExprs.data(), flags.get());
             return PyIntegerSet(context->getRef(), set);
           },
           nb::arg("num_dims"), nb::arg("num_symbols"), nb::arg("exprs"),
@@ -987,7 +982,8 @@ void populateIRAffine(nb::module_ &m) {
                   "Expected the number of symbol replacement expressions "
                   "to match that of symbols");
 
-            SmallVector<MlirAffineExpr> dimAffineExprs, symbolAffineExprs;
+            std::vector<MlirAffineExpr> dimAffineExprs;
+            std::vector<MlirAffineExpr> symbolAffineExprs;
             pyListToVector<PyAffineExpr>(
                 dimExprs, dimAffineExprs,
                 "attempting to create an IntegerSet by replacing dimensions");
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 05c0c5e825df3..c271497fcc9f8 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -6,12 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <algorithm>
 #include <cmath>
 #include <cstdint>
 #include <optional>
 #include <string>
 #include <string_view>
 #include <utility>
+#include <vector>
 
 #include "mlir-c/BuiltinAttributes.h"
 #include "mlir-c/BuiltinTypes.h"
@@ -21,15 +23,12 @@
 #include "mlir/Bindings/Python/NanobindAdaptors.h"
 #include "mlir/Bindings/Python/NanobindUtils.h"
 #include "llvm/ADT/ScopeExit.h"
-#include "llvm/Support/raw_ostream.h"
 
 namespace nb = nanobind;
 using namespace nanobind::literals;
 using namespace mlir;
 using namespace mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN;
 
-using llvm::SmallVector;
-
 //------------------------------------------------------------------------------
 // Docstrings (trivial, non-duplicated docstrings are included inline).
 //------------------------------------------------------------------------------
@@ -129,7 +128,7 @@ namespace MLIR_BINDINGS_PYTHON_DOMAIN {
 
 nb_buffer_info::nb_buffer_info(
     void *ptr, ssize_t itemsize, const char *format, ssize_t ndim,
-    SmallVector<ssize_t, 4> shape_in, SmallVector<ssize_t, 4> strides_in,
+    std::vector<ssize_t> shape_in, std::vector<ssize_t> strides_in,
     bool readonly,
     std::unique_ptr<Py_buffer, void (*)(Py_buffer *)> owned_view_in)
     : ptr(ptr), itemsize(itemsize), format(format), ndim(ndim),
@@ -255,7 +254,7 @@ void PyArrayAttribute::bindDerived(ClassTy &c) {
   c.def_static(
       "get",
       [](const nb::list &attributes, DefaultingPyMlirContext context) {
-        SmallVector<MlirAttribute> mlirAttributes;
+        std::vector<MlirAttribute> mlirAttributes;
         mlirAttributes.reserve(nb::len(attributes));
         for (auto attribute : attributes) {
           mlirAttributes.push_back(pyTryCast<PyAttribute>(attribute));
@@ -465,7 +464,7 @@ PySymbolRefAttribute::fromList(const std::vector<std::string> &symbols,
     throw std::runtime_error("SymbolRefAttr must be composed of at least "
                              "one symbol.");
   MlirStringRef rootSymbol = toMlirStringRef(symbols[0]);
-  SmallVector<MlirAttribute, 3> referenceAttrs;
+  std::vector<MlirAttribute> referenceAttrs;
   for (size_t i = 1; i < symbols.size(); ++i) {
     referenceAttrs.push_back(
         mlirFlatSymbolRefAttrGet(context.get(), toMlirStringRef(symbols[i])));
@@ -566,22 +565,21 @@ PyDenseElementsAttribute::getFromList(const nb::list &attributes,
     if ((!mlirTypeIsAShaped(*explicitType) ||
          !mlirShapedTypeHasStaticShape(*explicitType))) {
 
-      std::string message;
-      llvm::raw_string_ostream os(message);
-      os << "Expected a static ShapedType for the shaped_type parameter: "
-         << nb::cast<std::string>(nb::repr(nb::cast(*explicitType)));
+      std::string message = nanobind::detail::join(
+          "Expected a static ShapedType for the shaped_type parameter: ",
+          nb::cast<std::string>(nb::repr(nb::cast(*explicitType))));
       throw nb::value_error(message.c_str());
     }
     shapedType = *explicitType;
   } else {
-    SmallVector<int64_t> shape = {static_cast<int64_t>(numAttributes)};
+    std::vector<int64_t> shape = {static_cast<int64_t>(numAttributes)};
     shapedType = mlirRankedTensorTypeGet(
         shape.size(), shape.data(),
         mlirAttributeGetType(pyTryCast<PyAttribute>(attributes[0])),
         mlirAttributeGetNull());
   }
 
-  SmallVector<MlirAttribute> mlirAttributes;
+  std::vector<MlirAttribute> mlirAttributes;
   mlirAttributes.reserve(numAttributes);
   for (const nb::handle &attribute : attributes) {
     MlirAttribute mlirAttribute = pyTryCast<PyAttribute>(attribute);
@@ -589,12 +587,11 @@ PyDenseElementsAttribute::getFromList(const nb::list &attributes,
     mlirAttributes.push_back(mlirAttribute);
 
     if (!mlirTypeEqual(mlirShapedTypeGetElementType(shapedType), attrType)) {
-      std::string message;
-      llvm::raw_string_ostream os(message);
-      os << "All attributes must be of the same type and match "
-         << "the type parameter: expected="
-         << nb::cast<std::string>(nb::repr(nb::cast(shapedType)))
-         << ", but got=" << nb::cast<std::string>(nb::repr(nb::cast(attrType)));
+      std::string message = nanobind::detail::join(
+          "All attributes must be of the same type and match the type "
+          "parameter: expected=",
+          nb::cast<std::string>(nb::repr(nb::cast(shapedType))),
+          ", but got=", nb::cast<std::string>(nb::repr(nb::cast(attrType))));
       throw nb::value_error(message.c_str());
     }
   }
@@ -810,11 +807,11 @@ bool PyDenseElementsAttribute::isSignedIntegerFormat(std::string_view format) {
 MlirType PyDenseElementsAttribute::getShapedType(
     std::optional<MlirType> bulkLoadElementType,
     std::optional<std::vector...
[truncated]

``````````

</details>


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


More information about the Mlir-commits mailing list