Skip to content

Commit dbc1e81

Browse files
committed
[DTLTO] Refactor the DTLTO code.
DTLTO implementation code has been moved from `llvm/lib/LTO/` to `llvm/lib/DTLTO/`. This refactor does not change any externally visible behavior, so existing DTLTO tests and documentation remain valid. The move was done to reduce code duplication, improve maintainability, and make it easier to adopt future performance improvements.
1 parent fca80b4 commit dbc1e81

13 files changed

Lines changed: 907 additions & 644 deletions

File tree

cross-project-tests/dtlto/dtlto-cache.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ RUN: -Wl,--thinlto-remote-compiler=%clang \
3131
RUN: -Wl,--thinlto-cache-dir=cache.dir \
3232
RUN: -Wl,--save-temps
3333

34-
# Check that there are no backend compilation jobs occurred.
35-
RUN: grep -wo args populate2.*.dist-file.json | wc -l | grep -qx "\s*1"
34+
# At this point we expect full cache hits. Distributor will not be invoked and a Json file will not be created.
35+
RUN: not ls cache.dir/*.dist-file.json
3636
RUN: ls cache.dir | count 3
3737

3838
RUN: %clang -O0 --target=x86_64-linux-gnu -flto=thin -c foo.c -o foo.O0.o

cross-project-tests/dtlto/savetemps-lock.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ RUN: | FileCheck %s --implicit-check-not=warning
3030
CHECK-DAG: Lock any files in the output directory.
3131
CHECK-DAG: warning: could not remove the file 'locked{{/|\\}}t.[[#PID:]].dist-file.json': {{.*}}
3232
# Filename composition: <archive>(<member> at <offset>).<task>.<pid>.<task>.<pid>.native.o.
33+
CHECK-DAG: warning: could not remove the file 'locked{{/|\\}}t.a(t1.o at [[#T1_OFFSET:]]).1.[[#%X,HEXPID:]].o': {{.*}}
3334
CHECK-DAG: warning: could not remove the file 'locked{{/|\\}}t.a(t1.o at [[#T1_OFFSET:]]).1.[[#%X,HEXPID:]].1.[[#PID]].native.o': {{.*}}
3435
CHECK-DAG: warning: could not remove the file 'locked{{/|\\}}t.a(t1.o at [[#T1_OFFSET]]).1.[[#%X,HEXPID]].1.[[#PID]].native.o.thinlto.bc': {{.*}}
36+
CHECK-DAG: warning: could not remove the file 'locked{{/|\\}}t.a(t2.o at [[#T2_OFFSET:]]).2.[[#%X,HEXPID]].o': {{.*}}
3537
CHECK-DAG: warning: could not remove the file 'locked{{/|\\}}t.a(t2.o at [[#T2_OFFSET:]]).2.[[#%X,HEXPID]].2.[[#PID]].native.o': {{.*}}
3638
CHECK-DAG: warning: could not remove the file 'locked{{/|\\}}t.a(t2.o at [[#T2_OFFSET]]).2.[[#%X,HEXPID]].2.[[#PID]].native.o.thinlto.bc': {{.*}}
37-
CHECK-DAG: warning: could not remove temporary DTLTO input file 'locked{{/|\\}}t.a(t1.o at [[#T1_OFFSET]]).1.[[#%X,HEXPID]].o': {{.*}}
38-
CHECK-DAG: warning: could not remove temporary DTLTO input file 'locked{{/|\\}}t.a(t2.o at [[#T2_OFFSET]]).2.[[#%X,HEXPID]].o': {{.*}}
3939

4040
#--- t1.c
4141
__attribute__((retain)) int t1(int x) { return x; }

lld/COFF/LTO.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,7 @@ BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {
126126

127127
// Initialize ltoObj.
128128
lto::ThinBackend backend;
129-
if (!ctx.config.dtltoDistributor.empty()) {
130-
backend = lto::createOutOfProcessThinBackend(
131-
llvm::hardware_concurrency(ctx.config.thinLTOJobs),
132-
/*OnWrite=*/nullptr,
133-
/*ShouldEmitIndexFiles=*/false,
134-
/*ShouldEmitImportFiles=*/false, ctx.config.outputFile,
135-
ctx.config.dtltoDistributor, ctx.config.dtltoDistributorArgs,
136-
ctx.config.dtltoCompiler, ctx.config.dtltoCompilerPrependArgs,
137-
ctx.config.dtltoCompilerArgs, !ctx.config.saveTempsArgs.empty(),
138-
createAddBufferFn(files, file_names));
139-
} else if (ctx.config.thinLTOIndexOnly) {
129+
if (ctx.config.thinLTOIndexOnly || !ctx.config.dtltoDistributor.empty()) {
140130
auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
141131
backend = lto::createWriteIndexesThinBackend(
142132
llvm::hardware_concurrency(ctx.config.thinLTOJobs),
@@ -155,7 +145,12 @@ BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {
155145
else
156146
ltoObj = std::make_unique<lto::DTLTO>(
157147
createConfig(), backend, ctx.config.ltoPartitions,
158-
llvm::lto::LTO::LTOKind::LTOK_Default, ctx.config.outputFile,
148+
llvm::lto::LTO::LTOKind::LTOK_Default, nullptr,
149+
ctx.config.thinLTOEmitImportsFiles, ctx.config.thinLTOIndexOnly,
150+
ctx.config.outputFile, ctx.config.dtltoDistributor,
151+
ctx.config.dtltoDistributorArgs, ctx.config.dtltoCompiler,
152+
ctx.config.dtltoCompilerPrependArgs, ctx.config.dtltoCompilerArgs,
153+
createAddBufferFn(files, file_names),
159154
!ctx.config.saveTempsArgs.empty());
160155
}
161156

lld/ELF/LTO.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,13 @@ BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
184184
// Initialize ltoObj.
185185
lto::ThinBackend backend;
186186
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
187-
if (ctx.arg.thinLTOIndexOnly) {
187+
if (ctx.arg.thinLTOIndexOnly || !ctx.arg.dtltoDistributor.empty()) {
188188
backend = lto::createWriteIndexesThinBackend(
189189
llvm::hardware_concurrency(ctx.arg.thinLTOJobs),
190190
std::string(ctx.arg.thinLTOPrefixReplaceOld),
191191
std::string(ctx.arg.thinLTOPrefixReplaceNew),
192192
std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
193193
ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
194-
} else if (!ctx.arg.dtltoDistributor.empty()) {
195-
backend = lto::createOutOfProcessThinBackend(
196-
llvm::hardware_concurrency(ctx.arg.thinLTOJobs), onIndexWrite,
197-
ctx.arg.thinLTOEmitIndexFiles, ctx.arg.thinLTOEmitImportsFiles,
198-
ctx.arg.outputFile, ctx.arg.dtltoDistributor,
199-
ctx.arg.dtltoDistributorArgs, ctx.arg.dtltoCompiler,
200-
ctx.arg.dtltoCompilerPrependArgs, ctx.arg.dtltoCompilerArgs,
201-
!ctx.arg.saveTempsArgs.empty(), createAddBufferFn(files, filenames));
202194
} else {
203195
backend = lto::createInProcessThinBackend(
204196
llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs),
@@ -210,14 +202,19 @@ BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
210202
llvm::lto::LTO::LTOKind::LTOK_UnifiedThin,
211203
llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular,
212204
llvm::lto::LTO::LTOKind::LTOK_Default};
205+
213206
if (ctx.arg.dtltoDistributor.empty())
214207
ltoObj = std::make_unique<lto::LTO>(createConfig(ctx), backend,
215208
ctx.arg.ltoPartitions,
216209
ltoModes[ctx.arg.ltoKind]);
217210
else
218211
ltoObj = std::make_unique<lto::DTLTO>(
219212
createConfig(ctx), backend, ctx.arg.ltoPartitions,
220-
ltoModes[ctx.arg.ltoKind], ctx.arg.outputFile,
213+
ltoModes[ctx.arg.ltoKind], onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
214+
ctx.arg.thinLTOEmitImportsFiles, ctx.arg.outputFile,
215+
ctx.arg.dtltoDistributor, ctx.arg.dtltoDistributorArgs,
216+
ctx.arg.dtltoCompiler, ctx.arg.dtltoCompilerPrependArgs,
217+
ctx.arg.dtltoCompilerArgs, createAddBufferFn(files, filenames),
221218
!ctx.arg.saveTempsArgs.empty());
222219
// Initialize usedStartStop.
223220
if (ctx.bitcodeFiles.empty())

lld/test/ELF/dtlto/timetrace.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ RUN: %python filter_order_and_pprint.py %t.json | FileCheck %s
3232
## Check that DTLTO add input file events are recorded.
3333
CHECK: "name": "Add input for DTLTO"
3434
CHECK: "name": "Add input for DTLTO"
35-
CHECK: "name": "Remove temporary inputs for DTLTO"
35+
CHECK: "name": "Remove DTLTO temporary files"
3636
CHECK: "name": "Serialize bitcode input for DTLTO"
3737
CHECK-SAME: "detail": "t1.a(t1.bc at [[#ARCHIVE_OFFSET:]]).1.[[PID:[A-F0-9]+]].o"
3838
CHECK: "name": "Total Add input for DTLTO"
3939
CHECK-SAME: "count": 2,
40-
CHECK: "name": "Total Remove temporary inputs for DTLTO"
40+
CHECK: "name": "Total Remove DTLTO temporary files"
4141
CHECK-SAME: "count": 1,
4242
CHECK: "name": "Total Serialize bitcode input for DTLTO"
4343
CHECK-SAME: "count": 1,

0 commit comments

Comments
 (0)