[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:22:57 PDT 2026


https://github.com/kuhar created https://github.com/llvm/llvm-project/pull/186190

MlirStringRef is copied into a Python str by nanobind's type_caster anyway, so the intermediate std::string was a redundant allocation.

>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] [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;
       },



More information about the Mlir-commits mailing list