[Mlir-commits] [mlir] [MLIR][Python] Remove partial LLVM APIs in python bindings (5/n) (PR #180644)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Feb 10 08:40:59 PST 2026
https://github.com/RattataKing updated https://github.com/llvm/llvm-project/pull/180644
>From f37f1477cf71a300cb64bb1ae9c197ff0ca884a9 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Mon, 9 Feb 2026 17:17:47 +0000
Subject: [PATCH 01/13] Add local llvm::Regex::escape
---
mlir/lib/Bindings/Python/Globals.cpp | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/mlir/lib/Bindings/Python/Globals.cpp b/mlir/lib/Bindings/Python/Globals.cpp
index d39ebfa1f518c..e3fb79de6e13d 100644
--- a/mlir/lib/Bindings/Python/Globals.cpp
+++ b/mlir/lib/Bindings/Python/Globals.cpp
@@ -8,7 +8,9 @@
#include "mlir/Bindings/Python/IRCore.h"
+#include <cstring>
#include <optional>
+#include <string_view>
#include <vector>
#include "mlir/Bindings/Python/Globals.h"
@@ -22,6 +24,21 @@
namespace nb = nanobind;
using namespace mlir;
+/// Local helper adapted from llvm::Regex::escape.
+namespace {
+static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
+
+static std::string escapeRegex(std::string_view String) {
+ std::string RegexStr;
+ for (char C : String) {
+ if (std::strchr(RegexMetachars, C))
+ RegexStr += '\\';
+ RegexStr += C;
+ }
+ return RegexStr;
+}
+} // namespace
+
// -----------------------------------------------------------------------------
// PyGlobals
// -----------------------------------------------------------------------------
@@ -258,7 +275,7 @@ void PyGlobals::TracebackLoc::setLocTracebackFramesLimit(size_t value) {
void PyGlobals::TracebackLoc::registerTracebackFileInclusion(
const std::string &file) {
nanobind::ft_lock_guard lock(mutex);
- auto reg = "^" + llvm::Regex::escape(file);
+ auto reg = "^" + escapeRegex(file);
if (userTracebackIncludeFiles.insert(reg).second)
rebuildUserTracebackIncludeRegex = true;
if (userTracebackExcludeFiles.count(reg)) {
@@ -270,7 +287,7 @@ void PyGlobals::TracebackLoc::registerTracebackFileInclusion(
void PyGlobals::TracebackLoc::registerTracebackFileExclusion(
const std::string &file) {
nanobind::ft_lock_guard lock(mutex);
- auto reg = "^" + llvm::Regex::escape(file);
+ auto reg = "^" + escapeRegex(file);
if (userTracebackExcludeFiles.insert(reg).second)
rebuildUserTracebackExcludeRegex = true;
if (userTracebackIncludeFiles.count(reg)) {
>From 9794ca84d0200e8cf2c0c73206fcdf51251fc310 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Mon, 9 Feb 2026 19:03:46 +0000
Subject: [PATCH 02/13] Add local llvm::join
---
.../mlir/Bindings/Python/NanobindUtils.h | 19 +++++++++++++++++++
mlir/lib/Bindings/Python/Globals.cpp | 4 ++--
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/mlir/include/mlir/Bindings/Python/NanobindUtils.h b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
index 2bd1025c49c36..6e43f4a6d7796 100644
--- a/mlir/include/mlir/Bindings/Python/NanobindUtils.h
+++ b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
@@ -16,6 +16,7 @@
#include <fstream>
#include <sstream>
#include <string>
+#include <string_view>
#include <type_traits>
#include <typeinfo>
#include <variant>
@@ -81,6 +82,24 @@ class Defaulting {
namespace nanobind {
namespace detail {
+/// Local helper adapted from llvm::join for a range, adding Separator between
+/// elements.
+template <typename Range>
+inline std::string joinRange(Range &&R, std::string_view Separator) {
+ auto Begin = R.begin();
+ auto End = R.end();
+ std::string S;
+ if (Begin == End)
+ return S;
+
+ S += *Begin;
+ while (++Begin != End) {
+ S += Separator;
+ S += *Begin;
+ }
+ return S;
+}
+
/// Helper function to concatenate arguments into a `std::string`.
template <typename... Ts>
inline std::string join(const Ts &...args) {
diff --git a/mlir/lib/Bindings/Python/Globals.cpp b/mlir/lib/Bindings/Python/Globals.cpp
index e3fb79de6e13d..36d1f182040fa 100644
--- a/mlir/lib/Bindings/Python/Globals.cpp
+++ b/mlir/lib/Bindings/Python/Globals.cpp
@@ -301,13 +301,13 @@ bool PyGlobals::TracebackLoc::isUserTracebackFilename(
nanobind::ft_lock_guard lock(mutex);
if (rebuildUserTracebackIncludeRegex) {
userTracebackIncludeRegex.assign(
- llvm::join(userTracebackIncludeFiles, "|"));
+ nanobind::detail::joinRange(userTracebackIncludeFiles, "|"));
rebuildUserTracebackIncludeRegex = false;
isUserTracebackFilenameCache.clear();
}
if (rebuildUserTracebackExcludeRegex) {
userTracebackExcludeRegex.assign(
- llvm::join(userTracebackExcludeFiles, "|"));
+ nanobind::detail::joinRange(userTracebackExcludeFiles, "|"));
rebuildUserTracebackExcludeRegex = false;
isUserTracebackFilenameCache.clear();
}
>From a30e3ee00764ab52f30ffca2f2ecaef2c6e8c028 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Mon, 9 Feb 2026 21:04:26 +0000
Subject: [PATCH 03/13] Add local llvm::scope_exit
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 37 +++++++++++++++++++++--
1 file changed, 34 insertions(+), 3 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index c271497fcc9f8..143ddd67f2651 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -22,7 +22,6 @@
#include "mlir/Bindings/Python/Nanobind.h"
#include "mlir/Bindings/Python/NanobindAdaptors.h"
#include "mlir/Bindings/Python/NanobindUtils.h"
-#include "llvm/ADT/ScopeExit.h"
namespace nb = nanobind;
using namespace nanobind::literals;
@@ -122,6 +121,38 @@ subsequent processing.
type or if the buffer does not meet expectations.
)";
+/// Local helper adapted from llvm::scope_exit.
+namespace {
+template <typename Callable>
+class [[nodiscard]] scope_exit {
+ Callable ExitFunction;
+ bool Engaged = true; // False once moved-from or release()d.
+
+public:
+ template <typename Fp>
+ explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}
+
+ scope_exit(scope_exit &&Rhs)
+ : ExitFunction(std::move(Rhs.ExitFunction)), Engaged(Rhs.Engaged) {
+ Rhs.release();
+ }
+ scope_exit(const scope_exit &) = delete;
+ scope_exit &operator=(scope_exit &&) = delete;
+ scope_exit &operator=(const scope_exit &) = delete;
+
+ void release() { Engaged = false; }
+
+ ~scope_exit() {
+ if (Engaged)
+ ExitFunction();
+ }
+};
+
+template <typename Callable>
+scope_exit(Callable) -> scope_exit<Callable>;
+
+} // namespace
+
namespace mlir {
namespace python {
namespace MLIR_BINDINGS_PYTHON_DOMAIN {
@@ -616,7 +647,7 @@ PyDenseElementsAttribute PyDenseElementsAttribute::getFromBuffer(
if (PyObject_GetBuffer(array.ptr(), &view, flags) != 0) {
throw nb::python_error();
}
- llvm::scope_exit freeBuffer([&]() { PyBuffer_Release(&view); });
+ scope_exit freeBuffer([&]() { PyBuffer_Release(&view); });
MlirContext context = contextWrapper->get();
MlirAttribute attr = getAttributeFromBuffer(
@@ -1126,7 +1157,7 @@ PyDenseResourceElementsAttribute::getFromBuffer(
// This scope releaser will only release if we haven't yet transferred
// ownership.
- llvm::scope_exit freeBuffer([&]() {
+ scope_exit freeBuffer([&]() {
if (view)
PyBuffer_Release(view.get());
});
>From 9b7fdf8333414c520ee1ca6c747e3266b346fa07 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Mon, 9 Feb 2026 22:19:18 +0000
Subject: [PATCH 04/13] Add little endian checker to replace llvm::endianness
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 143ddd67f2651..5591996d1bdf5 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include <algorithm>
+#include <bit>
#include <cmath>
#include <cstdint>
#include <optional>
@@ -121,8 +122,18 @@ subsequent processing.
type or if the buffer does not meet expectations.
)";
-/// Local helper adapted from llvm::scope_exit.
namespace {
+/// Local helper checking if the current machine is little endian.
+bool isLittleEndian() {
+#if defined(__cpp_lib_endian)
+ return std::endian::native == std::endian::little;
+#else
+ const uint16_t value = 1;
+ return *reinterpret_cast<const uint8_t *>(&value) == 1;
+#endif
+}
+
+/// Local helper adapted from llvm::scope_exit.
template <typename Callable>
class [[nodiscard]] scope_exit {
Callable ExitFunction;
@@ -941,7 +952,7 @@ MlirAttribute PyDenseElementsAttribute::getAttributeFromBuffer(
MlirAttribute PyDenseElementsAttribute::getBitpackedAttributeFromBooleanBuffer(
Py_buffer &view, std::optional<std::vector<int64_t>> explicitShape,
MlirContext &context) {
- if (llvm::endianness::native != llvm::endianness::little) {
+ if (!isLittleEndian()) {
// Given we have no good way of testing the behavior on big-endian
// systems we will throw
throw nb::type_error("Constructing a bit-packed MLIR attribute is "
@@ -969,7 +980,7 @@ MlirAttribute PyDenseElementsAttribute::getBitpackedAttributeFromBooleanBuffer(
std::unique_ptr<nb_buffer_info>
PyDenseElementsAttribute::getBooleanBufferFromBitpackedAttribute() const {
- if (llvm::endianness::native != llvm::endianness::little) {
+ if (!isLittleEndian()) {
// Given we have no good way of testing the behavior on big-endian
// systems we will throw
throw nb::type_error("Constructing a numpy array from a MLIR attribute "
>From bb5747347179bbeede532db8fa9018747d323ec9 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Mon, 9 Feb 2026 22:24:07 +0000
Subject: [PATCH 05/13] Remove llvm::divideCeil
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 5591996d1bdf5..a1a33624a3ee4 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -988,7 +988,7 @@ PyDenseElementsAttribute::getBooleanBufferFromBitpackedAttribute() const {
}
int64_t numBooleans = mlirElementsAttrGetNumElements(*this);
- int64_t numBitpackedBytes = llvm::divideCeil(numBooleans, 8);
+ int64_t numBitpackedBytes = (numBooleans + 7) / 8;
uint8_t *bitpackedData = static_cast<uint8_t *>(
const_cast<void *>(mlirDenseElementsAttrGetRawData(*this)));
nb::ndarray<uint8_t, nb::numpy, nb::ndim<1>, nb::c_contig> packedArray(
>From 5f25d7de56001efa95e417b1696fd39da3eec9eb Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Mon, 9 Feb 2026 23:12:23 +0000
Subject: [PATCH 06/13] Clean llvm::map_to_vector and llvm::SmallVector<bool>
---
mlir/lib/Bindings/Python/IRTypes.cpp | 57 ++++++++++++++++++++--------
1 file changed, 41 insertions(+), 16 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRTypes.cpp b/mlir/lib/Bindings/Python/IRTypes.cpp
index a8e60f099ef67..d259a2df2d706 100644
--- a/mlir/lib/Bindings/Python/IRTypes.cpp
+++ b/mlir/lib/Bindings/Python/IRTypes.cpp
@@ -7,12 +7,14 @@
//===----------------------------------------------------------------------===//
// clang-format off
-#include "llvm/ADT/SmallVectorExtras.h"
#include "mlir/Bindings/Python/IRCore.h"
#include "mlir/Bindings/Python/IRTypes.h"
// clang-format on
+#include <iterator>
#include <optional>
+#include <type_traits>
+#include <vector>
#include "mlir-c/BuiltinAttributes.h"
#include "mlir-c/BuiltinTypes.h"
@@ -23,7 +25,22 @@ namespace nb = nanobind;
using namespace mlir;
using namespace mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN;
-using llvm::SmallVector;
+namespace {
+// Maps each element of a range through a function and returns the results in a
+// std::vector.
+template <typename Range, typename Fn>
+auto mapToVector(const Range &range, Fn fn) {
+ using OutT = std::decay_t<decltype(fn(*std::begin(range)))>;
+ std::vector<OutT> out;
+ out.reserve(std::distance(std::begin(range), std::end(range)));
+
+ for (const auto &x : range)
+ out.push_back(fn(x));
+
+ return out;
+}
+
+} // namespace
namespace mlir {
namespace python {
@@ -480,19 +497,23 @@ PyVectorType::getChecked(std::vector<int64_t> shape, PyType &elementType,
if (scalable->size() != shape.size())
throw nb::value_error("Expected len(scalable) == len(shape).");
- SmallVector<bool> scalableDimFlags = llvm::map_to_vector(
- *scalable, [](const nb::handle &h) { return nb::cast<bool>(h); });
+ std::vector<char> scalableDimFlags =
+ mapToVector(*scalable, [](const nb::handle &h) {
+ return static_cast<char>(nb::cast<bool>(h));
+ });
type = mlirVectorTypeGetScalableChecked(
- loc, shape.size(), shape.data(), scalableDimFlags.data(), elementType);
+ loc, shape.size(), shape.data(),
+ reinterpret_cast<const bool *>(scalableDimFlags.data()), elementType);
} else if (scalableDims) {
- SmallVector<bool> scalableDimFlags(shape.size(), false);
+ std::vector<char> scalableDimFlags(shape.size(), 0);
for (int64_t dim : *scalableDims) {
if (static_cast<size_t>(dim) >= scalableDimFlags.size() || dim < 0)
throw nb::value_error("Scalable dimension index out of bounds.");
- scalableDimFlags[dim] = true;
+ scalableDimFlags[dim] = 1;
}
type = mlirVectorTypeGetScalableChecked(
- loc, shape.size(), shape.data(), scalableDimFlags.data(), elementType);
+ loc, shape.size(), shape.data(),
+ reinterpret_cast<const bool *>(scalableDimFlags.data()), elementType);
} else {
type =
mlirVectorTypeGetChecked(loc, shape.size(), shape.data(), elementType);
@@ -517,19 +538,23 @@ PyVectorType PyVectorType::get(std::vector<int64_t> shape, PyType &elementType,
if (scalable->size() != shape.size())
throw nb::value_error("Expected len(scalable) == len(shape).");
- SmallVector<bool> scalableDimFlags = llvm::map_to_vector(
- *scalable, [](const nb::handle &h) { return nb::cast<bool>(h); });
- type = mlirVectorTypeGetScalable(shape.size(), shape.data(),
- scalableDimFlags.data(), elementType);
+ std::vector<char> scalableDimFlags =
+ mapToVector(*scalable, [](const nb::handle &h) {
+ return static_cast<char>(nb::cast<bool>(h));
+ });
+ type = mlirVectorTypeGetScalable(
+ shape.size(), shape.data(),
+ reinterpret_cast<const bool *>(scalableDimFlags.data()), elementType);
} else if (scalableDims) {
- SmallVector<bool> scalableDimFlags(shape.size(), false);
+ std::vector<char> scalableDimFlags(shape.size(), 0);
for (int64_t dim : *scalableDims) {
if (static_cast<size_t>(dim) >= scalableDimFlags.size() || dim < 0)
throw nb::value_error("Scalable dimension index out of bounds.");
- scalableDimFlags[dim] = true;
+ scalableDimFlags[dim] = 1;
}
- type = mlirVectorTypeGetScalable(shape.size(), shape.data(),
- scalableDimFlags.data(), elementType);
+ type = mlirVectorTypeGetScalable(
+ shape.size(), shape.data(),
+ reinterpret_cast<const bool *>(scalableDimFlags.data()), elementType);
} else {
type = mlirVectorTypeGet(shape.size(), shape.data(), elementType);
}
>From a720c9403d307d09bd8a4754c184396b6a83c5c0 Mon Sep 17 00:00:00 2001
From: RattataKing <amilyw23 at gmail.com>
Date: Mon, 9 Feb 2026 19:07:19 -0500
Subject: [PATCH 07/13] Inline regex str
Co-authored-by: Jakub Kuderski <kubakuderski at gmail.com>
---
mlir/lib/Bindings/Python/Globals.cpp | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/mlir/lib/Bindings/Python/Globals.cpp b/mlir/lib/Bindings/Python/Globals.cpp
index 36d1f182040fa..f1af6136a978e 100644
--- a/mlir/lib/Bindings/Python/Globals.cpp
+++ b/mlir/lib/Bindings/Python/Globals.cpp
@@ -25,10 +25,8 @@ namespace nb = nanobind;
using namespace mlir;
/// Local helper adapted from llvm::Regex::escape.
-namespace {
-static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
-
static std::string escapeRegex(std::string_view String) {
+ static constexpr char RegexMetachars[] = "()^$|*+?.[]\\{}";
std::string RegexStr;
for (char C : String) {
if (std::strchr(RegexMetachars, C))
@@ -37,7 +35,6 @@ static std::string escapeRegex(std::string_view String) {
}
return RegexStr;
}
-} // namespace
// -----------------------------------------------------------------------------
// PyGlobals
>From 06d95936253cdb8a3ee98f909df5ce5dcf011231 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 10 Feb 2026 07:23:36 +0000
Subject: [PATCH 08/13] Inline join ostringstream
---
.../mlir/Bindings/Python/NanobindUtils.h | 18 ------------------
mlir/lib/Bindings/Python/Globals.cpp | 14 ++++++++++++--
2 files changed, 12 insertions(+), 20 deletions(-)
diff --git a/mlir/include/mlir/Bindings/Python/NanobindUtils.h b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
index 6e43f4a6d7796..55d183ab2df42 100644
--- a/mlir/include/mlir/Bindings/Python/NanobindUtils.h
+++ b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
@@ -82,24 +82,6 @@ class Defaulting {
namespace nanobind {
namespace detail {
-/// Local helper adapted from llvm::join for a range, adding Separator between
-/// elements.
-template <typename Range>
-inline std::string joinRange(Range &&R, std::string_view Separator) {
- auto Begin = R.begin();
- auto End = R.end();
- std::string S;
- if (Begin == End)
- return S;
-
- S += *Begin;
- while (++Begin != End) {
- S += Separator;
- S += *Begin;
- }
- return S;
-}
-
/// Helper function to concatenate arguments into a `std::string`.
template <typename... Ts>
inline std::string join(const Ts &...args) {
diff --git a/mlir/lib/Bindings/Python/Globals.cpp b/mlir/lib/Bindings/Python/Globals.cpp
index f1af6136a978e..61b0d1637a03a 100644
--- a/mlir/lib/Bindings/Python/Globals.cpp
+++ b/mlir/lib/Bindings/Python/Globals.cpp
@@ -10,6 +10,7 @@
#include <cstring>
#include <optional>
+#include <sstream>
#include <string_view>
#include <vector>
@@ -296,15 +297,24 @@ void PyGlobals::TracebackLoc::registerTracebackFileExclusion(
bool PyGlobals::TracebackLoc::isUserTracebackFilename(
const std::string_view file) {
nanobind::ft_lock_guard lock(mutex);
+ auto joinWithPipe = [](const std::unordered_set<std::string> &set) {
+ std::ostringstream os;
+ for (auto it = set.begin(); it != set.end(); ++it) {
+ if (it != set.begin())
+ os << "|";
+ os << *it;
+ }
+ return os.str();
+ };
if (rebuildUserTracebackIncludeRegex) {
userTracebackIncludeRegex.assign(
- nanobind::detail::joinRange(userTracebackIncludeFiles, "|"));
+ joinWithPipe(userTracebackIncludeFiles));
rebuildUserTracebackIncludeRegex = false;
isUserTracebackFilenameCache.clear();
}
if (rebuildUserTracebackExcludeRegex) {
userTracebackExcludeRegex.assign(
- nanobind::detail::joinRange(userTracebackExcludeFiles, "|"));
+ joinWithPipe(userTracebackExcludeFiles));
rebuildUserTracebackExcludeRegex = false;
isUserTracebackFilenameCache.clear();
}
>From 43afee3da6b7c01382a5182a0d44ecb3506a3489 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 10 Feb 2026 14:47:16 +0000
Subject: [PATCH 09/13] Use memcpy and remove conditional branch
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index a1a33624a3ee4..c19ea4ff3f77a 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -7,9 +7,9 @@
//===----------------------------------------------------------------------===//
#include <algorithm>
-#include <bit>
#include <cmath>
#include <cstdint>
+#include <cstring>
#include <optional>
#include <string>
#include <string_view>
@@ -125,12 +125,10 @@ subsequent processing.
namespace {
/// Local helper checking if the current machine is little endian.
bool isLittleEndian() {
-#if defined(__cpp_lib_endian)
- return std::endian::native == std::endian::little;
-#else
const uint16_t value = 1;
- return *reinterpret_cast<const uint8_t *>(&value) == 1;
-#endif
+ unsigned char first_byte = 0;
+ std::memcpy(&first_byte, &value, 1);
+ return first_byte == 1;
}
/// Local helper adapted from llvm::scope_exit.
>From cf915d7198559d540d1e3d180f73ef9ccd1d6419 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 10 Feb 2026 15:06:43 +0000
Subject: [PATCH 10/13] Remove mapToVector and use for loop
---
mlir/lib/Bindings/Python/IRTypes.cpp | 37 ++++++++--------------------
1 file changed, 10 insertions(+), 27 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRTypes.cpp b/mlir/lib/Bindings/Python/IRTypes.cpp
index d259a2df2d706..691f22f44deae 100644
--- a/mlir/lib/Bindings/Python/IRTypes.cpp
+++ b/mlir/lib/Bindings/Python/IRTypes.cpp
@@ -11,9 +11,7 @@
#include "mlir/Bindings/Python/IRTypes.h"
// clang-format on
-#include <iterator>
#include <optional>
-#include <type_traits>
#include <vector>
#include "mlir-c/BuiltinAttributes.h"
@@ -25,23 +23,6 @@ namespace nb = nanobind;
using namespace mlir;
using namespace mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN;
-namespace {
-// Maps each element of a range through a function and returns the results in a
-// std::vector.
-template <typename Range, typename Fn>
-auto mapToVector(const Range &range, Fn fn) {
- using OutT = std::decay_t<decltype(fn(*std::begin(range)))>;
- std::vector<OutT> out;
- out.reserve(std::distance(std::begin(range), std::end(range)));
-
- for (const auto &x : range)
- out.push_back(fn(x));
-
- return out;
-}
-
-} // namespace
-
namespace mlir {
namespace python {
namespace MLIR_BINDINGS_PYTHON_DOMAIN {
@@ -497,10 +478,11 @@ PyVectorType::getChecked(std::vector<int64_t> shape, PyType &elementType,
if (scalable->size() != shape.size())
throw nb::value_error("Expected len(scalable) == len(shape).");
- std::vector<char> scalableDimFlags =
- mapToVector(*scalable, [](const nb::handle &h) {
- return static_cast<char>(nb::cast<bool>(h));
- });
+ std::vector<char> scalableDimFlags;
+ scalableDimFlags.reserve(scalable->size());
+ for (const nb::handle &h : *scalable) {
+ scalableDimFlags.push_back(nb::cast<bool>(h) ? 1 : 0);
+ }
type = mlirVectorTypeGetScalableChecked(
loc, shape.size(), shape.data(),
reinterpret_cast<const bool *>(scalableDimFlags.data()), elementType);
@@ -538,10 +520,11 @@ PyVectorType PyVectorType::get(std::vector<int64_t> shape, PyType &elementType,
if (scalable->size() != shape.size())
throw nb::value_error("Expected len(scalable) == len(shape).");
- std::vector<char> scalableDimFlags =
- mapToVector(*scalable, [](const nb::handle &h) {
- return static_cast<char>(nb::cast<bool>(h));
- });
+ std::vector<char> scalableDimFlags;
+ scalableDimFlags.reserve(scalable->size());
+ for (const nb::handle &h : *scalable) {
+ scalableDimFlags.push_back(nb::cast<bool>(h) ? 1 : 0);
+ }
type = mlirVectorTypeGetScalable(
shape.size(), shape.data(),
reinterpret_cast<const bool *>(scalableDimFlags.data()), elementType);
>From 525a3e2adcbbc33ed2a681fd3ba2fb519c496618 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 10 Feb 2026 15:07:49 +0000
Subject: [PATCH 11/13] Clangd format
---
mlir/lib/Bindings/Python/Globals.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/mlir/lib/Bindings/Python/Globals.cpp b/mlir/lib/Bindings/Python/Globals.cpp
index 61b0d1637a03a..d7f6e1f2fdeb2 100644
--- a/mlir/lib/Bindings/Python/Globals.cpp
+++ b/mlir/lib/Bindings/Python/Globals.cpp
@@ -307,14 +307,12 @@ bool PyGlobals::TracebackLoc::isUserTracebackFilename(
return os.str();
};
if (rebuildUserTracebackIncludeRegex) {
- userTracebackIncludeRegex.assign(
- joinWithPipe(userTracebackIncludeFiles));
+ userTracebackIncludeRegex.assign(joinWithPipe(userTracebackIncludeFiles));
rebuildUserTracebackIncludeRegex = false;
isUserTracebackFilenameCache.clear();
}
if (rebuildUserTracebackExcludeRegex) {
- userTracebackExcludeRegex.assign(
- joinWithPipe(userTracebackExcludeFiles));
+ userTracebackExcludeRegex.assign(joinWithPipe(userTracebackExcludeFiles));
rebuildUserTracebackExcludeRegex = false;
isUserTracebackFilenameCache.clear();
}
>From 13eb96062a40986531c6201d8d054655da15d6e4 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 10 Feb 2026 16:38:17 +0000
Subject: [PATCH 12/13] Set static type
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index c19ea4ff3f77a..f15549cfdbf0d 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -122,15 +122,15 @@ subsequent processing.
type or if the buffer does not meet expectations.
)";
-namespace {
/// Local helper checking if the current machine is little endian.
-bool isLittleEndian() {
+static bool isLittleEndian() {
const uint16_t value = 1;
unsigned char first_byte = 0;
std::memcpy(&first_byte, &value, 1);
return first_byte == 1;
}
+namespace {
/// Local helper adapted from llvm::scope_exit.
template <typename Callable>
class [[nodiscard]] scope_exit {
@@ -159,7 +159,6 @@ class [[nodiscard]] scope_exit {
template <typename Callable>
scope_exit(Callable) -> scope_exit<Callable>;
-
} // namespace
namespace mlir {
>From 1ddd69aeee286392119699ad45c21ebe9cceb563 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Tue, 10 Feb 2026 16:40:40 +0000
Subject: [PATCH 13/13] Drop cmake llvm support for mlir-py bindings
---
mlir/cmake/modules/AddMLIRPython.cmake | 1 -
1 file changed, 1 deletion(-)
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 97873d0c07ab8..8d28999585787 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -474,7 +474,6 @@ function(add_mlir_python_modules name)
MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
_PRIVATE_SUPPORT_LIB
LINK_LIBS PRIVATE
- LLVMSupport
${sources_target}
${ARG_COMMON_CAPI_LINK_LIBS}
)
More information about the Mlir-commits
mailing list