[lld] [lld][COFF][LTO] Implement /opt:emitllvm option (PR #66964)
Matheus Izvekov via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 27 15:10:46 PDT 2023
https://github.com/mizvekov updated https://github.com/llvm/llvm-project/pull/66964
>From b5e750e9df628834cdd0d85f12e744649d037ff0 Mon Sep 17 00:00:00 2001
From: Matheus Izvekov <mizvekov at gmail.com>
Date: Thu, 21 Sep 2023 02:12:32 +0200
Subject: [PATCH] [lld][COFF][LTO] Implement /opt:emitllvm option
With this new option, bitcode will be emited instead of object code.
This is analogous to the `--plugin-opt=emit-llvm` option in the ELF
linker.
---
lld/COFF/Config.h | 1 +
lld/COFF/Driver.cpp | 5 ++++-
lld/COFF/LTO.cpp | 9 +++++++++
lld/test/COFF/lto-emit-llvm.ll | 14 ++++++++++++++
4 files changed, 28 insertions(+), 1 deletion(-)
create mode 100644 lld/test/COFF/lto-emit-llvm.ll
diff --git a/lld/COFF/Config.h b/lld/COFF/Config.h
index 4ade2c953c73e40..ee6829c3d225017 100644
--- a/lld/COFF/Config.h
+++ b/lld/COFF/Config.h
@@ -311,6 +311,7 @@ struct Configuration {
bool pseudoRelocs = false;
bool stdcallFixup = false;
bool writeCheckSum = false;
+ bool emitLLVM = false;
};
} // namespace lld::coff
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 61a04a74aa60278..03a6c62bbd75678 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1823,6 +1823,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
ltoDebugPM = true;
} else if (s == "noltodebugpassmanager") {
ltoDebugPM = false;
+ } else if (s == "emitllvm") {
+ config->emitLLVM = true;
} else if (s.consume_front("lldlto=")) {
if (s.getAsInteger(10, config->ltoo) || config->ltoo > 3)
error("/opt:lldlto: invalid optimization level: " + s);
@@ -2395,7 +2397,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
// If -thinlto-index-only is given, we should create only "index
// files" and not object files. Index file creation is already done
// in addCombinedLTOObject, so we are done if that's the case.
- if (config->thinLTOIndexOnly)
+ // Likewise, for /opt:emitllvm we only emit bitcode.
+ if (config->emitLLVM || config->thinLTOIndexOnly)
return;
// If we generated native object files from bitcode files, this resolves
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 6ca000b466a1264..ee9cae0852d5345 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -87,6 +87,15 @@ lto::Config BitcodeCompiler::createConfig() {
c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate;
c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch;
+ if (ctx.config.emitLLVM) {
+ c.PostInternalizeModuleHook = [this](size_t task, const Module &m) {
+ if (std::unique_ptr<raw_fd_ostream> os =
+ openLTOOutputFile(ctx.config.outputFile))
+ WriteBitcodeToFile(m, *os, false);
+ return false;
+ };
+ }
+
if (ctx.config.saveTemps)
checkError(c.addSaveTemps(std::string(ctx.config.outputFile) + ".",
/*UseInputModulePath*/ true));
diff --git a/lld/test/COFF/lto-emit-llvm.ll b/lld/test/COFF/lto-emit-llvm.ll
new file mode 100644
index 000000000000000..d7ab61d043a0099
--- /dev/null
+++ b/lld/test/COFF/lto-emit-llvm.ll
@@ -0,0 +1,14 @@
+; REQUIRES: x86
+; RUN: llvm-as -o %T/lto.obj %s
+
+; RUN: lld-link /opt:emitllvm /out:%T/lto.bc /entry:main /subsystem:console %T/lto.obj
+; RUN: llvm-dis %T/lto.bc -o - | FileCheck %s
+
+; CHECK: define void @main()
+
+target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+define void @main() {
+ ret void
+}
More information about the llvm-commits
mailing list