public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lutztonineubert at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/106921] New: [11/12.1] -O1 and -fipa-icf  -fpartial-inlining causes wrong code
Date: Tue, 13 Sep 2022 06:55:29 +0000	[thread overview]
Message-ID: <bug-106921-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 106921
           Summary: [11/12.1] -O1 and -fipa-icf  -fpartial-inlining causes
                    wrong code
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lutztonineubert at gmail dot com
  Target Milestone: ---

Short summary:

The following code returns 1 if compiled with -O2 (which is wrong) and does
return 0 if compiled without optimization.

```
#include <array>
#include <cstddef>
#include <exception>

#define GCC_VERSION (__GNUC__ * 10000 \
                     + __GNUC_MINOR__ * 100 \
                     + __GNUC_PATCHLEVEL__)
static_assert(GCC_VERSION == 110300);

template <size_t Bits>
class bitset {
 private:
  using word_t = size_t;
  static constexpr size_t bits_per_word = sizeof(word_t) * 8;
  static constexpr size_t number_of_words = (Bits / bits_per_word) + (((Bits %
bits_per_word) == 0) ? 0 : 1);

 public:
  bool all_first(size_t n) const {
    {
      if (n > Bits) {
#ifdef RETURN_INSTEAD_TERMINATE
        return false;
#else
        std::terminate();
#endif
      }
      size_t i = 0;
      for (; n > bits_per_word; n -= bits_per_word, i++) {
        if (words_[i] != ~word_t{0}) {
          return false;
        }
      }
      word_t last_word = words_[i];
      for (; n != 0; n--) {
        if ((last_word & 1) != 1) {
          return false;
        }
        last_word >>= 1;
      }
      return true;
    }
  }

  void fill() noexcept {
      for (auto& word : words_) {
          word = ~word_t{0};
      }
  }

 private:
  std::array<word_t, number_of_words> words_{};
};

volatile int X = 0;

int main() {
  if (X == 1) {
    bitset<123> bitset;
    static_cast<void>(bitset.all_first(123));
  } else {
    bitset<256> bitset;
    bitset.fill();
    if (!bitset.all_first(255)) {
      return 1;
    }
  }
  return 0;
}

```

See: https://gcc.godbolt.org/z/bEexjrKP4

This issue does not exist in GCC 10 or GCC > 12.1. I couldn't test if it does
work in GCC 11.3.1 (or the trunk of it).

Additional:
* I could also trigger the issue with -O1 -fipa-icf  -fpartial-inlining 
* If we do a return false instead of a std::terminate, no wrong code is
generated.

I am sorry, but I couldn't reduced the code any further - this already took so
much time to figure out it is a compiler bug.

             reply	other threads:[~2022-09-13  6:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-13  6:55 lutztonineubert at gmail dot com [this message]
2022-09-13  9:17 ` [Bug c++/106921] " rguenth at gcc dot gnu.org
2022-09-13 11:31 ` [Bug c++/106921] [11/12/13 Regression] -O1 and -fipa-icf -fpartial-inlining causes wrong code since r11-4987-g602c6cfc79ce4ae6 marxin at gcc dot gnu.org
2022-09-25  7:01 ` lutztonineubert at gmail dot com
2022-09-26  9:33 ` marxin at gcc dot gnu.org
2022-10-18  7:52 ` rguenth at gcc dot gnu.org
2022-12-28 14:43 ` marxin at gcc dot gnu.org
2023-02-23 10:15 ` [Bug ipa/106921] " tkapela at poczta dot fm
2023-05-29 10:07 ` [Bug ipa/106921] [11/12/13/14 " jakub at gcc dot gnu.org
2023-11-21  8:41 ` lutztonineubert at gmail dot com
2024-03-09 21:11 ` pinskia 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-106921-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).