[llvm] 489a353 - [llvm-cov] Support for v4 format in convert-for-testing

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Tue May 11 14:42:05 PDT 2021


Author: Petr Hosek
Date: 2021-05-11T14:41:55-07:00
New Revision: 489a3531a42fe97c7fa00255fc5e8d31a610492d

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

LOG: [llvm-cov] Support for v4 format in convert-for-testing

v4 moves function records to a dedicated section so we need to write
and read it separately.

https://reviews.llvm.org/D100535

Added: 
    

Modified: 
    llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
    llvm/tools/llvm-cov/TestingSupport.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
index 3f1249af8e89b..d7352f2c2b3a9 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
@@ -875,16 +875,43 @@ loadTestingFormat(StringRef Data) {
   InstrProfSymtab ProfileNames;
   if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
     return std::move(E);
-  StringRef CoverageMapping = Data.substr(ProfileNamesSize);
+  Data = Data.substr(ProfileNamesSize);
   // Skip the padding bytes because coverage map data has an alignment of 8.
-  if (CoverageMapping.empty())
-    return make_error<CoverageMapError>(coveragemap_error::truncated);
-  size_t Pad = offsetToAlignedAddr(CoverageMapping.data(), Align(8));
-  if (CoverageMapping.size() < Pad)
+  size_t Pad = offsetToAlignedAddr(Data.data(), Align(8));
+  if (Data.size() < Pad)
+    return make_error<CoverageMapError>(coveragemap_error::malformed);
+  Data = Data.substr(Pad);
+  if (Data.size() < sizeof(CovMapHeader))
     return make_error<CoverageMapError>(coveragemap_error::malformed);
-  CoverageMapping = CoverageMapping.substr(Pad);
+  auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
+      Data.substr(0, sizeof(CovMapHeader)).data());
+  CovMapVersion Version =
+      (CovMapVersion)CovHeader->getVersion<support::endianness::little>();
+  StringRef CoverageMapping, CoverageRecords;
+  if (Version < CovMapVersion::Version4) {
+    CoverageMapping = Data;
+    if (CoverageMapping.empty())
+      return make_error<CoverageMapError>(coveragemap_error::truncated);
+  } else {
+    uint32_t FilenamesSize =
+        CovHeader->getFilenamesSize<support::endianness::little>();
+    uint32_t CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
+    CoverageMapping = Data.substr(0, CoverageMappingSize);
+    if (CoverageMapping.empty())
+      return make_error<CoverageMapError>(coveragemap_error::truncated);
+    Data = Data.substr(CoverageMappingSize);
+    // Skip the padding bytes because coverage records data has an alignment
+    // of 8.
+    Pad = offsetToAlignedAddr(Data.data(), Align(8));
+    if (Data.size() < Pad)
+      return make_error<CoverageMapError>(coveragemap_error::malformed);
+    CoverageRecords = Data.substr(Pad);
+    if (CoverageRecords.empty())
+      return make_error<CoverageMapError>(coveragemap_error::truncated);
+  }
   return BinaryCoverageReader::createCoverageReaderFromBuffer(
-      CoverageMapping, "", std::move(ProfileNames), BytesInAddress, Endian);
+      CoverageMapping, CoverageRecords.str(), std::move(ProfileNames),
+      BytesInAddress, Endian);
 }
 
 /// Find all sections that match \p Name. There may be more than one if comdats

diff  --git a/llvm/tools/llvm-cov/TestingSupport.cpp b/llvm/tools/llvm-cov/TestingSupport.cpp
index 93621c6ab1bd3..9c6b25f2f5851 100644
--- a/llvm/tools/llvm-cov/TestingSupport.cpp
+++ b/llvm/tools/llvm-cov/TestingSupport.cpp
@@ -48,7 +48,7 @@ int convertForTestingMain(int argc, const char *argv[]) {
 
   // Look for the sections that we are interested in.
   int FoundSectionCount = 0;
-  SectionRef ProfileNames, CoverageMapping;
+  SectionRef ProfileNames, CoverageMapping, CoverageRecords;
   auto ObjFormat = OF->getTripleObjectFormat();
   for (const auto &Section : OF->sections()) {
     StringRef Name;
@@ -65,16 +65,20 @@ int convertForTestingMain(int argc, const char *argv[]) {
     } else if (Name == llvm::getInstrProfSectionName(
                            IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
       CoverageMapping = Section;
+    } else if (Name == llvm::getInstrProfSectionName(
+                           IPSK_covfun, ObjFormat, /*AddSegmentInfo=*/false)) {
+      CoverageRecords = Section;
     } else
       continue;
     ++FoundSectionCount;
   }
-  if (FoundSectionCount != 2)
+  if (FoundSectionCount != 3)
     return 1;
 
   // Get the contents of the given sections.
   uint64_t ProfileNamesAddress = ProfileNames.getAddress();
   StringRef CoverageMappingData;
+  StringRef CoverageRecordsData;
   StringRef ProfileNamesData;
   if (Expected<StringRef> E = CoverageMapping.getContents())
     CoverageMappingData = *E;
@@ -82,6 +86,12 @@ int convertForTestingMain(int argc, const char *argv[]) {
     consumeError(E.takeError());
     return 1;
   }
+  if (Expected<StringRef> E = CoverageRecords.getContents())
+    CoverageRecordsData = *E;
+  else {
+    consumeError(E.takeError());
+    return 1;
+  }
   if (Expected<StringRef> E = ProfileNames.getContents())
     ProfileNamesData = *E;
   else {
@@ -104,6 +114,10 @@ int convertForTestingMain(int argc, const char *argv[]) {
   for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
     OS.write(uint8_t(0));
   OS << CoverageMappingData;
+  // Coverage records data is expected to have an alignment of 8.
+  for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
+    OS.write(uint8_t(0));
+  OS << CoverageRecordsData;
 
   return 0;
 }


        


More information about the llvm-commits mailing list