Skip to content

Commit d12b99a

Browse files
authored
[lld][COFF][LTO] Implement /lldemit:asm option (llvm#67079)
With this new option, assembly code will be emited instead of object code. This is analogous to the `--lto-emit-asm` option in the ELF linker.
1 parent 40653b6 commit d12b99a

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

lld/COFF/Config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum class ExportSource {
4848
ModuleDefinition,
4949
};
5050

51-
enum class EmitKind { Obj, LLVM };
51+
enum class EmitKind { Obj, LLVM, ASM };
5252

5353
// Represents an /export option.
5454
struct Export {

lld/COFF/Driver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
18591859
config->emit = EmitKind::Obj;
18601860
else if (s == "llvm")
18611861
config->emit = EmitKind::LLVM;
1862+
else if (s == "asm")
1863+
config->emit = EmitKind::ASM;
18621864
else
18631865
error("/lldemit: unknown option: " + s);
18641866
}

lld/COFF/LTO.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ lto::Config BitcodeCompiler::createConfig() {
9494
WriteBitcodeToFile(m, *os, false);
9595
return false;
9696
};
97+
} else if (ctx.config.emit == EmitKind::ASM) {
98+
c.CGFileType = CodeGenFileType::AssemblyFile;
99+
c.Options.MCOptions.AsmVerbose = true;
97100
}
98101

99102
if (ctx.config.saveTemps)
@@ -213,6 +216,8 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
213216
pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, files);
214217

215218
std::vector<InputFile *> ret;
219+
bool emitASM = ctx.config.emit == EmitKind::ASM;
220+
const char *Ext = emitASM ? ".s" : ".obj";
216221
for (unsigned i = 0; i != maxTasks; ++i) {
217222
StringRef bitcodeFilePath;
218223
// Get the native object contents either from the cache or from memory. Do
@@ -235,20 +240,21 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
235240
if (bitcodeFilePath == "ld-temp.o") {
236241
ltoObjName =
237242
saver().save(Twine(ctx.config.outputFile) + ".lto" +
238-
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ".obj");
243+
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + Ext);
239244
} else {
240245
StringRef directory = sys::path::parent_path(bitcodeFilePath);
241-
StringRef baseName = sys::path::filename(bitcodeFilePath);
246+
StringRef baseName = sys::path::stem(bitcodeFilePath);
242247
StringRef outputFileBaseName = sys::path::filename(ctx.config.outputFile);
243248
SmallString<64> path;
244249
sys::path::append(path, directory,
245-
outputFileBaseName + ".lto." + baseName);
250+
outputFileBaseName + ".lto." + baseName + Ext);
246251
sys::path::remove_dots(path, true);
247252
ltoObjName = saver().save(path.str());
248253
}
249-
if (ctx.config.saveTemps)
254+
if (ctx.config.saveTemps || emitASM)
250255
saveBuffer(buf[i].second, ltoObjName);
251-
ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName)));
256+
if (!emitASM)
257+
ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName)));
252258
}
253259

254260
return ret;

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

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; REQUIRES: x86
2+
; RUN: llvm-as %s -o %t.obj
3+
4+
; RUN: lld-link /lldemit:asm /dll /noentry /include:f1 /include:f2 %t.obj /opt:lldltopartitions=1 /out:%t.1p /lldsavetemps
5+
; RUN: cat %t.1p.lto.s | FileCheck %s
6+
; RUN: llvm-dis %t.1p.0.4.opt.bc -o - | FileCheck --check-prefix=OPT %s
7+
8+
; RUN: lld-link /lldemit:asm /dll /noentry /include:f1 /include:f2 %t.obj /opt:lldltopartitions=2 /out:%t.2p
9+
; RUN: cat %t.2p.lto.s %t.2p.lto.1.s | FileCheck %s
10+
11+
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
12+
target triple = "x86_64-pc-windows-msvc"
13+
14+
;; Note: we also check for the presence of comments; /lldemit:asm output should be verbose.
15+
16+
; CHECK-DAG: # -- Begin function f1
17+
; CHECK-DAG: f1:
18+
; OPT: define void @f1()
19+
define void @f1() {
20+
ret void
21+
}
22+
23+
; CHECK-DAG: # -- Begin function f2
24+
; CHECK-DAG: f2:
25+
; OPT: define void @f2()
26+
define void @f2() {
27+
ret void
28+
}

lld/test/COFF/pdb-thinlto.ll

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ declare void @foo()
2929

3030
; CHECK: Modules
3131
; CHECK: ============================================================
32-
; CHECK: Mod 0000 | `{{.*}}main.exe.lto.main.bc`:
33-
; CHECK: Obj: `{{.*}}main.exe.lto.main.bc`:
34-
; CHECK: Mod 0001 | `{{.*}}main.exe.lto.foo.bc`:
35-
; CHECK: Obj: `{{.*}}main.exe.lto.foo.bc`:
32+
; CHECK: Mod 0000 | `{{.*}}main.exe.lto.main.obj`:
33+
; CHECK: Obj: `{{.*}}main.exe.lto.main.obj`:
34+
; CHECK: Mod 0001 | `{{.*}}main.exe.lto.foo.obj`:
35+
; CHECK: Obj: `{{.*}}main.exe.lto.foo.obj`:
3636
; CHECK: Mod 0002 | `* Linker *`:

0 commit comments

Comments
 (0)