Coverage Report

Created: 2018-01-17 17:22

/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/TargetOptionsCommandFlags.h"
16
#include "llvm/ADT/STLExtras.h"
17
#include "llvm/ADT/SmallString.h"
18
#include "llvm/ADT/StringRef.h"
19
#include "llvm/ADT/Twine.h"
20
#include "llvm/IR/DiagnosticPrinter.h"
21
#include "llvm/LTO/Caching.h"
22
#include "llvm/LTO/Config.h"
23
#include "llvm/LTO/LTO.h"
24
#include "llvm/Object/SymbolicFile.h"
25
#include "llvm/Support/CodeGen.h"
26
#include "llvm/Support/Error.h"
27
#include "llvm/Support/FileSystem.h"
28
#include "llvm/Support/MemoryBuffer.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include <algorithm>
31
#include <cstddef>
32
#include <memory>
33
#include <string>
34
#include <system_error>
35
#include <vector>
36
37
using namespace llvm;
38
using namespace llvm::object;
39
40
using namespace lld;
41
using namespace lld::coff;
42
43
1
static void diagnosticHandler(const DiagnosticInfo &DI) {
44
1
  SmallString<128> ErrStorage;
45
1
  raw_svector_ostream OS(ErrStorage);
46
1
  DiagnosticPrinterRawOStream DP(OS);
47
1
  DI.print(DP);
48
1
  warn(ErrStorage);
49
1
}
50
51
327
static void checkError(Error E) {
52
327
  handleAllErrors(std::move(E),
53
327
                  [&](ErrorInfoBase &EIB) 
{ error(EIB.message()); }1
);
54
327
}
55
56
55
static void saveBuffer(StringRef Buffer, const Twine &Path) {
57
55
  std::error_code EC;
58
55
  raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
59
55
  if (EC)
60
0
    error("cannot create " + Path + ": " + EC.message());
61
55
  OS << Buffer;
62
55
}
63
64
0
static std::unique_ptr<lto::LTO> createLTO() {
65
0
  lto::Config Conf;
66
0
  Conf.Options = InitTargetOptionsFromCodeGenFlags();
67
0
  // Use static reloc model on 32-bit x86 because it usually results in more
68
0
  // compact code, and because there are also known code generation bugs when
69
0
  // using the PIC model (see PR34306).
70
0
  if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
71
0
    Conf.RelocModel = Reloc::Static;
72
0
  else
73
0
    Conf.RelocModel = Reloc::PIC_;
74
0
  Conf.DisableVerify = true;
75
0
  Conf.DiagHandler = diagnosticHandler;
76
0
  Conf.OptLevel = Config->LTOOptLevel;
77
0
  if (Config->SaveTemps)
78
0
    checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
79
0
                                 /*UseInputModulePath*/ true));
80
0
  lto::ThinBackend Backend;
81
0
  if (Config->LTOJobs != 0)
82
0
    Backend = lto::createInProcessThinBackend(Config->LTOJobs);
83
0
  return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
84
0
                                     Config->LTOPartitions);
85
0
}
86
87
0
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
88
89
0
BitcodeCompiler::~BitcodeCompiler() = default;
90
91
0
static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
92
93
0
void BitcodeCompiler::add(BitcodeFile &F) {
94
0
  lto::InputFile &Obj = *F.Obj;
95
0
  unsigned SymNum = 0;
96
0
  std::vector<Symbol *> SymBodies = F.getSymbols();
97
0
  std::vector<lto::SymbolResolution> Resols(SymBodies.size());
98
0
99
0
  // Provide a resolution to the LTO API for each symbol.
100
0
  for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
101
0
    Symbol *Sym = SymBodies[SymNum];
102
0
    lto::SymbolResolution &R = Resols[SymNum];
103
0
    ++SymNum;
104
0
105
0
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
106
0
    // reports two symbols for module ASM defined. Without this check, lld
107
0
    // flags an undefined in IR with a definition in ASM as prevailing.
108
0
    // Once IRObjectFile is fixed to report only one symbol this hack can
109
0
    // be removed.
110
0
    R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
111
0
    R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
112
0
    if (R.Prevailing)
113
0
      undefine(Sym);
114
0
  }
115
0
  checkError(LTOObj->add(std::move(F.Obj), Resols));
116
0
}
117
118
// Merge all the bitcode files we have seen, codegen the result
119
// and return the resulting objects.
120
0
std::vector<StringRef> BitcodeCompiler::compile() {
121
0
  unsigned MaxTasks = LTOObj->getMaxTasks();
122
0
  Buff.resize(MaxTasks);
123
0
  Files.resize(MaxTasks);
124
0
125
0
  // The /lldltocache option specifies the path to a directory in which to cache
126
0
  // native object files for ThinLTO incremental builds. If a path was
127
0
  // specified, configure LTO to use it as the cache directory.
128
0
  lto::NativeObjectCache Cache;
129
0
  if (!Config->LTOCache.empty())
130
0
    Cache = check(
131
0
        lto::localCache(Config->LTOCache,
132
0
                        [&](size_t Task, std::unique_ptr<MemoryBuffer> MB,
133
0
                            StringRef Path) { Files[Task] = std::move(MB); }));
134
0
135
0
  checkError(LTOObj->run(
136
0
      [&](size_t Task) {
137
0
        return llvm::make_unique<lto::NativeObjectStream>(
138
0
            llvm::make_unique<raw_svector_ostream>(Buff[Task]));
139
0
      },
140
0
      Cache));
141
0
142
0
  if (!Config->LTOCache.empty())
143
0
    pruneCache(Config->LTOCache, Config->LTOCachePolicy);
144
0
145
0
  std::vector<StringRef> Ret;
146
0
  for (unsigned I = 0; I != MaxTasks; ++I) {
147
0
    if (Buff[I].empty())
148
0
      continue;
149
0
    if (Config->SaveTemps) {
150
0
      if (I == 0)
151
0
        saveBuffer(Buff[I], Config->OutputFile + ".lto.obj");
152
0
      else
153
0
        saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.obj");
154
0
    }
155
0
    Ret.emplace_back(Buff[I].data(), Buff[I].size());
156
0
  }
157
0
158
0
  for (std::unique_ptr<MemoryBuffer> &File : Files)
159
0
    if (File)
160
0
      Ret.push_back(File->getBuffer());
161
0
162
0
  return Ret;
163
0
}