[all-commits] [llvm/llvm-project] 89a92f: [mlir] Rework subclass construction in PybindAdapt...

ftynse via All-commits all-commits at lists.llvm.org
Wed Jan 19 09:09:21 PST 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 89a92fb3ba668d0a4ec25c1268c31d9b35ab17e7
      https://github.com/llvm/llvm-project/commit/89a92fb3ba668d0a4ec25c1268c31d9b35ab17e7
  Author: Alex Zinenko <zinenko at google.com>
  Date:   2022-01-19 (Wed, 19 Jan 2022)

  Changed paths:
    M mlir/cmake/modules/MLIRDetectPythonEnv.cmake
    M mlir/include/mlir/Bindings/Python/PybindAdaptors.h
    M mlir/python/mlir/dialects/python_test.py
    M mlir/python/requirements.txt
    M mlir/test/python/CMakeLists.txt
    M mlir/test/python/dialects/python_test.py
    M mlir/test/python/lib/PythonTestCAPI.cpp
    M mlir/test/python/lib/PythonTestCAPI.h
    M mlir/test/python/lib/PythonTestDialect.cpp
    M mlir/test/python/lib/PythonTestDialect.h
    M mlir/test/python/lib/PythonTestModule.cpp
    M mlir/test/python/python_test_ops.td

  Log Message:
  -----------
  [mlir] Rework subclass construction in PybindAdaptors.h

The constructor function was being defined without indicating its "__init__"
name, which made it interpret it as a regular fuction rather than a
constructor. When overload resolution failed, Pybind would attempt to print the
arguments actually passed to the function, including "self", which is not
initialized since the constructor couldn't be called. This would result in
"__repr__" being called with "self" referencing an uninitialized MLIR C API
object, which in turn would cause undefined behavior when attempting to print
in C++. Even if the correct name is provided, the mechanism used by
PybindAdaptors.h to bind constructors directly as "__init__" functions taking
"self" is deprecated by Pybind. The new mechanism does not seem to have access
to a fully-constructed "self" object (i.e., the constructor in C++ takes a
`pybind11::detail::value_and_holder` that cannot be forwarded back to Python).

Instead, redefine "__new__" to perform the required checks (there are no
additional initialization needed for attributes and types as they are all
wrappers around a C++ pointer). "__new__" can call its equivalent on a
superclass without needing "self".

Bump pybind11 dependency to 3.8.0, which is the first version that allows one
to redefine "__new__".

Reviewed By: stellaraccident

Differential Revision: https://reviews.llvm.org/D117646




More information about the All-commits mailing list