about summary refs log tree commit diff
path: root/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2020-11-26 13:39:09 +0100
committerGitHub <noreply@github.com>2020-11-26 13:39:09 +0100
commit0ae653a5318f4eaa9a3f319d1a622b6f2b354029 (patch)
treeee1ccbe3c67ccf329e507247083d1bb8be108e8c /compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
parent85c11de2f2ecc9dcc1b94f550351af0cd02e96dc (diff)
parentfdbc121620704ab50bd427ff1a2bb3282ffac745 (diff)
downloadrust-0ae653a5318f4eaa9a3f319d1a622b6f2b354029.tar.gz
rust-0ae653a5318f4eaa9a3f319d1a622b6f2b354029.zip
Rollup merge of #79365 - richkadel:llvm-cov-map-version-4, r=wesleywiser
Upgrades the coverage map to Version 4

Changes the coverage map injected into binaries compiled with
`-Zinstrument-coverage` to LLVM Coverage Mapping Format, Version 4 (from
Version 3). Note, binaries compiled with this version will require LLVM
tools from at least LLVM Version 11.

r? ``@wesleywiser``
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp')
-rw-r--r--compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp52
1 files changed, 37 insertions, 15 deletions
diff --git a/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
index 2b1143a4ecf..25badc3f4e1 100644
--- a/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
+++ b/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
@@ -3,7 +3,6 @@
 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/ADT/ArrayRef.h"
-#include "llvm/Support/LEB128.h"
 
 #include <iostream>
 
@@ -13,15 +12,14 @@ extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
     const char* const Filenames[],
     size_t FilenamesLen,
     RustStringRef BufferOut) {
-  // LLVM 11's CoverageFilenamesSectionWriter uses its new `Version4` format,
-  // so we're manually writing the `Version3` format ourselves.
-  RawRustStringOstream OS(BufferOut);
-  encodeULEB128(FilenamesLen, OS);
+  SmallVector<StringRef,32> FilenameRefs;
   for (size_t i = 0; i < FilenamesLen; i++) {
-    StringRef Filename(Filenames[i]);
-    encodeULEB128(Filename.size(), OS);
-    OS << Filename;
+    FilenameRefs.push_back(StringRef(Filenames[i]));
   }
+  auto FilenamesWriter = coverage::CoverageFilenamesSectionWriter(
+    makeArrayRef(FilenameRefs));
+  RawRustStringOstream OS(BufferOut);
+  FilenamesWriter.write(OS);
 }
 
 extern "C" void LLVMRustCoverageWriteMappingToBuffer(
@@ -45,20 +43,40 @@ extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, con
   return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
 }
 
-extern "C" uint64_t LLVMRustCoverageComputeHash(const char *Name) {
-  StringRef NameRef(Name);
-  return IndexedInstrProf::ComputeHash(NameRef);
+extern "C" uint64_t LLVMRustCoverageHashCString(const char *StrVal) {
+  StringRef StrRef(StrVal);
+  return IndexedInstrProf::ComputeHash(StrRef);
+}
+
+extern "C" uint64_t LLVMRustCoverageHashByteArray(
+    const char *Bytes,
+    unsigned NumBytes) {
+  StringRef StrRef(Bytes, NumBytes);
+  return IndexedInstrProf::ComputeHash(StrRef);
 }
 
-extern "C" void LLVMRustCoverageWriteSectionNameToString(LLVMModuleRef M,
-                                                         RustStringRef Str) {
+static void WriteSectionNameToString(LLVMModuleRef M,
+                                     InstrProfSectKind SK,
+                                     RustStringRef Str) {
   Triple TargetTriple(unwrap(M)->getTargetTriple());
-  auto name = getInstrProfSectionName(IPSK_covmap,
-                                      TargetTriple.getObjectFormat());
+  auto name = getInstrProfSectionName(SK, TargetTriple.getObjectFormat());
   RawRustStringOstream OS(Str);
   OS << name;
 }
 
+extern "C" void LLVMRustCoverageWriteMapSectionNameToString(LLVMModuleRef M,
+                                                            RustStringRef Str) {
+  WriteSectionNameToString(M, IPSK_covmap, Str);
+}
+
+extern "C" void LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M,
+                                                             RustStringRef Str) {
+#if LLVM_VERSION_GE(11, 0)
+  WriteSectionNameToString(M, IPSK_covfun, Str);
+// else do nothing; the `Version` check will abort codegen on the Rust side
+#endif
+}
+
 extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
   auto name = getCoverageMappingVarName();
   RawRustStringOstream OS(Str);
@@ -66,5 +84,9 @@ extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
 }
 
 extern "C" uint32_t LLVMRustCoverageMappingVersion() {
+#if LLVM_VERSION_GE(11, 0)
+  return coverage::CovMapVersion::Version4;
+#else
   return coverage::CovMapVersion::Version3;
+#endif
 }