Coverage Report

Created: 2017-09-21 03:39

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