https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100831

            Bug ID: 100831
           Summary: Top-level function invocation not shown for warning in
                    inlined function
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ipa
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jl_gccbugs at conductive dot de
                CC: marxin at gcc dot gnu.org
  Target Milestone: ---

The following lto warning does not point to the top-level invocation or show
the invocation stack. The warning however depends on the parameters
`copy_and_swap_32_unaligned()` is called with and therefore should do so.

> $ g++ -flto -c -O2 -mavx -Wall -Wpedantic -Wextra func.cpp
> $ g++ -flto -c -O2 -mavx -Wall -Wpedantic -Wextra main.cpp
> $ g++ -flto func.o main.o
> func.cpp: In function ‘copy_and_swap_32_unaligned.constprop’:
> func.cpp:14:38: warning: iteration 4611686018427387903 invokes undefined 
> behavior [-Waggressive-loop-optimizations]
>    14 |     dest[i] = __builtin_bswap32(src[i]);
>       |                                      ^
> func.cpp:13:12: note: within this loop
>    13 |   for (; i < count; ++i) {  // handle residual elements
>       |            ^

The body of `copy_and_swap_32_unaligned()` does not really matter here, it is
subject of another bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100801)

func.cpp:
> #include <cstdint>
> #include <x86intrin.h>
> 
> void copy_and_swap_32_unaligned(uint32_t* dest, const uint32_t* src, size_t 
> count) {
>   __m128i shufmask =  _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 
> 0, 1, 2, 3);
> 
>   size_t i;
>   for (i = 0; i + 4 <= count; i += 4) {
>     __m128i input = _mm_loadu_si128(reinterpret_cast<const 
> __m128i*>(&src[i]));
>     __m128i output = _mm_shuffle_epi8(input, shufmask);
>     _mm_storeu_si128(reinterpret_cast<__m128i*>(&dest[i]), output);
>   }
>   for (; i < count; ++i) {  // handle residual elements
>     dest[i] = __builtin_bswap32(src[i]);
>   }
> }

main.cpp:
> #include <cstdint>
> #include <cstdio>
> 
> void copy_and_swap_32_unaligned(uint32_t* dest, const uint32_t* src, size_t 
> count);
> 
> struct SomeStruct {
>   uint32_t data[12];
> 
>   explicit SomeStruct(const void* ptr) {
>     copy_and_swap_32_unaligned(reinterpret_cast<uint32_t*>(this),
>                       reinterpret_cast<const uint32_t*>(ptr),
>                       sizeof(SomeStruct) / 4);
>   }
> 
>   void Store(void* ptr) {
>     copy_and_swap_32_unaligned(reinterpret_cast<uint32_t*>(ptr),
>                       reinterpret_cast<const uint32_t*>(this),
>                       sizeof(SomeStruct) / 4);
>   }
> };
> 
> void test(uint32_t* buf) {
>   SomeStruct ctx(buf);
>   ctx.data[0]++;
>   ctx.data[1] += 5;
>   ctx.Store(buf);
> }
> 
> int main() {
>   uint32_t x[12] = {0x12345678,0x00000000, 0xA0B0C0D0, 0xDEADBEEF};
>   printf("0x%X 0x%X 0x%X 0x%X\n", x[0], x[1], x[2], x[3]);
>   // 0x12345678 0x0 0xA0B0C0D0 0xDEADBEEF
>   test(x);
>   printf("0x%X 0x%X 0x%X 0x%X\n", x[0], x[1], x[2], x[3]);
>   // 0x13345678 0x5000000 0xA0B0C0D0 0xDEADBEEF
> }

An untested patch has been suggested by Martin Sebor on the mailing list thread
(https://gcc.gnu.org/pipermail/gcc-help/2021-May/140339.html) related to the
other bug mentioned:

diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c
index b5add827018..fcc6e39e3f6 100644
--- a/gcc/tree-ssa-loop-niter.c
+++ b/gcc/tree-ssa-loop-niter.c
@@ -3389,7 +3389,7 @@ do_warn_aggressive_loop_optimizations (class loop 
*loop,
              ? UNSIGNED : SIGNED);
    auto_diagnostic_group d;
    if (warning_at (gimple_location (stmt), 
OPT_Waggressive_loop_optimizations,
-                 "iteration %s invokes undefined behavior", buf))
+                 "%Giteration %s invokes undefined behavior", stmt, buf))
      inform (gimple_location (estmt), "within this loop");
    loop->warned_aggressive_loop_optimizations = true;
  }

Reply via email to