[Mlir-commits] [mlir] b45ce46 - [mlir][python] Avoid needless std::string copies in properties. NFC. (#186190)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 12 11:53:39 PDT 2026


Author: Jakub Kuderski
Date: 2026-03-12T14:53:33-04:00
New Revision: b45ce46905ca63b3b4c5f4c62397a433ce681127

URL: https://github.com/llvm/llvm-project/commit/b45ce46905ca63b3b4c5f4c62397a433ce681127
DIFF: https://github.com/llvm/llvm-project/commit/b45ce46905ca63b3b4c5f4c62397a433ce681127.diff

LOG: [mlir][python] Avoid needless std::string copies in properties. NFC. (#186190)

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>

Added: 
    

Modified: 
    mlir/lib/Bindings/Python/DialectLLVM.cpp
    mlir/lib/Bindings/Python/IRAttributes.cpp

Removed: 
    


################################################################################
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..ba5f184488f5f 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -511,14 +511,13 @@ 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)};
-        for (int i = 0; i < mlirSymbolRefAttrGetNumNestedReferences(self);
-             ++i) {
-          MlirStringRef nestedRef = mlirSymbolRefAttrGetRootReference(
-              mlirSymbolRefAttrGetNestedReference(self, i));
-          symbols.push_back(std::string(nestedRef.data, nestedRef.length));
+        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)));
         }
         return symbols;
       },


        


More information about the Mlir-commits mailing list