public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106921] New: [11/12.1] -O1 and -fipa-icf  -fpartial-inlining causes wrong code
@ 2022-09-13  6:55 lutztonineubert at gmail dot com
  2022-09-13  9:17 ` [Bug c++/106921] " rguenth at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: lutztonineubert at gmail dot com @ 2022-09-13  6:55 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-03-09 21:11 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-13  6:55 [Bug c++/106921] New: [11/12.1] -O1 and -fipa-icf -fpartial-inlining causes wrong code lutztonineubert at gmail dot com
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

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).