[Mlir-commits] [mlir] [mlir][python] Avoid needless std::string copies in properties. NFC. (PR #186190)
Jakub Kuderski
llvmlistbot at llvm.org
Thu Mar 12 10:24:51 PDT 2026
https://github.com/kuhar updated https://github.com/llvm/llvm-project/pull/186190
>From a75f891036d54058b10cfb2840749afbee4f38c2 Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Thu, 12 Mar 2026 13:16:22 -0400
Subject: [PATCH 1/2] [mlir][python] Avoid needless std::string copies in
properties. NFC.
MlirStringRef is copied into a Python str by nanobind's type_caster
anyway, so the intermediate std::string was a redundant allocation.
Co-Authored-By: Claude Opus 4.6 <noreply at anthropic.com>
---
mlir/lib/Bindings/Python/DialectLLVM.cpp | 15 +++++++--------
mlir/lib/Bindings/Python/IRAttributes.cpp | 10 ++++------
2 files changed, 11 insertions(+), 14 deletions(-)
diff --git a/mlir/lib/Bindings/Python/DialectLLVM.cpp b/mlir/lib/Bindings/Python/DialectLLVM.cpp
index dc06d0a3bf671..58f0a3989d270 100644
--- a/mlir/lib/Bindings/Python/DialectLLVM.cpp
+++ b/mlir/lib/Bindings/Python/DialectLLVM.cpp
@@ -128,14 +128,13 @@ struct StructType : PyConcreteType<StructType> {
"name"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
"context"_a = nb::none());
- c.def_prop_ro(
- "name", [](const StructType &type) -> std::optional<std::string> {
- if (mlirLLVMStructTypeIsLiteral(type))
- return std::nullopt;
-
- MlirStringRef stringRef = mlirLLVMStructTypeGetIdentifier(type);
- return std::string(stringRef.data, stringRef.length);
- });
+ c.def_prop_ro("name",
+ [](const StructType &type) -> std::optional<MlirStringRef> {
+ if (mlirLLVMStructTypeIsLiteral(type))
+ return std::nullopt;
+
+ return mlirLLVMStructTypeGetIdentifier(type);
+ });
c.def_prop_ro("body", [](const StructType &type) -> nb::object {
// Don't crash in absence of a body.
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 997f16978fa58..bf949cd87de5f 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -511,14 +511,12 @@ void PySymbolRefAttribute::bindDerived(ClassTy &c) {
c.def_prop_ro(
"value",
[](PySymbolRefAttribute &self) {
- MlirStringRef rootRef = mlirSymbolRefAttrGetRootReference(self);
- std::vector<std::string> symbols = {
- std::string(rootRef.data, rootRef.length)};
+ std::vector<MlirStringRef> symbols = {
+ mlirSymbolRefAttrGetRootReference(self)};
for (int i = 0; i < mlirSymbolRefAttrGetNumNestedReferences(self);
++i) {
- MlirStringRef nestedRef = mlirSymbolRefAttrGetRootReference(
- mlirSymbolRefAttrGetNestedReference(self, i));
- symbols.push_back(std::string(nestedRef.data, nestedRef.length));
+ symbols.push_back(mlirSymbolRefAttrGetRootReference(
+ mlirSymbolRefAttrGetNestedReference(self, i)));
}
return symbols;
},
>From 3fe3d3f7acfa6e50edab17ce881c0c5bae8be189 Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Thu, 12 Mar 2026 13:24:38 -0400
Subject: [PATCH 2/2] Also reserve vector
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index bf949cd87de5f..ba5f184488f5f 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -511,10 +511,11 @@ void PySymbolRefAttribute::bindDerived(ClassTy &c) {
c.def_prop_ro(
"value",
[](PySymbolRefAttribute &self) {
- std::vector<MlirStringRef> symbols = {
- mlirSymbolRefAttrGetRootReference(self)};
- for (int i = 0; i < mlirSymbolRefAttrGetNumNestedReferences(self);
- ++i) {
+ intptr_t numNested = mlirSymbolRefAttrGetNumNestedReferences(self);
+ std::vector<MlirStringRef> symbols;
+ symbols.reserve(numNested + 1);
+ symbols.push_back(mlirSymbolRefAttrGetRootReference(self));
+ for (intptr_t i = 0; i < numNested; ++i) {
symbols.push_back(mlirSymbolRefAttrGetRootReference(
mlirSymbolRefAttrGetNestedReference(self, i)));
}
More information about the Mlir-commits
mailing list