]>
Commit | Line | Data |
---|---|---|
1b1a35ee | 1 | #include "LLVMWrapper.h" |
3dfed10e XL |
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" | |
3dfed10e XL |
6 | |
7 | #include <iostream> | |
8 | ||
9 | using namespace llvm; | |
10 | ||
6a06907d XL |
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 | ||
3dfed10e XL |
22 | extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer( |
23 | const char* const Filenames[], | |
24 | size_t FilenamesLen, | |
25 | RustStringRef BufferOut) { | |
cdc7bbd5 XL |
26 | #if LLVM_VERSION_GE(13,0) |
27 | SmallVector<std::string,32> FilenameRefs; | |
28 | for (size_t i = 0; i < FilenamesLen; i++) { | |
29 | FilenameRefs.push_back(std::string(Filenames[i])); | |
30 | } | |
31 | #else | |
fc512014 | 32 | SmallVector<StringRef,32> FilenameRefs; |
3dfed10e | 33 | for (size_t i = 0; i < FilenamesLen; i++) { |
fc512014 | 34 | FilenameRefs.push_back(StringRef(Filenames[i])); |
3dfed10e | 35 | } |
cdc7bbd5 | 36 | #endif |
fc512014 XL |
37 | auto FilenamesWriter = coverage::CoverageFilenamesSectionWriter( |
38 | makeArrayRef(FilenameRefs)); | |
39 | RawRustStringOstream OS(BufferOut); | |
40 | FilenamesWriter.write(OS); | |
3dfed10e XL |
41 | } |
42 | ||
43 | extern "C" void LLVMRustCoverageWriteMappingToBuffer( | |
44 | const unsigned *VirtualFileMappingIDs, | |
45 | unsigned NumVirtualFileMappingIDs, | |
46 | const coverage::CounterExpression *Expressions, | |
47 | unsigned NumExpressions, | |
6a06907d | 48 | LLVMRustCounterMappingRegion *RustMappingRegions, |
3dfed10e XL |
49 | unsigned NumMappingRegions, |
50 | RustStringRef BufferOut) { | |
6a06907d XL |
51 | // Convert from FFI representation to LLVM representation. |
52 | SmallVector<coverage::CounterMappingRegion, 0> MappingRegions; | |
53 | MappingRegions.reserve(NumMappingRegions); | |
54 | for (const auto &Region : makeArrayRef(RustMappingRegions, NumMappingRegions)) { | |
55 | MappingRegions.emplace_back( | |
56 | Region.Count, Region.FileID, Region.ExpandedFileID, | |
57 | Region.LineStart, Region.ColumnStart, Region.LineEnd, Region.ColumnEnd, | |
58 | Region.Kind); | |
59 | } | |
3dfed10e XL |
60 | auto CoverageMappingWriter = coverage::CoverageMappingWriter( |
61 | makeArrayRef(VirtualFileMappingIDs, NumVirtualFileMappingIDs), | |
62 | makeArrayRef(Expressions, NumExpressions), | |
6a06907d | 63 | MappingRegions); |
3dfed10e XL |
64 | RawRustStringOstream OS(BufferOut); |
65 | CoverageMappingWriter.write(OS); | |
66 | } | |
67 | ||
68 | extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, const char *FuncName) { | |
69 | StringRef FuncNameRef(FuncName); | |
70 | return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef)); | |
71 | } | |
72 | ||
fc512014 XL |
73 | extern "C" uint64_t LLVMRustCoverageHashCString(const char *StrVal) { |
74 | StringRef StrRef(StrVal); | |
75 | return IndexedInstrProf::ComputeHash(StrRef); | |
76 | } | |
77 | ||
78 | extern "C" uint64_t LLVMRustCoverageHashByteArray( | |
79 | const char *Bytes, | |
80 | unsigned NumBytes) { | |
81 | StringRef StrRef(Bytes, NumBytes); | |
82 | return IndexedInstrProf::ComputeHash(StrRef); | |
3dfed10e XL |
83 | } |
84 | ||
fc512014 XL |
85 | static void WriteSectionNameToString(LLVMModuleRef M, |
86 | InstrProfSectKind SK, | |
87 | RustStringRef Str) { | |
3dfed10e | 88 | Triple TargetTriple(unwrap(M)->getTargetTriple()); |
fc512014 | 89 | auto name = getInstrProfSectionName(SK, TargetTriple.getObjectFormat()); |
3dfed10e XL |
90 | RawRustStringOstream OS(Str); |
91 | OS << name; | |
92 | } | |
93 | ||
fc512014 XL |
94 | extern "C" void LLVMRustCoverageWriteMapSectionNameToString(LLVMModuleRef M, |
95 | RustStringRef Str) { | |
96 | WriteSectionNameToString(M, IPSK_covmap, Str); | |
97 | } | |
98 | ||
99 | extern "C" void LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M, | |
100 | RustStringRef Str) { | |
101 | #if LLVM_VERSION_GE(11, 0) | |
102 | WriteSectionNameToString(M, IPSK_covfun, Str); | |
103 | // else do nothing; the `Version` check will abort codegen on the Rust side | |
104 | #endif | |
105 | } | |
106 | ||
3dfed10e XL |
107 | extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) { |
108 | auto name = getCoverageMappingVarName(); | |
109 | RawRustStringOstream OS(Str); | |
110 | OS << name; | |
111 | } | |
112 | ||
113 | extern "C" uint32_t LLVMRustCoverageMappingVersion() { | |
fc512014 XL |
114 | #if LLVM_VERSION_GE(11, 0) |
115 | return coverage::CovMapVersion::Version4; | |
116 | #else | |
3dfed10e | 117 | return coverage::CovMapVersion::Version3; |
fc512014 | 118 | #endif |
3dfed10e | 119 | } |