]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
Auto merge of #82122 - bstrie:dep4real, r=dtolnay
[rust.git] / compiler / rustc_llvm / llvm-wrapper / CoverageMappingWrapper.cpp
1 #include "LLVMWrapper.h"
2 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
3 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
4 #include "llvm/ProfileData/InstrProf.h"
5 #include "llvm/ADT/ArrayRef.h"
6
7 #include <iostream>
8
9 using namespace llvm;
10
11 struct LLVMRustCounterMappingRegion {
12   coverage::Counter Count;
13   uint32_t FileID;
14   uint32_t ExpandedFileID;
15   uint32_t LineStart;
16   uint32_t ColumnStart;
17   uint32_t LineEnd;
18   uint32_t ColumnEnd;
19   coverage::CounterMappingRegion::RegionKind Kind;
20 };
21
22 extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
23     const char* const Filenames[],
24     size_t FilenamesLen,
25     RustStringRef BufferOut) {
26   SmallVector<StringRef,32> FilenameRefs;
27   for (size_t i = 0; i < FilenamesLen; i++) {
28     FilenameRefs.push_back(StringRef(Filenames[i]));
29   }
30   auto FilenamesWriter = coverage::CoverageFilenamesSectionWriter(
31     makeArrayRef(FilenameRefs));
32   RawRustStringOstream OS(BufferOut);
33   FilenamesWriter.write(OS);
34 }
35
36 extern "C" void LLVMRustCoverageWriteMappingToBuffer(
37     const unsigned *VirtualFileMappingIDs,
38     unsigned NumVirtualFileMappingIDs,
39     const coverage::CounterExpression *Expressions,
40     unsigned NumExpressions,
41     LLVMRustCounterMappingRegion *RustMappingRegions,
42     unsigned NumMappingRegions,
43     RustStringRef BufferOut) {
44   // Convert from FFI representation to LLVM representation.
45   SmallVector<coverage::CounterMappingRegion, 0> MappingRegions;
46   MappingRegions.reserve(NumMappingRegions);
47   for (const auto &Region : makeArrayRef(RustMappingRegions, NumMappingRegions)) {
48     MappingRegions.emplace_back(
49         Region.Count, Region.FileID, Region.ExpandedFileID,
50         Region.LineStart, Region.ColumnStart, Region.LineEnd, Region.ColumnEnd,
51         Region.Kind);
52   }
53   auto CoverageMappingWriter = coverage::CoverageMappingWriter(
54       makeArrayRef(VirtualFileMappingIDs, NumVirtualFileMappingIDs),
55       makeArrayRef(Expressions, NumExpressions),
56       MappingRegions);
57   RawRustStringOstream OS(BufferOut);
58   CoverageMappingWriter.write(OS);
59 }
60
61 extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, const char *FuncName) {
62   StringRef FuncNameRef(FuncName);
63   return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
64 }
65
66 extern "C" uint64_t LLVMRustCoverageHashCString(const char *StrVal) {
67   StringRef StrRef(StrVal);
68   return IndexedInstrProf::ComputeHash(StrRef);
69 }
70
71 extern "C" uint64_t LLVMRustCoverageHashByteArray(
72     const char *Bytes,
73     unsigned NumBytes) {
74   StringRef StrRef(Bytes, NumBytes);
75   return IndexedInstrProf::ComputeHash(StrRef);
76 }
77
78 static void WriteSectionNameToString(LLVMModuleRef M,
79                                      InstrProfSectKind SK,
80                                      RustStringRef Str) {
81   Triple TargetTriple(unwrap(M)->getTargetTriple());
82   auto name = getInstrProfSectionName(SK, TargetTriple.getObjectFormat());
83   RawRustStringOstream OS(Str);
84   OS << name;
85 }
86
87 extern "C" void LLVMRustCoverageWriteMapSectionNameToString(LLVMModuleRef M,
88                                                             RustStringRef Str) {
89   WriteSectionNameToString(M, IPSK_covmap, Str);
90 }
91
92 extern "C" void LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M,
93                                                              RustStringRef Str) {
94 #if LLVM_VERSION_GE(11, 0)
95   WriteSectionNameToString(M, IPSK_covfun, Str);
96 // else do nothing; the `Version` check will abort codegen on the Rust side
97 #endif
98 }
99
100 extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
101   auto name = getCoverageMappingVarName();
102   RawRustStringOstream OS(Str);
103   OS << name;
104 }
105
106 extern "C" uint32_t LLVMRustCoverageMappingVersion() {
107 #if LLVM_VERSION_GE(11, 0)
108   return coverage::CovMapVersion::Version4;
109 #else
110   return coverage::CovMapVersion::Version3;
111 #endif
112 }