[PATCH] D29475: [LTO] Add ability to emit assembly to new LTO API

Tobias Edler von Koch via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 3 09:07:47 PST 2017

tobiasvk updated this revision to Diff 86977.
tobiasvk added a comment.

Move test to test/LTO/Resolution as per Peter's request.

Thank you all for your comments.

I agree about the need for an assembler 'driver' in the linker to make this
work and the potential fragility. This is what we have and it's worked fine on
our side with the previous LTO API. Note that clang's non-LTO -save-temps flow
calls the assembler separately (via clang -cc1as), too. It's perfectly doable
to ensure that the linker is called with the correct options to pass through to
the assembler when clang is used as the driver for LTO.

I like Rafael's idea of using the MC layer to emit both assembly and an object
at the same time with the same settings (if that's what you're proposing) - we
could use that for the non-LTO flow in clang too. I haven't checked how easy
that would be, but it's perhaps worth considering in the future.

Anyway, if there are no further objections, I'd like this to go in so the
legacy and resolution-based LTO APIs are feature-compatible in this respect.



Index: tools/llvm-lto2/llvm-lto2.cpp
--- tools/llvm-lto2/llvm-lto2.cpp
+++ tools/llvm-lto2/llvm-lto2.cpp
@@ -199,6 +199,9 @@
     return 1;
+  if (FileType.getNumOccurrences())
+    Conf.CGFileType = FileType;
   Conf.OverrideTriple = OverrideTriple;
   Conf.DefaultTriple = DefaultTriple;
Index: include/llvm/LTO/Config.h
--- include/llvm/LTO/Config.h
+++ include/llvm/LTO/Config.h
@@ -17,6 +17,7 @@
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/Support/CodeGen.h"
+#include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include <functional>
@@ -41,6 +42,7 @@
   Reloc::Model RelocModel = Reloc::PIC_;
   CodeModel::Model CodeModel = CodeModel::Default;
   CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
+  TargetMachine::CodeGenFileType CGFileType = TargetMachine::CGFT_ObjectFile;
   unsigned OptLevel = 2;
   bool DisableVerify = false;
Index: test/LTO/Resolution/X86/asm-output.ll
--- /dev/null
+++ test/LTO/Resolution/X86/asm-output.ll
@@ -0,0 +1,19 @@
+; Test the ability to emit assembly code from the resolution-based LTO API
+; RUN: llvm-as < %s > %t1.bc
+; RUN: llvm-lto2 -filetype=asm -r %t1.bc,main,px -o %t2 %t1.bc
+; RUN: FileCheck --check-prefix=ASM %s < %t2.0
+; RUN: llvm-lto2 -filetype=obj -r %t1.bc,main,px -o %t2 %t1.bc
+; RUN: llvm-objdump -d %t2.0 | FileCheck --check-prefix=ASM %s
+; ASM: main:
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+define i32 @main() {
+  ret i32 23
Index: lib/LTO/LTOBackend.cpp
--- lib/LTO/LTOBackend.cpp
+++ lib/LTO/LTOBackend.cpp
@@ -271,8 +271,7 @@
   auto Stream = AddStream(Task);
   legacy::PassManager CodeGenPasses;
-  if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
-                              TargetMachine::CGFT_ObjectFile))
+  if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, Conf.CGFileType))
     report_fatal_error("Failed to setup codegen");
Index: lib/LTO/LTO.cpp
--- lib/LTO/LTO.cpp
+++ lib/LTO/LTO.cpp
@@ -94,6 +94,7 @@
+  AddUnsigned(Conf.CGFileType);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29475.86977.patch
Type: text/x-patch
Size: 2647 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170203/080b74bf/attachment.bin>

More information about the llvm-commits mailing list