The -finstrument-functions is not enabled with -O0
| Bugzilla Link | 49143 |
| Version | trunk |
| OS | Linux |
| CC | @aeubanks,@zygoloid,@lenary |
Extended Description
The 'foo' function below is not instrumented when built with -O0, only when using higher optimization options (such as -O1):
$ ./bin/clang -v 2>&1 | head -n2 clang version 13.0.0 (https://github.com/llvm/llvm-project.git 90081f3020e38727eb30506d052cbb4e3a489eb6) Target: aarch64-unknown-linux-gnu $ cat test.c void foo () { }
static int count;
attribute ((noinline)) attribute ((no_instrument_function)) void __cyg_profile_func_enter (void *fn, void *parent) { count++; } attribute ((noinline)) attribute ((no_instrument_function)) void __cyg_profile_func_exit (void *fn, void *parent) { count++; } $ ./bin/clang -O0 -finstrument-functions -S -emit-llvm test.c -S -o - | head -n 12 $ ./bin/clang -O1 -finstrument-functions -S -emit-llvm test.c -S -o - | head -n 14 ; ModuleID = 'test.c' source_filename = "test.c" target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" target triple = "aarch64-unknown-linux-gnu"
@count = internal unnamed_addr global i32 0, align 4
; Function Attrs: nofree norecurse nounwind uwtable willreturn define dso_local void @foo() local_unnamed_addr #0 { entry: call void @__cyg_profile_func_enter(i8* undef, i8* undef) call void @__cyg_profile_func_exit(i8* undef, i8* undef) ret void }
Is it because the pass isn't marked as required? See https://llvm.org/docs/WritingAnLLVMNewPMPass.html#required-passes.
Enabling pass manager debug logging via -Xclang -fdebug-pass-manager (or -debug-pass-manager in opt) might show the reason.
I am trying to fix this bug and just removing the check Level != PassBuilder::OptimizationLevel::O0 in BackendUtil.cpp when adding EntryExitInstrumenterPass is not suffice (the pass fail to be applied because a pre-requisite).
It's a bit late for that change to be reverted.
And it looks like EntryExitInstrumenterPass still wasn't added at -O0 before that change, so that change does look NFC to me, it was just always different from the legacy PM.
Weird that it's added from Clang for the new PM but added to all pipelines in LLVM for the legacy PM.
Anyway, the easy fix is to remove the check for Level != PassBuilder::OptimizationLevel::O0 in BackendUtil.cpp when adding EntryExitInstrumenterPass.
This was caused by https://reviews.llvm.org/rG3a7b57b7cad32a4b59700e029d80a06c35f0a1a3 - I have commented and asked for the relevant change to be reverted, as the commit was marked NFC and was evidently not NFC.