Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/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::wasm;
43
44
static std::unique_ptr<lto::LTO> createLTO() {
45
  lto::Config C;
46
  C.Options = InitTargetOptionsFromCodeGenFlags();
47
48
  // Always emit a section per function/datum with LTO.
49
  C.Options.FunctionSections = true;
50
  C.Options.DataSections = true;
51
52
  C.DisableVerify = Config->DisableVerify;
53
  C.DiagHandler = diagnosticHandler;
54
  C.OptLevel = Config->LTOO;
55
56
  if (Config->SaveTemps)
57
    checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
58
                              /*UseInputModulePath*/ true));
59
60
  lto::ThinBackend Backend;
61
  if (Config->ThinLTOJobs != -1U)
62
    Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
63
  return llvm::make_unique<lto::LTO>(std::move(C), Backend,
64
                                     Config->LTOPartitions);
65
}
66
67
0
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
68
69
0
BitcodeCompiler::~BitcodeCompiler() = default;
70
71
0
static void undefine(Symbol *S) {
72
0
  if (isa<DefinedFunction>(S))
73
0
    replaceSymbol<UndefinedFunction>(S, S->getName(), 0);
74
0
  else if (isa<DefinedData>(S))
75
0
    replaceSymbol<UndefinedData>(S, S->getName(), 0);
76
0
  else
77
0
    llvm_unreachable("unexpected symbol kind");
78
0
}
79
80
0
void BitcodeCompiler::add(BitcodeFile &F) {
81
0
  lto::InputFile &Obj = *F.Obj;
82
0
  unsigned SymNum = 0;
83
0
  ArrayRef<Symbol *> Syms = F.getSymbols();
84
0
  std::vector<lto::SymbolResolution> Resols(Syms.size());
85
0
86
0
  // Provide a resolution to the LTO API for each symbol.
87
0
  for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
88
0
    Symbol *Sym = Syms[SymNum];
89
0
    lto::SymbolResolution &R = Resols[SymNum];
90
0
    ++SymNum;
91
0
92
0
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
93
0
    // reports two symbols for module ASM defined. Without this check, lld
94
0
    // flags an undefined in IR with a definition in ASM as prevailing.
95
0
    // Once IRObjectFile is fixed to report only one symbol this hack can
96
0
    // be removed.
97
0
    R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
98
0
    R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj;
99
0
    if (R.Prevailing)
100
0
      undefine(Sym);
101
0
  }
102
0
  checkError(LTOObj->add(std::move(F.Obj), Resols));
103
0
}
104
105
// Merge all the bitcode files we have seen, codegen the result
106
// and return the resulting objects.
107
0
std::vector<StringRef> BitcodeCompiler::compile() {
108
0
  unsigned MaxTasks = LTOObj->getMaxTasks();
109
0
  Buf.resize(MaxTasks);
110
0
  Files.resize(MaxTasks);
111
0
112
0
  // The --thinlto-cache-dir option specifies the path to a directory in which
113
0
  // to cache native object files for ThinLTO incremental builds. If a path was
114
0
  // specified, configure LTO to use it as the cache directory.
115
0
  lto::NativeObjectCache Cache;
116
0
  if (!Config->ThinLTOCacheDir.empty())
117
0
    Cache = check(
118
0
        lto::localCache(Config->ThinLTOCacheDir,
119
0
                        [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
120
0
                          Files[Task] = std::move(MB);
121
0
                        }));
122
0
123
0
  checkError(LTOObj->run(
124
0
      [&](size_t Task) {
125
0
        return llvm::make_unique<lto::NativeObjectStream>(
126
0
            llvm::make_unique<raw_svector_ostream>(Buf[Task]));
127
0
      },
128
0
      Cache));
129
0
130
0
  if (!Config->ThinLTOCacheDir.empty())
131
0
    pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
132
0
133
0
  std::vector<StringRef> Ret;
134
0
  for (unsigned I = 0; I != MaxTasks; ++I) {
135
0
    if (Buf[I].empty())
136
0
      continue;
137
0
    if (Config->SaveTemps) {
138
0
      if (I == 0)
139
0
        saveBuffer(Buf[I], Config->OutputFile + ".lto.o");
140
0
      else
141
0
        saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o");
142
0
    }
143
0
    Ret.emplace_back(Buf[I].data(), Buf[I].size());
144
0
  }
145
0
146
0
  for (std::unique_ptr<MemoryBuffer> &File : Files)
147
0
    if (File)
148
0
      Ret.push_back(File->getBuffer());
149
0
150
0
  return Ret;
151
0
}