[llvm] r270509 - [ThinLTO] Refactor module loader handling into new LTO file (NFC)

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Mon May 23 15:54:07 PDT 2016


Author: tejohnson
Date: Mon May 23 17:54:06 2016
New Revision: 270509

URL: http://llvm.org/viewvc/llvm-project?rev=270509&view=rev
Log:
[ThinLTO] Refactor module loader handling into new LTO file (NFC)

Moved the ModuleLoader and supporting helper loadModuleFromBuffer out of
ThinLTOCodeGenerator and into new LTO.h/LTO.cpp files. This is in
preparation for a patch that will utilize these in the gold-plugin.

Note that there are some other pending patches (D20268 and D20290) that
also plan to refactor common interfaces and functionality into this same
pair of new files.

Added:
    llvm/trunk/include/llvm/LTO/LTO.h
    llvm/trunk/lib/LTO/LTO.cpp
Modified:
    llvm/trunk/lib/LTO/CMakeLists.txt
    llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp

Added: llvm/trunk/include/llvm/LTO/LTO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/LTO.h?rev=270509&view=auto
==============================================================================
--- llvm/trunk/include/llvm/LTO/LTO.h (added)
+++ llvm/trunk/include/llvm/LTO/LTO.h Mon May 23 17:54:06 2016
@@ -0,0 +1,52 @@
+//===-LTO.h - LLVM Link Time Optimizer ------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares functions and classes used to support LTO. It is intended
+// to be used both by LTO classes as well as by clients (gold-plugin) that
+// don't utilize the LTO code generator interfaces.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LTO_LTO_H
+#define LLVM_LTO_LTO_H
+
+#include "llvm/ADT/StringMap.h"
+
+namespace llvm {
+
+class LLVMContext;
+class MemoryBufferRef;
+class Module;
+
+/// Helper to load a module from bitcode.
+std::unique_ptr<Module> loadModuleFromBuffer(const MemoryBufferRef &Buffer,
+                                             LLVMContext &Context, bool Lazy);
+
+/// Provide a "loader" for the FunctionImporter to access function from other
+/// modules.
+class ModuleLoader {
+  /// The context that will be used for importing.
+  LLVMContext &Context;
+
+  /// Map from Module identifier to MemoryBuffer. Used by clients like the
+  /// FunctionImported to request loading a Module.
+  StringMap<MemoryBufferRef> &ModuleMap;
+
+public:
+  ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap)
+      : Context(Context), ModuleMap(ModuleMap) {}
+
+  /// Load a module on demand.
+  std::unique_ptr<Module> operator()(StringRef Identifier) {
+    return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true);
+  }
+};
+}
+
+#endif

Modified: llvm/trunk/lib/LTO/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/CMakeLists.txt?rev=270509&r1=270508&r2=270509&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/CMakeLists.txt (original)
+++ llvm/trunk/lib/LTO/CMakeLists.txt Mon May 23 17:54:06 2016
@@ -48,6 +48,7 @@ endif()
 
 
 add_llvm_library(LLVMLTO
+  LTO.cpp
   LTOModule.cpp
   LTOCodeGenerator.cpp
   UpdateCompilerUsed.cpp

Added: llvm/trunk/lib/LTO/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTO.cpp?rev=270509&view=auto
==============================================================================
--- llvm/trunk/lib/LTO/LTO.cpp (added)
+++ llvm/trunk/lib/LTO/LTO.cpp Mon May 23 17:54:06 2016
@@ -0,0 +1,42 @@
+//===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements functions and classes used to support LTO.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LTO/LTO.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+
+// Simple helper to load a module from bitcode
+std::unique_ptr<Module> loadModuleFromBuffer(const MemoryBufferRef &Buffer,
+                                             LLVMContext &Context, bool Lazy) {
+  SMDiagnostic Err;
+  ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
+  if (Lazy) {
+    ModuleOrErr =
+        getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
+                             /* ShouldLazyLoadMetadata */ Lazy);
+  } else {
+    ModuleOrErr = parseBitcodeFile(Buffer, Context);
+  }
+  if (std::error_code EC = ModuleOrErr.getError()) {
+    Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
+                       EC.message());
+    Err.print("ThinLTO", errs());
+    report_fatal_error("Can't load module, abort.");
+  }
+  return std::move(ModuleOrErr.get());
+}
+}

Modified: llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp?rev=270509&r1=270508&r2=270509&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp (original)
+++ llvm/trunk/lib/LTO/ThinLTOCodeGenerator.cpp Mon May 23 17:54:06 2016
@@ -32,6 +32,7 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Mangler.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
 #include "llvm/Linker/Linker.h"
 #include "llvm/MC/SubtargetFeature.h"
 #include "llvm/Object/IRObjectFile.h"
@@ -41,7 +42,6 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/SHA1.h"
-#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Target/TargetMachine.h"
@@ -74,28 +74,6 @@ static void diagnosticHandler(const Diag
   errs() << '\n';
 }
 
-// Simple helper to load a module from bitcode
-static std::unique_ptr<Module>
-loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
-                     bool Lazy) {
-  SMDiagnostic Err;
-  ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
-  if (Lazy) {
-    ModuleOrErr =
-        getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
-                             /* ShouldLazyLoadMetadata */ Lazy);
-  } else {
-    ModuleOrErr = parseBitcodeFile(Buffer, Context);
-  }
-  if (std::error_code EC = ModuleOrErr.getError()) {
-    Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
-                       EC.message());
-    Err.print("ThinLTO", errs());
-    report_fatal_error("Can't load module, abort.");
-  }
-  return std::move(ModuleOrErr.get());
-}
-
 // Simple helper to save temporary files for debug.
 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
                             unsigned count, StringRef Suffix) {
@@ -260,26 +238,6 @@ generateModuleMap(const std::vector<Memo
   return ModuleMap;
 }
 
-/// Provide a "loader" for the FunctionImporter to access function from other
-/// modules.
-class ModuleLoader {
-  /// The context that will be used for importing.
-  LLVMContext &Context;
-
-  /// Map from Module identifier to MemoryBuffer. Used by clients like the
-  /// FunctionImported to request loading a Module.
-  StringMap<MemoryBufferRef> &ModuleMap;
-
-public:
-  ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap)
-      : Context(Context), ModuleMap(ModuleMap) {}
-
-  /// Load a module on demand.
-  std::unique_ptr<Module> operator()(StringRef Identifier) {
-    return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true);
-  }
-};
-
 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
   if (renameModuleForThinLTO(TheModule, Index))
     report_fatal_error("renameModuleForThinLTO failed");




More information about the llvm-commits mailing list