axom icon indicating copy to clipboard operation
axom copied to clipboard

Fix "pointless comparison of unsigned integer to zero" warning from fmt memory_buffer

Open kennyweiss opened this issue 7 months ago • 0 comments

A user reported the following warning from axom's fmt using clang on LLNL's rzansel:

.../clang-ibm-16.0.6-cuda-11.2.0-gcc-8.3.1/axom/0.9.1a-cuda/include/axom/fmt/core.h(411): warning: pointless comparison of unsigned integer with zero
          detected during:
            instantiation of "auto axom::fmt::v9::detail::to_unsigned(Int)->std::make_unsigned<Int>::type [with Int=size_t]"
.../clang-ibm-16.0.6-cuda-11.2.0-gcc-8.3.1/axom/0.9.1a-cuda/include/axom/fmt/format.h(570): here
            instantiation of "auto axom::fmt::v9::detail::fill_n(T *, Size, char)->T * [with T=char, Size=size_t]"
.../clang-ibm-16.0.6-cuda-11.2.0-gcc-8.3.1/axom/0.9.1a-cuda/include/axom/fmt/format.h(843): here
            instantiation of "axom::fmt::v9::basic_memory_buffer<T, SIZE, Allocator>::basic_memory_buffer(const Allocator &) [with T=char, SIZE=500UL, Allocator=std::allocator<char>]"
.../clang-ibm-16.0.6-cuda-11.2.0-gcc-8.3.1/axom/0.9.1a-cuda/include/axom/fmt/format-inl.h(72): here

This seems to arise from the following fmt function when adding a (compile time) empty string to a memory_buffer:

// <algorithm> is spectacularly slow to compile in C++20 so use a simple fill_n
// instead (#1998).
template <typename OutputIt, typename Size, typename T>
FMT_CONSTEXPR auto fill_n(OutputIt out, Size count, const T& value)
    -> OutputIt {
  for (Size i = 0; i < count; ++i) *out++ = value;
  return out;
}

https://sourcegraph.com/github.com/fmtlib/fmt@b61c8c3d23b7e6fdf9d44593877dba1c8a291be1/-/blob/include/fmt/format.h?L573 called from: https://sourcegraph.com/github.com/fmtlib/fmt/-/blob/include/fmt/format.h?L872

Once we can reproduce the warning, some possible proposals for resolving it include:

  • avoid adding a compile-time detectable empty string to a memory_buffer, if we can find the source of this warning
  • attempting to patch the function to avoid the comparison (if possible), e.g. wrapping it with
     if(count>0) {...}
    
    under the assumption (guess) that the compiler will not issue a warning for the explicit conditional
  • adding a pragma to disable this warning, e.g.
  #pragma GCC diagnostic push
  #pragma GCC diagnostic ignored "-Wtype-limits"
    {...}
  #pragma GCC diagnostic pop

kennyweiss avatar Jun 28 '24 19:06 kennyweiss