Coverage Report

Created: 2018-08-19 14:04

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/LTO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LTO.cpp ------------------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "LTO.h"
11
#include "Config.h"
12
#include "InputFiles.h"
13
#include "Symbols.h"
14
#include "lld/Common/ErrorHandler.h"
15
#include "lld/Common/Strings.h"
16
#include "lld/Common/TargetOptionsCommandFlags.h"
17
#include "llvm/ADT/STLExtras.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/ADT/StringRef.h"
20
#include "llvm/ADT/Twine.h"
21
#include "llvm/IR/DiagnosticPrinter.h"
22
#include "llvm/LTO/Caching.h"
23
#include "llvm/LTO/Config.h"
24
#include "llvm/LTO/LTO.h"
25
#include "llvm/Object/SymbolicFile.h"
26
#include "llvm/Support/CodeGen.h"
27
#include "llvm/Support/Error.h"
28
#include "llvm/Support/FileSystem.h"
29
#include "llvm/Support/MemoryBuffer.h"
30
#include "llvm/Support/raw_ostream.h"
31
#include <algorithm>
32
#include <cstddef>
33
#include <memory>
34
#include <string>
35
#include <system_error>
36
#include <vector>
37
38
using namespace llvm;
39
using namespace llvm::object;
40
41
using namespace lld;
42
using namespace lld::coff;
43
44
39
static std::unique_ptr<lto::LTO> createLTO() {
45
39
  lto::Config C;
46
39
  C.Options = InitTargetOptionsFromCodeGenFlags();
47
39
48
39
  // Always emit a section per function/datum with LTO. LLVM LTO should get most
49
39
  // of the benefit of linker GC, but there are still opportunities for ICF.
50
39
  C.Options.FunctionSections = true;
51
39
  C.Options.DataSections = true;
52
39
53
39
  // Use static reloc model on 32-bit x86 because it usually results in more
54
39
  // compact code, and because there are also known code generation bugs when
55
39
  // using the PIC model (see PR34306).
56
39
  if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
57
2
    C.RelocModel = Reloc::Static;
58
37
  else
59
37
    C.RelocModel = Reloc::PIC_;
60
39
  C.DisableVerify = true;
61
39
  C.DiagHandler = diagnosticHandler;
62
39
  C.OptLevel = Config->LTOO;
63
39
  if (Config->SaveTemps)
64
2
    checkError(C.addSaveTemps(std::string(Config->OutputFile) + ".",
65
2
                              /*UseInputModulePath*/ true));
66
39
  lto::ThinBackend Backend;
67
39
  if (Config->ThinLTOJobs != 0)
68
0
    Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
69
39
  return llvm::make_unique<lto::LTO>(std::move(C), Backend,
70
39
                                     Config->LTOPartitions);
71
39
}
72
73
39
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
74
75
39
BitcodeCompiler::~BitcodeCompiler() = default;
76
77
76
static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
78
79
52
void BitcodeCompiler::add(BitcodeFile &F) {
80
52
  lto::InputFile &Obj = *F.Obj;
81
52
  unsigned SymNum = 0;
82
52
  std::vector<Symbol *> SymBodies = F.getSymbols();
83
52
  std::vector<lto::SymbolResolution> Resols(SymBodies.size());
84
52
85
52
  // Provide a resolution to the LTO API for each symbol.
86
108
  for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
87
108
    Symbol *Sym = SymBodies[SymNum];
88
108
    lto::SymbolResolution &R = Resols[SymNum];
89
108
    ++SymNum;
90
108
91
108
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
92
108
    // reports two symbols for module ASM defined. Without this check, lld
93
108
    // flags an undefined in IR with a definition in ASM as prevailing.
94
108
    // Once IRObjectFile is fixed to report only one symbol this hack can
95
108
    // be removed.
96
108
    R.Prevailing = !ObjSym.isUndefined() && 
Sym->getFile() == &F86
;
97
108
    R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
98
108
    if (R.Prevailing)
99
76
      undefine(Sym);
100
108
  }
101
52
  checkError(LTOObj->add(std::move(F.Obj), Resols));
102
52
}
103
104
// Merge all the bitcode files we have seen, codegen the result
105
// and return the resulting objects.
106
39
std::vector<StringRef> BitcodeCompiler::compile() {
107
39
  unsigned MaxTasks = LTOObj->getMaxTasks();
108
39
  Buf.resize(MaxTasks);
109
39
  Files.resize(MaxTasks);
110
39
111
39
  // The /lldltocache option specifies the path to a directory in which to cache
112
39
  // native object files for ThinLTO incremental builds. If a path was
113
39
  // specified, configure LTO to use it as the cache directory.
114
39
  lto::NativeObjectCache Cache;
115
39
  if (!Config->LTOCache.empty())
116
1
    Cache = check(lto::localCache(
117
2
        Config->LTOCache, [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
118
2
          Files[Task] = std::move(MB);
119
2
        }));
120
39
121
39
  checkError(LTOObj->run(
122
47
      [&](size_t Task) {
123
47
        return llvm::make_unique<lto::NativeObjectStream>(
124
47
            llvm::make_unique<raw_svector_ostream>(Buf[Task]));
125
47
      },
126
39
      Cache));
127
39
128
39
  if (!Config->LTOCache.empty())
129
1
    pruneCache(Config->LTOCache, Config->LTOCachePolicy);
130
39
131
39
  std::vector<StringRef> Ret;
132
88
  for (unsigned I = 0; I != MaxTasks; 
++I49
) {
133
49
    if (Buf[I].empty())
134
2
      continue;
135
47
    if (Config->SaveTemps) {
136
4
      if (I == 0)
137
2
        saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
138
2
      else
139
2
        saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
140
4
    }
141
47
    Ret.emplace_back(Buf[I].data(), Buf[I].size());
142
47
  }
143
39
144
39
  for (std::unique_ptr<MemoryBuffer> &File : Files)
145
49
    if (File)
146
2
      Ret.push_back(File->getBuffer());
147
39
148
39
  return Ret;
149
39
}