@@ -331,21 +331,7 @@ void SymbolTable::printMap(llvm::raw_ostream &OS) {
331
331
}
332
332
}
333
333
334
- void SymbolTable::addCombinedLTOObject () {
335
- if (BitcodeFiles.empty ())
336
- return ;
337
-
338
- // Diagnose any undefined symbols early, but do not resolve weak externals,
339
- // as resolution breaks the invariant that each Symbol points to a unique
340
- // SymbolBody, which we rely on to replace DefinedBitcode symbols correctly.
341
- reportRemainingUndefines (/* Resolve=*/ false );
342
-
343
- // Create an object file and add it to the symbol table by replacing any
344
- // DefinedBitcode symbols with the definitions in the object file.
345
- LTOCodeGenerator CG;
346
- CG.setOptLevel (Config->LTOOptLevel );
347
- ObjectFile *Obj = createLTOObject (&CG);
348
-
334
+ void SymbolTable::addCombinedLTOObject (ObjectFile *Obj) {
349
335
for (SymbolBody *Body : Obj->getSymbols ()) {
350
336
if (!Body->isExternal ())
351
337
continue ;
@@ -371,6 +357,25 @@ void SymbolTable::addCombinedLTOObject() {
371
357
if (Comp < 0 )
372
358
Sym->Body = Body;
373
359
}
360
+ }
361
+
362
+ void SymbolTable::addCombinedLTOObjects () {
363
+ if (BitcodeFiles.empty ())
364
+ return ;
365
+
366
+ // Diagnose any undefined symbols early, but do not resolve weak externals,
367
+ // as resolution breaks the invariant that each Symbol points to a unique
368
+ // SymbolBody, which we rely on to replace DefinedBitcode symbols correctly.
369
+ reportRemainingUndefines (/* Resolve=*/ false );
370
+
371
+ // Create an object file and add it to the symbol table by replacing any
372
+ // DefinedBitcode symbols with the definitions in the object file.
373
+ LTOCodeGenerator CG;
374
+ CG.setOptLevel (Config->LTOOptLevel );
375
+ std::vector<ObjectFile *> Objs = createLTOObjects (&CG);
376
+
377
+ for (ObjectFile *Obj : Objs)
378
+ addCombinedLTOObject (Obj);
374
379
375
380
size_t NumBitcodeFiles = BitcodeFiles.size ();
376
381
run ();
@@ -379,8 +384,8 @@ void SymbolTable::addCombinedLTOObject() {
379
384
}
380
385
381
386
// Combine and compile bitcode files and then return the result
382
- // as a regular COFF object file .
383
- ObjectFile *SymbolTable::createLTOObject (LTOCodeGenerator *CG) {
387
+ // as a vector of regular COFF object files .
388
+ std::vector< ObjectFile *> SymbolTable::createLTOObjects (LTOCodeGenerator *CG) {
384
389
// All symbols referenced by non-bitcode objects must be preserved.
385
390
for (ObjectFile *File : ObjectFiles)
386
391
for (SymbolBody *Body : File->getSymbols ())
@@ -406,14 +411,32 @@ ObjectFile *SymbolTable::createLTOObject(LTOCodeGenerator *CG) {
406
411
CG->addModule (BitcodeFiles[I]->getModule ());
407
412
408
413
std::string ErrMsg;
409
- LTOMB = CG->compile (false , false , false , ErrMsg); // take MB ownership
410
- if (!LTOMB)
414
+ if (!CG->optimize (false , false , false , ErrMsg))
415
+ error (ErrMsg);
416
+
417
+ Objs.resize (Config->LTOJobs );
418
+ // Use std::list to avoid invalidation of pointers in OSPtrs.
419
+ std::list<raw_svector_ostream> OSs;
420
+ std::vector<raw_pwrite_stream *> OSPtrs;
421
+ for (SmallVector<char , 0 > &Obj : Objs) {
422
+ OSs.emplace_back (Obj);
423
+ OSPtrs.push_back (&OSs.back ());
424
+ }
425
+
426
+ if (!CG->compileOptimized (OSPtrs, ErrMsg))
411
427
error (ErrMsg);
412
- auto *Obj = new ObjectFile (LTOMB->getMemBufferRef ());
413
- Files.emplace_back (Obj);
414
- ObjectFiles.push_back (Obj);
415
- Obj->parse ();
416
- return Obj;
428
+
429
+ std::vector<ObjectFile *> ObjFiles;
430
+ for (SmallVector<char , 0 > &Obj : Objs) {
431
+ auto *ObjFile = new ObjectFile (
432
+ MemoryBufferRef (StringRef (Obj.data (), Obj.size ()), " <LTO object>" ));
433
+ Files.emplace_back (ObjFile);
434
+ ObjectFiles.push_back (ObjFile);
435
+ ObjFile->parse ();
436
+ ObjFiles.push_back (ObjFile);
437
+ }
438
+
439
+ return ObjFiles;
417
440
}
418
441
419
442
} // namespace coff
0 commit comments