Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Error.h"
13
#include "InputFiles.h"
14
#include "Symbols.h"
15
#include "lld/Core/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
405
static void checkError(Error E) {
52
405
  handleAllErrors(std::move(E),
53
1
                  [&](ErrorInfoBase &EIB) { error(EIB.message()); });
54
405
}
55
56
54
static void saveBuffer(StringRef Buffer, const Twine &Path) {
57
54
  std::error_code EC;
58
54
  raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
59
54
  if (EC)
60
0
    error("cannot create " + Path + ": " + EC.message());
61
54
  OS << Buffer;
62
54
}
63
64
36
static std::unique_ptr<lto::LTO> createLTO() {
65
36
  lto::Config Conf;
66
36
  Conf.Options = InitTargetOptionsFromCodeGenFlags();
67
36
  Conf.RelocModel = Reloc::PIC_;
68
36
  Conf.DisableVerify = true;
69
36
  Conf.DiagHandler = diagnosticHandler;
70
36
  Conf.OptLevel = Config->LTOOptLevel;
71
36
  if (Config->SaveTemps)
72
2
    checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
73
2
                                 /*UseInputModulePath*/ true));
74
36
  lto::ThinBackend Backend;
75
36
  if (Config->LTOJobs != 0)
76
0
    Backend = lto::createInProcessThinBackend(Config->LTOJobs);
77
36
  return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
78
36
                                     Config->LTOPartitions);
79
36
}
80
81
36
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
82
83
36
BitcodeCompiler::~BitcodeCompiler() = default;
84
85
70
static void undefine(Symbol *S) {
86
70
  replaceBody<Undefined>(S, S->body()->getName());
87
70
}
88
89
49
void BitcodeCompiler::add(BitcodeFile &F) {
90
49
  lto::InputFile &Obj = *F.Obj;
91
49
  unsigned SymNum = 0;
92
49
  std::vector<SymbolBody *> SymBodies = F.getSymbols();
93
49
  std::vector<lto::SymbolResolution> Resols(SymBodies.size());
94
49
95
49
  // Provide a resolution to the LTO API for each symbol.
96
101
  for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
97
101
    SymbolBody *B = SymBodies[SymNum];
98
101
    Symbol *Sym = B->symbol();
99
101
    lto::SymbolResolution &R = Resols[SymNum];
100
101
    ++SymNum;
101
101
102
101
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
103
101
    // reports two symbols for module ASM defined. Without this check, lld
104
101
    // flags an undefined in IR with a definition in ASM as prevailing.
105
101
    // Once IRObjectFile is fixed to report only one symbol this hack can
106
101
    // be removed.
107
80
    R.Prevailing = !ObjSym.isUndefined() && B->getFile() == &F;
108
101
    R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
109
101
    if (R.Prevailing)
110
70
      undefine(Sym);
111
101
  }
112
49
  checkError(LTOObj->add(std::move(F.Obj), Resols));
113
49
}
114
115
// Merge all the bitcode files we have seen, codegen the result
116
// and return the resulting objects.
117
36
std::vector<StringRef> BitcodeCompiler::compile() {
118
36
  unsigned MaxTasks = LTOObj->getMaxTasks();
119
36
  Buff.resize(MaxTasks);
120
36
  Files.resize(MaxTasks);
121
36
122
36
  // The /lldltocache option specifies the path to a directory in which to cache
123
36
  // native object files for ThinLTO incremental builds. If a path was
124
36
  // specified, configure LTO to use it as the cache directory.
125
36
  lto::NativeObjectCache Cache;
126
36
  if (!Config->LTOCache.empty())
127
1
    Cache = check(
128
1
        lto::localCache(Config->LTOCache,
129
1
                        [&](size_t Task, std::unique_ptr<MemoryBuffer> MB,
130
2
                            StringRef Path) { Files[Task] = std::move(MB); }));
131
36
132
36
  checkError(LTOObj->run(
133
41
      [&](size_t Task) {
134
41
        return llvm::make_unique<lto::NativeObjectStream>(
135
41
            llvm::make_unique<raw_svector_ostream>(Buff[Task]));
136
41
      },
137
36
      Cache));
138
36
139
36
  if (!Config->LTOCache.empty())
140
1
    pruneCache(Config->LTOCache, Config->LTOCachePolicy);
141
36
142
36
  std::vector<StringRef> Ret;
143
82
  for (unsigned I = 0; 
I != MaxTasks82
;
++I46
) {
144
46
    if (Buff[I].empty())
145
5
      continue;
146
41
    
if (41
Config->SaveTemps41
) {
147
3
      if (I == 0)
148
2
        saveBuffer(Buff[I], Config->OutputFile + ".lto.obj");
149
3
      else
150
1
        saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.obj");
151
3
    }
152
46
    Ret.emplace_back(Buff[I].data(), Buff[I].size());
153
46
  }
154
36
155
36
  for (std::unique_ptr<MemoryBuffer> &File : Files)
156
46
    
if (46
File46
)
157
2
      Ret.push_back(File->getBuffer());
158
36
159
36
  return Ret;
160
36
}