llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[coverage] -fprofile-list cannot be used to ignore non-emitted code

Open justincady opened this issue 2 years ago • 3 comments

It appears -fprofile-list cannot be used to ignore coverage for non-emitted code. Here's an example (LLVM 17.0.6):

// discard.c
static int foo() {
    return 55;
}

int main() {
    foo();
    return 0;
}
# ignore.list
!fun:foo
# build.sh
clang -fprofile-instr-generate -fcoverage-mapping discard.c -o discard -fprofile-list=ignore.list -mllvm -enable-name-compression=false
./discard
llvm-profdata merge -sparse default.profraw -o default.profdata
llvm-cov show ./discard -instr-profile=default.profdata
llvm-objdump -j__llvm_prf_names -s discard

Running it as above:

$ ./build.sh
    1|       |static int foo() {
    2|       |    return 55;
    3|       |}
    4|       |
    5|      1|int main() {
    6|      1|    foo();
    7|      1|    return 0;
    8|      1|}


discard:        file format elf64-x86-64
Contents of section __llvm_prf_names:
 2091f8 04006d61 696e                        ..main

Notice two details:

  • There is no coverage reporting for foo().
  • There is no name foo in __llvm_prf_names.

But when we remove the call to foo() from main():

$ ./build.sh
    1|      0|static int foo() {
    2|      0|    return 55;
    3|      0|}
    4|       |
    5|      1|int main() {
    6|       |    //foo();
    7|      1|    return 0;
    8|      1|}


discard:        file format elf64-x86-64
Contents of section __llvm_prf_names:
 2091f8 12006d61 696e0164 69736361 72642e63  ..main.discard.c
 209208 3a666f6f                             :foo
  • There is now coverage reporting for foo().
  • The name foo is present in __llvm_prf_names.

This seems like a bug with -fprofile-list. Or, if not via that flag, how can uncalled code be explicitly ignored such that it is not instrumented for coverage reports and associated data is not emitted to the various coverage sections?

cc @ZequanWu

justincady avatar Jul 03 '24 15:07 justincady

Could you please try 18 or main branch? https://godbolt.org should be helpful.

EugeneZelenko avatar Jul 03 '24 15:07 EugeneZelenko

It looks like a bug. This flag was added at https://reviews.llvm.org/D94820. @petrhosek, do you take a look on this?

ZequanWu avatar Jul 03 '24 18:07 ZequanWu

@EugeneZelenko I couldn't figure out how to get the coverage report generated in Compiler Explorer, but if it's helpful this Godbolt demonstrates the __llvm_prf_names section issue with 18.1.0.

justincady avatar Jul 03 '24 18:07 justincady