Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/LTO/LTO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
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
// This file implements functions and classes used to support LTO.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/LTO/LTO.h"
14
#include "llvm/ADT/Statistic.h"
15
#include "llvm/Analysis/TargetLibraryInfo.h"
16
#include "llvm/Analysis/TargetTransformInfo.h"
17
#include "llvm/Bitcode/BitcodeReader.h"
18
#include "llvm/Bitcode/BitcodeWriter.h"
19
#include "llvm/CodeGen/Analysis.h"
20
#include "llvm/Config/llvm-config.h"
21
#include "llvm/IR/AutoUpgrade.h"
22
#include "llvm/IR/DiagnosticPrinter.h"
23
#include "llvm/IR/Intrinsics.h"
24
#include "llvm/IR/LegacyPassManager.h"
25
#include "llvm/IR/Mangler.h"
26
#include "llvm/IR/Metadata.h"
27
#include "llvm/IR/RemarkStreamer.h"
28
#include "llvm/LTO/LTOBackend.h"
29
#include "llvm/LTO/SummaryBasedOptimizations.h"
30
#include "llvm/Linker/IRMover.h"
31
#include "llvm/Object/IRObjectFile.h"
32
#include "llvm/Support/Error.h"
33
#include "llvm/Support/ManagedStatic.h"
34
#include "llvm/Support/MemoryBuffer.h"
35
#include "llvm/Support/Path.h"
36
#include "llvm/Support/SHA1.h"
37
#include "llvm/Support/SourceMgr.h"
38
#include "llvm/Support/TargetRegistry.h"
39
#include "llvm/Support/ThreadPool.h"
40
#include "llvm/Support/Threading.h"
41
#include "llvm/Support/VCSRevision.h"
42
#include "llvm/Support/raw_ostream.h"
43
#include "llvm/Target/TargetMachine.h"
44
#include "llvm/Target/TargetOptions.h"
45
#include "llvm/Transforms/IPO.h"
46
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
47
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
48
#include "llvm/Transforms/Utils/SplitModule.h"
49
50
#include <set>
51
52
using namespace llvm;
53
using namespace lto;
54
using namespace object;
55
56
#define DEBUG_TYPE "lto"
57
58
static cl::opt<bool>
59
    DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
60
                   cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
61
62
/// Enable global value internalization in LTO.
63
cl::opt<bool> EnableLTOInternalization(
64
    "enable-lto-internalization", cl::init(true), cl::Hidden,
65
    cl::desc("Enable global value internalization in LTO"));
66
67
// Computes a unique hash for the Module considering the current list of
68
// export/import and other global analysis results.
69
// The hash is produced in \p Key.
70
void llvm::computeLTOCacheKey(
71
    SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
72
    StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
73
    const FunctionImporter::ExportSetTy &ExportList,
74
    const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
75
    const GVSummaryMapTy &DefinedGlobals,
76
    const std::set<GlobalValue::GUID> &CfiFunctionDefs,
77
110
    const std::set<GlobalValue::GUID> &CfiFunctionDecls) {
78
110
  // Compute the unique hash for this entry.
79
110
  // This is based on the current compiler version, the module itself, the
80
110
  // export list, the hash for every single module in the import list, the
81
110
  // list of ResolvedODR for the module, and the list of preserved symbols.
82
110
  SHA1 Hasher;
83
110
84
110
  // Start with the compiler revision
85
110
  Hasher.update(LLVM_VERSION_STRING);
86
#ifdef LLVM_REVISION
87
  Hasher.update(LLVM_REVISION);
88
#endif
89
90
110
  // Include the parts of the LTO configuration that affect code generation.
91
697
  auto AddString = [&](StringRef Str) {
92
697
    Hasher.update(Str);
93
697
    Hasher.update(ArrayRef<uint8_t>{0});
94
697
  };
95
1.90k
  auto AddUnsigned = [&](unsigned I) {
96
1.90k
    uint8_t Data[4];
97
1.90k
    Data[0] = I;
98
1.90k
    Data[1] = I >> 8;
99
1.90k
    Data[2] = I >> 16;
100
1.90k
    Data[3] = I >> 24;
101
1.90k
    Hasher.update(ArrayRef<uint8_t>{Data, 4});
102
1.90k
  };
103
136
  auto AddUint64 = [&](uint64_t I) {
104
136
    uint8_t Data[8];
105
136
    Data[0] = I;
106
136
    Data[1] = I >> 8;
107
136
    Data[2] = I >> 16;
108
136
    Data[3] = I >> 24;
109
136
    Data[4] = I >> 32;
110
136
    Data[5] = I >> 40;
111
136
    Data[6] = I >> 48;
112
136
    Data[7] = I >> 56;
113
136
    Hasher.update(ArrayRef<uint8_t>{Data, 8});
114
136
  };
115
110
  AddString(Conf.CPU);
116
110
  // FIXME: Hash more of Options. For now all clients initialize Options from
117
110
  // command-line flags (which is unsupported in production), but may set
118
110
  // RelaxELFRelocations. The clang driver can also pass FunctionSections,
119
110
  // DataSections and DebuggerTuning via command line flags.
120
110
  AddUnsigned(Conf.Options.RelaxELFRelocations);
121
110
  AddUnsigned(Conf.Options.FunctionSections);
122
110
  AddUnsigned(Conf.Options.DataSections);
123
110
  AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
124
110
  for (auto &A : Conf.MAttrs)
125
33
    AddString(A);
126
110
  if (Conf.RelocModel)
127
76
    AddUnsigned(*Conf.RelocModel);
128
34
  else
129
34
    AddUnsigned(-1);
130
110
  if (Conf.CodeModel)
131
1
    AddUnsigned(*Conf.CodeModel);
132
109
  else
133
109
    AddUnsigned(-1);
134
110
  AddUnsigned(Conf.CGOptLevel);
135
110
  AddUnsigned(Conf.CGFileType);
136
110
  AddUnsigned(Conf.OptLevel);
137
110
  AddUnsigned(Conf.UseNewPM);
138
110
  AddUnsigned(Conf.Freestanding);
139
110
  AddString(Conf.OptPipeline);
140
110
  AddString(Conf.AAPipeline);
141
110
  AddString(Conf.OverrideTriple);
142
110
  AddString(Conf.DefaultTriple);
143
110
  AddString(Conf.DwoDir);
144
110
145
110
  // Include the hash for the current module
146
110
  auto ModHash = Index.getModuleHash(ModuleID);
147
110
  Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
148
110
  for (auto F : ExportList)
149
45
    // The export list can impact the internalization, be conservative here
150
45
    Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
151
110
152
110
  // Include the hash for every module we import functions from. The set of
153
110
  // imported symbols for each module may affect code generation and is
154
110
  // sensitive to link order, so include that as well.
155
110
  for (auto &Entry : ImportList) {
156
34
    auto ModHash = Index.getModuleHash(Entry.first());
157
34
    Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
158
34
159
34
    AddUint64(Entry.second.size());
160
34
    for (auto &Fn : Entry.second)
161
51
      AddUint64(Fn);
162
34
  }
163
110
164
110
  // Include the hash for the resolved ODR.
165
110
  for (auto &Entry : ResolvedODR) {
166
6
    Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
167
6
                                    sizeof(GlobalValue::GUID)));
168
6
    Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
169
6
                                    sizeof(GlobalValue::LinkageTypes)));
170
6
  }
171
110
172
110
  // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
173
110
  // defined in this module.
174
110
  std::set<GlobalValue::GUID> UsedCfiDefs;
175
110
  std::set<GlobalValue::GUID> UsedCfiDecls;
176
110
177
110
  // Typeids used in this module.
178
110
  std::set<GlobalValue::GUID> UsedTypeIds;
179
110
180
231
  auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
181
231
    if (CfiFunctionDefs.count(ValueGUID))
182
2
      UsedCfiDefs.insert(ValueGUID);
183
231
    if (CfiFunctionDecls.count(ValueGUID))
184
1
      UsedCfiDecls.insert(ValueGUID);
185
231
  };
186
110
187
194
  auto AddUsedThings = [&](GlobalValueSummary *GS) {
188
194
    if (!GS) 
return0
;
189
194
    AddUnsigned(GS->isLive());
190
194
    AddUnsigned(GS->canAutoHide());
191
194
    for (const ValueInfo &VI : GS->refs()) {
192
24
      AddUnsigned(VI.isDSOLocal());
193
24
      AddUsedCfiGlobal(VI.getGUID());
194
24
    }
195
194
    if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
196
14
      AddUnsigned(GVS->maybeReadOnly());
197
14
      AddUnsigned(GVS->maybeWriteOnly());
198
14
    }
199
194
    if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
200
173
      for (auto &TT : FS->type_tests())
201
12
        UsedTypeIds.insert(TT);
202
173
      for (auto &TT : FS->type_test_assume_vcalls())
203
0
        UsedTypeIds.insert(TT.GUID);
204
173
      for (auto &TT : FS->type_checked_load_vcalls())
205
0
        UsedTypeIds.insert(TT.GUID);
206
173
      for (auto &TT : FS->type_test_assume_const_vcalls())
207
10
        UsedTypeIds.insert(TT.VFunc.GUID);
208
173
      for (auto &TT : FS->type_checked_load_const_vcalls())
209
0
        UsedTypeIds.insert(TT.VFunc.GUID);
210
173
      for (auto &ET : FS->calls()) {
211
65
        AddUnsigned(ET.first.isDSOLocal());
212
65
        AddUsedCfiGlobal(ET.first.getGUID());
213
65
      }
214
173
    }
215
194
  };
216
110
217
110
  // Include the hash for the linkage type to reflect internalization and weak
218
110
  // resolution, and collect any used type identifier resolutions.
219
140
  for (auto &GS : DefinedGlobals) {
220
140
    GlobalValue::LinkageTypes Linkage = GS.second->linkage();
221
140
    Hasher.update(
222
140
        ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
223
140
    AddUsedCfiGlobal(GS.first);
224
140
    AddUsedThings(GS.second);
225
140
  }
226
110
227
110
  // Imported functions may introduce new uses of type identifier resolutions,
228
110
  // so we need to collect their used resolutions as well.
229
110
  for (auto &ImpM : ImportList)
230
50
    
for (auto &ImpF : ImpM.second)34
{
231
50
      GlobalValueSummary *S = Index.findSummaryInModule(ImpF, ImpM.first());
232
50
      AddUsedThings(S);
233
50
      // If this is an alias, we also care about any types/etc. that the aliasee
234
50
      // may reference.
235
50
      if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
236
5
        AddUsedThings(AS->getBaseObject());
237
50
    }
238
110
239
110
  auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
240
8
    AddString(TId);
241
8
242
8
    AddUnsigned(S.TTRes.TheKind);
243
8
    AddUnsigned(S.TTRes.SizeM1BitWidth);
244
8
245
8
    AddUint64(S.TTRes.AlignLog2);
246
8
    AddUint64(S.TTRes.SizeM1);
247
8
    AddUint64(S.TTRes.BitMask);
248
8
    AddUint64(S.TTRes.InlineBits);
249
8
250
8
    AddUint64(S.WPDRes.size());
251
8
    for (auto &WPD : S.WPDRes) {
252
4
      AddUnsigned(WPD.first);
253
4
      AddUnsigned(WPD.second.TheKind);
254
4
      AddString(WPD.second.SingleImplName);
255
4
256
4
      AddUint64(WPD.second.ResByArg.size());
257
4
      for (auto &ByArg : WPD.second.ResByArg) {
258
2
        AddUint64(ByArg.first.size());
259
2
        for (uint64_t Arg : ByArg.first)
260
0
          AddUint64(Arg);
261
2
        AddUnsigned(ByArg.second.TheKind);
262
2
        AddUint64(ByArg.second.Info);
263
2
        AddUnsigned(ByArg.second.Byte);
264
2
        AddUnsigned(ByArg.second.Bit);
265
2
      }
266
4
    }
267
8
  };
268
110
269
110
  // Include the hash for all type identifiers used by this module.
270
110
  for (GlobalValue::GUID TId : UsedTypeIds) {
271
22
    auto TidIter = Index.typeIds().equal_range(TId);
272
30
    for (auto It = TidIter.first; It != TidIter.second; 
++It8
)
273
8
      AddTypeIdSummary(It->second.first, It->second.second);
274
22
  }
275
110
276
110
  AddUnsigned(UsedCfiDefs.size());
277
110
  for (auto &V : UsedCfiDefs)
278
2
    AddUint64(V);
279
110
280
110
  AddUnsigned(UsedCfiDecls.size());
281
110
  for (auto &V : UsedCfiDecls)
282
1
    AddUint64(V);
283
110
284
110
  if (!Conf.SampleProfile.empty()) {
285
0
    auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
286
0
    if (FileOrErr) {
287
0
      Hasher.update(FileOrErr.get()->getBuffer());
288
0
289
0
      if (!Conf.ProfileRemapping.empty()) {
290
0
        FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
291
0
        if (FileOrErr)
292
0
          Hasher.update(FileOrErr.get()->getBuffer());
293
0
      }
294
0
    }
295
0
  }
296
110
297
110
  Key = toHex(Hasher.result());
298
110
}
299
300
static void thinLTOResolvePrevailingGUID(
301
    ValueInfo VI, DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
302
    function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
303
        isPrevailing,
304
    function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
305
        recordNewLinkage,
306
976
    const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
307
976
  for (auto &S : VI.getSummaryList()) {
308
954
    GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
309
954
    // Ignore local and appending linkage values since the linker
310
954
    // doesn't resolve them.
311
954
    if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
312
954
        
GlobalValue::isAppendingLinkage(S->linkage())821
)
313
140
      continue;
314
814
    // We need to emit only one of these. The prevailing module will keep it,
315
814
    // but turned into a weak, while the others will drop it when possible.
316
814
    // This is both a compile-time optimization and a correctness
317
814
    // transformation. This is necessary for correctness when we have exported
318
814
    // a reference - we need to convert the linkonce to weak to
319
814
    // ensure a copy is kept to satisfy the exported reference.
320
814
    // FIXME: We may want to split the compile time and correctness
321
814
    // aspects into separate routines.
322
814
    if (isPrevailing(VI.getGUID(), S.get())) {
323
705
      if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
324
100
        S->setLinkage(GlobalValue::getWeakLinkage(
325
100
            GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
326
100
        // The kept copy is eligible for auto-hiding (hidden visibility) if all
327
100
        // copies were (i.e. they were all linkonce_odr global unnamed addr).
328
100
        // If any copy is not (e.g. it was originally weak_odr), then the symbol
329
100
        // must remain externally available (e.g. a weak_odr from an explicitly
330
100
        // instantiated template). Additionally, if it is in the
331
100
        // GUIDPreservedSymbols set, that means that it is visibile outside
332
100
        // the summary (e.g. in a native object or a bitcode file without
333
100
        // summary), and in that case we cannot hide it as it isn't possible to
334
100
        // check all copies.
335
100
        S->setCanAutoHide(VI.canAutoHide() &&
336
100
                          
!GUIDPreservedSymbols.count(VI.getGUID())10
);
337
100
      }
338
705
    }
339
109
    // Alias and aliasee can't be turned into available_externally.
340
109
    else if (!isa<AliasSummary>(S.get()) &&
341
109
             
!GlobalInvolvedWithAlias.count(S.get())60
)
342
52
      S->setLinkage(GlobalValue::AvailableExternallyLinkage);
343
814
    if (S->linkage() != OriginalLinkage)
344
148
      recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
345
814
  }
346
976
}
347
348
/// Resolve linkage for prevailing symbols in the \p Index.
349
//
350
// We'd like to drop these functions if they are no longer referenced in the
351
// current module. However there is a chance that another module is still
352
// referencing them because of the import. We make sure we always emit at least
353
// one copy.
354
void llvm::thinLTOResolvePrevailingInIndex(
355
    ModuleSummaryIndex &Index,
356
    function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
357
        isPrevailing,
358
    function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
359
        recordNewLinkage,
360
256
    const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
361
256
  // We won't optimize the globals that are referenced by an alias for now
362
256
  // Ideally we should turn the alias into a global and duplicate the definition
363
256
  // when needed.
364
256
  DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
365
256
  for (auto &I : Index)
366
976
    for (auto &S : I.second.SummaryList)
367
954
      if (auto AS = dyn_cast<AliasSummary>(S.get()))
368
175
        GlobalInvolvedWithAlias.insert(&AS->getAliasee());
369
256
370
256
  for (auto &I : Index)
371
976
    thinLTOResolvePrevailingGUID(Index.getValueInfo(I), GlobalInvolvedWithAlias,
372
976
                                 isPrevailing, recordNewLinkage,
373
976
                                 GUIDPreservedSymbols);
374
256
}
375
376
316
static bool isWeakObjectWithRWAccess(GlobalValueSummary *GVS) {
377
316
  if (auto *VarSummary = dyn_cast<GlobalVarSummary>(GVS->getBaseObject()))
378
24
    return !VarSummary->maybeReadOnly() && 
!VarSummary->maybeWriteOnly()12
&&
379
24
           
(12
VarSummary->linkage() == GlobalValue::WeakODRLinkage12
||
380
12
            
VarSummary->linkage() == GlobalValue::LinkOnceODRLinkage11
);
381
292
  return false;
382
292
}
383
384
static void thinLTOInternalizeAndPromoteGUID(
385
    GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
386
976
    function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
387
976
  for (auto &S : GVSummaryList) {
388
954
    if (isExported(S->modulePath(), GUID)) {
389
567
      if (GlobalValue::isLocalLinkage(S->linkage()))
390
40
        S->setLinkage(GlobalValue::ExternalLinkage);
391
567
    } else 
if (387
EnableLTOInternalization387
&&
392
387
               // Ignore local and appending linkage values since the linker
393
387
               // doesn't resolve them.
394
387
               
!GlobalValue::isLocalLinkage(S->linkage())383
&&
395
387
               
S->linkage() != GlobalValue::AppendingLinkage345
&&
396
387
               // We can't internalize available_externally globals because this
397
387
               // can break function pointer equality.
398
387
               
S->linkage() != GlobalValue::AvailableExternallyLinkage338
&&
399
387
               // Functions and read-only variables with linkonce_odr and
400
387
               // weak_odr linkage can be internalized. We can't internalize
401
387
               // linkonce_odr and weak_odr variables which are both modified
402
387
               // and read somewhere in the program because reads and writes
403
387
               // will become inconsistent.
404
387
               
!isWeakObjectWithRWAccess(S.get())316
)
405
313
      S->setLinkage(GlobalValue::InternalLinkage);
406
954
  }
407
976
}
408
409
// Update the linkages in the given \p Index to mark exported values
410
// as external and non-exported values as internal.
411
void llvm::thinLTOInternalizeAndPromoteInIndex(
412
    ModuleSummaryIndex &Index,
413
256
    function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
414
256
  for (auto &I : Index)
415
976
    thinLTOInternalizeAndPromoteGUID(I.second.SummaryList, I.first, isExported);
416
256
}
417
418
// Requires a destructor for std::vector<InputModule>.
419
940
InputFile::~InputFile() = default;
420
421
942
Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
422
942
  std::unique_ptr<InputFile> File(new InputFile);
423
942
424
942
  Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
425
942
  if (!FOrErr)
426
4
    return FOrErr.takeError();
427
938
428
938
  File->TargetTriple = FOrErr->TheReader.getTargetTriple();
429
938
  File->SourceFileName = FOrErr->TheReader.getSourceFileName();
430
938
  File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
431
938
  File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
432
938
  File->ComdatTable = FOrErr->TheReader.getComdatTable();
433
938
434
1.90k
  for (unsigned I = 0; I != FOrErr->Mods.size(); 
++I962
) {
435
962
    size_t Begin = File->Symbols.size();
436
962
    for (const irsymtab::Reader::SymbolRef &Sym :
437
962
         FOrErr->TheReader.module_symbols(I))
438
2.57k
      // Skip symbols that are irrelevant to LTO. Note that this condition needs
439
2.57k
      // to match the one in Skip() in LTO::addRegularLTO().
440
2.57k
      if (Sym.isGlobal() && 
!Sym.isFormatSpecific()2.38k
)
441
2.30k
        File->Symbols.push_back(Sym);
442
962
    File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
443
962
  }
444
938
445
938
  File->Mods = FOrErr->Mods;
446
938
  File->Strtab = std::move(FOrErr->Strtab);
447
938
  return std::move(File);
448
938
}
449
450
682
StringRef InputFile::getName() const {
451
682
  return Mods[0].getModuleIdentifier();
452
682
}
453
454
349
BitcodeModule &InputFile::getSingleBitcodeModule() {
455
349
  assert(Mods.size() == 1 && "Expect only one bitcode module");
456
349
  return Mods[0];
457
349
}
458
459
LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
460
                                      Config &Conf)
461
    : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
462
      Ctx(Conf), CombinedModule(llvm::make_unique<Module>("ld-temp.o", Ctx)),
463
3.06k
      Mover(llvm::make_unique<IRMover>(*CombinedModule)) {}
464
465
LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
466
3.06k
    : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
467
3.06k
  if (!Backend)
468
2.83k
    this->Backend =
469
2.83k
        createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
470
3.06k
}
471
472
LTO::LTO(Config Conf, ThinBackend Backend,
473
         unsigned ParallelCodeGenParallelismLevel)
474
    : Conf(std::move(Conf)),
475
      RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
476
3.06k
      ThinLTO(std::move(Backend)) {}
477
478
// Requires a destructor for MapVector<BitcodeModule>.
479
3.05k
LTO::~LTO() = default;
480
481
// Add the symbols in the given module to the GlobalResolutions map, and resolve
482
// their partitions.
483
void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
484
                               ArrayRef<SymbolResolution> Res,
485
697
                               unsigned Partition, bool InSummary) {
486
697
  auto *ResI = Res.begin();
487
697
  auto *ResE = Res.end();
488
697
  (void)ResE;
489
1.36k
  for (const InputFile::Symbol &Sym : Syms) {
490
1.36k
    assert(ResI != ResE);
491
1.36k
    SymbolResolution Res = *ResI++;
492
1.36k
493
1.36k
    StringRef Name = Sym.getName();
494
1.36k
    Triple TT(RegularLTO.CombinedModule->getTargetTriple());
495
1.36k
    // Strip the __imp_ prefix from COFF dllimport symbols (similar to the
496
1.36k
    // way they are handled by lld), otherwise we can end up with two
497
1.36k
    // global resolutions (one with and one for a copy of the symbol without).
498
1.36k
    if (TT.isOSBinFormatCOFF() && 
Name.startswith("__imp_")156
)
499
4
      Name = Name.substr(strlen("__imp_"));
500
1.36k
    auto &GlobalRes = GlobalResolutions[Name];
501
1.36k
    GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
502
1.36k
    if (Res.Prevailing) {
503
919
      assert(!GlobalRes.Prevailing &&
504
919
             "Multiple prevailing defs are not allowed");
505
919
      GlobalRes.Prevailing = true;
506
919
      GlobalRes.IRName = Sym.getIRName();
507
919
    } else 
if (446
!GlobalRes.Prevailing446
&&
GlobalRes.IRName.empty()347
) {
508
323
      // Sometimes it can be two copies of symbol in a module and prevailing
509
323
      // symbol can have no IR name. That might happen if symbol is defined in
510
323
      // module level inline asm block. In case we have multiple modules with
511
323
      // the same symbol we want to use IR name of the prevailing symbol.
512
323
      // Otherwise, if we haven't seen a prevailing symbol, set the name so that
513
323
      // we can later use it to check if there is any prevailing copy in IR.
514
323
      GlobalRes.IRName = Sym.getIRName();
515
323
    }
516
1.36k
517
1.36k
    // Set the partition to external if we know it is re-defined by the linker
518
1.36k
    // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
519
1.36k
    // regular object, is referenced from llvm.compiler_used, or was already
520
1.36k
    // recorded as being referenced from a different partition.
521
1.36k
    if (Res.LinkerRedefined || 
Res.VisibleToRegularObj1.35k
||
Sym.isUsed()603
||
522
1.36k
        
(589
GlobalRes.Partition != GlobalResolution::Unknown589
&&
523
929
         
GlobalRes.Partition != Partition184
)) {
524
929
      GlobalRes.Partition = GlobalResolution::External;
525
929
    } else
526
436
      // First recorded reference, save the current partition.
527
436
      GlobalRes.Partition = Partition;
528
1.36k
529
1.36k
    // Flag as visible outside of summary if visible from a regular object or
530
1.36k
    // from a module that does not have a summary.
531
1.36k
    GlobalRes.VisibleOutsideSummary |=
532
1.36k
        (Res.VisibleToRegularObj || 
Sym.isUsed()605
||
!InSummary591
);
533
1.36k
  }
534
697
}
535
536
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
537
272
                                  ArrayRef<SymbolResolution> Res) {
538
272
  StringRef Path = Input->getName();
539
272
  OS << Path << '\n';
540
272
  auto ResI = Res.begin();
541
702
  for (const InputFile::Symbol &Sym : Input->symbols()) {
542
702
    assert(ResI != Res.end());
543
702
    SymbolResolution Res = *ResI++;
544
702
545
702
    OS << "-r=" << Path << ',' << Sym.getName() << ',';
546
702
    if (Res.Prevailing)
547
436
      OS << 'p';
548
702
    if (Res.FinalDefinitionInLinkageUnit)
549
278
      OS << 'l';
550
702
    if (Res.VisibleToRegularObj)
551
323
      OS << 'x';
552
702
    if (Res.LinkerRedefined)
553
4
      OS << 'r';
554
702
    OS << '\n';
555
702
  }
556
272
  OS.flush();
557
272
  assert(ResI == Res.end());
558
272
}
559
560
Error LTO::add(std::unique_ptr<InputFile> Input,
561
673
               ArrayRef<SymbolResolution> Res) {
562
673
  assert(!CalledGetMaxTasks);
563
673
564
673
  if (Conf.ResolutionFile)
565
272
    writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
566
673
567
673
  if (RegularLTO.CombinedModule->getTargetTriple().empty())
568
455
    RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
569
673
570
673
  const SymbolResolution *ResI = Res.begin();
571
1.36k
  for (unsigned I = 0; I != Input->Mods.size(); 
++I694
)
572
697
    if (Error Err = addModule(*Input, I, ResI, Res.end()))
573
3
      return Err;
574
673
575
673
  assert(ResI == Res.end());
576
670
  return Error::success();
577
673
}
578
579
Error LTO::addModule(InputFile &Input, unsigned ModI,
580
                     const SymbolResolution *&ResI,
581
697
                     const SymbolResolution *ResE) {
582
697
  Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
583
697
  if (!LTOInfo)
584
0
    return LTOInfo.takeError();
585
697
586
697
  if (EnableSplitLTOUnit.hasValue()) {
587
242
    // If only some modules were split, flag this in the index so that
588
242
    // we can skip or error on optimizations that need consistently split
589
242
    // modules (whole program devirt and lower type tests).
590
242
    if (EnableSplitLTOUnit.getValue() != LTOInfo->EnableSplitLTOUnit)
591
3
      ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
592
242
  } else
593
455
    EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
594
697
595
697
  BitcodeModule BM = Input.Mods[ModI];
596
697
  auto ModSyms = Input.module_symbols(ModI);
597
697
  addModuleToGlobalRes(ModSyms, {ResI, ResE},
598
697
                       LTOInfo->IsThinLTO ? 
ThinLTO.ModuleMap.size() + 1343
:
0354
,
599
697
                       LTOInfo->HasSummary);
600
697
601
697
  if (LTOInfo->IsThinLTO)
602
343
    return addThinLTO(BM, ModSyms, ResI, ResE);
603
354
604
354
  Expected<RegularLTOState::AddedModule> ModOrErr =
605
354
      addRegularLTO(BM, ModSyms, ResI, ResE);
606
354
  if (!ModOrErr)
607
0
    return ModOrErr.takeError();
608
354
609
354
  if (!LTOInfo->HasSummary)
610
322
    return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
611
32
612
32
  // Regular LTO module summaries are added to a dummy module that represents
613
32
  // the combined regular LTO module.
614
32
  if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, "", -1ull))
615
0
    return Err;
616
32
  RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
617
32
  return Error::success();
618
32
}
619
620
// Checks whether the given global value is in a non-prevailing comdat
621
// (comdat containing values the linker indicated were not prevailing,
622
// which we then dropped to available_externally), and if so, removes
623
// it from the comdat. This is called for all global values to ensure the
624
// comdat is empty rather than leaving an incomplete comdat. It is needed for
625
// regular LTO modules, in case we are in a mixed-LTO mode (both regular
626
// and thin LTO modules) compilation. Since the regular LTO module will be
627
// linked first in the final native link, we want to make sure the linker
628
// doesn't select any of these incomplete comdats that would be left
629
// in the regular LTO module without this cleanup.
630
static void
631
handleNonPrevailingComdat(GlobalValue &GV,
632
100
                          std::set<const Comdat *> &NonPrevailingComdats) {
633
100
  Comdat *C = GV.getComdat();
634
100
  if (!C)
635
55
    return;
636
45
637
45
  if (!NonPrevailingComdats.count(C))
638
43
    return;
639
2
640
2
  // Additionally need to drop externally visible global values from the comdat
641
2
  // to available_externally, so that there aren't multiply defined linker
642
2
  // errors.
643
2
  if (!GV.hasLocalLinkage())
644
1
    GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
645
2
646
2
  if (auto GO = dyn_cast<GlobalObject>(&GV))
647
2
    GO->setComdat(nullptr);
648
2
}
649
650
// Add a regular LTO object to the link.
651
// The resulting module needs to be linked into the combined LTO module with
652
// linkRegularLTO.
653
Expected<LTO::RegularLTOState::AddedModule>
654
LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
655
                   const SymbolResolution *&ResI,
656
354
                   const SymbolResolution *ResE) {
657
354
  RegularLTOState::AddedModule Mod;
658
354
  Expected<std::unique_ptr<Module>> MOrErr =
659
354
      BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
660
354
                       /*IsImporting*/ false);
661
354
  if (!MOrErr)
662
0
    return MOrErr.takeError();
663
354
  Module &M = **MOrErr;
664
354
  Mod.M = std::move(*MOrErr);
665
354
666
354
  if (Error Err = M.materializeMetadata())
667
0
    return std::move(Err);
668
354
  UpgradeDebugInfo(M);
669
354
670
354
  ModuleSymbolTable SymTab;
671
354
  SymTab.addModule(&M);
672
354
673
354
  for (GlobalVariable &GV : M.globals())
674
162
    if (GV.hasAppendingLinkage())
675
12
      Mod.Keep.push_back(&GV);
676
354
677
354
  DenseSet<GlobalObject *> AliasedGlobals;
678
354
  for (auto &GA : M.aliases())
679
17
    if (GlobalObject *GO = GA.getBaseObject())
680
17
      AliasedGlobals.insert(GO);
681
354
682
354
  // In this function we need IR GlobalValues matching the symbols in Syms
683
354
  // (which is not backed by a module), so we need to enumerate them in the same
684
354
  // order. The symbol enumeration order of a ModuleSymbolTable intentionally
685
354
  // matches the order of an irsymtab, but when we read the irsymtab in
686
354
  // InputFile::create we omit some symbols that are irrelevant to LTO. The
687
354
  // Skip() function skips the same symbols from the module as InputFile does
688
354
  // from the symbol table.
689
354
  auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
690
1.00k
  auto Skip = [&]() {
691
1.06k
    while (MsymI != MsymE) {
692
715
      auto Flags = SymTab.getSymbolFlags(*MsymI);
693
715
      if ((Flags & object::BasicSymbolRef::SF_Global) &&
694
715
          
!(Flags & object::BasicSymbolRef::SF_FormatSpecific)670
)
695
648
        return;
696
67
      ++MsymI;
697
67
    }
698
1.00k
  };
699
354
  Skip();
700
354
701
354
  std::set<const Comdat *> NonPrevailingComdats;
702
648
  for (const InputFile::Symbol &Sym : Syms) {
703
648
    assert(ResI != ResE);
704
648
    SymbolResolution Res = *ResI++;
705
648
706
648
    assert(MsymI != MsymE);
707
648
    ModuleSymbolTable::Symbol Msym = *MsymI++;
708
648
    Skip();
709
648
710
648
    if (GlobalValue *GV = Msym.dyn_cast<GlobalValue *>()) {
711
631
      if (Res.Prevailing) {
712
447
        if (Sym.isUndefined())
713
5
          continue;
714
442
        Mod.Keep.push_back(GV);
715
442
        // For symbols re-defined with linker -wrap and -defsym options,
716
442
        // set the linkage to weak to inhibit IPO. The linkage will be
717
442
        // restored by the linker.
718
442
        if (Res.LinkerRedefined)
719
4
          GV->setLinkage(GlobalValue::WeakAnyLinkage);
720
442
721
442
        GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
722
442
        if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
723
24
          GV->setLinkage(GlobalValue::getWeakLinkage(
724
24
              GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
725
442
      } else 
if (184
isa<GlobalObject>(GV)184
&&
726
184
                 
(179
GV->hasLinkOnceODRLinkage()179
||
GV->hasWeakODRLinkage()163
||
727
179
                  
GV->hasAvailableExternallyLinkage()157
) &&
728
184
                 
!AliasedGlobals.count(cast<GlobalObject>(GV))27
) {
729
25
        // Any of the above three types of linkage indicates that the
730
25
        // chosen prevailing symbol will have the same semantics as this copy of
731
25
        // the symbol, so we may be able to link it with available_externally
732
25
        // linkage. We will decide later whether to do that when we link this
733
25
        // module (in linkRegularLTO), based on whether it is undefined.
734
25
        Mod.Keep.push_back(GV);
735
25
        GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
736
25
        if (GV->hasComdat())
737
10
          NonPrevailingComdats.insert(GV->getComdat());
738
25
        cast<GlobalObject>(GV)->setComdat(nullptr);
739
25
      }
740
631
741
631
      // Set the 'local' flag based on the linker resolution for this symbol.
742
631
      
if (626
Res.FinalDefinitionInLinkageUnit626
) {
743
152
        GV->setDSOLocal(true);
744
152
        if (GV->hasDLLImportStorageClass())
745
0
          GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
746
0
                                 DefaultStorageClass);
747
152
      }
748
626
    }
749
648
    // Common resolution: collect the maximum size/alignment over all commons.
750
648
    // We also record if we see an instance of a common as prevailing, so that
751
648
    // if none is prevailing we can ignore it later.
752
648
    
if (643
Sym.isCommon()643
) {
753
20
      // FIXME: We should figure out what to do about commons defined by asm.
754
20
      // For now they aren't reported correctly by ModuleSymbolTable.
755
20
      auto &CommonRes = RegularLTO.Commons[Sym.getIRName()];
756
20
      CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
757
20
      CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment());
758
20
      CommonRes.Prevailing |= Res.Prevailing;
759
20
    }
760
643
761
643
  }
762
354
  if (!M.getComdatSymbolTable().empty())
763
30
    for (GlobalValue &GV : M.global_values())
764
100
      handleNonPrevailingComdat(GV, NonPrevailingComdats);
765
354
  assert(MsymI == MsymE);
766
354
  return std::move(Mod);
767
354
}
768
769
Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
770
354
                          bool LivenessFromIndex) {
771
354
  std::vector<GlobalValue *> Keep;
772
479
  for (GlobalValue *GV : Mod.Keep) {
773
479
    if (LivenessFromIndex && 
!ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())49
)
774
3
      continue;
775
476
776
476
    if (!GV->hasAvailableExternallyLinkage()) {
777
450
      Keep.push_back(GV);
778
450
      continue;
779
450
    }
780
26
781
26
    // Only link available_externally definitions if we don't already have a
782
26
    // definition.
783
26
    GlobalValue *CombinedGV =
784
26
        RegularLTO.CombinedModule->getNamedValue(GV->getName());
785
26
    if (CombinedGV && 
!CombinedGV->isDeclaration()13
)
786
13
      continue;
787
13
788
13
    Keep.push_back(GV);
789
13
  }
790
354
791
354
  return RegularLTO.Mover->move(std::move(Mod.M), Keep,
792
354
                                [](GlobalValue &, IRMover::ValueAdder) 
{}23
,
793
354
                                /* IsPerformingImport */ false);
794
354
}
795
796
// Add a ThinLTO module to the link.
797
Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
798
                      const SymbolResolution *&ResI,
799
343
                      const SymbolResolution *ResE) {
800
343
  if (Error Err =
801
0
          BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
802
0
                         ThinLTO.ModuleMap.size()))
803
0
    return Err;
804
343
805
717
  
for (const InputFile::Symbol &Sym : Syms)343
{
806
717
    assert(ResI != ResE);
807
717
    SymbolResolution Res = *ResI++;
808
717
809
717
    if (!Sym.getIRName().empty()) {
810
711
      auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
811
711
          Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
812
711
      if (Res.Prevailing) {
813
459
        ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
814
459
815
459
        // For linker redefined symbols (via --wrap or --defsym) we want to
816
459
        // switch the linkage to `weak` to prevent IPOs from happening.
817
459
        // Find the summary in the module for this very GV and record the new
818
459
        // linkage so that we can switch it when we import the GV.
819
459
        if (Res.LinkerRedefined)
820
4
          if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
821
4
                  GUID, BM.getModuleIdentifier()))
822
4
            S->setLinkage(GlobalValue::WeakAnyLinkage);
823
459
      }
824
711
825
711
      // If the linker resolved the symbol to a local definition then mark it
826
711
      // as local in the summary for the module we are adding.
827
711
      if (Res.FinalDefinitionInLinkageUnit) {
828
354
        if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
829
266
                GUID, BM.getModuleIdentifier())) {
830
266
          S->setDSOLocal(true);
831
266
        }
832
354
      }
833
711
    }
834
717
  }
835
343
836
343
  if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
837
1
    return make_error<StringError>(
838
1
        "Expected at most one ThinLTO module per bitcode file",
839
1
        inconvertibleErrorCode());
840
342
841
342
  return Error::success();
842
342
}
843
844
2.86k
unsigned LTO::getMaxTasks() const {
845
2.86k
  CalledGetMaxTasks = true;
846
2.86k
  return RegularLTO.ParallelCodeGenParallelismLevel + ThinLTO.ModuleMap.size();
847
2.86k
}
848
849
// If only some of the modules were split, we cannot correctly handle
850
// code that contains type tests or type checked loads.
851
451
Error LTO::checkPartiallySplit() {
852
451
  if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
853
448
    return Error::success();
854
3
855
3
  Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
856
3
      Intrinsic::getName(Intrinsic::type_test));
857
3
  Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
858
3
      Intrinsic::getName(Intrinsic::type_checked_load));
859
3
860
3
  // First check if there are type tests / type checked loads in the
861
3
  // merged regular LTO module IR.
862
3
  if ((TypeTestFunc && 
!TypeTestFunc->use_empty()0
) ||
863
3
      (TypeCheckedLoadFunc && 
!TypeCheckedLoadFunc->use_empty()0
))
864
0
    return make_error<StringError>(
865
0
        "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
866
0
        inconvertibleErrorCode());
867
3
868
3
  // Otherwise check if there are any recorded in the combined summary from the
869
3
  // ThinLTO modules.
870
6
  
for (auto &P : ThinLTO.CombinedIndex)3
{
871
6
    for (auto &S : P.second.SummaryList) {
872
2
      auto *FS = dyn_cast<FunctionSummary>(S.get());
873
2
      if (!FS)
874
1
        continue;
875
1
      if (!FS->type_test_assume_vcalls().empty() ||
876
1
          !FS->type_checked_load_vcalls().empty() ||
877
1
          
!FS->type_test_assume_const_vcalls().empty()0
||
878
1
          
!FS->type_checked_load_const_vcalls().empty()0
||
879
1
          
!FS->type_tests().empty()0
)
880
1
        return make_error<StringError>(
881
1
            "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
882
1
            inconvertibleErrorCode());
883
1
    }
884
6
  }
885
3
  
return Error::success()2
;
886
3
}
887
888
452
Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) {
889
452
  // Compute "dead" symbols, we don't want to import/export these!
890
452
  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
891
452
  DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
892
1.05k
  for (auto &Res : GlobalResolutions) {
893
1.05k
    // Normally resolution have IR name of symbol. We can do nothing here
894
1.05k
    // otherwise. See comments in GlobalResolution struct for more details.
895
1.05k
    if (Res.second.IRName.empty())
896
16
      continue;
897
1.04k
898
1.04k
    GlobalValue::GUID GUID = GlobalValue::getGUID(
899
1.04k
        GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
900
1.04k
901
1.04k
    if (Res.second.VisibleOutsideSummary && 
Res.second.Prevailing815
)
902
717
      GUIDPreservedSymbols.insert(GlobalValue::getGUID(
903
717
          GlobalValue::dropLLVMManglingEscape(Res.second.IRName)));
904
1.04k
905
1.04k
    GUIDPrevailingResolutions[GUID] =
906
1.04k
        Res.second.Prevailing ? 
PrevailingType::Yes903
:
PrevailingType::No138
;
907
1.04k
  }
908
452
909
452
  auto isPrevailing = [&](GlobalValue::GUID G) {
910
147
    auto It = GUIDPrevailingResolutions.find(G);
911
147
    if (It == GUIDPrevailingResolutions.end())
912
30
      return PrevailingType::Unknown;
913
117
    return It->second;
914
117
  };
915
452
  computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
916
452
                                  isPrevailing, Conf.OptLevel > 0);
917
452
918
452
  // Setup output file to emit statistics.
919
452
  auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
920
452
  if (!StatsFileOrErr)
921
0
    return StatsFileOrErr.takeError();
922
452
  std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
923
452
924
452
  // Finalize linking of regular LTO modules containing summaries now that
925
452
  // we have computed liveness information.
926
452
  for (auto &M : RegularLTO.ModsWithSummaries)
927
32
    if (Error Err = linkRegularLTO(std::move(M),
928
0
                                   /*LivenessFromIndex=*/true))
929
0
      return Err;
930
452
931
452
  // Ensure we don't have inconsistently split LTO units with type tests.
932
452
  if (Error Err = checkPartiallySplit())
933
1
    return Err;
934
451
935
451
  Error Result = runRegularLTO(AddStream);
936
451
  if (!Result)
937
446
    Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
938
451
939
451
  if (StatsFile)
940
0
    PrintStatisticsJSON(StatsFile->os());
941
451
942
451
  return Result;
943
451
}
944
945
450
Error LTO::runRegularLTO(AddStreamFn AddStream) {
946
450
  // Make sure commons have the right size/alignment: we kept the largest from
947
450
  // all the prevailing when adding the inputs, and we apply it here.
948
450
  const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
949
450
  for (auto &I : RegularLTO.Commons) {
950
13
    if (!I.second.Prevailing)
951
4
      // Don't do anything if no instance of this common was prevailing.
952
4
      continue;
953
9
    GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
954
9
    if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
955
7
      // Don't create a new global if the type is already correct, just make
956
7
      // sure the alignment is correct.
957
7
      OldGV->setAlignment(I.second.Align);
958
7
      continue;
959
7
    }
960
2
    ArrayType *Ty =
961
2
        ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
962
2
    auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
963
2
                                  GlobalValue::CommonLinkage,
964
2
                                  ConstantAggregateZero::get(Ty), "");
965
2
    GV->setAlignment(I.second.Align);
966
2
    if (OldGV) {
967
2
      OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType()));
968
2
      GV->takeName(OldGV);
969
2
      OldGV->eraseFromParent();
970
2
    } else {
971
0
      GV->setName(I.first);
972
0
    }
973
2
  }
974
450
975
450
  if (Conf.PreOptModuleHook &&
976
450
      
!Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule)169
)
977
0
    return Error::success();
978
450
979
450
  if (!Conf.CodeGenOnly) {
980
1.04k
    for (const auto &R : GlobalResolutions) {
981
1.04k
      if (!R.second.isPrevailingIRSymbol())
982
153
        continue;
983
895
      if (R.second.Partition != 0 &&
984
895
          
R.second.Partition != GlobalResolution::External813
)
985
58
        continue;
986
837
987
837
      GlobalValue *GV =
988
837
          RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
989
837
      // Ignore symbols defined in other partitions.
990
837
      // Also skip declarations, which are not allowed to have internal linkage.
991
837
      if (!GV || 
GV->hasLocalLinkage()464
||
GV->isDeclaration()464
)
992
400
        continue;
993
437
      GV->setUnnamedAddr(R.second.UnnamedAddr ? 
GlobalValue::UnnamedAddr::Global11
994
437
                                              : 
GlobalValue::UnnamedAddr::None426
);
995
437
      if (EnableLTOInternalization && 
R.second.Partition == 0435
)
996
80
        GV->setLinkage(GlobalValue::InternalLinkage);
997
437
    }
998
450
999
450
    if (Conf.PostInternalizeModuleHook &&
1000
450
        
!Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule)170
)
1001
1
      return Error::success();
1002
449
  }
1003
449
  return backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1004
449
                 std::move(RegularLTO.CombinedModule), ThinLTO.CombinedIndex);
1005
449
}
1006
1007
/// This class defines the interface to the ThinLTO backend.
1008
class lto::ThinBackendProc {
1009
protected:
1010
  Config &Conf;
1011
  ModuleSummaryIndex &CombinedIndex;
1012
  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries;
1013
1014
public:
1015
  ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex,
1016
                  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries)
1017
      : Conf(Conf), CombinedIndex(CombinedIndex),
1018
201
        ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {}
1019
1020
201
  virtual ~ThinBackendProc() {}
1021
  virtual Error start(
1022
      unsigned Task, BitcodeModule BM,
1023
      const FunctionImporter::ImportMapTy &ImportList,
1024
      const FunctionImporter::ExportSetTy &ExportList,
1025
      const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1026
      MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
1027
  virtual Error wait() = 0;
1028
};
1029
1030
namespace {
1031
class InProcessThinBackend : public ThinBackendProc {
1032
  ThreadPool BackendThreadPool;
1033
  AddStreamFn AddStream;
1034
  NativeObjectCache Cache;
1035
  std::set<GlobalValue::GUID> CfiFunctionDefs;
1036
  std::set<GlobalValue::GUID> CfiFunctionDecls;
1037
1038
  Optional<Error> Err;
1039
  std::mutex ErrMu;
1040
1041
public:
1042
  InProcessThinBackend(
1043
      Config &Conf, ModuleSummaryIndex &CombinedIndex,
1044
      unsigned ThinLTOParallelismLevel,
1045
      const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1046
      AddStreamFn AddStream, NativeObjectCache Cache)
1047
      : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
1048
        BackendThreadPool(ThinLTOParallelismLevel),
1049
166
        AddStream(std::move(AddStream)), Cache(std::move(Cache)) {
1050
166
    for (auto &Name : CombinedIndex.cfiFunctionDefs())
1051
2
      CfiFunctionDefs.insert(
1052
2
          GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
1053
166
    for (auto &Name : CombinedIndex.cfiFunctionDecls())
1054
1
      CfiFunctionDecls.insert(
1055
1
          GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
1056
166
  }
1057
1058
  Error runThinLTOBackendThread(
1059
      AddStreamFn AddStream, NativeObjectCache Cache, unsigned Task,
1060
      BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1061
      const FunctionImporter::ImportMapTy &ImportList,
1062
      const FunctionImporter::ExportSetTy &ExportList,
1063
      const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1064
      const GVSummaryMapTy &DefinedGlobals,
1065
279
      MapVector<StringRef, BitcodeModule> &ModuleMap) {
1066
279
    auto RunThinBackend = [&](AddStreamFn AddStream) {
1067
261
      LTOLLVMContext BackendContext(Conf);
1068
261
      Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1069
261
      if (!MOrErr)
1070
0
        return MOrErr.takeError();
1071
261
1072
261
      return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1073
261
                         ImportList, DefinedGlobals, ModuleMap);
1074
261
    };
1075
279
1076
279
    auto ModuleID = BM.getModuleIdentifier();
1077
279
1078
279
    if (!Cache || 
!CombinedIndex.modulePaths().count(ModuleID)82
||
1079
279
        all_of(CombinedIndex.getModuleHash(ModuleID),
1080
97
               [](uint32_t V) { return V == 0; }))
1081
201
      // Cache disabled or no entry for this module in the combined index or
1082
201
      // no module hash.
1083
201
      return RunThinBackend(AddStream);
1084
78
1085
78
    SmallString<40> Key;
1086
78
    // The module may be cached, this helps handling it.
1087
78
    computeLTOCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList,
1088
78
                       ExportList, ResolvedODR, DefinedGlobals, CfiFunctionDefs,
1089
78
                       CfiFunctionDecls);
1090
78
    if (AddStreamFn CacheAddStream = Cache(Task, Key))
1091
60
      return RunThinBackend(CacheAddStream);
1092
18
1093
18
    return Error::success();
1094
18
  }
1095
1096
  Error start(
1097
      unsigned Task, BitcodeModule BM,
1098
      const FunctionImporter::ImportMapTy &ImportList,
1099
      const FunctionImporter::ExportSetTy &ExportList,
1100
      const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1101
279
      MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1102
279
    StringRef ModulePath = BM.getModuleIdentifier();
1103
279
    assert(ModuleToDefinedGVSummaries.count(ModulePath));
1104
279
    const GVSummaryMapTy &DefinedGlobals =
1105
279
        ModuleToDefinedGVSummaries.find(ModulePath)->second;
1106
279
    BackendThreadPool.async(
1107
279
        [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1108
279
            const FunctionImporter::ImportMapTy &ImportList,
1109
279
            const FunctionImporter::ExportSetTy &ExportList,
1110
279
            const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1111
279
                &ResolvedODR,
1112
279
            const GVSummaryMapTy &DefinedGlobals,
1113
279
            MapVector<StringRef, BitcodeModule> &ModuleMap) {
1114
278
          Error E = runThinLTOBackendThread(
1115
278
              AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1116
278
              ResolvedODR, DefinedGlobals, ModuleMap);
1117
278
          if (E) {
1118
0
            std::unique_lock<std::mutex> L(ErrMu);
1119
0
            if (Err)
1120
0
              Err = joinErrors(std::move(*Err), std::move(E));
1121
0
            else
1122
0
              Err = std::move(E);
1123
0
          }
1124
278
        },
1125
279
        BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1126
279
        std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1127
279
    return Error::success();
1128
279
  }
1129
1130
166
  Error wait() override {
1131
166
    BackendThreadPool.wait();
1132
166
    if (Err)
1133
0
      return std::move(*Err);
1134
166
    else
1135
166
      return Error::success();
1136
166
  }
1137
};
1138
} // end anonymous namespace
1139
1140
3.02k
ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) {
1141
3.02k
  return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
1142
3.02k
             const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1143
3.02k
             AddStreamFn AddStream, NativeObjectCache Cache) {
1144
166
    return llvm::make_unique<InProcessThinBackend>(
1145
166
        Conf, CombinedIndex, ParallelismLevel, ModuleToDefinedGVSummaries,
1146
166
        AddStream, Cache);
1147
166
  };
1148
3.02k
}
1149
1150
// Given the original \p Path to an output file, replace any path
1151
// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1152
// resulting directory if it does not yet exist.
1153
std::string lto::getThinLTOOutputFile(const std::string &Path,
1154
                                      const std::string &OldPrefix,
1155
65
                                      const std::string &NewPrefix) {
1156
65
  if (OldPrefix.empty() && 
NewPrefix.empty()63
)
1157
63
    return Path;
1158
2
  SmallString<128> NewPath(Path);
1159
2
  llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1160
2
  StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1161
2
  if (!ParentPath.empty()) {
1162
2
    // Make sure the new directory exists, creating it if necessary.
1163
2
    if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1164
0
      llvm::errs() << "warning: could not create directory '" << ParentPath
1165
0
                   << "': " << EC.message() << '\n';
1166
2
  }
1167
2
  return NewPath.str();
1168
2
}
1169
1170
namespace {
1171
class WriteIndexesThinBackend : public ThinBackendProc {
1172
  std::string OldPrefix, NewPrefix;
1173
  bool ShouldEmitImportsFiles;
1174
  raw_fd_ostream *LinkedObjectsFile;
1175
  lto::IndexWriteCallback OnWrite;
1176
1177
public:
1178
  WriteIndexesThinBackend(
1179
      Config &Conf, ModuleSummaryIndex &CombinedIndex,
1180
      const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1181
      std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1182
      raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1183
      : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
1184
        OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1185
        ShouldEmitImportsFiles(ShouldEmitImportsFiles),
1186
35
        LinkedObjectsFile(LinkedObjectsFile), OnWrite(OnWrite) {}
1187
1188
  Error start(
1189
      unsigned Task, BitcodeModule BM,
1190
      const FunctionImporter::ImportMapTy &ImportList,
1191
      const FunctionImporter::ExportSetTy &ExportList,
1192
      const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1193
58
      MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1194
58
    StringRef ModulePath = BM.getModuleIdentifier();
1195
58
    std::string NewModulePath =
1196
58
        getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
1197
58
1198
58
    if (LinkedObjectsFile)
1199
3
      *LinkedObjectsFile << NewModulePath << '\n';
1200
58
1201
58
    std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1202
58
    gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1203
58
                                     ImportList, ModuleToSummariesForIndex);
1204
58
1205
58
    std::error_code EC;
1206
58
    raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1207
58
                      sys::fs::OpenFlags::F_None);
1208
58
    if (EC)
1209
1
      return errorCodeToError(EC);
1210
57
    WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
1211
57
1212
57
    if (ShouldEmitImportsFiles) {
1213
36
      EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1214
36
                            ModuleToSummariesForIndex);
1215
36
      if (EC)
1216
2
        return errorCodeToError(EC);
1217
55
    }
1218
55
1219
55
    if (OnWrite)
1220
32
      OnWrite(ModulePath);
1221
55
    return Error::success();
1222
55
  }
1223
1224
32
  Error wait() override { return Error::success(); }
1225
};
1226
} // end anonymous namespace
1227
1228
ThinBackend lto::createWriteIndexesThinBackend(
1229
    std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1230
38
    raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1231
38
  return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
1232
38
             const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1233
38
             AddStreamFn AddStream, NativeObjectCache Cache) {
1234
35
    return llvm::make_unique<WriteIndexesThinBackend>(
1235
35
        Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
1236
35
        ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite);
1237
35
  };
1238
38
}
1239
1240
Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
1241
446
                      const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
1242
446
  if (ThinLTO.ModuleMap.empty())
1243
245
    return Error::success();
1244
201
1245
201
  if (Conf.CombinedIndexHook && 
!Conf.CombinedIndexHook(ThinLTO.CombinedIndex)84
)
1246
0
    return Error::success();
1247
201
1248
201
  // Collect for each module the list of function it defines (GUID ->
1249
201
  // Summary).
1250
201
  StringMap<GVSummaryMapTy>
1251
201
      ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
1252
201
  ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
1253
201
      ModuleToDefinedGVSummaries);
1254
201
  // Create entries for any modules that didn't have any GV summaries
1255
201
  // (either they didn't have any GVs to start with, or we suppressed
1256
201
  // generation of the summaries because they e.g. had inline assembly
1257
201
  // uses that couldn't be promoted/renamed on export). This is so
1258
201
  // InProcessThinBackend::start can still launch a backend thread, which
1259
201
  // is passed the map of summaries for the module, without any special
1260
201
  // handling for this case.
1261
201
  for (auto &Mod : ThinLTO.ModuleMap)
1262
337
    if (!ModuleToDefinedGVSummaries.count(Mod.first))
1263
26
      ModuleToDefinedGVSummaries.try_emplace(Mod.first);
1264
201
1265
201
  // Synthesize entry counts for functions in the CombinedIndex.
1266
201
  computeSyntheticCounts(ThinLTO.CombinedIndex);
1267
201
1268
201
  StringMap<FunctionImporter::ImportMapTy> ImportLists(
1269
201
      ThinLTO.ModuleMap.size());
1270
201
  StringMap<FunctionImporter::ExportSetTy> ExportLists(
1271
201
      ThinLTO.ModuleMap.size());
1272
201
  StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
1273
201
1274
201
  if (DumpThinCGSCCs)
1275
1
    ThinLTO.CombinedIndex.dumpSCCs(outs());
1276
201
1277
201
  if (Conf.OptLevel > 0)
1278
197
    ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1279
197
                             ImportLists, ExportLists);
1280
201
1281
201
  // Figure out which symbols need to be internalized. This also needs to happen
1282
201
  // at -O0 because summary-based DCE is implemented using internalization, and
1283
201
  // we must apply DCE consistently with the full LTO module in order to avoid
1284
201
  // undefined references during the final link.
1285
201
  std::set<GlobalValue::GUID> ExportedGUIDs;
1286
550
  for (auto &Res : GlobalResolutions) {
1287
550
    // If the symbol does not have external references or it is not prevailing,
1288
550
    // then not need to mark it as exported from a ThinLTO partition.
1289
550
    if (Res.second.Partition != GlobalResolution::External ||
1290
550
        
!Res.second.isPrevailingIRSymbol()466
)
1291
116
      continue;
1292
434
    auto GUID = GlobalValue::getGUID(
1293
434
        GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1294
434
    // Mark exported unless index-based analysis determined it to be dead.
1295
434
    if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
1296
424
      ExportedGUIDs.insert(GUID);
1297
434
  }
1298
201
1299
201
  // Any functions referenced by the jump table in the regular LTO object must
1300
201
  // be exported.
1301
201
  for (auto &Def : ThinLTO.CombinedIndex.cfiFunctionDefs())
1302
7
    ExportedGUIDs.insert(
1303
7
        GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Def)));
1304
201
1305
533
  auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
1306
533
    const auto &ExportList = ExportLists.find(ModuleIdentifier);
1307
533
    return (ExportList != ExportLists.end() &&
1308
533
            
ExportList->second.count(GUID)202
) ||
1309
533
           
ExportedGUIDs.count(GUID)382
;
1310
533
  };
1311
201
  thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported);
1312
201
1313
201
  auto isPrevailing = [&](GlobalValue::GUID GUID,
1314
435
                          const GlobalValueSummary *S) {
1315
435
    return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
1316
435
  };
1317
201
  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
1318
201
                              GlobalValue::GUID GUID,
1319
201
                              GlobalValue::LinkageTypes NewLinkage) {
1320
39
    ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
1321
39
  };
1322
201
  thinLTOResolvePrevailingInIndex(ThinLTO.CombinedIndex, isPrevailing,
1323
201
                                  recordNewLinkage, GUIDPreservedSymbols);
1324
201
1325
201
  std::unique_ptr<ThinBackendProc> BackendProc =
1326
201
      ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1327
201
                      AddStream, Cache);
1328
201
1329
201
  // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for combined
1330
201
  // module and parallel code generation partitions.
1331
201
  unsigned Task = RegularLTO.ParallelCodeGenParallelismLevel;
1332
337
  for (auto &Mod : ThinLTO.ModuleMap) {
1333
337
    if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first],
1334
3
                                     ExportLists[Mod.first],
1335
3
                                     ResolvedODR[Mod.first], ThinLTO.ModuleMap))
1336
3
      return E;
1337
334
    ++Task;
1338
334
  }
1339
201
1340
201
  
return BackendProc->wait()198
;
1341
201
}
1342
1343
Expected<std::unique_ptr<ToolOutputFile>>
1344
lto::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
1345
                              StringRef RemarksPasses, StringRef RemarksFormat,
1346
848
                              bool RemarksWithHotness, int Count) {
1347
848
  std::string Filename = RemarksFilename;
1348
848
  if (!Filename.empty() && 
Count != -115
)
1349
5
    Filename += ".thin." + llvm::utostr(Count) + ".yaml";
1350
848
1351
848
  auto ResultOrErr = llvm::setupOptimizationRemarks(
1352
848
      Context, Filename, RemarksPasses, RemarksFormat, RemarksWithHotness);
1353
848
  if (Error E = ResultOrErr.takeError())
1354
0
    return std::move(E);
1355
848
1356
848
  if (*ResultOrErr)
1357
15
    (*ResultOrErr)->keep();
1358
848
1359
848
  return ResultOrErr;
1360
848
}
1361
1362
Expected<std::unique_ptr<ToolOutputFile>>
1363
500
lto::setupStatsFile(StringRef StatsFilename) {
1364
500
  // Setup output file to emit statistics.
1365
500
  if (StatsFilename.empty())
1366
500
    return nullptr;
1367
0
1368
0
  llvm::EnableStatistics(false);
1369
0
  std::error_code EC;
1370
0
  auto StatsFile =
1371
0
      llvm::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::F_None);
1372
0
  if (EC)
1373
0
    return errorCodeToError(EC);
1374
0
1375
0
  StatsFile->keep();
1376
0
  return std::move(StatsFile);
1377
0
}