Coverage Report

Created: 2019-07-24 05:18

/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
31
static std::unique_ptr<lto::LTO> createLTO() {
43
31
  lto::Config c;
44
31
  c.Options = initTargetOptionsFromCodeGenFlags();
45
31
46
31
  // Always emit a section per function/data with LTO.
47
31
  c.Options.FunctionSections = true;
48
31
  c.Options.DataSections = true;
49
31
50
31
  c.DisableVerify = config->disableVerify;
51
31
  c.DiagHandler = diagnosticHandler;
52
31
  c.OptLevel = config->ltoo;
53
31
  c.MAttrs = getMAttrs();
54
31
  c.CGOptLevel = args::getCGOptLevel(config->ltoo);
55
31
56
31
  if (config->relocatable)
57
8
    c.RelocModel = None;
58
23
  else if (config->isPic)
59
0
    c.RelocModel = Reloc::PIC_;
60
23
  else
61
23
    c.RelocModel = Reloc::Static;
62
31
63
31
  if (config->saveTemps)
64
5
    checkError(c.addSaveTemps(config->outputFile.str() + ".",
65
5
                              /*UseInputModulePath*/ true));
66
31
67
31
  lto::ThinBackend backend;
68
31
  if (config->thinLTOJobs != -1U)
69
2
    backend = lto::createInProcessThinBackend(config->thinLTOJobs);
70
31
  return llvm::make_unique<lto::LTO>(std::move(c), backend,
71
31
                                     config->ltoPartitions);
72
31
}
73
74
31
BitcodeCompiler::BitcodeCompiler() : ltoObj(createLTO()) {}
75
76
31
BitcodeCompiler::~BitcodeCompiler() = default;
77
78
48
static void undefine(Symbol *s) {
79
48
  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
3
  else if (isa<DefinedData>(s))
84
3
    replaceSymbol<UndefinedData>(s, s->getName(), 0, s->getFile());
85
3
  else
86
3
    
llvm_unreachable0
("unexpected symbol kind");
87
48
}
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
65
  for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
97
65
    Symbol *sym = syms[symNum];
98
65
    lto::SymbolResolution &r = resols[symNum];
99
65
    ++symNum;
100
65
101
65
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
102
65
    // reports two symbols for module ASM defined. Without this check, lld
103
65
    // flags an undefined in IR with a definition in ASM as prevailing.
104
65
    // Once IRObjectFile is fixed to report only one symbol this hack can
105
65
    // be removed.
106
65
    r.Prevailing = !objSym.isUndefined() && 
sym->getFile() == &f50
;
107
65
    r.VisibleToRegularObj = config->relocatable || 
sym->isUsedInRegularObj45
||
108
65
                            
(20
r.Prevailing20
&&
sym->isExported()11
);
109
65
    if (r.Prevailing)
110
48
      undefine(sym);
111
65
112
65
    // We tell LTO to not apply interprocedural optimization for wrapped
113
65
    // (with --wrap) symbols because otherwise LTO would inline them while
114
65
    // their values are still not final.
115
65
    r.LinkerRedefined = !sym->canInline;
116
65
  }
117
42
  checkError(ltoObj->add(std::move(f.obj), resols));
118
42
}
119
120
// Merge all the bitcode files we have seen, codegen the result
121
// and return the resulting objects.
122
31
std::vector<StringRef> BitcodeCompiler::compile() {
123
31
  unsigned maxTasks = ltoObj->getMaxTasks();
124
31
  buf.resize(maxTasks);
125
31
  files.resize(maxTasks);
126
31
127
31
  // The --thinlto-cache-dir option specifies the path to a directory in which
128
31
  // to cache native object files for ThinLTO incremental builds. If a path was
129
31
  // specified, configure LTO to use it as the cache directory.
130
31
  lto::NativeObjectCache cache;
131
31
  if (!config->thinLTOCacheDir.empty())
132
5
    cache = check(
133
5
        lto::localCache(config->thinLTOCacheDir,
134
10
                        [&](size_t task, std::unique_ptr<MemoryBuffer> mb) {
135
10
                          files[task] = std::move(mb);
136
10
                        }));
137
31
138
31
  checkError(ltoObj->run(
139
38
      [&](size_t task) {
140
38
        return llvm::make_unique<lto::NativeObjectStream>(
141
38
            llvm::make_unique<raw_svector_ostream>(buf[task]));
142
38
      },
143
31
      cache));
144
31
145
31
  if (!config->thinLTOCacheDir.empty())
146
5
    pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy);
147
31
148
31
  std::vector<StringRef> ret;
149
79
  for (unsigned i = 0; i != maxTasks; 
++i48
) {
150
48
    if (buf[i].empty())
151
10
      continue;
152
38
    if (config->saveTemps) {
153
10
      if (i == 0)
154
5
        saveBuffer(buf[i], config->outputFile + ".lto.o");
155
5
      else
156
5
        saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
157
10
    }
158
38
    ret.emplace_back(buf[i].data(), buf[i].size());
159
38
  }
160
31
161
31
  for (std::unique_ptr<MemoryBuffer> &file : files)
162
48
    if (file)
163
10
      ret.push_back(file->getBuffer());
164
31
165
31
  return ret;
166
31
}