[llvm-branch-commits] [llvm] cd119b3 - [gcov] Ignore blocks from another file

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Aug 21 22:51:20 PDT 2023


Author: Fangrui Song
Date: 2023-08-22T07:42:27+02:00
New Revision: cd119b354a934575aa0c8f84ac71a7be1f3def7f

URL: https://github.com/llvm/llvm-project/commit/cd119b354a934575aa0c8f84ac71a7be1f3def7f
DIFF: https://github.com/llvm/llvm-project/commit/cd119b354a934575aa0c8f84ac71a7be1f3def7f.diff

LOG: [gcov] Ignore blocks from another file

Constructs such as inline variables, #line, and #include can create
lexical blocks with a different filename.
GCOVProfiling and llvm-cov gcov currently don't handle such cases (see
GCOVLines::writeOut and GCOVFile::readGCNO) and would incorrectly
attribute the line number to the current file.

For now, ignore such blocks. Missing line execution counts is better
than wrong ones.

---

As a workaround that Apple targets don't use -mconstructor-aliases yet,
allow line execution count 4 on the A::A line (1f34e282e8066281eb1447e21e44a2a2e9983e79).

(cherry picked from commit 406e81b79d26dae6838cc69d10a3e22635da09ef)

Added: 
    compiler-rt/test/profile/Posix/gcov-file-change.cpp

Modified: 
    llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/test/profile/Posix/gcov-file-change.cpp b/compiler-rt/test/profile/Posix/gcov-file-change.cpp
new file mode 100644
index 00000000000000..9d3bc79591f271
--- /dev/null
+++ b/compiler-rt/test/profile/Posix/gcov-file-change.cpp
@@ -0,0 +1,41 @@
+// RUN: rm -rf %t && split-file %s %t && cd %t
+// RUN: %clangxx --coverage main.cpp -o t
+// RUN: %run ./t
+// RUN: llvm-cov gcov -t t-main. | FileCheck %s
+
+//--- main.cpp
+#include "a.h"
+#include <stdio.h>
+
+// CHECK:      Runs:1
+/// __cxx_global_var_init contains a block from a.h. Don't attribute its lines to main.cpp.
+// CHECK-NOT:  {{^ +[0-9]+:}}
+
+inline auto *const inl_var_main = // CHECK:      1: [[#]]:inline auto
+    new A;                        // CHECK-NEXT: 1: [[#]]:
+void foo(int x) {                 // CHECK-NEXT: 1: [[#]]:
+  if (x) {                        // CHECK-NEXT: 1: [[#]]:
+#include "a.inc"
+  }
+}
+// CHECK-NOT:  {{^ +[0-9]+:}}
+
+int main(int argc, char *argv[]) { // CHECK:      1: [[#]]:int main
+  foo(1);                          // CHECK-NEXT: 1: [[#]]:
+}                                  // CHECK-NEXT: 1: [[#]]:
+// CHECK-NOT:  {{^ +[0-9]+:}}
+
+// CHECK:      Source:a.h
+// CHECK:      1: 1:struct A
+// CHECK-NOT:  {{^ +[0-9]+:}}
+
+//--- a.h
+/// Apple targets doesn't enable -mconstructor-aliases by default and the count may be 4.
+struct A { A() { } };              // CHECK:      {{[24]}}: [[#]]:struct A
+inline auto *const inl_var_a =
+    new A;
+/// TODO a.inc:1 should have line execution.
+// CHECK-NOT:  {{^ +[0-9]+:}}
+
+//--- a.inc
+puts("");

diff  --git a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
index 21f0b1a9229386..75adcabc0d34ea 100644
--- a/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
@@ -898,7 +898,9 @@ bool GCOVProfiler::emitProfileNotes(
 
           if (Line == Loc.getLine()) continue;
           Line = Loc.getLine();
-          if (SP != getDISubprogram(Loc.getScope()))
+          MDNode *Scope = Loc.getScope();
+          // TODO: Handle blocks from another file due to #line, #include, etc.
+          if (isa<DILexicalBlockFile>(Scope) || SP != getDISubprogram(Scope))
             continue;
 
           GCOVLines &Lines = Block.getFile(Filename);


        


More information about the llvm-branch-commits mailing list