Skip to content

clang-13 performs inlining after mcount instrumentation #52853

Open
@tmiasko

Description

@tmiasko

In clang-13 inlining might follow the mcount instrumentation used by -pg.
The mcount relies on frame pointers to recover a call graph, so inlining after
inserting calls to mcount produces misleading end results. In the example
below, g which is called only once is recorded as being called 1025 times:

int f(int x) {
  return (x & (x << 1u)) == 0;
}
#include <stdio.h>
int f(int);
int __attribute__ ((noinline)) g() {
  int c = 0;
  for (int i = 0; i < (1 << 10); ++i)
    c += f(i);
  return c;
}

int main() {
  printf("%d\n", g());
}
$ clang-13 -g -pg -flto -O2 -c a.c
$ clang-13 -g -pg -flto -O2 -c b.c
$ clang-13 -g -pg -flto -O2 a.o b.o
$ ./a.out > /dev/null && gprof -b | grep -A2 calls
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
  0.00      0.00     0.00     1025     0.00     0.00  g
$ clang-12 -g -pg -flto -O2 -c a.c
$ clang-12 -g -pg -flto -O2 -c b.c
$ clang-12 -g -pg -flto -O2 a.o b.o
$ ./a.out > /dev/null && gprof -b | grep -A2 calls
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
   0.00      0.00     0.00        1     0.00     0.00  g

This seems to be result of changes from https://reviews.llvm.org/D97608,
which moved PostInlineEntryExitInstrumentationPass out of TargetPassConfig::addIRPasses.

cc @aeubanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    LTOLink time optimization (regular/full LTO or ThinLTO)llvm:optimizations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions