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

            Bug ID: 127629
           Summary: All optimization levels (except -O0) slow down `if
                    consteval` and `__builtin_is_constant_evaluated()`
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.lazaric.gcc at gmail dot com
  Target Milestone: ---

```cpp
constexpr unsigned N = 100000;

constexpr unsigned& get(unsigned* data, unsigned i) {
  if not consteval {
    if (i >= N) throw;
  }
  return data[i];
}

constexpr auto res = [] {
  unsigned a[N]{};
  a[0] = 0;
  a[1] = 1;
  for (unsigned i = 2; i < N; ++i) get(a, i) = get(a, i - 1) + get(a, i - 2);
  return a[N-1];
}();
```

Compiling with "-fsyntax-only -std=c++26" takes ~1s.
Compiling with "-fsyntax-only -std=c++26 -Og" takes ~2s.

I extended gcc locally with logging of max constexpr_ops_count it sees.
First  compilation reports 8199868.
Second compilation reports 8799853.

On godbolt I get different number for second though, 8299866.

First:  https://godbolt.org/z/YWfsh7qnh
Second: https://godbolt.org/z/a5a7Ma9a5

The ops limits are sharp, reducing by 1 fails to compile.

If I replace the `if not consteval` with `!__builtin_is_constant_evaluated()`,
I see similar times locally, and similar deltas in ops count.
First:  8499862
Second: 9399841

The ops counts don't change with different non-zero optimization levels,
it just differs between `-O0` and the rest.

Replacing `-Og` with a bunch of optimizations I got from `-Q --help=optimizers`
behaves like `-O0`, not like `-Og`.
```
-fbit-tests
-fcombine-stack-adjustments
-fcompare-elim
-fcprop-registers
-fdefer-pop
-fforward-propagate
-fguess-branch-probability
-finline
-fipa-profile
-fipa-pure-const
-fipa-reference
-fipa-reference-addressable
-fjump-tables
-fomit-frame-pointer
-freorder-blocks
-fshrink-wrap
-fsplit-wide-types
-fthread-jumps
-ftoplevel-reorder
-ftree-builtin-call-dce
-ftree-ccp
-ftree-ch
-ftree-coalesce-vars
-ftree-copy-prop
-ftree-dce
-ftree-dominator-opts
-ftree-fre
-ftree-sink
-ftree-slsr
-ftree-ter
```

Reply via email to