Skip to content

Commit d0aad9f

Browse files
committed
[LTO] Constify lto::Config reference passed to backends (NFC)
The lto::Config object saved on the global LTO object should not be updated by any of the LTO backends. Otherwise we could run into interference between threads utilizing it. Motivated by some proposed changes that would have caused it to get modified in the ThinLTO backends.
1 parent a0f4600 commit d0aad9f

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

llvm/include/llvm/LTO/LTO.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ using NativeObjectCache =
222222
/// The details of this type definition aren't important; clients can only
223223
/// create a ThinBackend using one of the create*ThinBackend() functions below.
224224
using ThinBackend = std::function<std::unique_ptr<ThinBackendProc>(
225-
Config &C, ModuleSummaryIndex &CombinedIndex,
225+
const Config &C, ModuleSummaryIndex &CombinedIndex,
226226
StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
227227
AddStreamFn AddStream, NativeObjectCache Cache)>;
228228

@@ -306,7 +306,8 @@ class LTO {
306306
Config Conf;
307307

308308
struct RegularLTOState {
309-
RegularLTOState(unsigned ParallelCodeGenParallelismLevel, Config &Conf);
309+
RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
310+
const Config &Conf);
310311
struct CommonResolution {
311312
uint64_t Size = 0;
312313
MaybeAlign Align;

llvm/include/llvm/LTO/LTOBackend.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ namespace lto {
3535

3636
/// Runs a regular LTO backend. The regular LTO backend can also act as the
3737
/// regular LTO phase of ThinLTO, which may need to access the combined index.
38-
Error backend(Config &C, AddStreamFn AddStream,
38+
Error backend(const Config &C, AddStreamFn AddStream,
3939
unsigned ParallelCodeGenParallelismLevel,
4040
std::unique_ptr<Module> M, ModuleSummaryIndex &CombinedIndex);
4141

4242
/// Runs a ThinLTO backend.
43-
Error thinBackend(Config &C, unsigned Task, AddStreamFn AddStream, Module &M,
44-
const ModuleSummaryIndex &CombinedIndex,
43+
Error thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream,
44+
Module &M, const ModuleSummaryIndex &CombinedIndex,
4545
const FunctionImporter::ImportMapTy &ImportList,
4646
const GVSummaryMapTy &DefinedGlobals,
4747
MapVector<StringRef, BitcodeModule> &ModuleMap);

llvm/lib/LTO/LTO.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ BitcodeModule &InputFile::getSingleBitcodeModule() {
467467
}
468468

469469
LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
470-
Config &Conf)
470+
const Config &Conf)
471471
: ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
472472
Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
473473
Mover(std::make_unique<IRMover>(*CombinedModule)) {}
@@ -1029,12 +1029,12 @@ ArrayRef<const char*> LTO::getRuntimeLibcallSymbols() {
10291029
/// This class defines the interface to the ThinLTO backend.
10301030
class lto::ThinBackendProc {
10311031
protected:
1032-
Config &Conf;
1032+
const Config &Conf;
10331033
ModuleSummaryIndex &CombinedIndex;
10341034
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries;
10351035

10361036
public:
1037-
ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex,
1037+
ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex,
10381038
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries)
10391039
: Conf(Conf), CombinedIndex(CombinedIndex),
10401040
ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {}
@@ -1062,7 +1062,7 @@ class InProcessThinBackend : public ThinBackendProc {
10621062

10631063
public:
10641064
InProcessThinBackend(
1065-
Config &Conf, ModuleSummaryIndex &CombinedIndex,
1065+
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
10661066
unsigned ThinLTOParallelismLevel,
10671067
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
10681068
AddStreamFn AddStream, NativeObjectCache Cache)
@@ -1160,7 +1160,7 @@ class InProcessThinBackend : public ThinBackendProc {
11601160
} // end anonymous namespace
11611161

11621162
ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) {
1163-
return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
1163+
return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
11641164
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
11651165
AddStreamFn AddStream, NativeObjectCache Cache) {
11661166
return std::make_unique<InProcessThinBackend>(
@@ -1198,7 +1198,7 @@ class WriteIndexesThinBackend : public ThinBackendProc {
11981198

11991199
public:
12001200
WriteIndexesThinBackend(
1201-
Config &Conf, ModuleSummaryIndex &CombinedIndex,
1201+
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
12021202
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
12031203
std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
12041204
raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
@@ -1250,7 +1250,7 @@ class WriteIndexesThinBackend : public ThinBackendProc {
12501250
ThinBackend lto::createWriteIndexesThinBackend(
12511251
std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
12521252
raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1253-
return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
1253+
return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
12541254
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
12551255
AddStreamFn AddStream, NativeObjectCache Cache) {
12561256
return std::make_unique<WriteIndexesThinBackend>(

llvm/lib/LTO/LTOBackend.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Error Config::addSaveTemps(std::string OutputFileName,
128128
namespace {
129129

130130
std::unique_ptr<TargetMachine>
131-
createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) {
131+
createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
132132
StringRef TheTriple = M.getTargetTriple();
133133
SubtargetFeatures Features;
134134
Features.getDefaultSubtargetFeatures(Triple(TheTriple));
@@ -153,7 +153,7 @@ createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) {
153153
CodeModel, Conf.CGOptLevel));
154154
}
155155

156-
static void runNewPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
156+
static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
157157
unsigned OptLevel, bool IsThinLTO,
158158
ModuleSummaryIndex *ExportSummary,
159159
const ModuleSummaryIndex *ImportSummary) {
@@ -269,7 +269,7 @@ static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM,
269269
MPM.run(Mod, MAM);
270270
}
271271

272-
static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
272+
static void runOldPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
273273
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
274274
const ModuleSummaryIndex *ImportSummary) {
275275
legacy::PassManager passes;
@@ -300,7 +300,7 @@ static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
300300
passes.run(Mod);
301301
}
302302

303-
bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
303+
bool opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
304304
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
305305
const ModuleSummaryIndex *ImportSummary) {
306306
// FIXME: Plumb the combined index into the new pass manager.
@@ -319,7 +319,7 @@ static cl::opt<bool> EmbedBitcode(
319319
"lto-embed-bitcode", cl::init(false),
320320
cl::desc("Embed LLVM bitcode in object files produced by LTO"));
321321

322-
static void EmitBitcodeSection(Module &M, Config &Conf) {
322+
static void EmitBitcodeSection(Module &M, const Config &Conf) {
323323
if (!EmbedBitcode)
324324
return;
325325
SmallVector<char, 0> Buffer;
@@ -332,7 +332,7 @@ static void EmitBitcodeSection(Module &M, Config &Conf) {
332332
/*EmbedMarker*/ false, /*CmdArgs*/ nullptr);
333333
}
334334

335-
void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
335+
void codegen(const Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
336336
unsigned Task, Module &Mod) {
337337
if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
338338
return;
@@ -372,7 +372,7 @@ void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
372372
DwoOut->keep();
373373
}
374374

375-
void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
375+
void splitCodeGen(const Config &C, TargetMachine *TM, AddStreamFn AddStream,
376376
unsigned ParallelCodeGenParallelismLevel,
377377
std::unique_ptr<Module> Mod) {
378378
ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
@@ -420,7 +420,7 @@ void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
420420
CodegenThreadPool.wait();
421421
}
422422

423-
Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
423+
Expected<const Target *> initAndLookupTarget(const Config &C, Module &Mod) {
424424
if (!C.OverrideTriple.empty())
425425
Mod.setTargetTriple(C.OverrideTriple);
426426
else if (Mod.getTargetTriple().empty())
@@ -432,7 +432,6 @@ Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
432432
return make_error<StringError>(Msg, inconvertibleErrorCode());
433433
return T;
434434
}
435-
436435
}
437436

438437
static Error
@@ -446,7 +445,7 @@ finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) {
446445
return Error::success();
447446
}
448447

449-
Error lto::backend(Config &C, AddStreamFn AddStream,
448+
Error lto::backend(const Config &C, AddStreamFn AddStream,
450449
unsigned ParallelCodeGenParallelismLevel,
451450
std::unique_ptr<Module> Mod,
452451
ModuleSummaryIndex &CombinedIndex) {
@@ -500,7 +499,7 @@ static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
500499
}
501500
}
502501

503-
Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
502+
Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
504503
Module &Mod, const ModuleSummaryIndex &CombinedIndex,
505504
const FunctionImporter::ImportMapTy &ImportList,
506505
const GVSummaryMapTy &DefinedGlobals,

0 commit comments

Comments
 (0)