public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "xry111 at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/110557] New: Wrong code for x86_64-linux-gnu with -O3 -mavx2: vectorized loop mishandles signed bit-fields
Date: Wed, 05 Jul 2023 11:10:20 +0000	[thread overview]
Message-ID: <bug-110557-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 110557
           Summary: Wrong code for x86_64-linux-gnu with -O3 -mavx2:
                    vectorized loop mishandles signed bit-fields
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xry111 at gcc dot gnu.org
  Target Milestone: ---

$ cat test.cc

#include <cstddef>

size_t max(size_t a, size_t b) { return a < b ? b : a; }

struct Item {
  int x : 8;
  std::ptrdiff_t y : 56;
};

__attribute__((noipa)) std::size_t test(Item *a, int cnt) {
  std::size_t size = 0;
  for (int i = 0; i < cnt; i++)
    size = max(static_cast<size_t>(-(a[i].y * 4)), size);
  return size;
}

int main() {
  struct Item items[] = {
      {1, -1},
      {2, -2},
      {3, -3},
      {4, -4},
  };

  if (test(items, 4) != 16)
    __builtin_trap();
}

$ g++ test.cc -Wall -Wextra -O3 -fsanitize=undefined
$ ./a.out && echo ok
ok
$ g++ test.cc -Wall -Wextra -O3 -mavx2
$ ./a.out && echo ok
Illegal instruction (core dumped)

Reproducible with GCC 13.1 and trunk.  Interestingly if I convert the test case
to  C (instead of C++) the issue won't reproduce.

This test case is reduced from WebKit, it crashes with GCC 13.1 and -O3 -mavx2.
 The WebKit code has "<< 2" instead of "* 4", thus invoking an undefined
behavior.  But it still crashes even if I change "<< 2" to "* 4".

             reply	other threads:[~2023-07-05 11:10 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-05 11:10 xry111 at gcc dot gnu.org [this message]
2023-07-05 11:11 ` [Bug target/110557] [13/14 Regression] " xry111 at gcc dot gnu.org
2023-07-05 13:15 ` [Bug tree-optimization/110557] " pinskia at gcc dot gnu.org
2023-07-06  8:17 ` rguenth at gcc dot gnu.org
2023-07-06 10:17 ` avieira at gcc dot gnu.org
2023-07-06 10:21 ` xry111 at gcc dot gnu.org
2023-07-06 11:30 ` xry111 at gcc dot gnu.org
2023-07-06 14:58 ` avieira at gcc dot gnu.org
2023-07-06 16:18 ` xry111 at gcc dot gnu.org
2023-07-06 16:32 ` xry111 at gcc dot gnu.org
2023-07-10 10:36 ` cvs-commit at gcc dot gnu.org
2023-07-10 10:36 ` cvs-commit at gcc dot gnu.org
2023-07-10 10:38 ` xry111 at gcc dot gnu.org
2023-07-11  8:16 ` xry111 at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110557-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).