[Mlir-commits] [mlir] [mlir][python] Avoid needless std::string copies in properties. NFC. (PR #186190)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Mar 12 10:23:33 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Jakub Kuderski (kuhar)
<details>
<summary>Changes</summary>
MlirStringRef is copied into a Python str by nanobind's type_caster anyway, so the intermediate std::string was a redundant allocation.
---
Full diff: https://github.com/llvm/llvm-project/pull/186190.diff
2 Files Affected:
- (modified) mlir/lib/Bindings/Python/DialectLLVM.cpp (+7-8)
- (modified) mlir/lib/Bindings/Python/IRAttributes.cpp (+4-6)
``````````diff
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;
},
``````````
</details>
https://github.com/llvm/llvm-project/pull/186190
More information about the Mlir-commits
mailing list