pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[QUESTION] pybind11+eigen warning in O2 optimization with gcc

Open anciaux opened this issue 4 years ago • 3 comments

Thanks for maintaining this project. I love it.

When I compile the ode below, mainly involved with the use of Eigen+Pybind11 I have unexpected compilation warnings appearing:

#include <Eigen/Dense>
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>

namespace py = pybind11;

void foo() {
  py::object val;
  pybind11::cast<Eigen::Vector3d>(val);
}

I compile it with: g++ -Wall -c -O2 -I $EIGEN_INCLUDE_PATH -I $PYBIND11_INCLUDE_PATH -I $PYTHON_INCLUDE_PATH test.cc to obtain:

Eigen/src/Core/DenseStorage.h:211:9: warning: ‘<anonymous>’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  211 |         m_data = std::move(other.m_data);

This suggest a temporary object is partially initiallized during the cast (or confuses the compiler) I am trying to remove such worrying warnings for sanity. Any help is really appreciated.

Information of the system setup:

eigen: 391094c50743f28f9174f455661f650bf07e0177 pybind11: f61855b9d8821e2576960109a2a67379a6c2366f python-3.8 compiler: g++ (Debian 10.2.1-6) 10.2.1 20210110

anciaux avatar May 21 '21 15:05 anciaux

I am having the same issue

New install of Ubuntu 22.04,

  • Eigen 3.4.0-2ubuntu2
  • Python 3.10.4
  • g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
  • pybind11-dev 2.9.1-2
/* Simple binding which produces warnings under g++11 */

#include <stdio.h>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <pybind11/eigen.h>
#include <pybind11/cast.h>

void init_bistest(pybind11::module &m)
{
    m.def(
        "PassVector3d", [](const Eigen::Vector3d& v) {
            fprintf(stderr, "test=(%9.4f,%9.4f,%9.4f)\n", v(0), v(1), v(2));
        },
        pybind11::arg("v"),
        "Test passing Eigen Vector3d using Pybind11"
    );
}
$ g++ -O3 -std=c++17 -Wall -I/usr/include/python3.10 -I/usr/lib/python3/dist-packages/pybind11/include -I /usr/include/eigen3 -c pybind/pb_bind_bistest.cpp -o ../build/rbuild/pybind/pb_bind_bistest.o

In file included from /usr/include/eigen3/Eigen/Core:294,
                from /usr/lib/python3/dist-packages/pybind11/include/pybind11/eigen.h:30,
                from pybind/pb_bind_bistest.cpp:8:
In member function ‘Eigen::PlainObjectBase<Derived>& Eigen::PlainObjectBase<Derived>::operator=(Eigen::PlainObjectBase<Derived>&&) [with Derived = Eigen::Matrix<double, 3, 1>]’,
   inlined from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::operator=(Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&&) [with _Scalar = double; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 3; int _MaxCols = 1]’ at /usr/include/eigen3/Eigen/src/Core/Matrix.h:281:22,
   inlined from ‘bool pybind11::detail::type_caster<Type, typename std::enable_if<std::integral_constant<bool, (pybind11::detail::negation<std::integral_constant<bool, (decltype (pybind11::detail::is_template_base_of_impl<Eigen::DenseBase>::check((typename pybind11::detail::intrinsic_type<T>::type*)(nullptr)))::value && std::is_base_of<Eigen::MapBase<Derived, 0>, T>::value)> >::value && decltype (pybind11::detail::is_template_base_of_impl<Eigen::PlainObjectBase>::check((typename pybind11::detail::intrinsic_type<T>::type*)(nullptr)))::value)>::value, void>::type>::load(pybind11::handle, bool) [with Type = Eigen::Matrix<double, 3, 1>]’ at /usr/lib/python3/dist-packages/pybind11/include/pybind11/eigen.h:276:15:
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:504:7: warning: ‘*(Eigen::DenseStorage<double, 3, 3, 1, 0>*)((char*)&<unnamed> + offsetof(Eigen::Matrix<double, 3, 1, 0, 3, 1>,Eigen::Matrix<double, 3, 1, 0, 3, 1>::<unnamed>.Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >::<unnamed>.Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >::<unnamed>.Eigen::DenseBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >::<unnamed>.Eigen::DenseCoeffsBase<Eigen::Matrix<double, 3, 1, 0, 3, 1>, 3>::<unnamed>.Eigen::DenseCoeffsBase<Eigen::Matrix<double, 3, 1, 0, 3, 1>, 1>::<unnamed>.Eigen::DenseCoeffsBase<Eigen::Matrix<double, 3, 1, 0, 3, 1>, 0>::<unnamed>))’ may be used uninitialized [-Wmaybe-uninitialized]
 504 |       m_storage = std::move(other.m_storage);
     |       ^~~~~~~~~
In file included from pybind/pb_bind_bistest.cpp:8:
/usr/lib/python3/dist-packages/pybind11/include/pybind11/eigen.h: In member function ‘bool pybind11::detail::type_caster<Type, typename std::enable_if<std::integral_constant<bool, (pybind11::detail::negation<std::integral_constant<bool, (decltype (pybind11::detail::is_template_base_of_impl<Eigen::DenseBase>::check((typename pybind11::detail::intrinsic_type<T>::type*)(nullptr)))::value && std::is_base_of<Eigen::MapBase<Derived, 0>, T>::value)> >::value && decltype (pybind11::detail::is_template_base_of_impl<Eigen::PlainObjectBase>::check((typename pybind11::detail::intrinsic_type<T>::type*)(nullptr)))::value)>::value, void>::type>::load(pybind11::handle, bool) [with Type = Eigen::Matrix<double, 3, 1>]’:
/usr/lib/python3/dist-packages/pybind11/include/pybind11/eigen.h:276:17: note: ‘<anonymous>’ declared here
 276 |         value = Type(fits.rows, fits.cols);
     |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~

petrochemical avatar Sep 19 '22 22:09 petrochemical