Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[mlir][py] Fix nanobind uninitialized values#148944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
rupprecht merged 1 commit intollvm:mainfromrupprecht:mlir-nano-msan
Jul 15, 2025

Conversation

rupprecht
Copy link
Collaborator

After#143866, we no longer always write tovalue, causing it to be uninitialized. This can lead to mysterious crashes, e.g. inpython_test.py /testCustomAttribute when we attempt to evaluateTestAttr(42), it does not setvalue, butmlirAttributeIsNull(value) happens to return false for garbage memory, and we end up trying to interpret it as a function instead of skipping it.

Fix this by only readingvalue if it has been assigned. If it hasn't,return false seems the right choice for all these methods, i.e. indicate thatfrom_python failed.

makslevental reacted with thumbs up emoji
Afterllvm#143866, we no longer always write to `value`, causing it to be uninitialized. This can lead to mysterious crashes, e.g. in `testCustomAttribute` when we attempt to evaluate `TestAttr(42)`, it does not set `value`, but `mlirAttributeIsNull(value)` happens to return false for garbage memory, and we end up trying to interpret it as a function instead of skipping it.
@llvmbot
Copy link
Member

@llvm/pr-subscribers-mlir

Author: Jordan Rupprecht (rupprecht)

Changes

After #143866, we no longer always write tovalue, causing it to be uninitialized. This can lead to mysterious crashes, e.g. inpython_test.py /testCustomAttribute when we attempt to evaluateTestAttr(42), it does not setvalue, butmlirAttributeIsNull(value) happens to return false for garbage memory, and we end up trying to interpret it as a function instead of skipping it.

Fix this by only readingvalue if it has been assigned. If it hasn't,return false seems the right choice for all these methods, i.e. indicate thatfrom_python failed.


Full diff:https://github.com/llvm/llvm-project/pull/148944.diff

1 Files Affected:

  • (modified) mlir/include/mlir/Bindings/Python/NanobindAdaptors.h (+48-24)
diff --git a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.hindex 8dcf91e5806bd..1428d5ccf00f4 100644--- a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h+++ b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h@@ -65,9 +65,11 @@ template <> struct type_caster<MlirAffineMap> {   NB_TYPE_CASTER(MlirAffineMap, const_name("MlirAffineMap"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToAffineMap(capsule->ptr());-    return !mlirAffineMapIsNull(value);+      return !mlirAffineMapIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirAffineMap v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -85,9 +87,11 @@ template <> struct type_caster<MlirAttribute> {   NB_TYPE_CASTER(MlirAttribute, const_name("MlirAttribute"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToAttribute(capsule->ptr());-    return !mlirAttributeIsNull(value);+      return !mlirAttributeIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirAttribute v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -106,9 +110,11 @@ template <> struct type_caster<MlirBlock> {   NB_TYPE_CASTER(MlirBlock, const_name("MlirBlock"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToBlock(capsule->ptr());-    return !mlirBlockIsNull(value);+      return !mlirBlockIsNull(value);+    }+    return false;   } };@@ -137,9 +143,11 @@ template <> struct type_caster<MlirDialectRegistry> {   NB_TYPE_CASTER(MlirDialectRegistry, const_name("MlirDialectRegistry"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToDialectRegistry(capsule->ptr());-    return !mlirDialectRegistryIsNull(value);+      return !mlirDialectRegistryIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirDialectRegistry v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -163,9 +171,11 @@ struct type_caster<MlirLocation> {                 .attr("Location")                 .attr("current");     }-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToLocation(capsule->ptr());-    return !mlirLocationIsNull(value);+      return !mlirLocationIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirLocation v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -183,9 +193,11 @@ template <> struct type_caster<MlirModule> {   NB_TYPE_CASTER(MlirModule, const_name("MlirModule"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToModule(capsule->ptr());-    return !mlirModuleIsNull(value);+      return !mlirModuleIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirModule v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -204,9 +216,11 @@ struct type_caster<MlirFrozenRewritePatternSet> {   NB_TYPE_CASTER(MlirFrozenRewritePatternSet,                  const_name("MlirFrozenRewritePatternSet"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToFrozenRewritePatternSet(capsule->ptr());-    return value.ptr != nullptr;+      return value.ptr != nullptr;+    }+    return false;   }   static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy,                          handle) noexcept {@@ -224,9 +238,11 @@ template <> struct type_caster<MlirOperation> {   NB_TYPE_CASTER(MlirOperation, const_name("MlirOperation"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToOperation(capsule->ptr());-    return !mlirOperationIsNull(value);+      return !mlirOperationIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirOperation v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -246,9 +262,11 @@ template <> struct type_caster<MlirValue> {   NB_TYPE_CASTER(MlirValue, const_name("MlirValue"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToValue(capsule->ptr());-    return !mlirValueIsNull(value);+      return !mlirValueIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirValue v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -269,9 +287,11 @@ template <> struct type_caster<MlirPassManager> {   NB_TYPE_CASTER(MlirPassManager, const_name("MlirPassManager"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToPassManager(capsule->ptr());-    return !mlirPassManagerIsNull(value);+      return !mlirPassManagerIsNull(value);+    }+    return false;   } };@@ -280,9 +300,11 @@ template <> struct type_caster<MlirTypeID> {   NB_TYPE_CASTER(MlirTypeID, const_name("MlirTypeID"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToTypeID(capsule->ptr());-    return !mlirTypeIDIsNull(value);+      return !mlirTypeIDIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirTypeID v, rv_policy,                          cleanup_list *cleanup) noexcept {@@ -302,9 +324,11 @@ template <> struct type_caster<MlirType> {   NB_TYPE_CASTER(MlirType, const_name("MlirType"))   bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {-    if (auto capsule = mlirApiObjectToCapsule(src))+    if (auto capsule = mlirApiObjectToCapsule(src)) {       value = mlirPythonCapsuleToType(capsule->ptr());-    return !mlirTypeIsNull(value);+      return !mlirTypeIsNull(value);+    }+    return false;   }   static handle from_cpp(MlirType t, rv_policy,                          cleanup_list *cleanup) noexcept {

Copy link
Contributor

@maksleventalmakslevental left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

That's my fault - I was being too clever 😓. This is actually the pattern that was here before and I changed it to the more terse version thinking it was an oversight but I guess it was exactly to catch this error.

Thanks.

@rupprechtrupprecht merged commit1a940bf intollvm:mainJul 15, 2025
10 of 11 checks passed
@rupprecht
Copy link
CollaboratorAuthor

That's my fault - I was being too clever 😓. This is actually the pattern that was here before and I changed it to the more terse version thinking it was an oversight but I guess it was exactly to catch this error.

Thanks.

No problem, that's why we have tests :)

makslevental reacted with heart emoji

@makslevental
Copy link
Contributor

The windows fail here is/was due to#148963

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@maksleventalmaksleventalmakslevental approved these changes

@ftynseftynseAwaiting requested review from ftynse

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@rupprecht@llvmbot@makslevental

[8]ページ先頭

©2009-2025 Movatter.jp