[llvm] [llvm-profdata] Reject merging single-byte-coverage with count profiles (PR #213177)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 31 09:53:11 PDT 2026


https://github.com/SharonXSharon updated https://github.com/llvm/llvm-project/pull/213177

>From 2b8ddf8a576ebd116e83c397449df0d0aef1bc44 Mon Sep 17 00:00:00 2001
From: Sharon Xu <sharonxu at fb.com>
Date: Thu, 30 Jul 2026 17:04:07 -0700
Subject: [PATCH] [llvm-profdata] Reject merging single-byte-coverage with
 count profiles

---
 llvm/include/llvm/ProfileData/InstrProf.h      |  1 +
 .../include/llvm/ProfileData/InstrProfWriter.h |  5 +++++
 llvm/lib/ProfileData/InstrProf.cpp             |  4 ++++
 .../llvm-profdata/merge-incompatible.test      | 18 ++++++++++++++++++
 llvm/tools/llvm-profdata/llvm-profdata.cpp     |  7 ++++++-
 5 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h
index a25709e0ef380..cd07a012297ef 100644
--- a/llvm/include/llvm/ProfileData/InstrProf.h
+++ b/llvm/include/llvm/ProfileData/InstrProf.h
@@ -434,6 +434,7 @@ enum class instrprof_error {
   zlib_unavailable,
   raw_profile_version_mismatch,
   counter_value_too_large,
+  coverage_count_mismatch,
 };
 
 /// An ordered list of functions identified by their NameRef found in
diff --git a/llvm/include/llvm/ProfileData/InstrProfWriter.h b/llvm/include/llvm/ProfileData/InstrProfWriter.h
index 93212c19769e0..cf2f4d9f48008 100644
--- a/llvm/include/llvm/ProfileData/InstrProfWriter.h
+++ b/llvm/include/llvm/ProfileData/InstrProfWriter.h
@@ -196,6 +196,11 @@ class InstrProfWriter {
           instrprof_error::unsupported_version,
           "cannot merge FunctionEntryOnly profiles and BB profiles together");
     }
+    if (static_cast<bool>((ProfileKind & InstrProfKind::SingleByteCoverage) ^
+                          (Other & InstrProfKind::SingleByteCoverage))) {
+      return make_error<InstrProfError>(
+          instrprof_error::coverage_count_mismatch);
+    }
 
     // Now we update the profile type with the bits that are set.
     ProfileKind |= Other;
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index 1002c80af7801..692786a146d46 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -167,6 +167,10 @@ static std::string getInstrProfErrString(instrprof_error Err,
   case instrprof_error::counter_value_too_large:
     OS << "excessively large counter value suggests corrupted profile data";
     break;
+  case instrprof_error::coverage_count_mismatch:
+    OS << "cannot merge single-byte-coverage profiles with count "
+          "(non-coverage) profiles";
+    break;
   }
 
   // If optional error message is not empty, append it to the message.
diff --git a/llvm/test/tools/llvm-profdata/merge-incompatible.test b/llvm/test/tools/llvm-profdata/merge-incompatible.test
index fca72a452069e..60844b43d4a68 100644
--- a/llvm/test/tools/llvm-profdata/merge-incompatible.test
+++ b/llvm/test/tools/llvm-profdata/merge-incompatible.test
@@ -1,4 +1,6 @@
 RUN: export LSAN_OPTIONS=detect_leaks=0
+RUN: rm -rf %t && split-file %s %t
+
 RUN: not llvm-profdata merge %p/Inputs/fe-basic.proftext %p/Inputs/ir-basic.proftext -o /dev/null 2>&1 | FileCheck %s
 CHECK: ir-basic.proftext: Merge IR generated profile with Clang generated profile.
 
@@ -6,3 +8,19 @@ CHECK: ir-basic.proftext: Merge IR generated profile with Clang generated profil
 // we do not know which files have incompatible kinds.
 RUN: not llvm-profdata merge %p/Inputs/fe-basic.proftext %p/Inputs/ir-basic.proftext --num-threads=2 -o /dev/null 2>&1 | FileCheck %s --check-prefix=THREADS
 THREADS: unsupported instrumentation profile format version
+
+// Single-byte-coverage profiles cannot be merged with count profiles, in either
+// order, because the merged profile would be marked as coverage-only.
+RUN: not llvm-profdata merge %p/Inputs/ir-basic.proftext %t/coverage.proftext -o /dev/null 2>&1 | FileCheck %s --check-prefix=COVERAGE
+RUN: not llvm-profdata merge %t/coverage.proftext %p/Inputs/ir-basic.proftext -o /dev/null 2>&1 | FileCheck %s --check-prefix=COVERAGE
+RUN: not llvm-profdata merge %p/Inputs/ir-basic.proftext %t/coverage.proftext --num-threads=2 -o /dev/null 2>&1 | FileCheck %s --check-prefix=COVERAGE
+COVERAGE: cannot merge single-byte-coverage profiles with count (non-coverage) profiles
+
+//--- coverage.proftext
+:ir
+:single_byte_coverage
+foo2
+29667547796
+2
+1
+1
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index a50f427d3269c..5a0bafaf56ecb 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -812,7 +812,12 @@ loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
 
   auto Reader = std::move(ReaderOrErr.get());
   if (Error E = WC->Writer.mergeProfileKind(Reader->getProfileKind())) {
-    consumeError(std::move(E));
+    auto [ErrorCode, Msg] = InstrProfError::take(std::move(E));
+    if (ErrorCode == instrprof_error::coverage_count_mismatch) {
+      WC->Errors.emplace_back(make_error<InstrProfError>(ErrorCode, Msg),
+                              Filename);
+      return;
+    }
     WC->Errors.emplace_back(
         make_error<StringError>(
             "Merge IR generated profile with Clang generated profile.",



More information about the llvm-commits mailing list