[llvm-bugs] [Bug 51156] New: [LLVM-COV] Wrong coverage that may be caused by "ASM" and "volatile"

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jul 21 08:05:19 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51156

            Bug ID: 51156
           Summary: [LLVM-COV] Wrong coverage that may be caused by "ASM"
                    and "volatile"
           Product: Runtime Libraries
           Version: 11.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: libprofile library
          Assignee: unassignedbugs at nondot.org
          Reporter: cnwy1996 at outlook.com
                CC: llvm-bugs at lists.llvm.org

$ clang -v
clang version 11.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/wangyang/llvm-project/build/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

$ cat test.c
#include<stdlib.h>
#include<stdio.h>
volatile int cnt = 0;
__attribute__((noinline, noclone)) static int last(void) {
  return ++cnt % 5 == 0;
}

__attribute__((noinline, noclone)) static void do_it(void) {
  asm volatile("" : : "r"(&cnt) : "memory");
}

__attribute__((noinline, noclone)) static void f1(void) {
  for (;; do_it()) {
    printf("Enter loop\n");
    if (last())
      break;
  }
  do_it(); /* { dg-final { gdb-test . "cnt" "5" } } */
}

__attribute__((noinline, noclone)) static void f2(void) {
  while (1) {
    if (last())
      break;
    do_it();
  }
  do_it(); /* { dg-final { gdb-test . "cnt" "10" } } */
}

__attribute__((noinline, noclone)) static void f3(void) {
  for (;; do_it())
    if (last())
      break;
  do_it(); /* { dg-final { gdb-test . "cnt" "15" } } */
}
void (*volatile fnp1)(void) = f1;
void (*volatile fnp2)(void) = f2;
void (*volatile fnp3)(void) = f3;

int main() {
  asm volatile("" : : "r"(&fnp1) : "memory");
  asm volatile("" : : "r"(&fnp2) : "memory");
  asm volatile("" : : "r"(&fnp3) : "memory");
  fnp1();
  fnp2();
  fnp3();
}

$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=test.profraw
test.c; ./a.out; llvm-profdata merge test.profraw -o test.profdata; llvm-cov
show a.out -instr-profile=test.profdata test.c > test.lcov; cat test.lcov
Enter loop
Enter loop
Enter loop
Enter loop
Enter loop
    1|       |#include<stdlib.h>
    2|       |#include<stdio.h>
    3|       |volatile int cnt = 0;
    4|     15|__attribute__((noinline, noclone)) static int last(void) {
    5|     15|  return ++cnt % 5 == 0;
    6|     15|}
    7|       |
    8|     15|__attribute__((noinline, noclone)) static void do_it(void) {
    9|     15|  asm volatile("" : : "r"(&cnt) : "memory");
   10|     15|}
   11|       |
   12|      1|__attribute__((noinline, noclone)) static void f1(void) {
   13|      5|  for (;; do_it()) {
   14|      5|    printf("Enter loop\n");
   15|      5|    if (last())
   16|      1|      break;
   17|      5|  }
   18|      1|  do_it(); /* { dg-final { gdb-test . "cnt" "5" } } */
   19|      1|}
   20|       |
   21|      1|__attribute__((noinline, noclone)) static void f2(void) {
   22|      5|  while (1) {
   23|      5|    if (last())
   24|      1|      break;
   25|      4|    do_it();
   26|      4|  }
   27|      1|  do_it(); /* { dg-final { gdb-test . "cnt" "10" } } */
   28|      1|}
   29|       |
   30|      1|__attribute__((noinline, noclone)) static void f3(void) {
   31|      4|  for (;; do_it())
   32|      5|    if (last())
   33|      1|      break;
   34|      1|  do_it(); /* { dg-final { gdb-test . "cnt" "15" } } */
   35|      1|}
   36|       |void (*volatile fnp1)(void) = f1;
   37|       |void (*volatile fnp2)(void) = f2;
   38|       |void (*volatile fnp3)(void) = f3;
   39|       |
   40|      1|int main() {
   41|      1|  asm volatile("" : : "r"(&fnp1) : "memory");
   42|      1|  asm volatile("" : : "r"(&fnp2) : "memory");
   43|      1|  asm volatile("" : : "r"(&fnp3) : "memory");
   44|      1|  fnp1();
   45|      1|  fnp2();
   46|      1|  fnp3();
   47|      1|}

Line 31 should be executed 5 times.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210721/3c66959c/attachment.html>


More information about the llvm-bugs mailing list