mpl icon indicating copy to clipboard operation
mpl copied to clipboard

Fix GCC -Wmaybe-uninitialized

Open zhao-shihan opened this issue 8 months ago • 1 comments

I find that allgathering std::array<char, N> (N>=229) will trigger a compile warning with GCC 14.2/13.3/12.4 (but ok for Clang). Here is an example:

#include <mpl/mpl.hpp>

int main() {
  constexpr std::size_t n{229}; // n >= 229 will trigger gcc 14.2 -Wmaybe-uninitialized

  const auto& commWorld{mpl::environment::comm_world()};
  std::array<char, n> a{};
  std::vector<std::array<char, n>> b(commWorld.size());
  commWorld.allgather(a, b.data());

  return EXIT_SUCCESS;
}

And the warning looks like this:

In file included from /home/zhaoshihan/Documents/repos/mpl/mpl/mpl.hpp:51,
                 from /home/zhaoshihan/Documents/repos/mpl/examples/arrays.cc:4:
In member function ‘mpl::struct_layout<S>& mpl::struct_layout<S>::register_struct(const S&) [with S = std::array<char, 256>]’,
    inlined from ‘mpl::struct_builder<std::array<_Tp, _Nm> >::struct_builder() [with T = char; long unsigned int N = 256]’ at /home/zhaoshihan/Documents/repos/mpl/mpl/datatype.hpp:402:30:
/home/zhaoshihan/Documents/repos/mpl/mpl/datatype.hpp:135:22: warning: ‘array’ may be used uninitialized [-Wmaybe-uninitialized]
  135 |       MPI_Get_address(const_cast<S *>(&x), &base_);
      |       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/zhaoshihan/Documents/repos/mpl/mpl/mpl.hpp:5:
/usr/lib/x86_64-linux-gnu/openmpi/include/mpi.h: In constructor ‘mpl::struct_builder<std::array<_Tp, _Nm> >::struct_builder() [with T = char; long unsigned int N = 256]’:
/usr/lib/x86_64-linux-gnu/openmpi/include/mpi.h:1677:20: note: by argument 1 of type ‘const void*’ to ‘int MPI_Get_address(const void*, MPI_Aint*)’ declared here
 1677 | OMPI_DECLSPEC  int MPI_Get_address(const void *location, MPI_Aint *address);
      |                    ^~~~~~~~~~~~~~~
/home/zhaoshihan/Documents/repos/mpl/mpl/datatype.hpp:401:24: note: ‘array’ declared here
  401 |       std::array<T, N> array;
      |                        ^~~~~

The interesting thing is if N<229 the warning is gone.

Nevertheless, I believe we can simply add a value initialization to make GCC happy since this array is just a template here.

zhao-shihan avatar Jun 17 '25 13:06 zhao-shihan

I also noticed a unused displs local variables and I removed them.

zhao-shihan avatar Jun 17 '25 18:06 zhao-shihan

Thanks, looks good to me.

rabauke avatar Oct 17 '25 23:10 rabauke