Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/LTO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LTO.cpp ------------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "LTO.h"
10
#include "Config.h"
11
#include "InputFiles.h"
12
#include "LinkerScript.h"
13
#include "SymbolTable.h"
14
#include "Symbols.h"
15
#include "lld/Common/Args.h"
16
#include "lld/Common/ErrorHandler.h"
17
#include "lld/Common/TargetOptionsCommandFlags.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/ADT/SmallString.h"
20
#include "llvm/ADT/StringRef.h"
21
#include "llvm/ADT/Twine.h"
22
#include "llvm/BinaryFormat/ELF.h"
23
#include "llvm/Bitcode/BitcodeReader.h"
24
#include "llvm/Bitcode/BitcodeWriter.h"
25
#include "llvm/IR/DiagnosticPrinter.h"
26
#include "llvm/LTO/Caching.h"
27
#include "llvm/LTO/Config.h"
28
#include "llvm/LTO/LTO.h"
29
#include "llvm/Object/SymbolicFile.h"
30
#include "llvm/Support/CodeGen.h"
31
#include "llvm/Support/Error.h"
32
#include "llvm/Support/FileSystem.h"
33
#include "llvm/Support/MemoryBuffer.h"
34
#include <algorithm>
35
#include <cstddef>
36
#include <memory>
37
#include <string>
38
#include <system_error>
39
#include <vector>
40
41
using namespace llvm;
42
using namespace llvm::object;
43
using namespace llvm::ELF;
44
45
using namespace lld;
46
using namespace lld::elf;
47
48
// Creates an empty file to store a list of object files for final
49
// linking of distributed ThinLTO.
50
11
static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) {
51
11
  std::error_code EC;
52
11
  auto Ret =
53
11
      llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None);
54
11
  if (EC) {
55
2
    error("cannot open " + File + ": " + EC.message());
56
2
    return nullptr;
57
2
  }
58
9
  return Ret;
59
9
}
60
61
6
static std::string getThinLTOOutputFile(StringRef ModulePath) {
62
6
  return lto::getThinLTOOutputFile(ModulePath,
63
6
                                   Config->ThinLTOPrefixReplace.first,
64
6
                                   Config->ThinLTOPrefixReplace.second);
65
6
}
66
67
2.55k
static lto::Config createConfig() {
68
2.55k
  lto::Config C;
69
2.55k
70
2.55k
  // LLD supports the new relocations and address-significance tables.
71
2.55k
  C.Options = initTargetOptionsFromCodeGenFlags();
72
2.55k
  C.Options.RelaxELFRelocations = true;
73
2.55k
  C.Options.EmitAddrsig = true;
74
2.55k
75
2.55k
  // Always emit a section per function/datum with LTO.
76
2.55k
  C.Options.FunctionSections = true;
77
2.55k
  C.Options.DataSections = true;
78
2.55k
79
2.55k
  if (Config->Relocatable)
80
82
    C.RelocModel = None;
81
2.47k
  else if (Config->Pic)
82
969
    C.RelocModel = Reloc::PIC_;
83
1.50k
  else
84
1.50k
    C.RelocModel = Reloc::Static;
85
2.55k
86
2.55k
  C.CodeModel = getCodeModelFromCMModel();
87
2.55k
  C.DisableVerify = Config->DisableVerify;
88
2.55k
  C.DiagHandler = diagnosticHandler;
89
2.55k
  C.OptLevel = Config->LTOO;
90
2.55k
  C.CPU = getCPUStr();
91
2.55k
  C.MAttrs = getMAttrs();
92
2.55k
  C.CGOptLevel = args::getCGOptLevel(Config->LTOO);
93
2.55k
94
2.55k
  // Set up a custom pipeline if we've been asked to.
95
2.55k
  C.OptPipeline = Config->LTONewPmPasses;
96
2.55k
  C.AAPipeline = Config->LTOAAPipeline;
97
2.55k
98
2.55k
  // Set up optimization remarks if we've been asked to.
99
2.55k
  C.RemarksFilename = Config->OptRemarksFilename;
100
2.55k
  C.RemarksPasses = Config->OptRemarksPasses;
101
2.55k
  C.RemarksWithHotness = Config->OptRemarksWithHotness;
102
2.55k
103
2.55k
  C.SampleProfile = Config->LTOSampleProfile;
104
2.55k
  C.UseNewPM = Config->LTONewPassManager;
105
2.55k
  C.DebugPassManager = Config->LTODebugPassManager;
106
2.55k
  C.DwoDir = Config->DwoDir;
107
2.55k
108
2.55k
  C.CSIRProfile = Config->LTOCSProfileFile;
109
2.55k
  C.RunCSIRInstr = Config->LTOCSProfileGenerate;
110
2.55k
111
2.55k
  if (Config->EmitLLVM) {
112
2
    C.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
113
2
      if (std::unique_ptr<raw_fd_ostream> OS = openFile(Config->OutputFile))
114
2
        WriteBitcodeToFile(M, *OS, false);
115
2
      return false;
116
2
    };
117
1
  }
118
2.55k
119
2.55k
  if (Config->SaveTemps)
120
51
    checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
121
51
                              /*UseInputModulePath*/ true));
122
2.55k
  return C;
123
2.55k
}
124
125
2.55k
BitcodeCompiler::BitcodeCompiler() {
126
2.55k
  // Initialize IndexFile.
127
2.55k
  if (!Config->ThinLTOIndexOnlyArg.empty())
128
1
    IndexFile = openFile(Config->ThinLTOIndexOnlyArg);
129
2.55k
130
2.55k
  // Initialize LTOObj.
131
2.55k
  lto::ThinBackend Backend;
132
2.55k
  if (Config->ThinLTOIndexOnly) {
133
21
    auto OnIndexWrite = [&](StringRef S) { ThinIndices.erase(S); };
134
14
    Backend = lto::createWriteIndexesThinBackend(
135
14
        Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second,
136
14
        Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite);
137
2.54k
  } else if (Config->ThinLTOJobs != -1U) {
138
2
    Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
139
2
  }
140
2.55k
141
2.55k
  LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend,
142
2.55k
                                       Config->LTOPartitions);
143
2.55k
144
2.55k
  // Initialize UsedStartStop.
145
16.6k
  for (Symbol *Sym : Symtab->getSymbols()) {
146
16.6k
    StringRef S = Sym->getName();
147
16.6k
    for (StringRef Prefix : {"__start_", "__stop_"})
148
33.2k
      if (S.startswith(Prefix))
149
34
        UsedStartStop.insert(S.substr(Prefix.size()));
150
16.6k
  }
151
2.55k
}
152
153
2.55k
BitcodeCompiler::~BitcodeCompiler() = default;
154
155
247
void BitcodeCompiler::add(BitcodeFile &F) {
156
247
  lto::InputFile &Obj = *F.Obj;
157
247
  bool IsExec = !Config->Shared && 
!Config->Relocatable104
;
158
247
159
247
  if (Config->ThinLTOIndexOnly)
160
25
    ThinIndices.insert(Obj.getName());
161
247
162
247
  ArrayRef<Symbol *> Syms = F.getSymbols();
163
247
  ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols();
164
247
  std::vector<lto::SymbolResolution> Resols(Syms.size());
165
247
166
247
  // Provide a resolution to the LTO API for each symbol.
167
640
  for (size_t I = 0, E = Syms.size(); I != E; 
++I393
) {
168
393
    Symbol *Sym = Syms[I];
169
393
    const lto::InputFile::Symbol &ObjSym = ObjSyms[I];
170
393
    lto::SymbolResolution &R = Resols[I];
171
393
172
393
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
173
393
    // reports two symbols for module ASM defined. Without this check, lld
174
393
    // flags an undefined in IR with a definition in ASM as prevailing.
175
393
    // Once IRObjectFile is fixed to report only one symbol this hack can
176
393
    // be removed.
177
393
    R.Prevailing = !ObjSym.isUndefined() && 
Sym->File == &F292
;
178
393
179
393
    // We ask LTO to preserve following global symbols:
180
393
    // 1) All symbols when doing relocatable link, so that them can be used
181
393
    //    for doing final link.
182
393
    // 2) Symbols that are used in regular objects.
183
393
    // 3) C named sections if we have corresponding __start_/__stop_ symbol.
184
393
    // 4) Symbols that are defined in bitcode files and used for dynamic linking.
185
393
    R.VisibleToRegularObj = Config->Relocatable || 
Sym->IsUsedInRegularObj387
||
186
393
                            
(269
R.Prevailing269
&&
Sym->includeInDynsym()179
) ||
187
393
                            
UsedStartStop.count(ObjSym.getSectionName())152
;
188
393
    const auto *DR = dyn_cast<Defined>(Sym);
189
393
    R.FinalDefinitionInLinkageUnit =
190
393
        (IsExec || 
Sym->Visibility != STV_DEFAULT229
) &&
DR208
&&
191
393
        // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
192
393
        // will be generated by for them, triggering linker errors.
193
393
        // Symbol section is always null for bitcode symbols, hence the check
194
393
        // for isElf(). Skip linker script defined symbols as well: they have
195
393
        // no File defined.
196
393
        
!(186
DR->Section == nullptr186
&&
(183
!Sym->File183
||
Sym->File->isElf()175
));
197
393
198
393
    if (R.Prevailing)
199
273
      replaceSymbol(Sym, Undefined{nullptr, Sym->getName(), STB_GLOBAL,
200
273
                                   STV_DEFAULT, Sym->Type});
201
393
202
393
    // We tell LTO to not apply interprocedural optimization for wrapped
203
393
    // (with --wrap) symbols because otherwise LTO would inline them while
204
393
    // their values are still not final.
205
393
    R.LinkerRedefined = !Sym->CanInline;
206
393
  }
207
247
  checkError(LTOObj->add(std::move(F.Obj), Resols));
208
247
}
209
210
// If LazyObjFile has not been added to link, emit empty index files.
211
// This is needed because this is what GNU gold plugin does and we have a
212
// distributed build system that depends on that behavior.
213
14
static void thinLTOCreateEmptyIndexFiles() {
214
14
  for (LazyObjFile *F : LazyObjFiles) {
215
3
    if (F->AddedToLink || 
!isBitcode(F->MB)2
)
216
1
      continue;
217
2
    std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(F->getName()));
218
2
    std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc");
219
2
    if (!OS)
220
0
      continue;
221
2
222
2
    ModuleSummaryIndex M(/*HaveGVs*/ false);
223
2
    M.setSkipModuleByDistributedBackend();
224
2
    WriteIndexToFile(M, *OS);
225
2
    if (Config->ThinLTOEmitImportsFiles)
226
1
      openFile(Path + ".imports");
227
2
  }
228
14
}
229
230
// Merge all the bitcode files we have seen, codegen the result
231
// and return the resulting ObjectFile(s).
232
2.55k
std::vector<InputFile *> BitcodeCompiler::compile() {
233
2.55k
  unsigned MaxTasks = LTOObj->getMaxTasks();
234
2.55k
  Buf.resize(MaxTasks);
235
2.55k
  Files.resize(MaxTasks);
236
2.55k
237
2.55k
  // The --thinlto-cache-dir option specifies the path to a directory in which
238
2.55k
  // to cache native object files for ThinLTO incremental builds. If a path was
239
2.55k
  // specified, configure LTO to use it as the cache directory.
240
2.55k
  lto::NativeObjectCache Cache;
241
2.55k
  if (!Config->ThinLTOCacheDir.empty())
242
6
    Cache = check(
243
6
        lto::localCache(Config->ThinLTOCacheDir,
244
12
                        [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
245
12
                          Files[Task] = std::move(MB);
246
12
                        }));
247
2.55k
248
2.55k
  if (!BitcodeFiles.empty())
249
172
    checkError(LTOObj->run(
250
209
        [&](size_t Task) {
251
209
          return llvm::make_unique<lto::NativeObjectStream>(
252
209
              llvm::make_unique<raw_svector_ostream>(Buf[Task]));
253
209
        },
254
172
        Cache));
255
2.55k
256
2.55k
  // Emit empty index files for non-indexed files
257
2.55k
  for (StringRef S : ThinIndices) {
258
4
    std::string Path = getThinLTOOutputFile(S);
259
4
    openFile(Path + ".thinlto.bc");
260
4
    if (Config->ThinLTOEmitImportsFiles)
261
1
      openFile(Path + ".imports");
262
4
  }
263
2.55k
264
2.55k
  if (Config->ThinLTOIndexOnly) {
265
14
    thinLTOCreateEmptyIndexFiles();
266
14
267
14
    if (!Config->LTOObjPath.empty())
268
1
      saveBuffer(Buf[0], Config->LTOObjPath);
269
14
270
14
    // ThinLTO with index only option is required to generate only the index
271
14
    // files. After that, we exit from linker and ThinLTO backend runs in a
272
14
    // distributed environment.
273
14
    if (IndexFile)
274
1
      IndexFile->close();
275
14
    return {};
276
14
  }
277
2.54k
278
2.54k
  if (!Config->ThinLTOCacheDir.empty())
279
6
    pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
280
2.54k
281
2.54k
  if (!Config->LTOObjPath.empty()) {
282
1
    saveBuffer(Buf[0], Config->LTOObjPath);
283
3
    for (unsigned I = 1; I != MaxTasks; 
++I2
)
284
2
      saveBuffer(Buf[I], Config->LTOObjPath + Twine(I));
285
1
  }
286
2.54k
287
2.54k
  if (Config->SaveTemps) {
288
51
    saveBuffer(Buf[0], Config->OutputFile + ".lto.o");
289
60
    for (unsigned I = 1; I != MaxTasks; 
++I9
)
290
9
      saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o");
291
51
  }
292
2.54k
293
2.54k
  std::vector<InputFile *> Ret;
294
5.13k
  for (unsigned I = 0; I != MaxTasks; 
++I2.59k
)
295
2.59k
    if (!Buf[I].empty())
296
196
      Ret.push_back(createObjectFile(MemoryBufferRef(Buf[I], "lto.tmp")));
297
2.54k
298
2.54k
  for (std::unique_ptr<MemoryBuffer> &File : Files)
299
2.59k
    if (File)
300
12
      Ret.push_back(createObjectFile(*File));
301
2.54k
  return Ret;
302
2.54k
}