]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
New upstream version 1.50.0+dfsg1
[rustc.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 extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
12 const char* const Filenames[],
13 size_t FilenamesLen,
14 RustStringRef BufferOut) {
15 SmallVector<StringRef,32> FilenameRefs;
16 for (size_t i = 0; i < FilenamesLen; i++) {
17 FilenameRefs.push_back(StringRef(Filenames[i]));
18 }
19 auto FilenamesWriter = coverage::CoverageFilenamesSectionWriter(
20 makeArrayRef(FilenameRefs));
21 RawRustStringOstream OS(BufferOut);
22 FilenamesWriter.write(OS);
23 }
24
25 extern "C" void LLVMRustCoverageWriteMappingToBuffer(
26 const unsigned *VirtualFileMappingIDs,
27 unsigned NumVirtualFileMappingIDs,
28 const coverage::CounterExpression *Expressions,
29 unsigned NumExpressions,
30 coverage::CounterMappingRegion *MappingRegions,
31 unsigned NumMappingRegions,
32 RustStringRef BufferOut) {
33 auto CoverageMappingWriter = coverage::CoverageMappingWriter(
34 makeArrayRef(VirtualFileMappingIDs, NumVirtualFileMappingIDs),
35 makeArrayRef(Expressions, NumExpressions),
36 makeMutableArrayRef(MappingRegions, NumMappingRegions));
37 RawRustStringOstream OS(BufferOut);
38 CoverageMappingWriter.write(OS);
39 }
40
41 extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, const char *FuncName) {
42 StringRef FuncNameRef(FuncName);
43 return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
44 }
45
46 extern "C" uint64_t LLVMRustCoverageHashCString(const char *StrVal) {
47 StringRef StrRef(StrVal);
48 return IndexedInstrProf::ComputeHash(StrRef);
49 }
50
51 extern "C" uint64_t LLVMRustCoverageHashByteArray(
52 const char *Bytes,
53 unsigned NumBytes) {
54 StringRef StrRef(Bytes, NumBytes);
55 return IndexedInstrProf::ComputeHash(StrRef);
56 }
57
58 static void WriteSectionNameToString(LLVMModuleRef M,
59 InstrProfSectKind SK,
60 RustStringRef Str) {
61 Triple TargetTriple(unwrap(M)->getTargetTriple());
62 auto name = getInstrProfSectionName(SK, TargetTriple.getObjectFormat());
63 RawRustStringOstream OS(Str);
64 OS << name;
65 }
66
67 extern "C" void LLVMRustCoverageWriteMapSectionNameToString(LLVMModuleRef M,
68 RustStringRef Str) {
69 WriteSectionNameToString(M, IPSK_covmap, Str);
70 }
71
72 extern "C" void LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M,
73 RustStringRef Str) {
74 #if LLVM_VERSION_GE(11, 0)
75 WriteSectionNameToString(M, IPSK_covfun, Str);
76 // else do nothing; the `Version` check will abort codegen on the Rust side
77 #endif
78 }
79
80 extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
81 auto name = getCoverageMappingVarName();
82 RawRustStringOstream OS(Str);
83 OS << name;
84 }
85
86 extern "C" uint32_t LLVMRustCoverageMappingVersion() {
87 #if LLVM_VERSION_GE(11, 0)
88 return coverage::CovMapVersion::Version4;
89 #else
90 return coverage::CovMapVersion::Version3;
91 #endif
92 }