[all-commits] [llvm/llvm-project] bbc297: [mlir][python] Make the Context/Operation capsule ...

Stella Laurenzo via All-commits all-commits at lists.llvm.org
Wed Dec 20 12:19:13 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: bbc29768683b394b34600347f46be2b8245ddb30
      https://github.com/llvm/llvm-project/commit/bbc29768683b394b34600347f46be2b8245ddb30
  Author: Stella Laurenzo <stellaraccident at gmail.com>
  Date:   2023-12-20 (Wed, 20 Dec 2023)

  Changed paths:
    M mlir/lib/Bindings/Python/IRCore.cpp
    M mlir/lib/Bindings/Python/IRModule.h
    M mlir/test/python/ir/context_lifecycle.py
    M mlir/test/python/ir/operation.py

  Log Message:
  -----------
  [mlir][python] Make the Context/Operation capsule creation methods work as documented. (#76010)

This fixes a longstanding bug in the `Context._CAPICreate` method
whereby it was not taking ownership of the PyMlirContext wrapper when
casting to a Python object. The result was minimally that all such
contexts transferred in that way would leak. In addition, counter to the
documentation for the `_CAPICreate` helper (see
`mlir-c/Bindings/Python/Interop.h`) and the `forContext` /
`forOperation` methods, we were silently upgrading any unknown
context/operation pointer to steal-ownership semantics. This is
dangerous and was causing some subtle bugs downstream where this
facility is getting the most use.

This patch corrects the semantics and will only do an ownership transfer
for `_CAPICreate`, and it will further require that it is an ownership
transfer (if already transferred, it was just silently succeeding).
Removing the mis-aligned behavior made it clear where the downstream was
doing the wrong thing.

It also adds some `_testing_` functions to create unowned context and
operation capsules so that this can be fully tested upstream, reworking
the tests to verify the behavior.

In some torture testing downstream, I was not able to trigger any memory
corruption with the newly enforced semantics. When getting it wrong, a
regular exception is raised.




More information about the All-commits mailing list