Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-13 performs inlining after mcount instrumentation #52853

Open
tmiasko opened this issue Dec 23, 2021 · 4 comments
Open

clang-13 performs inlining after mcount instrumentation #52853

tmiasko opened this issue Dec 23, 2021 · 4 comments
Labels
llvm:optimizations LTO Link time optimization (regular/full LTO or ThinLTO)

Comments

@tmiasko
Copy link
Contributor

tmiasko commented Dec 23, 2021

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

@aeubanks
Copy link
Contributor

This is because we invoke OptimizerLastEPCallbacks multiple times during LTO, once in the first compile and then in the final optimization stage. In BackendUtil.cpp we shouldn't add the PreInlineEE pass if we're in (Thin)LTO backend compile, and we shouldn't add PostInlineEE if we're in a (Thin)LTO frontend compile.

(It's vacation time so probably won't get around to fixing this soon)

@nikic
Copy link
Contributor

nikic commented May 19, 2022

This is because we invoke OptimizerLastEPCallbacks multiple times during LTO, once in the first compile and then in the final optimization stage. In BackendUtil.cpp we shouldn't add the PreInlineEE pass if we're in (Thin)LTO backend compile, and we shouldn't add PostInlineEE if we're in a (Thin)LTO frontend compile.

At least for the post-inline case, wouldn't it be better undo that part of the change, and move this back into the backend pipeline? Having a pass that needs to be inserted into the post-link LTO pipeline by the frontend seems problematic, as the frontend may not control the LTO pipeline if linker plugin LTO rather than compiler LTO is used.

@Endilll Endilll added llvm:optimizations LTO Link time optimization (regular/full LTO or ThinLTO) and removed new issue labels Oct 1, 2023
aeubanks pushed a commit that referenced this issue May 31, 2024
…pelines (#92171)

Move EntryExitInstrumenter(PostInlining=true) to as late as possible and
EntryExitInstrumenter(PostInlining=false) to an early pre-inlining stage
(but skip for ThinLTO post-link).

This should fix the issues reported in
rust-lang/rust#92109 and
#52853. These are caused
by https://reviews.llvm.org/D97608.
@alehander92
Copy link

is this fixed now? #92171 is merged and it documents that it tries to fix this issue

@pasko
Copy link
Contributor

pasko commented Dec 3, 2024

is this fixed now? #92171 is merged and it documents that it tries to fix this issue

I did not test it, but chances are it is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:optimizations LTO Link time optimization (regular/full LTO or ThinLTO)
Projects
None yet
Development

No branches or pull requests

6 participants