From: Steven Wu <stevenwu@apple.com>
Date: Wed, 9 Dec 2015 03:37:51 +0000 (+0000)
Subject: Fix the order of destructors in LibLTOCodeGenerator
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=1208ef58cc26a928add3f5d797f6f1c5811bd1ed;p=oota-llvm.git

Fix the order of destructors in LibLTOCodeGenerator

Summary:
The order of destructors in LTOCodeGenerator gets changed in r254696.
It is possible for LTOCodeGenerator to have a MergedModule created in
the OwnedContext, in which case the module must be destructed before
the context.

Reviewers: rafael, dexonsmith

Subscribers: llvm-commits, joker.eph

Differential Revision: http://reviews.llvm.org/D15346

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255092 91177308-0d34-0410-b5e6-96231b3b80d8
---

diff --git a/include/llvm/LTO/LTOCodeGenerator.h b/include/llvm/LTO/LTOCodeGenerator.h
index 876defbdcd3..8a79e6044f5 100644
--- a/include/llvm/LTO/LTOCodeGenerator.h
+++ b/include/llvm/LTO/LTOCodeGenerator.h
@@ -148,6 +148,8 @@ struct LTOCodeGenerator {
 
   LLVMContext &getContext() { return Context; }
 
+  void resetMergedModule() { MergedModule.reset(); }
+
 private:
   void initializeLTOPasses();
 
diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp
index d13de57e830..d8f99c050a3 100644
--- a/tools/lto/lto.cpp
+++ b/tools/lto/lto.cpp
@@ -124,6 +124,10 @@ struct LibLTOCodeGenerator : LTOCodeGenerator {
       : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) {
     setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
 
+  // Reset the module first in case MergedModule is created in OwnedContext.
+  // Module must be destructed before its context gets destructed.
+  ~LibLTOCodeGenerator() { resetMergedModule(); }
+
   std::unique_ptr<MemoryBuffer> NativeObjectFile;
   std::unique_ptr<LLVMContext> OwnedContext;
 };