[PATCH] D24020: [LTO] Added flag to generate assembly file with after LTO passes

Bhargav Reddy Godala via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 30 01:37:20 PDT 2016


bunty2020 created this revision.
bunty2020 added a reviewer: tejohnson.
bunty2020 added a subscriber: llvm-commits.
Herald added a subscriber: mehdi_amini.

save temps option currently doesn't generate assembly file with -flto flag.
To generate assembly file after LTO passes use -Wl,-plugin-opt=-flto-print-asm flag

https://reviews.llvm.org/D24020

Files:
  lib/LTO/LTOBackend.cpp

Index: lib/LTO/LTOBackend.cpp
===================================================================
--- lib/LTO/LTOBackend.cpp
+++ lib/LTO/LTOBackend.cpp
@@ -21,6 +21,7 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/LTO/LTO.h"
 #include "llvm/MC/SubtargetFeature.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/TargetRegistry.h"
@@ -34,6 +35,18 @@
 using namespace llvm;
 using namespace lto;
 
+static cl::opt<std::string>
+FLTOAsmFilename("flto-asm-file",
+             cl::desc("Assembly file name to print assembly code after "
+                      "LTO phase. Works only with flto-print-asm enabled."),
+             cl::Hidden);
+
+static cl::opt<bool>
+FLTOPrintAsm("flto-print-asm",
+             cl::desc("Print assembly file in after LTO phase"),
+             cl::init(false), cl::Hidden);
+
+
 Error Config::addSaveTemps(std::string OutputFileName,
                            bool UseInputModulePath) {
   ShouldDiscardValueNames = false;
@@ -156,14 +169,48 @@
   };
 }
 
+// Generates assembly file with -Wl,-plugin-opt=-flto-print-asm
+void genAssemblyFile(TargetMachine *TM, Module &M){
+  int FD;
+  std::error_code EC;
+  SmallString<32> OutputFile;
+
+  if(FLTOAsmFilename == ""){
+    EC = sys::fs::createTemporaryFile("llvm-flto", "s", FD, OutputFile);
+    errs() << "Wrtiting to file : " << OutputFile << "\n";
+  }else{
+    EC = sys::fs::openFileForWrite(StringRef(FLTOAsmFilename),
+                                                 FD, sys::fs::F_None);
+  }
+
+  if (EC)
+    report_fatal_error( "Could not open file: %s", EC.message().c_str());
+
+  std::unique_ptr<raw_pwrite_stream>
+    AsmStream = llvm::make_unique<llvm::raw_fd_ostream>(FD, true);
+
+  legacy::PassManager CodeGenPasses;
+
+  if (TM->addPassesToEmitFile(CodeGenPasses, *AsmStream,
+                              TargetMachine::CGFT_AssemblyFile))
+    report_fatal_error("Failed to setup assembly gen in codegen");
+
+  CodeGenPasses.run(M);
+}
 void codegen(Config &C, TargetMachine *TM, AddOutputFn AddOutput, unsigned Task,
              Module &M) {
   if (C.PreCodeGenModuleHook && !C.PreCodeGenModuleHook(Task, M))
     return;
 
   auto Output = AddOutput(Task);
   std::unique_ptr<raw_pwrite_stream> OS = Output->getStream();
   legacy::PassManager CodeGenPasses;
+
+  // Generate assembly file
+  if(FLTOPrintAsm){
+    genAssemblyFile(TM, M);
+  }
+
   if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
                               TargetMachine::CGFT_ObjectFile))
     report_fatal_error("Failed to setup codegen");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24020.69648.patch
Type: text/x-patch
Size: 2628 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160830/3557bf70/attachment.bin>


More information about the llvm-commits mailing list