[Bug c++/107452] New: Failed to catch C++ exception thrown from multiarch-function (x64 CPUs)

2022-10-28 Thread kim.walisch at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107452

Bug ID: 107452
   Summary: Failed to catch C++ exception thrown from
multiarch-function (x64 CPUs)
   Product: gcc
   Version: 11.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: kim.walisch at gmail dot com
  Target Milestone: ---

Hi,

Tested using: GCC 11.2.0, Ubuntu 22.10 x64
Tested using: GCC 9.4.0, Ubuntu 18.04 x64

I am using the GCC multiarch feature (also known as function multiversioning:
https://gcc.gnu.org/onlinedocs/gcc/Function-Multiversioning.html) in my
primesieve C++ project to take advantage of the latest supported CPU
instruction set e.g. AVX, AVX2, AVX512 (on x64 CPUs).

Today I found out that if I throw a C++ exception from a multiarch-function and
I try to catch that exception outside of the originating multiarch-function but
within the same translation unit, then catching the exception fails and my
program simply aborts.

My exception is thrown from here:
https://github.com/kimwalisch/primesieve/blob/776c102f92905401613a83508d60744d41df7c73/src/PrimeGenerator.cpp#L332
It should be caught here:
https://github.com/kimwalisch/primesieve/blob/776c102f92905401613a83508d60744d41df7c73/src/iterator-c.cpp#L151



My bug can be reproduced using these steps:

git clone https://github.com/kimwalisch/primesieve.git
cd primesieve && mkdir build && cd build
git checkout 776c102f92905401613a83508d60744d41df7c73
CXXFLAGS="-O2 -Wall -Wextra -pedantic" cmake ..  -DBUILD_TESTS=ON
-DCMAKE_BUILD_TYPE=Debug -DWITH_MULTIARCH=ON  && make -j8
test/next_prime2

The test/next_prime2 will fail with the following error message:

terminate called after throwing an instance of 'primesieve::primesieve_error'
  what():  cannot generate primes > 2^64
Aborted



If I recompile without function multiversioning (-DWITH_MULTIARCH=OFF) the same
exception is caught successfully:

rm -rf *
CXXFLAGS="-O2 -Wall -Wextra -pedantic" cmake ..  -DBUILD_TESTS=ON
-DCMAKE_BUILD_TYPE=Debug -DWITH_MULTIARCH=OFF && make -j8
test/next_prime2

The test/next_prime2 completes successfully:

...
primesieve_iterator: cannot generate primes > 2^64
next_prime(18446744073709551615) = PRIMESIEVE_ERROR:   OK
primesieve_iterator: cannot generate primes > 2^64
next_prime(18446744073709551615) = PRIMESIEVE_ERROR:   OK

All tests passed successfully!



Clang also supports function multiversioning on Linux & x64 CPUs. And with
Clang this issue is not present, with Clang catching C++ exceptions thrown from
a multiarch-function works flawlessly (tested using Clang 14.0.0 on Ubuntu
22.10 x64):

rm -rf *
CXX=clang++ CC=clang CXXFLAGS="-O2 -Wall -Wextra -pedantic" cmake .. 
-DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DWITH_MULTIARCH=ON && make -j8
test/next_prime2

The test/next_prime2 completes successfully:

...
primesieve_iterator: cannot generate primes > 2^64
next_prime(18446744073709551615) = PRIMESIEVE_ERROR:   OK
primesieve_iterator: cannot generate primes > 2^64
next_prime(18446744073709551615) = PRIMESIEVE_ERROR:   OK

All tests passed successfully!



Is this a known GCC issue? If needed I could also try to write a minimal test
that reproduces this issue.

[Bug c++/107287] New: -Wuninitialized false positive (in all GCC versions)

2022-10-17 Thread kim.walisch at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107287

Bug ID: 107287
   Summary: -Wuninitialized false positive (in all GCC versions)
   Product: gcc
   Version: 12.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: kim.walisch at gmail dot com
  Target Milestone: ---

Hi,

In my primecount C++ project I have hit a -Wuninitialized false positive with
GCC. It happens basically with every g++ version >= 8 && <= 12 that I have
tested accross Ubuntu 18.04, 20.04 and 22.04 (x64). Unfortunately I have not
been able to create a minimal test case that reliably triggers this issue on
all GCC versions. However, I was able to create a minimal test case on
godbolt.org that triggers the issue on GCC trunk:

#include 
#include 

extern double get_time();

long func(std::vector& vect, bool print)
{
double time;
if (print)
time = get_time();

int sum = 0;
for (int n : vect)
sum += n;

if (print)
std::cout << time;

return sum;
}

When compiled with "x86-64 gcc (trunk)" and "-O3 -Wall -Werror -pedantic" this
produces the following warning (see https://godbolt.org/z/qv3W8j44b):



In file included from
/opt/compiler-explorer/gcc-trunk-20221017/include/c++/13.0.0/iostream:41,
 from :1:
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char;
_Traits = std::char_traits]',
inlined from 'long int func(std::vector&, bool)' at :18:22:
/opt/compiler-explorer/gcc-trunk-20221017/include/c++/13.0.0/ostream:223:25:
error: 'time' may be used uninitialized [-Werror=maybe-uninitialized]
  223 |   { return _M_insert(__f); }
  |~^
: In function 'long int func(std::vector&, bool)':
:9:12: note: 'time' was declared here
9 | double time;
  |^~~~
cc1plus: all warnings being treated as errors
Compiler returned: 1



Using my primecount project this GCC warning can be reproduced reliably using:


git clone https://github.com/kimwalisch/primecount.git
git checkout git checkout 57f18f9207dc00aaadcd3f4c1e76e320e1387061
cmake . -DCMAKE_CXX_FLAGS="-Wall -Wextra -pedantic -Werror"
make -j8

My code is basically identical to the minimal test case above
(https://github.com/kimwalisch/primecount/blob/57f18f9207dc00aaadcd3f4c1e76e320e1387061/src/phi.cpp#L386):


int64_t phi(int64_t x,
int64_t a,
int threads,
bool is_print)
{
  double time;

  if (is_print)
  {
print("");
print("=== phi(x, a) ===");
time = get_time();
  }

  int64_t sum = phi_OpenMP(x, a, threads);

  if (is_print)
print("phi", sum, time);

  return sum;
}


Also note that there is no warning if my code is compiled with Clang and
-Wuninitialized.

Regards,
Kim Walisch

[Bug tree-optimization/101831] Spurious maybe-uninitialized warning on std::array::size

2022-01-21 Thread kim.walisch at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101831

kim.walisch at gmail dot com changed:

   What|Removed |Added

 CC||kim.walisch at gmail dot com

--- Comment #2 from kim.walisch at gmail dot com ---
> I'm not sure that the std::array use case is common enough to justify the  
> potential for the false negatives

I just hit the same GCC warning on completely valid code. Both Clang & MSVC
correctly do not issue any warning.

void Foo::func()
{
  std::array pos;
  assert(pos.size() == static_global_array.size());
  ...
}

In member function ‘void Foo::func()’:
warning: ‘pos’ may be used uninitialized [-Wmaybe-uninitialized]
  312 |   assert(pos.size() == buffers_.size());