Skip to content

Commit f7669ba

Browse files
authored
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO link, `ld.lld --save-temps a.o d/b.o -o out` will create ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of `out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file name from bitcode file names helps debugging. The relocatable file name from the first regular LTO partition does not change: `out.lto.o`. The second, if present due to `--lto-partition=`, changes from `out1.lto.o` to `lto.1.o`. For an archive member, e.g. `d/a.a(coll.o at 8)`, the relocatable file is `d/out.lto.a.a(coll.o at 8).o`. `--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o out` now creates `out.lto.s` instead of `out`, therefore the `--lto-emit-asm -o -` idiom no longer works. However, I think this new behavior (which matches COFF) is better since keeping or removing `--lto-emit-asm` will dump different files, instead of overwriting the `-o` output file from an executable/shared object to an assembly file. Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu Reviewed By: teresajohnson Pull Request: llvm#78835
1 parent f6ced35 commit f7669ba

14 files changed

+116
-80
lines changed

lld/ELF/LTO.cpp

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "SymbolTable.h"
1313
#include "Symbols.h"
1414
#include "lld/Common/Args.h"
15+
#include "lld/Common/CommonLinkerContext.h"
1516
#include "lld/Common/ErrorHandler.h"
1617
#include "lld/Common/Filesystem.h"
1718
#include "lld/Common/Strings.h"
@@ -28,6 +29,7 @@
2829
#include "llvm/Support/Error.h"
2930
#include "llvm/Support/FileSystem.h"
3031
#include "llvm/Support/MemoryBuffer.h"
32+
#include "llvm/Support/Path.h"
3133
#include <algorithm>
3234
#include <cstddef>
3335
#include <memory>
@@ -305,6 +307,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
305307
unsigned maxTasks = ltoObj->getMaxTasks();
306308
buf.resize(maxTasks);
307309
files.resize(maxTasks);
310+
filenames.resize(maxTasks);
308311

309312
// The --thinlto-cache-dir option specifies the path to a directory in which
310313
// to cache native object files for ThinLTO incremental builds. If a path was
@@ -315,13 +318,15 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
315318
[&](size_t task, const Twine &moduleName,
316319
std::unique_ptr<MemoryBuffer> mb) {
317320
files[task] = std::move(mb);
321+
filenames[task] = moduleName.str();
318322
}));
319323

320324
if (!ctx.bitcodeFiles.empty())
321325
checkError(ltoObj->run(
322326
[&](size_t task, const Twine &moduleName) {
327+
buf[task].first = moduleName.str();
323328
return std::make_unique<CachedFileStream>(
324-
std::make_unique<raw_svector_ostream>(buf[task]));
329+
std::make_unique<raw_svector_ostream>(buf[task].second));
325330
},
326331
cache));
327332

@@ -340,7 +345,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
340345

341346
if (config->thinLTOIndexOnly) {
342347
if (!config->ltoObjPath.empty())
343-
saveBuffer(buf[0], config->ltoObjPath);
348+
saveBuffer(buf[0].second, config->ltoObjPath);
344349

345350
// ThinLTO with index only option is required to generate only the index
346351
// files. After that, we exit from linker and ThinLTO backend runs in a
@@ -354,32 +359,57 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
354359
pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
355360

356361
if (!config->ltoObjPath.empty()) {
357-
saveBuffer(buf[0], config->ltoObjPath);
362+
saveBuffer(buf[0].second, config->ltoObjPath);
358363
for (unsigned i = 1; i != maxTasks; ++i)
359-
saveBuffer(buf[i], config->ltoObjPath + Twine(i));
360-
}
361-
362-
if (config->saveTempsArgs.contains("prelink")) {
363-
if (!buf[0].empty())
364-
saveBuffer(buf[0], config->outputFile + ".lto.o");
365-
for (unsigned i = 1; i != maxTasks; ++i)
366-
saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
367-
}
368-
369-
if (config->ltoEmitAsm) {
370-
saveBuffer(buf[0], config->outputFile);
371-
for (unsigned i = 1; i != maxTasks; ++i)
372-
saveBuffer(buf[i], config->outputFile + Twine(i));
373-
return {};
364+
saveBuffer(buf[i].second, config->ltoObjPath + Twine(i));
374365
}
375366

367+
bool savePrelink = config->saveTempsArgs.contains("prelink");
376368
std::vector<InputFile *> ret;
377-
for (unsigned i = 0; i != maxTasks; ++i)
378-
if (!buf[i].empty())
379-
ret.push_back(createObjFile(MemoryBufferRef(buf[i], "lto.tmp")));
369+
const char *ext = config->ltoEmitAsm ? ".s" : ".o";
370+
for (unsigned i = 0; i != maxTasks; ++i) {
371+
StringRef bitcodeFilePath;
372+
StringRef objBuf;
373+
if (files[i]) {
374+
// When files[i] is not null, we get the native relocatable file from the
375+
// cache. filenames[i] contains the original BitcodeFile's identifier.
376+
objBuf = files[i]->getBuffer();
377+
bitcodeFilePath = filenames[i];
378+
} else {
379+
// Get the native relocatable file after in-process LTO compilation.
380+
objBuf = buf[i].second;
381+
bitcodeFilePath = buf[i].first;
382+
}
383+
if (objBuf.empty())
384+
continue;
380385

381-
for (std::unique_ptr<MemoryBuffer> &file : files)
382-
if (file)
383-
ret.push_back(createObjFile(*file));
386+
// If the input bitcode file is path/to/x.o and -o specifies a.out, the
387+
// corresponding native relocatable file path will look like:
388+
// path/to/a.out.lto.x.o.
389+
StringRef ltoObjName;
390+
if (bitcodeFilePath == "ld-temp.o") {
391+
ltoObjName =
392+
saver().save(Twine(config->outputFile) + ".lto" +
393+
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
394+
} else {
395+
StringRef directory = sys::path::parent_path(bitcodeFilePath);
396+
// For an archive member, which has an identifier like "d/a.a(coll.o at
397+
// 8)" (see BitcodeFile::BitcodeFile), use the filename; otherwise, use
398+
// the stem (d/a.o => a).
399+
StringRef baseName = bitcodeFilePath.ends_with(")")
400+
? sys::path::filename(bitcodeFilePath)
401+
: sys::path::stem(bitcodeFilePath);
402+
StringRef outputFileBaseName = sys::path::filename(config->outputFile);
403+
SmallString<256> path;
404+
sys::path::append(path, directory,
405+
outputFileBaseName + ".lto." + baseName + ext);
406+
sys::path::remove_dots(path, true);
407+
ltoObjName = saver().save(path.str());
408+
}
409+
if (savePrelink || config->ltoEmitAsm)
410+
saveBuffer(buf[i].second, ltoObjName);
411+
if (!config->ltoEmitAsm)
412+
ret.push_back(createObjFile(MemoryBufferRef(objBuf, ltoObjName)));
413+
}
384414
return ret;
385415
}

lld/ELF/LTO.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ class BitcodeCompiler {
4646

4747
private:
4848
std::unique_ptr<llvm::lto::LTO> ltoObj;
49-
std::vector<SmallString<0>> buf;
49+
// An array of (module name, native relocatable file content) pairs.
50+
SmallVector<std::pair<std::string, SmallString<0>>, 0> buf;
5051
std::vector<std::unique_ptr<MemoryBuffer>> files;
52+
SmallVector<std::string, 0> filenames;
5153
llvm::DenseSet<StringRef> usedStartStop;
5254
std::unique_ptr<llvm::raw_fd_ostream> indexFile;
5355
llvm::DenseSet<StringRef> thinIndices;

lld/test/ELF/common-archive-lookup.s

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@
6161
# RUN: ld.lld --no-fortran-common -o 11 main.o --start-lib 1.o strong_data_only.o --end-lib
6262
# RUN: llvm-readobj --syms 11 | FileCheck --check-prefix=NFC %s
6363

64-
# RUN: ld.lld -o - main.o 4.a --fortran-common --lto-emit-asm | FileCheck --check-prefix=ASM %s
64+
# RUN: ld.lld -o out main.o 4.a --fortran-common --lto-emit-asm
65+
# RUN: FileCheck --check-prefix=ASM %s < out.lto.s
6566

66-
# RUN: ld.lld -o - main.o --start-lib 1.bc 2.bc --end-lib --fortran-common --lto-emit-asm | \
67-
# RUN: FileCheck --check-prefix=ASM %s
67+
# RUN: rm out.lto.s
68+
# RUN: ld.lld -o out main.o --start-lib 1.bc 2.bc --end-lib --fortran-common --lto-emit-asm
69+
# RUN: FileCheck --check-prefix=ASM %s < out.lto.s
6870

6971
## COMMON overrides weak. Don't extract 3.bc which provides a weak definition.
7072
# RUN: ld.lld -o /dev/null main.o --start-lib 1.bc 3.bc --end-lib -y block | FileCheck --check-prefix=LTO_WEAK %s

lld/test/ELF/lto/cache.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353

5454
; RUN: rm -fr cache && mkdir cache
5555
; RUN: ld.lld --thinlto-cache-dir=cache --save-temps -o out b.bc a.bc -M | FileCheck %s --check-prefix=MAP
56-
; RUN: ls out1.lto.o a.bc.0.preopt.bc b.bc.0.preopt.bc
56+
; RUN: ls out.lto.a.o a.bc.0.preopt.bc b.bc.0.preopt.bc
5757

58-
; MAP: llvmcache-{{.*}}:(.text)
59-
; MAP: llvmcache-{{.*}}:(.text)
58+
; MAP: out.lto.b.o:(.text)
59+
; MAP: out.lto.a.o:(.text)
6060

6161
;; Check that mllvm options participate in the cache key
6262
; RUN: rm -rf cache && mkdir cache

lld/test/ELF/lto/comdat-mixed-archive.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ TRACE-NEXT: lib.a(obj.o): definition of bar
3535
TRACE-NEXT: lib.a(obj.o): reference to foo
3636
TRACE-NEXT: <internal>: reference to foo
3737
;; The definition of "foo" is visible outside the LTO result.
38-
TRACE-NEXT: lto.tmp: definition of foo
39-
TRACE-NEXT: lto.tmp: reference to bar
38+
TRACE-NEXT: {{.*}}.lto.o: definition of foo
39+
TRACE-NEXT: {{.*}}.lto.o: reference to bar
4040

4141
;--- start.s
4242
.global _start, baz

lld/test/ELF/lto/emit-asm.ll

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
; REQUIRES: x86
22
; RUN: rm -rf %t && mkdir %t && cd %t
33
; RUN: llvm-as %s -o a.bc
4-
; RUN: ld.lld --lto-emit-asm -shared a.bc -o - | FileCheck %s
5-
; RUN: ld.lld --plugin-opt=emit-asm --plugin-opt=lto-partitions=2 -shared a.bc -o out.s
6-
; RUN: cat out.s out.s1 | FileCheck %s
4+
; RUN: ld.lld --lto-emit-asm -shared a.bc -o out 2>&1 | count 0
5+
; RUN: FileCheck %s < out.lto.s
6+
; RUN: ld.lld --plugin-opt=emit-asm --plugin-opt=lto-partitions=2 -shared a.bc -o out
7+
; RUN: cat out.lto.s out.lto.1.s | FileCheck %s
78

8-
; RUN: ld.lld --lto-emit-asm --save-temps -shared a.bc -o out.s
9-
; RUN: FileCheck --input-file out.s %s
10-
; RUN: llvm-dis out.s.0.4.opt.bc -o - | FileCheck --check-prefix=OPT %s
9+
; RUN: ld.lld --lto-emit-asm --save-temps -shared a.bc -o out
10+
; RUN: FileCheck --input-file out.lto.s %s
11+
; RUN: llvm-dis out.0.4.opt.bc -o - | FileCheck --check-prefix=OPT %s
1112

1213
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1314
target triple = "x86_64-unknown-linux-gnu"

lld/test/ELF/lto/exclude-libs-libcall.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
; RUN: llvm-readelf --dyn-syms %t.so | FileCheck %s
88

99
; TRACE: {{.*}}/b.a(b.o): lazy definition of __divti3
10-
; TRACE-NEXT: lto.tmp: reference to __divti3
10+
; TRACE-NEXT: {{.*}}.lto.o: reference to __divti3
1111
; TRACE-NEXT: {{.*}}/b.a(b.o): definition of __divti3
1212

1313
; CHECK: Symbol table '.dynsym' contains 2 entries:

lld/test/ELF/lto/obj-path.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@
6767
;; Ensure lld emits empty combined module if specific obj-path.
6868
; RUN: mkdir obj
6969
; RUN: ld.lld --plugin-opt=obj-path=objpath.o -shared 1.bc d/2.bc -o obj/out --save-temps
70-
; RUN: ls obj/out.lto.o obj/out1.lto.o obj/out2.lto.o
70+
; RUN: ls obj/out.lto.o out.lto.1.o d/out.lto.2.o
7171

7272
;; Ensure lld does not emit empty combined module by default.
7373
; RUN: rm -fr obj && mkdir obj
7474
; RUN: ld.lld -shared 1.bc d/2.bc -o obj/out --save-temps
75-
; RUN: ls obj/out*.lto.* | count 2
75+
; RUN: not test -e obj/out.lto.o
7676

7777
; EMPTY: file format elf64-x86-64
7878
; EMPTY-NOT: {{.}}

lld/test/ELF/lto/parallel-internalize.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
; RUN: ld.lld --lto-partitions=2 -save-temps -o out a.bc -e foo --lto-O0
55
; RUN: llvm-readobj --symbols --dyn-syms out | FileCheck %s
66
; RUN: llvm-nm out.lto.o | FileCheck --check-prefix=CHECK0 %s
7-
; RUN: llvm-nm out1.lto.o | FileCheck --check-prefix=CHECK1 %s
7+
; RUN: llvm-nm out.lto.1.o | FileCheck --check-prefix=CHECK1 %s
88

99
; CHECK: Symbols [
1010
; CHECK-NEXT: Symbol {

lld/test/ELF/lto/parallel.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
; RUN: llvm-as -o a.bc %s
44
; RUN: ld.lld --lto-partitions=2 -save-temps -o out a.bc -shared
55
; RUN: llvm-nm out.lto.o | FileCheck --check-prefix=CHECK0 %s
6-
; RUN: llvm-nm out1.lto.o | FileCheck --check-prefix=CHECK1 %s
6+
; RUN: llvm-nm out.lto.1.o | FileCheck --check-prefix=CHECK1 %s
77

88
; RUN: not ld.lld --lto-partitions=0 a.bc -o /dev/null 2>&1 | FileCheck --check-prefix=INVALID %s
99
; INVALID: --lto-partitions: number of threads must be > 0

lld/test/ELF/lto/pseudo-probe-lto.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; REQUIRES: x86
22
; RUN: opt < %s -passes=pseudo-probe -function-sections -o %t.o
3-
; RUN: ld.lld %t.o -shared --lto-emit-asm -o - | FileCheck %s
3+
; RUN: ld.lld %t.o -shared --lto-emit-asm -o %t
4+
; RUN: FileCheck %s < %t.lto.s
45

56
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
67
target triple = "x86_64-scei-ps4"

lld/test/ELF/lto/save-temps-eq.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
;; Create the .all dir with save-temps saving everything, this will be used to compare
1515
;; with the output from individualized save-temps later
1616
; RUN: ld.lld main.o thin1.o --save-temps -o %t/all/a.out
17-
; RUN: mv *.o.* %t/all
17+
; RUN: mv a.out.lto.* *.o.*.bc %t/all
1818
;; Sanity check that everything got moved
1919
; RUN: ls | count 2
2020

2121
;; Check precedence if both --save-temps and --save-temps= are present
2222
; RUN: ld.lld main.o thin1.o --save-temps=preopt --save-temps --save-temps=\opt -o %t/all2/a.out
2323
; RUN: cmp %t/all2/a.out %t/all/a.out
24-
; RUN: mv *.o.* %t/all2
24+
; RUN: mv a.out.lto.* *.o.* %t/all2
2525
; RUN: ls | count 2
2626
; RUN: diff -r %t/all %t/all2
2727

@@ -83,8 +83,8 @@
8383
;; Check prelink
8484
; RUN: ld.lld main.o thin1.o --save-temps=prelink
8585
; RUN: cmp %t/all/a.out a.out && rm -f a.out
86-
; RUN: cp *.lto.o %t/subset2
87-
; RUN: mv *.lto.o %t/all3
86+
; RUN: cp a.out.lto.*.o %t/subset2
87+
; RUN: mv a.out.lto.*.o %t/all3
8888
; RUN: ls | count 2
8989

9090
;; Check resolution
@@ -104,7 +104,7 @@
104104
; RUN: cmp %t/all/a.out a.out && rm -f a.out
105105
; RUN: mv *.0.preopt.* %t/subset
106106
; RUN: mv *.4.opt* %t/subset
107-
; RUN: mv *.lto.o %t/subset
107+
; RUN: mv a.out.lto.*.o %t/subset
108108
; RUN: ls | count 2
109109
; RUN: diff -r %t/subset2 %t/subset
110110

lld/test/ELF/lto/thin-archivecollision.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
; RUN: llvm-ar rcS d/a.a d/coll.o e/coll.o
1010
; RUN: ld.lld b.bc d/a.a -o out --save-temps
1111
; RUN: llvm-nm out | FileCheck %s
12-
; RUN: llvm-nm out2.lto.o | FileCheck %s --check-prefix=MOD2
13-
; RUN: llvm-nm out3.lto.o | FileCheck %s --check-prefix=MOD3
12+
;; d/out.lto.a.a(coll.o at 8).o out.lto.a.a(coll.o at 1916).o
13+
; RUN: llvm-nm d/out.lto.a.a*at*.o | FileCheck %s --check-prefix=MOD2
1414

1515
; Check we handle this case correctly even in presence of --whole-archive.
16-
; RUN: rm out1.lto.o out2.lto.o out3.lto.o
16+
; RUN: rm d/out.lto.a.a*at*.o
1717
; RUN: ld.lld b.bc --whole-archive d/a.a -o out --save-temps
1818
; RUN: llvm-nm out | FileCheck %s
19-
; RUN: ls out1.lto.o out2.lto.o out3.lto.o
19+
; RUN: llvm-nm d/out.lto.a.a*at*.o | FileCheck %s --check-prefix=MOD2
2020

2121
; CHECK: T _start
2222
; CHECK: T blah
2323
; CHECK: T foo
2424

25-
; MOD2: T foo
26-
; MOD3: T blah
25+
; MOD2-DAG: T foo
26+
; MOD2-DAG: T blah
2727

2828
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
2929
target triple = "x86_64-scei-ps4"

0 commit comments

Comments
 (0)