Fix the order of destructors in LibLTOCodeGenerator
authorSteven Wu <stevenwu@apple.com>
Wed, 9 Dec 2015 03:37:51 +0000 (03:37 +0000)
committerSteven Wu <stevenwu@apple.com>
Wed, 9 Dec 2015 03:37:51 +0000 (03:37 +0000)
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

llvm-svn: 255092

llvm/include/llvm/LTO/LTOCodeGenerator.h
llvm/tools/lto/lto.cpp

index 876defb..8a79e60 100644 (file)
@@ -148,6 +148,8 @@ struct LTOCodeGenerator {
 
   LLVMContext &getContext() { return Context; }
 
+  void resetMergedModule() { MergedModule.reset(); }
+
 private:
   void initializeLTOPasses();
 
index d13de57..d8f99c0 100644 (file)
@@ -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;
 };