Repository navigation
Commit 5d75ad3
authored
Avoid GCC -Warray-bounds false positive on fixed-size shape copies (#2946)
# Description
GCC 13 and 15 emit `-Warray-bounds` at `-O2`/`-O3` on ordinary code
using fixed-rank containers, for example assigning `0.5 * (even + odd)`
to a 1-D `xtensor` view. The warning comes from a `std::copy` between
two `std::array<std::size_t, 1>` when the source is a member at a
non-zero offset. GCC does not fold the length of the range and warns on
the `memmove` branch that libstdc++ keeps for ranges of more than one
element. That branch never runs. Reproducer without xtensor:
```cpp
struct holder { std::size_t m_size; std::array<std::size_t, 1> m_shape; };
std::array<std::size_t, 1> f(const holder& h)
{
std::array<std::size_t, 1> s;
std::copy(h.m_shape.cbegin(), h.m_shape.cend(), s.begin());
return s;
}
```
These copies now use `std::copy_n(src.begin(), src.size(),
dst.begin())`. `size()` is a constant for `std::array`, so GCC drops the
branch. At each site the destination is sized from the same source, so
the copied elements do not change. Changed sites:
`xfunction::broadcast_shape`, the `xbroadcast` constructor, `flip`
(shape and strides), and `average` with multi-dimensional weights.
`std::copy(data(), data() + size())` and `std::ranges::copy` still warn.
No unit test, since this is a compile-time warning and CI does not build
with `-Wall` on GCC. A file using 1-D, 2-D and 3-D views, reductions,
broadcasting, `flip` and `xtensor_fixed` now compiles without warnings
with GCC 13.4 and 15.3 at `-O2` and `-O3` with `-Wall -Wextra`, with and
without xsimd.1 parent 292f0c2 commit 5d75ad3
4 files changed
Lines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
712 | 712 | | |
713 | 713 | | |
714 | 714 | | |
715 | | - | |
| 715 | + | |
716 | 716 | | |
717 | 717 | | |
718 | 718 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1960 | 1960 | | |
1961 | 1961 | | |
1962 | 1962 | | |
1963 | | - | |
| 1963 | + | |
1964 | 1964 | | |
1965 | 1965 | | |
1966 | 1966 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
840 | 840 | | |
841 | 841 | | |
842 | 842 | | |
843 | | - | |
| 843 | + | |
844 | 844 | | |
845 | 845 | | |
846 | 846 | | |
847 | 847 | | |
848 | | - | |
| 848 | + | |
849 | 849 | | |
850 | 850 | | |
851 | 851 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
293 | 293 | | |
294 | 294 | | |
295 | 295 | | |
296 | | - | |
| 296 | + | |
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
| |||
0 commit comments