Skip to content

Commit dbf37e9

Browse files
committed
[ELF] Move InputFile storage from make<> to LinkerDriver::files
1 parent 2991a4e commit dbf37e9

File tree

6 files changed

+43
-34
lines changed

6 files changed

+43
-34
lines changed

lld/ELF/Config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ class LinkerDriver {
174174
bool inLib = false;
175175

176176
std::unique_ptr<BitcodeCompiler> lto;
177-
std::vector<InputFile *> files;
177+
SmallVector<std::unique_ptr<InputFile>, 0> files, ltoObjectFiles;
178178

179179
public:
180180
// See InputFile::groupId.
181181
uint32_t nextGroupId;
182182
bool isInGroup;
183-
InputFile *armCmseImpLib = nullptr;
183+
std::unique_ptr<InputFile> armCmseImpLib;
184184
SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles;
185185
};
186186

lld/ELF/Driver.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ bool LinkerDriver::tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName,
231231
IRObjectFile::findBitcodeInMemBuffer(mb);
232232
if (errorToBool(fatLTOData.takeError()))
233233
return false;
234-
files.push_back(
235-
make<BitcodeFile>(ctx, *fatLTOData, archiveName, offsetInArchive, lazy));
234+
files.push_back(std::make_unique<BitcodeFile>(ctx, *fatLTOData, archiveName,
235+
offsetInArchive, lazy));
236236
return true;
237237
}
238238

@@ -246,7 +246,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
246246
MemoryBufferRef mbref = *buffer;
247247

248248
if (ctx.arg.formatBinary) {
249-
files.push_back(make<BinaryFile>(ctx, mbref));
249+
files.push_back(std::make_unique<BinaryFile>(ctx, mbref));
250250
return;
251251
}
252252

@@ -259,8 +259,8 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
259259
if (inWholeArchive) {
260260
for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
261261
if (isBitcode(p.first))
262-
files.push_back(
263-
make<BitcodeFile>(ctx, p.first, path, p.second, false));
262+
files.push_back(std::make_unique<BitcodeFile>(ctx, p.first, path,
263+
p.second, false));
264264
else if (!tryAddFatLTOFile(p.first, path, p.second, false))
265265
files.push_back(createObjFile(ctx, p.first, path));
266266
}
@@ -288,7 +288,8 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
288288
if (!tryAddFatLTOFile(p.first, path, p.second, true))
289289
files.push_back(createObjFile(ctx, p.first, path, true));
290290
} else if (magic == file_magic::bitcode)
291-
files.push_back(make<BitcodeFile>(ctx, p.first, path, p.second, true));
291+
files.push_back(
292+
std::make_unique<BitcodeFile>(ctx, p.first, path, p.second, true));
292293
else
293294
Warn(ctx) << path << ": archive member '"
294295
<< p.first.getBufferIdentifier()
@@ -309,14 +310,14 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
309310
// the directory part is ignored. Note that path may be a temporary and
310311
// cannot be stored into SharedFile::soName.
311312
path = mbref.getBufferIdentifier();
312-
auto *f =
313-
make<SharedFile>(ctx, mbref, withLOption ? path::filename(path) : path);
313+
auto f = std::make_unique<SharedFile>(
314+
ctx, mbref, withLOption ? path::filename(path) : path);
314315
f->init();
315-
files.push_back(f);
316+
files.push_back(std::move(f));
316317
return;
317318
}
318319
case file_magic::bitcode:
319-
files.push_back(make<BitcodeFile>(ctx, mbref, "", 0, inLib));
320+
files.push_back(std::make_unique<BitcodeFile>(ctx, mbref, "", 0, inLib));
320321
break;
321322
case file_magic::elf_relocatable:
322323
if (!tryAddFatLTOFile(mbref, "", 0, inLib))
@@ -2040,7 +2041,7 @@ void LinkerDriver::inferMachineType() {
20402041
return;
20412042

20422043
bool inferred = false;
2043-
for (InputFile *f : files) {
2044+
for (auto &f : files) {
20442045
if (f->ekind == ELFNoneKind)
20452046
continue;
20462047
if (!inferred) {
@@ -2530,8 +2531,9 @@ void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
25302531
if (!ctx.bitcodeFiles.empty())
25312532
markBuffersAsDontNeed(ctx, skipLinkedOutput);
25322533

2533-
for (InputFile *file : lto->compile()) {
2534-
auto *obj = cast<ObjFile<ELFT>>(file);
2534+
ltoObjectFiles = lto->compile();
2535+
for (auto &file : ltoObjectFiles) {
2536+
auto *obj = cast<ObjFile<ELFT>>(file.get());
25352537
obj->parse(/*ignoreComdats=*/true);
25362538

25372539
// Parse '@' in symbol names for non-relocatable output.
@@ -3039,10 +3041,9 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
30393041
auto newInputFiles = ArrayRef(ctx.driver.files).slice(numInputFilesBeforeLTO);
30403042
if (!newInputFiles.empty()) {
30413043
DenseSet<StringRef> oldFilenames;
3042-
for (InputFile *f :
3043-
ArrayRef(ctx.driver.files).slice(0, numInputFilesBeforeLTO))
3044+
for (auto &f : ArrayRef(ctx.driver.files).slice(0, numInputFilesBeforeLTO))
30443045
oldFilenames.insert(f->getName());
3045-
for (InputFile *newFile : newInputFiles)
3046+
for (auto &newFile : newInputFiles)
30463047
if (!oldFilenames.contains(newFile->getName()))
30473048
Err(ctx) << "input file '" << newFile->getName() << "' added after LTO";
30483049
}

lld/ELF/InputFiles.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ InputFile::InputFile(Ctx &ctx, Kind k, MemoryBufferRef m)
216216
++ctx.driver.nextGroupId;
217217
}
218218

219+
InputFile::~InputFile() {}
220+
219221
std::optional<MemoryBufferRef> elf::readFile(Ctx &ctx, StringRef path) {
220222
llvm::TimeTraceScope timeScope("Load input files", path);
221223

@@ -345,19 +347,22 @@ extern template void ObjFile<ELF64LE>::importCmseSymbols();
345347
extern template void ObjFile<ELF64BE>::importCmseSymbols();
346348

347349
template <class ELFT>
348-
static void doParseFiles(Ctx &ctx, const std::vector<InputFile *> &files) {
350+
static void
351+
doParseFiles(Ctx &ctx,
352+
const SmallVector<std::unique_ptr<InputFile>, 0> &files) {
349353
// Add all files to the symbol table. This will add almost all symbols that we
350354
// need to the symbol table. This process might add files to the link due to
351355
// addDependentLibrary.
352356
for (size_t i = 0; i < files.size(); ++i) {
353357
llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName());
354-
doParseFile<ELFT>(ctx, files[i]);
358+
doParseFile<ELFT>(ctx, files[i].get());
355359
}
356360
if (ctx.driver.armCmseImpLib)
357361
cast<ObjFile<ELFT>>(*ctx.driver.armCmseImpLib).importCmseSymbols();
358362
}
359363

360-
void elf::parseFiles(Ctx &ctx, const std::vector<InputFile *> &files) {
364+
void elf::parseFiles(Ctx &ctx,
365+
const SmallVector<std::unique_ptr<InputFile>, 0> &files) {
361366
llvm::TimeTraceScope timeScope("Parse input files");
362367
invokeELFT(doParseFiles, ctx, files);
363368
}
@@ -1878,21 +1883,22 @@ InputFile *elf::createInternalFile(Ctx &ctx, StringRef name) {
18781883
return file;
18791884
}
18801885

1881-
ELFFileBase *elf::createObjFile(Ctx &ctx, MemoryBufferRef mb,
1882-
StringRef archiveName, bool lazy) {
1883-
ELFFileBase *f;
1886+
std::unique_ptr<ELFFileBase> elf::createObjFile(Ctx &ctx, MemoryBufferRef mb,
1887+
StringRef archiveName,
1888+
bool lazy) {
1889+
std::unique_ptr<ELFFileBase> f;
18841890
switch (getELFKind(ctx, mb, archiveName)) {
18851891
case ELF32LEKind:
1886-
f = make<ObjFile<ELF32LE>>(ctx, ELF32LEKind, mb, archiveName);
1892+
f = std::make_unique<ObjFile<ELF32LE>>(ctx, ELF32LEKind, mb, archiveName);
18871893
break;
18881894
case ELF32BEKind:
1889-
f = make<ObjFile<ELF32BE>>(ctx, ELF32BEKind, mb, archiveName);
1895+
f = std::make_unique<ObjFile<ELF32BE>>(ctx, ELF32BEKind, mb, archiveName);
18901896
break;
18911897
case ELF64LEKind:
1892-
f = make<ObjFile<ELF64LE>>(ctx, ELF64LEKind, mb, archiveName);
1898+
f = std::make_unique<ObjFile<ELF64LE>>(ctx, ELF64LEKind, mb, archiveName);
18931899
break;
18941900
case ELF64BEKind:
1895-
f = make<ObjFile<ELF64BE>>(ctx, ELF64BEKind, mb, archiveName);
1901+
f = std::make_unique<ObjFile<ELF64BE>>(ctx, ELF64BEKind, mb, archiveName);
18961902
break;
18971903
default:
18981904
llvm_unreachable("getELFKind");

lld/ELF/InputFiles.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ std::optional<MemoryBufferRef> readFile(Ctx &, StringRef path);
4444

4545
// Add symbols in File to the symbol table.
4646
void parseFile(Ctx &, InputFile *file);
47-
void parseFiles(Ctx &, const std::vector<InputFile *> &files);
47+
void parseFiles(Ctx &, const SmallVector<std::unique_ptr<InputFile>, 0> &);
4848

4949
// The root class of input files.
5050
class InputFile {
@@ -66,6 +66,7 @@ class InputFile {
6666
};
6767

6868
InputFile(Ctx &, Kind k, MemoryBufferRef m);
69+
virtual ~InputFile();
6970
Kind kind() const { return fileKind; }
7071

7172
bool isElf() const {
@@ -380,8 +381,9 @@ class BinaryFile : public InputFile {
380381
};
381382

382383
InputFile *createInternalFile(Ctx &, StringRef name);
383-
ELFFileBase *createObjFile(Ctx &, MemoryBufferRef mb,
384-
StringRef archiveName = "", bool lazy = false);
384+
std::unique_ptr<ELFFileBase> createObjFile(Ctx &, MemoryBufferRef mb,
385+
StringRef archiveName = "",
386+
bool lazy = false);
385387

386388
std::string replaceThinLTOSuffix(Ctx &, StringRef path);
387389

lld/ELF/LTO.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) {
310310

311311
// Merge all the bitcode files we have seen, codegen the result
312312
// and return the resulting ObjectFile(s).
313-
std::vector<InputFile *> BitcodeCompiler::compile() {
313+
SmallVector<std::unique_ptr<InputFile>, 0> BitcodeCompiler::compile() {
314314
unsigned maxTasks = ltoObj->getMaxTasks();
315315
buf.resize(maxTasks);
316316
files.resize(maxTasks);
@@ -373,7 +373,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
373373
}
374374

375375
bool savePrelink = ctx.arg.saveTempsArgs.contains("prelink");
376-
std::vector<InputFile *> ret;
376+
SmallVector<std::unique_ptr<InputFile>, 0> ret;
377377
const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o";
378378
for (unsigned i = 0; i != maxTasks; ++i) {
379379
StringRef bitcodeFilePath;

lld/ELF/LTO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BitcodeCompiler {
4242
~BitcodeCompiler();
4343

4444
void add(BitcodeFile &f);
45-
std::vector<InputFile *> compile();
45+
SmallVector<std::unique_ptr<InputFile>, 0> compile();
4646

4747
private:
4848
Ctx &ctx;

0 commit comments

Comments
 (0)