Skip to content

Commit 6b56a27

Browse files
committed
[ELF] Replace config-> with ctx.arg. in LTO.cpp
1 parent b177a9b commit 6b56a27

File tree

3 files changed

+85
-84
lines changed

3 files changed

+85
-84
lines changed

lld/ELF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2527,7 +2527,7 @@ template <class ELFT>
25272527
void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
25282528
llvm::TimeTraceScope timeScope("LTO");
25292529
// Compile bitcode files and replace bitcode symbols.
2530-
lto.reset(new BitcodeCompiler);
2530+
lto.reset(new BitcodeCompiler(ctx));
25312531
for (BitcodeFile *file : ctx.bitcodeFiles)
25322532
lto->add(*file);
25332533

lld/ELF/LTO.cpp

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,42 @@ using namespace llvm::ELF;
4343
using namespace lld;
4444
using namespace lld::elf;
4545

46-
static std::string getThinLTOOutputFile(StringRef modulePath) {
47-
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
48-
config->thinLTOPrefixReplaceNew);
46+
static std::string getThinLTOOutputFile(Ctx &ctx, StringRef modulePath) {
47+
return lto::getThinLTOOutputFile(modulePath, ctx.arg.thinLTOPrefixReplaceOld,
48+
ctx.arg.thinLTOPrefixReplaceNew);
4949
}
5050

51-
static lto::Config createConfig() {
51+
static lto::Config createConfig(Ctx &ctx) {
5252
lto::Config c;
5353

5454
// LLD supports the new relocations and address-significance tables.
5555
c.Options = initTargetOptionsFromCodeGenFlags();
5656
c.Options.EmitAddrsig = true;
57-
for (StringRef C : config->mllvmOpts)
57+
for (StringRef C : ctx.arg.mllvmOpts)
5858
c.MllvmArgs.emplace_back(C.str());
5959

6060
// Always emit a section per function/datum with LTO.
6161
c.Options.FunctionSections = true;
6262
c.Options.DataSections = true;
6363

64-
c.Options.BBAddrMap = config->ltoBBAddrMap;
64+
c.Options.BBAddrMap = ctx.arg.ltoBBAddrMap;
6565

6666
// Check if basic block sections must be used.
6767
// Allowed values for --lto-basic-block-sections are "all", "labels",
6868
// "<file name specifying basic block ids>", or none. This is the equivalent
6969
// of -fbasic-block-sections= flag in clang.
70-
if (!config->ltoBasicBlockSections.empty()) {
71-
if (config->ltoBasicBlockSections == "all") {
70+
if (!ctx.arg.ltoBasicBlockSections.empty()) {
71+
if (ctx.arg.ltoBasicBlockSections == "all") {
7272
c.Options.BBSections = BasicBlockSection::All;
73-
} else if (config->ltoBasicBlockSections == "labels") {
73+
} else if (ctx.arg.ltoBasicBlockSections == "labels") {
7474
c.Options.BBSections = BasicBlockSection::Labels;
75-
} else if (config->ltoBasicBlockSections == "none") {
75+
} else if (ctx.arg.ltoBasicBlockSections == "none") {
7676
c.Options.BBSections = BasicBlockSection::None;
7777
} else {
7878
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
79-
MemoryBuffer::getFile(config->ltoBasicBlockSections.str());
79+
MemoryBuffer::getFile(ctx.arg.ltoBasicBlockSections.str());
8080
if (!MBOrErr) {
81-
error("cannot open " + config->ltoBasicBlockSections + ":" +
81+
error("cannot open " + ctx.arg.ltoBasicBlockSections + ":" +
8282
MBOrErr.getError().message());
8383
} else {
8484
c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
@@ -88,114 +88,114 @@ static lto::Config createConfig() {
8888
}
8989

9090
c.Options.UniqueBasicBlockSectionNames =
91-
config->ltoUniqueBasicBlockSectionNames;
91+
ctx.arg.ltoUniqueBasicBlockSectionNames;
9292

9393
if (auto relocModel = getRelocModelFromCMModel())
9494
c.RelocModel = *relocModel;
95-
else if (config->relocatable)
95+
else if (ctx.arg.relocatable)
9696
c.RelocModel = std::nullopt;
97-
else if (config->isPic)
97+
else if (ctx.arg.isPic)
9898
c.RelocModel = Reloc::PIC_;
9999
else
100100
c.RelocModel = Reloc::Static;
101101

102102
c.CodeModel = getCodeModelFromCMModel();
103-
c.DisableVerify = config->disableVerify;
103+
c.DisableVerify = ctx.arg.disableVerify;
104104
c.DiagHandler = diagnosticHandler;
105-
c.OptLevel = config->ltoo;
105+
c.OptLevel = ctx.arg.ltoo;
106106
c.CPU = getCPUStr();
107107
c.MAttrs = getMAttrs();
108-
c.CGOptLevel = config->ltoCgo;
108+
c.CGOptLevel = ctx.arg.ltoCgo;
109109

110110
c.PTO.LoopVectorization = c.OptLevel > 1;
111111
c.PTO.SLPVectorization = c.OptLevel > 1;
112112

113113
// Set up a custom pipeline if we've been asked to.
114-
c.OptPipeline = std::string(config->ltoNewPmPasses);
115-
c.AAPipeline = std::string(config->ltoAAPipeline);
114+
c.OptPipeline = std::string(ctx.arg.ltoNewPmPasses);
115+
c.AAPipeline = std::string(ctx.arg.ltoAAPipeline);
116116

117117
// Set up optimization remarks if we've been asked to.
118-
c.RemarksFilename = std::string(config->optRemarksFilename);
119-
c.RemarksPasses = std::string(config->optRemarksPasses);
120-
c.RemarksWithHotness = config->optRemarksWithHotness;
121-
c.RemarksHotnessThreshold = config->optRemarksHotnessThreshold;
122-
c.RemarksFormat = std::string(config->optRemarksFormat);
118+
c.RemarksFilename = std::string(ctx.arg.optRemarksFilename);
119+
c.RemarksPasses = std::string(ctx.arg.optRemarksPasses);
120+
c.RemarksWithHotness = ctx.arg.optRemarksWithHotness;
121+
c.RemarksHotnessThreshold = ctx.arg.optRemarksHotnessThreshold;
122+
c.RemarksFormat = std::string(ctx.arg.optRemarksFormat);
123123

124124
// Set up output file to emit statistics.
125-
c.StatsFile = std::string(config->optStatsFilename);
125+
c.StatsFile = std::string(ctx.arg.optStatsFilename);
126126

127-
c.SampleProfile = std::string(config->ltoSampleProfile);
128-
for (StringRef pluginFn : config->passPlugins)
127+
c.SampleProfile = std::string(ctx.arg.ltoSampleProfile);
128+
for (StringRef pluginFn : ctx.arg.passPlugins)
129129
c.PassPlugins.push_back(std::string(pluginFn));
130-
c.DebugPassManager = config->ltoDebugPassManager;
131-
c.DwoDir = std::string(config->dwoDir);
130+
c.DebugPassManager = ctx.arg.ltoDebugPassManager;
131+
c.DwoDir = std::string(ctx.arg.dwoDir);
132132

133-
c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility;
133+
c.HasWholeProgramVisibility = ctx.arg.ltoWholeProgramVisibility;
134134
c.ValidateAllVtablesHaveTypeInfos =
135-
config->ltoValidateAllVtablesHaveTypeInfos;
135+
ctx.arg.ltoValidateAllVtablesHaveTypeInfos;
136136
c.AllVtablesHaveTypeInfos = ctx.ltoAllVtablesHaveTypeInfos;
137-
c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
137+
c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
138138
c.KeepSymbolNameCopies = false;
139139

140-
for (const llvm::StringRef &name : config->thinLTOModulesToCompile)
140+
for (const llvm::StringRef &name : ctx.arg.thinLTOModulesToCompile)
141141
c.ThinLTOModulesToCompile.emplace_back(name);
142142

143-
c.TimeTraceEnabled = config->timeTraceEnabled;
144-
c.TimeTraceGranularity = config->timeTraceGranularity;
143+
c.TimeTraceEnabled = ctx.arg.timeTraceEnabled;
144+
c.TimeTraceGranularity = ctx.arg.timeTraceGranularity;
145145

146-
c.CSIRProfile = std::string(config->ltoCSProfileFile);
147-
c.RunCSIRInstr = config->ltoCSProfileGenerate;
148-
c.PGOWarnMismatch = config->ltoPGOWarnMismatch;
146+
c.CSIRProfile = std::string(ctx.arg.ltoCSProfileFile);
147+
c.RunCSIRInstr = ctx.arg.ltoCSProfileGenerate;
148+
c.PGOWarnMismatch = ctx.arg.ltoPGOWarnMismatch;
149149

150-
if (config->emitLLVM) {
151-
c.PreCodeGenModuleHook = [](size_t task, const Module &m) {
150+
if (ctx.arg.emitLLVM) {
151+
c.PreCodeGenModuleHook = [&ctx](size_t task, const Module &m) {
152152
if (std::unique_ptr<raw_fd_ostream> os =
153-
openLTOOutputFile(config->outputFile))
153+
openLTOOutputFile(ctx.arg.outputFile))
154154
WriteBitcodeToFile(m, *os, false);
155155
return false;
156156
};
157157
}
158158

159-
if (config->ltoEmitAsm) {
159+
if (ctx.arg.ltoEmitAsm) {
160160
c.CGFileType = CodeGenFileType::AssemblyFile;
161161
c.Options.MCOptions.AsmVerbose = true;
162162
}
163163

164-
if (!config->saveTempsArgs.empty())
165-
checkError(c.addSaveTemps(config->outputFile.str() + ".",
164+
if (!ctx.arg.saveTempsArgs.empty())
165+
checkError(c.addSaveTemps(ctx.arg.outputFile.str() + ".",
166166
/*UseInputModulePath*/ true,
167-
config->saveTempsArgs));
167+
ctx.arg.saveTempsArgs));
168168
return c;
169169
}
170170

171-
BitcodeCompiler::BitcodeCompiler() {
171+
BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
172172
// Initialize indexFile.
173-
if (!config->thinLTOIndexOnlyArg.empty())
174-
indexFile = openFile(config->thinLTOIndexOnlyArg);
173+
if (!ctx.arg.thinLTOIndexOnlyArg.empty())
174+
indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg);
175175

176176
// Initialize ltoObj.
177177
lto::ThinBackend backend;
178178
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
179-
if (config->thinLTOIndexOnly) {
179+
if (ctx.arg.thinLTOIndexOnly) {
180180
backend = lto::createWriteIndexesThinBackend(
181-
std::string(config->thinLTOPrefixReplaceOld),
182-
std::string(config->thinLTOPrefixReplaceNew),
183-
std::string(config->thinLTOPrefixReplaceNativeObject),
184-
config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
181+
std::string(ctx.arg.thinLTOPrefixReplaceOld),
182+
std::string(ctx.arg.thinLTOPrefixReplaceNew),
183+
std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
184+
ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
185185
} else {
186186
backend = lto::createInProcessThinBackend(
187-
llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
188-
onIndexWrite, config->thinLTOEmitIndexFiles,
189-
config->thinLTOEmitImportsFiles);
187+
llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs),
188+
onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
189+
ctx.arg.thinLTOEmitImportsFiles);
190190
}
191191

192192
constexpr llvm::lto::LTO::LTOKind ltoModes[3] =
193193
{llvm::lto::LTO::LTOKind::LTOK_UnifiedThin,
194194
llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular,
195195
llvm::lto::LTO::LTOKind::LTOK_Default};
196-
ltoObj = std::make_unique<lto::LTO>(
197-
createConfig(), backend, config->ltoPartitions,
198-
ltoModes[config->ltoKind]);
196+
ltoObj = std::make_unique<lto::LTO>(createConfig(ctx), backend,
197+
ctx.arg.ltoPartitions,
198+
ltoModes[ctx.arg.ltoKind]);
199199

200200
// Initialize usedStartStop.
201201
if (ctx.bitcodeFiles.empty())
@@ -291,7 +291,7 @@ static void thinLTOCreateEmptyIndexFiles() {
291291
if (linkedBitCodeFiles.contains(f->getName()))
292292
continue;
293293
std::string path =
294-
replaceThinLTOSuffix(getThinLTOOutputFile(f->obj->getName()));
294+
replaceThinLTOSuffix(getThinLTOOutputFile(ctx, f->obj->getName()));
295295
std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
296296
if (!os)
297297
continue;
@@ -316,8 +316,8 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
316316
// to cache native object files for ThinLTO incremental builds. If a path was
317317
// specified, configure LTO to use it as the cache directory.
318318
FileCache cache;
319-
if (!config->thinLTOCacheDir.empty())
320-
cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
319+
if (!ctx.arg.thinLTOCacheDir.empty())
320+
cache = check(localCache("ThinLTO", "Thin", ctx.arg.thinLTOCacheDir,
321321
[&](size_t task, const Twine &moduleName,
322322
std::unique_ptr<MemoryBuffer> mb) {
323323
files[task] = std::move(mb);
@@ -334,21 +334,21 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
334334
cache));
335335

336336
// Emit empty index files for non-indexed files but not in single-module mode.
337-
if (config->thinLTOModulesToCompile.empty()) {
337+
if (ctx.arg.thinLTOModulesToCompile.empty()) {
338338
for (StringRef s : thinIndices) {
339-
std::string path = getThinLTOOutputFile(s);
339+
std::string path = getThinLTOOutputFile(ctx, s);
340340
openFile(path + ".thinlto.bc");
341-
if (config->thinLTOEmitImportsFiles)
341+
if (ctx.arg.thinLTOEmitImportsFiles)
342342
openFile(path + ".imports");
343343
}
344344
}
345345

346-
if (config->thinLTOEmitIndexFiles)
346+
if (ctx.arg.thinLTOEmitIndexFiles)
347347
thinLTOCreateEmptyIndexFiles();
348348

349-
if (config->thinLTOIndexOnly) {
350-
if (!config->ltoObjPath.empty())
351-
saveBuffer(buf[0].second, config->ltoObjPath);
349+
if (ctx.arg.thinLTOIndexOnly) {
350+
if (!ctx.arg.ltoObjPath.empty())
351+
saveBuffer(buf[0].second, ctx.arg.ltoObjPath);
352352

353353
// ThinLTO with index only option is required to generate only the index
354354
// files. After that, we exit from linker and ThinLTO backend runs in a
@@ -358,18 +358,18 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
358358
return {};
359359
}
360360

361-
if (!config->thinLTOCacheDir.empty())
362-
pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
361+
if (!ctx.arg.thinLTOCacheDir.empty())
362+
pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files);
363363

364-
if (!config->ltoObjPath.empty()) {
365-
saveBuffer(buf[0].second, config->ltoObjPath);
364+
if (!ctx.arg.ltoObjPath.empty()) {
365+
saveBuffer(buf[0].second, ctx.arg.ltoObjPath);
366366
for (unsigned i = 1; i != maxTasks; ++i)
367-
saveBuffer(buf[i].second, config->ltoObjPath + Twine(i));
367+
saveBuffer(buf[i].second, ctx.arg.ltoObjPath + Twine(i));
368368
}
369369

370-
bool savePrelink = config->saveTempsArgs.contains("prelink");
370+
bool savePrelink = ctx.arg.saveTempsArgs.contains("prelink");
371371
std::vector<InputFile *> ret;
372-
const char *ext = config->ltoEmitAsm ? ".s" : ".o";
372+
const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o";
373373
for (unsigned i = 0; i != maxTasks; ++i) {
374374
StringRef bitcodeFilePath;
375375
StringRef objBuf;
@@ -392,7 +392,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
392392
StringRef ltoObjName;
393393
if (bitcodeFilePath == "ld-temp.o") {
394394
ltoObjName =
395-
saver().save(Twine(config->outputFile) + ".lto" +
395+
saver().save(Twine(ctx.arg.outputFile) + ".lto" +
396396
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
397397
} else {
398398
StringRef directory = sys::path::parent_path(bitcodeFilePath);
@@ -402,16 +402,16 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
402402
StringRef baseName = bitcodeFilePath.ends_with(")")
403403
? sys::path::filename(bitcodeFilePath)
404404
: sys::path::stem(bitcodeFilePath);
405-
StringRef outputFileBaseName = sys::path::filename(config->outputFile);
405+
StringRef outputFileBaseName = sys::path::filename(ctx.arg.outputFile);
406406
SmallString<256> path;
407407
sys::path::append(path, directory,
408408
outputFileBaseName + ".lto." + baseName + ext);
409409
sys::path::remove_dots(path, true);
410410
ltoObjName = saver().save(path.str());
411411
}
412-
if (savePrelink || config->ltoEmitAsm)
412+
if (savePrelink || ctx.arg.ltoEmitAsm)
413413
saveBuffer(buf[i].second, ltoObjName);
414-
if (!config->ltoEmitAsm)
414+
if (!ctx.arg.ltoEmitAsm)
415415
ret.push_back(createObjFile(MemoryBufferRef(objBuf, ltoObjName)));
416416
}
417417
return ret;

lld/ELF/LTO.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@ class LTO;
3232
}
3333

3434
namespace lld::elf {
35-
35+
struct Ctx;
3636
class BitcodeFile;
3737
class InputFile;
3838

3939
class BitcodeCompiler {
4040
public:
41-
BitcodeCompiler();
41+
BitcodeCompiler(Ctx &ctx);
4242
~BitcodeCompiler();
4343

4444
void add(BitcodeFile &f);
4545
std::vector<InputFile *> compile();
4646

4747
private:
48+
Ctx &ctx;
4849
std::unique_ptr<llvm::lto::LTO> ltoObj;
4950
// An array of (module name, native relocatable file content) pairs.
5051
SmallVector<std::pair<std::string, SmallString<0>>, 0> buf;

0 commit comments

Comments
 (0)