Coverage Report

Created: 2019-05-19 14:56

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