Coverage Report

Created: 2019-01-18 03:29

/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
41
static std::unique_ptr<lto::LTO> createLTO() {
45
41
  lto::Config C;
46
41
  C.Options = InitTargetOptionsFromCodeGenFlags();
47
41
48
41
  // Always emit a section per function/datum with LTO. LLVM LTO should get most
49
41
  // of the benefit of linker GC, but there are still opportunities for ICF.
50
41
  C.Options.FunctionSections = true;
51
41
  C.Options.DataSections = true;
52
41
53
41
  // Use static reloc model on 32-bit x86 because it usually results in more
54
41
  // compact code, and because there are also known code generation bugs when
55
41
  // using the PIC model (see PR34306).
56
41
  if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
57
2
    C.RelocModel = Reloc::Static;
58
39
  else
59
39
    C.RelocModel = Reloc::PIC_;
60
41
  C.DisableVerify = true;
61
41
  C.DiagHandler = diagnosticHandler;
62
41
  C.OptLevel = Config->LTOO;
63
41
  C.CPU = GetCPUStr();
64
41
  C.MAttrs = GetMAttrs();
65
41
66
41
  if (Config->SaveTemps)
67
3
    checkError(C.addSaveTemps(std::string(Config->OutputFile) + ".",
68
3
                              /*UseInputModulePath*/ true));
69
41
  lto::ThinBackend Backend;
70
41
  if (Config->ThinLTOJobs != 0)
71
0
    Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
72
41
  return llvm::make_unique<lto::LTO>(std::move(C), Backend,
73
41
                                     Config->LTOPartitions);
74
41
}
75
76
41
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
77
78
41
BitcodeCompiler::~BitcodeCompiler() = default;
79
80
78
static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
81
82
54
void BitcodeCompiler::add(BitcodeFile &F) {
83
54
  lto::InputFile &Obj = *F.Obj;
84
54
  unsigned SymNum = 0;
85
54
  std::vector<Symbol *> SymBodies = F.getSymbols();
86
54
  std::vector<lto::SymbolResolution> Resols(SymBodies.size());
87
54
88
54
  // Provide a resolution to the LTO API for each symbol.
89
107
  for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
90
107
    Symbol *Sym = SymBodies[SymNum];
91
107
    lto::SymbolResolution &R = Resols[SymNum];
92
107
    ++SymNum;
93
107
94
107
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
95
107
    // reports two symbols for module ASM defined. Without this check, lld
96
107
    // flags an undefined in IR with a definition in ASM as prevailing.
97
107
    // Once IRObjectFile is fixed to report only one symbol this hack can
98
107
    // be removed.
99
107
    R.Prevailing = !ObjSym.isUndefined() && 
Sym->getFile() == &F88
;
100
107
    R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
101
107
    if (R.Prevailing)
102
78
      undefine(Sym);
103
107
  }
104
54
  checkError(LTOObj->add(std::move(F.Obj), Resols));
105
54
}
106
107
// Merge all the bitcode files we have seen, codegen the result
108
// and return the resulting objects.
109
41
std::vector<StringRef> BitcodeCompiler::compile() {
110
41
  unsigned MaxTasks = LTOObj->getMaxTasks();
111
41
  Buf.resize(MaxTasks);
112
41
  Files.resize(MaxTasks);
113
41
114
41
  // The /lldltocache option specifies the path to a directory in which to cache
115
41
  // native object files for ThinLTO incremental builds. If a path was
116
41
  // specified, configure LTO to use it as the cache directory.
117
41
  lto::NativeObjectCache Cache;
118
41
  if (!Config->LTOCache.empty())
119
1
    Cache = check(lto::localCache(
120
2
        Config->LTOCache, [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
121
2
          Files[Task] = std::move(MB);
122
2
        }));
123
41
124
41
  checkError(LTOObj->run(
125
49
      [&](size_t Task) {
126
49
        return llvm::make_unique<lto::NativeObjectStream>(
127
49
            llvm::make_unique<raw_svector_ostream>(Buf[Task]));
128
49
      },
129
41
      Cache));
130
41
131
41
  if (!Config->LTOCache.empty())
132
1
    pruneCache(Config->LTOCache, Config->LTOCachePolicy);
133
41
134
41
  std::vector<StringRef> Ret;
135
92
  for (unsigned I = 0; I != MaxTasks; 
++I51
) {
136
51
    if (Buf[I].empty())
137
2
      continue;
138
49
    if (Config->SaveTemps) {
139
8
      if (I == 0)
140
3
        saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
141
5
      else
142
5
        saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
143
8
    }
144
49
    Ret.emplace_back(Buf[I].data(), Buf[I].size());
145
49
  }
146
41
147
41
  for (std::unique_ptr<MemoryBuffer> &File : Files)
148
51
    if (File)
149
2
      Ret.push_back(File->getBuffer());
150
41
151
41
  return Ret;
152
41
}