<div dir="ltr">Hi Manman,<div><br></div><div>This commit introduces a memory leak, breaking ASan bootstrap: <a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/1878/steps/check-llvm%20asan/logs/stdio">http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/1878/steps/check-llvm%20asan/logs/stdio</a></div><div>Please investigate.</div><div><br></div><div class="gmail_extra"><div class="gmail_quote">On Mon, Feb 23, 2015 at 4:45 PM, Manman Ren <span dir="ltr"><<a href="mailto:manman.ren@gmail.com" target="_blank">manman.ren@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: mren<br>
Date: Mon Feb 23 18:45:56 2015<br>
New Revision: 230290<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=230290&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=230290&view=rev</a><br>
Log:<br>
[LTO API] add lto_codegen_set_module to set the destination module.<br>
<br>
When debugging LTO issues with ld64, we use -save-temps to save the merged<br>
optimized bitcode file, then invoke ld64 again on the single bitcode file to<br>
speed up debugging code generation passes and ld64 stuff after code generation.<br>
<br>
llvm linking a single bitcode file via lto_codegen_add_module will generate a<br>
different bitcode file from the single input. With the newly-added<br>
lto_codegen_set_module, we can make sure the destination module is the same as<br>
the input.<br>
<br>
lto_codegen_set_module will transfer the ownship of the module to code<br>
generator.<br>
<br>
rdar://19024554<br>
<br>
Added:<br>
    llvm/trunk/test/LTO/X86/set-merged.ll<br>
Modified:<br>
    llvm/trunk/include/llvm-c/lto.h<br>
    llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h<br>
    llvm/trunk/include/llvm/Linker/Linker.h<br>
    llvm/trunk/lib/LTO/LTOCodeGenerator.cpp<br>
    llvm/trunk/lib/Linker/LinkModules.cpp<br>
    llvm/trunk/tools/llvm-lto/llvm-lto.cpp<br>
    llvm/trunk/tools/lto/lto.cpp<br>
    llvm/trunk/tools/lto/lto.exports<br>
<br>
Modified: llvm/trunk/include/llvm-c/lto.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/lto.h?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/lto.h?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm-c/lto.h (original)<br>
+++ llvm/trunk/include/llvm-c/lto.h Mon Feb 23 18:45:56 2015<br>
@@ -40,7 +40,7 @@ typedef bool lto_bool_t;<br>
  * @{<br>
  */<br>
<br>
-#define LTO_API_VERSION 12<br>
+#define LTO_API_VERSION 13<br>
<br>
 /**<br>
  * \since prior to LTO_API_VERSION=3<br>
@@ -396,6 +396,17 @@ extern lto_bool_t<br>
 lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);<br>
<br>
 /**<br>
+ * Sets the object module for code generation. This will transfer the ownship of<br>
+ * the module to code generator.<br>
+ *<br>
+ * \c cg and \c mod must both be in the same context.<br>
+ *<br>
+ * \since prior to LTO_API_VERSION=13<br>
+ */<br>
+extern void<br>
+lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod);<br>
+<br>
+/**<br>
  * Sets if debug info should be generated.<br>
  * Returns true on error (check lto_get_error_message() for details).<br>
  *<br>
<br>
Modified: llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h (original)<br>
+++ llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h Mon Feb 23 18:45:56 2015<br>
@@ -67,6 +67,9 @@ struct LTOCodeGenerator {<br>
   // Merge given module, return true on success.<br>
   bool addModule(struct LTOModule *);<br>
<br>
+  // Set the destination module.<br>
+  void setModule(struct LTOModule *);<br>
+<br>
   void setTargetOptions(TargetOptions options);<br>
   void setDebugInfo(lto_debug_model);<br>
   void setCodePICModel(lto_codegen_model);<br>
<br>
Modified: llvm/trunk/include/llvm/Linker/Linker.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Linker/Linker.h?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Linker/Linker.h?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Linker/Linker.h (original)<br>
+++ llvm/trunk/include/llvm/Linker/Linker.h Mon Feb 23 18:45:56 2015<br>
@@ -70,6 +70,9 @@ public:<br>
   /// Returns true on error.<br>
   bool linkInModule(Module *Src);<br>
<br>
+  /// \brief Set the composite to the passed-in module.<br>
+  void setModule(Module *Dst);<br>
+<br>
   static bool LinkModules(Module *Dest, Module *Src,<br>
                           DiagnosticHandlerFunction DiagnosticHandler);<br>
<br>
<br>
Modified: llvm/trunk/lib/LTO/LTOCodeGenerator.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOCodeGenerator.cpp?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOCodeGenerator.cpp?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/LTO/LTOCodeGenerator.cpp (original)<br>
+++ llvm/trunk/lib/LTO/LTOCodeGenerator.cpp Mon Feb 23 18:45:56 2015<br>
@@ -141,6 +141,22 @@ bool LTOCodeGenerator::addModule(LTOModu<br>
   return !ret;<br>
 }<br>
<br>
+void LTOCodeGenerator::setModule(LTOModule *Mod) {<br>
+  assert(&Mod->getModule().getContext() == &Context &&<br>
+         "Expected module in same context");<br>
+<br>
+  // Delete the old merged module.<br>
+  if (IRLinker.getModule())<br>
+    IRLinker.deleteModule();<br>
+  AsmUndefinedRefs.clear();<br>
+<br>
+  IRLinker.setModule(&Mod->getModule());<br>
+<br>
+  const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();<br>
+  for (int I = 0, E = Undefs.size(); I != E; ++I)<br>
+    AsmUndefinedRefs[Undefs[I]] = 1;<br>
+}<br>
+<br>
 void LTOCodeGenerator::setTargetOptions(TargetOptions options) {<br>
   Options = options;<br>
 }<br>
<br>
Modified: llvm/trunk/lib/Linker/LinkModules.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Linker/LinkModules.cpp (original)<br>
+++ llvm/trunk/lib/Linker/LinkModules.cpp Mon Feb 23 18:45:56 2015<br>
@@ -1748,6 +1748,10 @@ bool Linker::linkInModule(Module *Src) {<br>
   return RetCode;<br>
 }<br>
<br>
+void Linker::setModule(Module *Dst) {<br>
+  init(Dst, DiagnosticHandler);<br>
+}<br>
+<br>
 //===----------------------------------------------------------------------===//<br>
 // LinkModules entrypoint.<br>
 //===----------------------------------------------------------------------===//<br>
<br>
Added: llvm/trunk/test/LTO/X86/set-merged.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LTO/X86/set-merged.ll?rev=230290&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LTO/X86/set-merged.ll?rev=230290&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/LTO/X86/set-merged.ll (added)<br>
+++ llvm/trunk/test/LTO/X86/set-merged.ll Mon Feb 23 18:45:56 2015<br>
@@ -0,0 +1,36 @@<br>
+; RUN: llvm-as < %s >%t1<br>
+; RUN: llvm-lto -exported-symbol=_main -set-merged-module -o %t2 %t1<br>
+; RUN: llvm-objdump -d %t2 | FileCheck %s<br>
+<br>
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-apple-macosx10.10.0"<br>
+<br>
+; CHECK: _main<br>
+; CHECK: movl $132<br>
+define i32 @_Z3fooi(i32 %a) {<br>
+entry:<br>
+  %a.addr = alloca i32, align 4<br>
+  store i32 %a, i32* %a.addr, align 4<br>
+  %0 = load i32* %a.addr, align 4<br>
+  %1 = load i32* %a.addr, align 4<br>
+  %call = call i32 @_Z4bar2i(i32 %1)<br>
+  %add = add nsw i32 %0, %call<br>
+  ret i32 %add<br>
+}<br>
+<br>
+define i32 @_Z4bar2i(i32 %a) {<br>
+entry:<br>
+  %a.addr = alloca i32, align 4<br>
+  store i32 %a, i32* %a.addr, align 4<br>
+  %0 = load i32* %a.addr, align 4<br>
+  %mul = mul nsw i32 2, %0<br>
+  ret i32 %mul<br>
+}<br>
+<br>
+define i32 @main() {<br>
+entry:<br>
+  %retval = alloca i32, align 4<br>
+  store i32 0, i32* %retval<br>
+  %call = call i32 @_Z3fooi(i32 44)<br>
+  ret i32 %call<br>
+}<br>
<br>
Modified: llvm/trunk/tools/llvm-lto/llvm-lto.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-lto/llvm-lto.cpp?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-lto/llvm-lto.cpp?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/llvm-lto/llvm-lto.cpp (original)<br>
+++ llvm/trunk/tools/llvm-lto/llvm-lto.cpp Mon Feb 23 18:45:56 2015<br>
@@ -69,6 +69,10 @@ static cl::opt<bool> ListSymbolsOnly(<br>
     "list-symbols-only", cl::init(false),<br>
     cl::desc("Instead of running LTO, list the symbols in each IR file"));<br>
<br>
+static cl::opt<bool> SetMergedModule(<br>
+    "set-merged-module", cl::init(false),<br>
+    cl::desc("Use the first input module as the merged module"));<br>
+<br>
 namespace {<br>
 struct ModuleInfo {<br>
   std::vector<bool> CanBeHidden;<br>
@@ -194,15 +198,22 @@ int main(int argc, char **argv) {<br>
       return 1;<br>
     }<br>
<br>
-    if (!CodeGen.addModule(Module.get()))<br>
+    LTOModule *LTOMod = Module.get();<br>
+<br>
+    // We use the first input module as the destination module when<br>
+    // SetMergedModule is true.<br>
+    if (SetMergedModule && i == BaseArg) {<br>
+      // Transfer ownership to the code generator.<br>
+      CodeGen.setModule(Module.release());<br></blockquote><div><br></div><div>^^</div><div>This is false. LTOCodeGenerator::setModule does *not* take the ownership of LTOModule - it</div><div>just leaks this object.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+    } else if (!CodeGen.addModule(Module.get()))<br>
       return 1;<br>
<br>
-    unsigned NumSyms = Module->getSymbolCount();<br>
+    unsigned NumSyms = LTOMod->getSymbolCount();<br>
     for (unsigned I = 0; I < NumSyms; ++I) {<br>
-      StringRef Name = Module->getSymbolName(I);<br>
+      StringRef Name = LTOMod->getSymbolName(I);<br>
       if (!DSOSymbolsSet.count(Name))<br>
         continue;<br>
-      lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);<br>
+      lto_symbol_attributes Attrs = LTOMod->getSymbolAttributes(I);<br>
       unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;<br>
       if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)<br>
         KeptDSOSyms.push_back(Name);<br>
<br>
Modified: llvm/trunk/tools/lto/lto.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.cpp?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.cpp?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/lto/lto.cpp (original)<br>
+++ llvm/trunk/tools/lto/lto.cpp Mon Feb 23 18:45:56 2015<br>
@@ -248,6 +248,10 @@ bool lto_codegen_add_module(lto_code_gen<br>
   return !unwrap(cg)->addModule(unwrap(mod));<br>
 }<br>
<br>
+void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {<br>
+  unwrap(cg)->setModule(unwrap(mod));<br>
+}<br>
+<br>
 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {<br>
   unwrap(cg)->setDebugInfo(debug);<br>
   return false;<br>
<br>
Modified: llvm/trunk/tools/lto/lto.exports<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.exports?rev=230290&r1=230289&r2=230290&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.exports?rev=230290&r1=230289&r2=230290&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/lto/lto.exports (original)<br>
+++ llvm/trunk/tools/lto/lto.exports Mon Feb 23 18:45:56 2015<br>
@@ -25,6 +25,7 @@ lto_module_dispose<br>
 lto_api_version<br>
 lto_codegen_set_diagnostic_handler<br>
 lto_codegen_add_module<br>
+lto_codegen_set_module<br>
 lto_codegen_add_must_preserve_symbol<br>
 lto_codegen_compile<br>
 lto_codegen_create<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div></div>
</div></div>