[Bug ipa/100831] New: Top-level function invocation not shown for warning in inlined function

2021-05-30 Thread jl_gccbugs at conductive dot de via Gcc-bugs
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 
> #include 
> 
> 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 __m128i*>([i]));
> __m128i output = _mm_shuffle_epi8(input, shufmask);
> _mm_storeu_si128(reinterpret_cast<__m128i*>([i]), output);
>   }
>   for (; i < count; ++i) {  // handle residual elements
> dest[i] = __builtin_bswap32(src[i]);
>   }
> }

main.cpp:
> #include 
> #include 
> 
> 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(this),
>   reinterpret_cast(ptr),
>   sizeof(SomeStruct) / 4);
>   }
> 
>   void Store(void* ptr) {
> copy_and_swap_32_unaligned(reinterpret_cast(ptr),
>   reinterpret_cast(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,0x, 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 0x500 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;
  }

[Bug tree-optimization/100801] Aggressive loop optimizations cause incorrect warning

2021-05-28 Thread jl_gccbugs at conductive dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100801

--- Comment #2 from Joel Linn  ---
Great. In the meantime I will use 
> if (count % 4 == 0) __builtin_unreachable();
at the start of the for loop to suppress the warning as suggested by Martin
Sebor https://gcc.gnu.org/pipermail/gcc-help/2021-May/140339.html

[Bug ipa/100801] New: Aggressive loop optimizations cause incorrect warning

2021-05-27 Thread jl_gccbugs at conductive dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100801

Bug ID: 100801
   Summary: Aggressive loop optimizations cause incorrect warning
   Product: gcc
   Version: 11.1.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 warning is triggered

> $ gcc-11 -c constproploopopt.c -O2 -Wall -mavx -g
> constproploopopt.c: In function ‘test’:
> constproploopopt.c:22:18: warning: iteration 4611686018427387903 invokes 
> undefined behavior [-Waggressive-loop-optimizations]
>22 | dest[i] = src[i];
>   |  ^
> constproploopopt.c:21:12: note: within this loop
>21 |   for (; i < count; ++i) {  // handle residual elements
>   |  ~~^~~

by this (minimal) code:

> #include 
> #include 
> #if defined(_MSC_VER)
> #include 
> #else
> #include 
> #endif
> 
> void copy_32_unaligned(uint32_t* dest, const uint32_t* src, size_t count) {
>   // invariant/nop
>   __m128i shufmask =  _mm_set_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 
> 3, 2, 1, 0);
> 
>   size_t i;
>   for (i = 0; i + 4 <= count; i += 4) {
> __m128i input = _mm_loadu_si128((const __m128i*)([i]));
> __m128i output = input;
> // no warning without the shuffle:
> output = _mm_shuffle_epi8(input, shufmask);
> _mm_storeu_si128((__m128i*)([i]), output);
>   }
>   for (; i < count; ++i) {  // handle residual elements
> dest[i] = src[i];
>   }
> }
> 
> void test(uint32_t* buf1, uint32_t* buf2) {
> copy_32_unaligned(buf2, buf1,
>   // multiples of 4 and greater or equal then 12 trigger 
> it:
>   12);
> }

>From objdump output I believe the generated code is correct though. The warning
seems to be incorrect in this context, especially since the "residual" loop
should be skipped for count=n*4 anyways.