Bug 106020 - Spurious warnings about stringop overflows with -march=skylake -O3
Summary: Spurious warnings about stringop overflows with -march=skylake -O3
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: diagnostic
: 106074 (view as bug list)
Depends on:
Blocks: Wstringop-overflow
  Show dependency treegraph
Reported: 2022-06-17 21:05 UTC by Matt Godbolt
Modified: 2023-07-18 01:52 UTC (History)
5 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2023-05-18 00:00:00

single file testcase (24.17 KB, application/x-xz)
2023-05-18 02:57 UTC, Andrew Pinski
Self contained testcase (344 bytes, text/plain)
2023-05-18 03:40 UTC, Andrew Pinski

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Godbolt 2022-06-17 21:05:15 UTC
When using Howard Hinnant's date library, and GCC 12.1 on x86, and then with LTO enabled, there are many apparently spurious errors after the read.constprop pass:

/opt/compiler-explorer/libs/date/v3.0.1/include/date/date.h: In function 'read.constprop':
/opt/compiler-explorer/libs/date/v3.0.1/include/date/date.h:6506:15: note: destination object 'buf' of size 11
 6506 |         CharT buf[std::numeric_limits<unsigned>::digits10+2u] = {};

It has proven tricky to get a smaller case but https://godbolt.org/z/hPaqz7TY1 demonstrates it with an inline version of date.h - a smaller version that doesn't inline the date library is https://godbolt.org/z/e75YG6ano and the code amounts to:

std::chrono::system_clock::time_point parse_datetime(const std::string &sv) {
    std::istringstream ss{sv};
    std::chrono::system_clock::time_point tp;
    ss >> date::parse("%FT%T%Z", tp); // ignoring errors for simplicity
    return tp;

(though it has to be linked in a separate TU with LTO enabled to trigger the issues).

I initially filed this as a bug in the date library itself: https://github.com/HowardHinnant/date/issues/740 but we now believe this is a compiler issue.

Compiling with GCC 11 or earlier, or with clang, or without LTO doesn't trigger these warnings.
Comment 1 Matt Godbolt 2022-06-17 21:06:06 UTC
Apologies for the unreduced issue, if I get a chance I'll try and shorten it, but I hoped someone might recognise what the issue is from just this.
Comment 2 Matt Godbolt 2022-06-17 21:07:49 UTC
There are many hundreds of similar errors in that example; perhaps this example is more of a clue:

/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/move.h:205:11: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
  205 |       __a = _GLIBCXX_MOVE(__b);

(which seems very suspicious, size 0 inside std::move?)
Comment 3 Andrew Pinski 2022-06-18 05:15:52 UTC
Can you attach both files instead of the godbolt link?
Comment 4 Andrew Pinski 2022-06-18 05:21:18 UTC
I suspect it is warning on some unreachable code which is not optimized away until later. Until a full testcase is attached, it is going to be hard. Also it would be better if not using cmake, just use a makefile if needed or just a normal shell script to describe how to compile the files (cmake adds so much stuff to it is sometimes hard to tell if it is doing the right thing).
Comment 5 Matt Godbolt 2022-06-18 13:45:35 UTC
Thanks! Understood re: cmake; I wouldn't have picked it but it was the easiest way to repro something on compiler explorer for Howard at the time. I'm sure we can get it down to a smaller cast and a shell script.
Comment 6 Matt Godbolt 2022-06-18 14:02:08 UTC
I'm afraid to say I've been unable to make a repro case in the short time I had to try - will get back to this but about to go on vacation (!). That's to say dumping the files from CE and using:

CXXFLAGS=-O3 -flto -fno-fat-lto-objects -fPIC
app: main.o date.o
	$(CXX) $(CXXFLAGS) -o app date.o main.o
	$(CXX) $(CXXFLAGS) -o main.o -c main.cpp
	$(CXX) $(CXXFLAGS) -o date.o -c date.cpp
	rm -f app main.o date.o

did _not_ cause the problem, so there's something else going on. Again, will try and get to this later, but apologies for the "hit and run" bug report :(
Comment 7 Andrew Pinski 2023-05-18 02:57:15 UTC
Created attachment 55107 [details]
single file testcase

`-O3 -W -Wall  t.cc -flto  -march=skylake` is enough to invoke the warning.

NOTE -march=skylake is important.
I think this might be a dup of another vectorizer issue with this warning then.
Comment 8 Andrew Pinski 2023-05-18 02:58:19 UTC
(In reply to Andrew Pinski from comment #7)
> Created attachment 55107 [details]
> single file testcase
> `-O3 -W -Wall  t.cc -flto  -march=skylake` is enough to invoke the warning.
> NOTE -march=skylake is important.
> I think this might be a dup of another vectorizer issue with this warning
> then.

Oh you don't need -flto to invoke the warning either.
Comment 9 Andrew Pinski 2023-05-18 03:29:40 UTC
Reduced much further (still has includes):
#include <limits>
#include <algorithm>
void f(const char*);
typedef char CharT;
void read(unsigned u)
        CharT buf[std::numeric_limits<unsigned>::digits10+2u] = {};
        CharT* e = buf;
            *e++ = static_cast<CharT>(CharT(u % 10) + CharT{'0'});
            u /= 10;
        } while (u > 0);
        e[0] = 0;
        std::reverse(buf, e);
Just compile with `-std=c++20 -O3 -march=skylake` is enough to get the warning.
Comment 10 Andrew Pinski 2023-05-18 03:40:02 UTC
Created attachment 55108 [details]
Self contained testcase

Still need `-O3 -march=skylake`
Comment 11 Andrew Pinski 2023-05-18 03:43:52 UTC
Confirmed at this point even with a nice reduced testcase for someone to look into.
Comment 12 Andrew Pinski 2023-05-18 03:58:35 UTC
*** Bug 106074 has been marked as a duplicate of this bug. ***
Comment 13 Matt Godbolt 2023-05-18 13:23:30 UTC
Thanks Andrew!