From 5886ec3034cd63af60cd6ad0743c1fe08060a4ff Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Fri, 6 Sep 2024 17:08:22 -0700 Subject: [PATCH 1/2] [Coverage] Ignore unused functions if the counter kind is Zero. --- .../test/profile/instrprof-merging-2.cpp | 54 +++++++++++++++++++ .../ProfileData/Coverage/CoverageMapping.cpp | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 compiler-rt/test/profile/instrprof-merging-2.cpp diff --git a/compiler-rt/test/profile/instrprof-merging-2.cpp b/compiler-rt/test/profile/instrprof-merging-2.cpp new file mode 100644 index 0000000000000..4c2579804774f --- /dev/null +++ b/compiler-rt/test/profile/instrprof-merging-2.cpp @@ -0,0 +1,54 @@ +// UNSUPPORTED: target={{.*windows.*}} + +// RUN: split-file %s %t +// RUN: %clangxx_profgen -fcoverage-mapping %t/test1.cpp -o %t/test1.exe +// RUN: %clangxx_profgen -fcoverage-mapping %t/test2.cpp -o %t/test2.exe +// RUN: env LLVM_PROFILE_FILE=%t/test1.profraw %run %t/test1.exe +// RUN: env LLVM_PROFILE_FILE=%t/test2.profraw %run %t/test2.exe +// RUN: llvm-profdata merge %t/test1.profraw %t/test2.profraw -o %t/merged.profdata +// RUN: llvm-cov show -instr-profile=%t/merged.profdata -object %t/test1.exe %t/test2.exe | FileCheck %s +// RUN: llvm-cov show -instr-profile=%t/merged.profdata -object %t/test2.exe %t/test1.exe | FileCheck %s + +// CHECK: |struct Test { +// CHECK-NEXT: 1| int getToTest() { +// CHECK-NEXT: 2| for (int i = 0; i < 1; i++) { +// CHECK-NEXT: 1| if (false) { +// CHECK-NEXT: 0| return 1; +// CHECK-NEXT: 0| } +// CHECK-NEXT: 1| } +// CHECK-NEXT: 1| if (true) { +// CHECK-NEXT: 1| return 1; +// CHECK-NEXT: 1| } +// CHECK-NEXT: 0| return 1; +// CHECK-NEXT: 1| } +// CHECK-NEXT: |}; +// CHECK-NEXT: | + +#--- test.h +struct Test { + int getToTest() { + for (int i = 0; i < 1; i++) { + if (false) { + return 1; + } + } + if (true) { + return 1; + } + return 1; + } +}; + +#--- test1.cpp +#include "test.h" +int main() { + Test t; + t.getToTest(); + return 0; +} + +#--- test2.cpp +#include "test.h" +int main() { + return 0; +} diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp index 21ce0ac17d618..eeef78a822008 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -851,7 +851,7 @@ Error CoverageMapping::loadFunctionRecord( // won't (in which case we don't unintuitively report functions as uncovered // when they have non-zero counts in the profile). if (Record.MappingRegions.size() == 1 && - Record.MappingRegions[0].Count.isZero() && Counts[0] > 0) + Record.MappingRegions[0].Count.isZero()) return Error::success(); MCDCDecisionRecorder MCDCDecisions; From 626a1c9cf5c12273e1b46c8be55a81650076ce52 Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Mon, 9 Sep 2024 10:54:04 -0700 Subject: [PATCH 2/2] disable clang-format --- compiler-rt/test/profile/instrprof-merging-2.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler-rt/test/profile/instrprof-merging-2.cpp b/compiler-rt/test/profile/instrprof-merging-2.cpp index 4c2579804774f..438394f9fb239 100644 --- a/compiler-rt/test/profile/instrprof-merging-2.cpp +++ b/compiler-rt/test/profile/instrprof-merging-2.cpp @@ -1,5 +1,6 @@ // UNSUPPORTED: target={{.*windows.*}} +// clang-format off // RUN: split-file %s %t // RUN: %clangxx_profgen -fcoverage-mapping %t/test1.cpp -o %t/test1.exe // RUN: %clangxx_profgen -fcoverage-mapping %t/test2.cpp -o %t/test2.exe