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

            Bug ID: 127999
           Summary: [12/13/14/15/16 Regression] -fprofile-use coverage
                    mismatch when a no_profile_instrument_function callee
                    is early-inlined
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ipa
          Assignee: unassigned at gcc dot gnu.org
          Reporter: harmen at stoppels dot ch
  Target Milestone: ---

A function marked no_profile_instrument_function that is called from an
instrumented function is not early-inlined with -fprofile-generate, but it is
early-inlined with -fprofile-use. The caller's CFG at instrumentation time then
differs from the one at profile-read time, so an otherwise identical
-fprofile-use build fails with -Werror=coverage-mismatch.

```
$ cat t.c
__attribute__((no_profile_instrument_function))
static inline int helper(int x) { return x & 1 ? x * 3 : x / 2; }

__attribute__((noinline)) int f(int n) {
  int s = 0;
  for (int i = 0; i < n; ++i)
    s += helper(i);
  return s;
}

int main(int argc, char **argv) { return f(argc * 1000) & 0x7f; }

$ gcc -O2 -fprofile-generate=$PWD/prof -c t.c -o t.o

$ gcc -fprofile-generate=$PWD/prof t.o -o t && ./t

$ gcc -O2 -fprofile-use=$PWD/prof -c t.c -o t.o
t.c: In function ‘f’:
t.c:4:31: error: number of counters in profile data for function ‘f’ does not
match its profile data (counter ‘arcs’, expected 2 and have 3)
[-Werror=coverage-mismatch]
    4 | __attribute__((noinline)) int f(int n) {
      |                               ^
t.c:4:31: error: the control flow of function ‘f’ does not match its profile
data (counter ‘time_profiler’) [-Werror=coverage-mismatch]
cc1: some warnings being treated as errors

```

In case it's helpful, the LLM I'm using points to the following:

> This appears to come from the no_profile_instrument_function check in
> can_early_inline_edge_p (PR gcov-profile/80223, moved there by the fix for
> PR ipa/103636), which is only active with -fprofile-generate 
> (profile_arc_flag).
> For the profile to match, the early inliner has to make the same decision in
> both modes, e.g. by also applying the check with -fprofile-use.

Reply via email to