Coverage Report

Created: 2019-05-19 14:56

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