Coverage Report

Created: 2018-10-20 12:32

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