forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenCoverage.cpp
186 lines (159 loc) · 6.97 KB
/
GenCoverage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//===--- GenCoverage.cpp - IR Generation for coverage ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements IR generation for the initialization of
// coverage related variables.
//
//===----------------------------------------------------------------------===//
#include "IRGenModule.h"
#include "SwiftTargetInfo.h"
#include "swift/SIL/SILModule.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/CoverageMappingWriter.h"
#include "llvm/Support/FileSystem.h"
using namespace swift;
using namespace irgen;
using llvm::coverage::CovMapVersion;
using llvm::coverage::CounterMappingRegion;
static bool isMachO(IRGenModule &IGM) {
return SwiftTargetInfo::get(IGM).OutputObjectFormat == llvm::Triple::MachO;
}
static StringRef getCoverageSection(IRGenModule &IGM) {
return llvm::getInstrProfCoverageSectionName(isMachO(IGM));
}
static StringRef getProfNamesSection(IRGenModule &IGM) {
return llvm::getInstrProfNameSectionName(isMachO(IGM));
}
void IRGenModule::emitCoverageMapping() {
const auto &Mappings = SILMod->getCoverageMapList();
// If there aren't any coverage maps, there's nothing to emit.
if (Mappings.empty())
return;
std::vector<StringRef> Files;
for (const auto &M : Mappings)
if (std::find(Files.begin(), Files.end(), M.getFile()) == Files.end())
Files.push_back(M.getFile());
// Awkwardly munge absolute filenames into a vector of StringRefs.
// TODO: This is heinous - the same thing is happening in clang, but the API
// really needs to be cleaned up for both.
llvm::SmallVector<std::string, 8> FilenameStrs;
llvm::SmallVector<StringRef, 8> FilenameRefs;
for (StringRef Name : Files) {
llvm::SmallString<256> Path(Name);
llvm::sys::fs::make_absolute(Path);
FilenameStrs.push_back(std::string(Path.begin(), Path.end()));
FilenameRefs.push_back(FilenameStrs.back());
}
// Encode the filenames first.
std::string FilenamesAndCoverageMappings;
llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
llvm::coverage::CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
size_t FilenamesSize = OS.str().size();
size_t CurrentSize, PrevSize = FilenamesSize;
// Now we need to build up the list of function records.
llvm::LLVMContext &Ctx = LLVMContext;
auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
llvm::Type *FunctionRecordTypes[] = {
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
#include "llvm/ProfileData/InstrProfData.inc"
#undef COVMAP_FUNC_RECORD
};
auto FunctionRecordTy =
llvm::StructType::get(Ctx, llvm::makeArrayRef(FunctionRecordTypes),
/*isPacked=*/true);
std::vector<llvm::Constant *> FunctionRecords;
std::vector<CounterMappingRegion> Regions;
for (const auto &M : Mappings) {
unsigned FileID =
std::find(Files.begin(), Files.end(), M.getFile()) - Files.begin();
Regions.clear();
for (const auto &MR : M.getMappedRegions())
Regions.emplace_back(CounterMappingRegion::makeRegion(
MR.Counter, /*FileID=*/0, MR.StartLine, MR.StartCol, MR.EndLine,
MR.EndCol));
// Append each function's regions into the encoded buffer.
llvm::coverage::CoverageMappingWriter W({FileID}, M.getExpressions(),
Regions);
W.write(OS);
std::string NameValue = llvm::getPGOFuncName(
M.getName(),
M.isPossiblyUsedExternally() ? llvm::GlobalValue::ExternalLinkage
: llvm::GlobalValue::PrivateLinkage,
M.getFile());
llvm::GlobalVariable *NamePtr = llvm::createPGOFuncNameVar(
*getModule(), llvm::GlobalValue::LinkOnceAnyLinkage, NameValue);
// The instr-profiling pass in llvm typically sets the function name ptr's
// section. We do it here because (1) SIL's int_instrprof_increment does not
// use this exact GlobalVariable, so llvm misses it and (2) we shouldn't
// expose all name ptrs to llvm via the getCoverageUnusedNamesVarName() API.
NamePtr->setSection(getProfNamesSection(*this));
NamePtr->setAlignment(1);
CurrentSize = OS.str().size();
unsigned MappingLen = CurrentSize - PrevSize;
StringRef CoverageMapping(OS.str().c_str() + PrevSize, MappingLen);
uint64_t FuncHash = M.getHash();
// Create a record for this function.
llvm::Constant *FunctionRecordVals[] = {
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
#include "llvm/ProfileData/InstrProfData.inc"
#undef COVMAP_FUNC_RECORD
};
FunctionRecords.push_back(llvm::ConstantStruct::get(
FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
PrevSize = CurrentSize;
}
size_t CoverageMappingSize = PrevSize - FilenamesSize;
// Append extra zeroes if necessary to ensure that the size of the filenames
// and coverage mappings is a multiple of 8.
if (size_t Rem = OS.str().size() % 8) {
CoverageMappingSize += 8 - Rem;
for (size_t I = 0, S = 8 - Rem; I < S; ++I)
OS << '\0';
}
auto *FilenamesAndMappingsVal =
llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
auto *RecordsTy =
llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
auto *RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
// Create the coverage data header.
llvm::Type *CovDataHeaderTypes[] = {
#define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
#include "llvm/ProfileData/InstrProfData.inc"
#undef COVMAP_HEADER
};
auto *CovDataHeaderTy =
llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
llvm::Constant *CovDataHeaderVals[] = {
#define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
#include "llvm/ProfileData/InstrProfData.inc"
#undef COVMAP_HEADER
};
auto *CovDataHeaderVal = llvm::ConstantStruct::get(
CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
// Combine the header, function records, and mappings together.
llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
FilenamesAndMappingsVal->getType()};
auto *CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
FilenamesAndMappingsVal};
auto *CovDataVal =
llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
auto CovData = new llvm::GlobalVariable(
*getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
CovDataVal, llvm::getCoverageMappingVarName());
CovData->setSection(getCoverageSection(*this));
CovData->setAlignment(8);
addUsedGlobal(CovData);
}