public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tomas.kalibera at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/105198] New: Wrong code for C loop (GCC 12 -O2, GCC 11 -O3)
Date: Thu, 07 Apr 2022 17:02:53 +0000	[thread overview]
Message-ID: <bug-105198-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105198
           Summary: Wrong code for C loop (GCC 12 -O2, GCC 11 -O3)
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tomas.kalibera at gmail dot com
  Target Milestone: ---

Created attachment 52770
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52770&action=edit
Reproducible example to compile and execute (see comment in file)

It seems that GCC produces wrong code for function next_set found in R package
MASS (traced down by Brian Ripley):

static void next_set(int *x, int n, int k)
{
    int i, j, tmp;

    j = k - 1;
    tmp = x[j]++;
    while(j > 0 && x[j] >= n - (k - 1 -j)) tmp = ++x[--j];
    for(i = j+1; i < k; i++)  x[i] =  ++tmp;
}

The attached standalone example reproduces the problem on a slightly modified
and instrumented variant of the function, for one specific input, see comments
in the code. 

Correct output (GCC 12 -O1, and can be seen from the C code):

n == 5, k == 3, x == 0 1 4
tmp == 2, j == 1, x == 0 2 5

Incorrect but seen on x86_64/Linux (GCC 12 -O2, GCC 11 -O3):

n == 5, k == 3, x == 0 1 4
tmp == 4, j == 2, x == 0 1 5

One can modify the example to get slightly simpler native code via writing to
the array in "main" via volatile variables.

It works with GCC 10 -O3.

             reply	other threads:[~2022-04-07 17:02 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-07 17:02 tomas.kalibera at gmail dot com [this message]
2022-04-08  8:32 ` [Bug tree-optimization/105198] [11/12 Regression] " rguenth at gcc dot gnu.org
2022-04-08  8:45 ` jakub at gcc dot gnu.org
2022-04-08  9:04 ` rguenth at gcc dot gnu.org
2022-04-08  9:12 ` rguenth at gcc dot gnu.org
2022-04-08 10:55 ` rguenth at gcc dot gnu.org
2022-04-08 12:10 ` [Bug tree-optimization/105198] [9/10/11/12 " rguenth at gcc dot gnu.org
2022-04-08 12:18 ` cvs-commit at gcc dot gnu.org
2022-04-08 12:20 ` [Bug tree-optimization/105198] [9/10/11 " rguenth at gcc dot gnu.org
2022-04-08 13:00 ` cvs-commit at gcc dot gnu.org
2022-04-08 13:00 ` [Bug tree-optimization/105198] [9/10 " rguenth at gcc dot gnu.org
2022-04-08 17:06 ` tomas.kalibera at gmail dot com
2022-05-06 12:47 ` cvs-commit at gcc dot gnu.org
2022-05-27  9:53 ` [Bug tree-optimization/105198] [9 " rguenth 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-105198-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).