[PATCH] D15101: Modify FunctionImport to take a callback to load modules

Mehdi AMINI via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 30 22:41:59 PST 2015


joker.eph created this revision.
joker.eph added a reviewer: tejohnson.
joker.eph added subscribers: llvm-commits, dexonsmith.

When linking static archive, there is no individual module files to
load. Instead they can be mmap'ed and could be initialized from a
buffer directly. The callback provide flexibility to override the
scheme for loading module from the summary.

http://reviews.llvm.org/D15101

Files:
  include/llvm/Transforms/IPO/FunctionImport.h
  lib/Transforms/IPO/FunctionImport.cpp

Index: lib/Transforms/IPO/FunctionImport.cpp
===================================================================
--- lib/Transforms/IPO/FunctionImport.cpp
+++ lib/Transforms/IPO/FunctionImport.cpp
@@ -46,7 +46,7 @@
 }
 
 // Get a Module for \p FileName from the cache, or load it lazily.
-Module &FunctionImporter::getOrLoadModule(StringRef FileName) {
+Module &ModuleLazyLoaderCache::operator()(StringRef FileName) {
   auto &Module = ModuleMap[FileName];
   if (!Module)
     Module = loadFile(FileName, Context);
@@ -58,7 +58,6 @@
 // The current implementation imports every called functions that exists in the
 // summaries index.
 bool FunctionImporter::importFunctions(Module &M) {
-  assert(&Context == &M.getContext());
 
   bool Changed = false;
 
@@ -127,7 +126,8 @@
                  << "\n");
 
     // Get the module for the import (potentially from the cache).
-    auto &Module = getOrLoadModule(FileName);
+    auto &Module = getLazyModule(FileName);
+    assert(&Module.getContext() == &M.getContext());
 
     // The function that we will import!
     GlobalValue *SGV = Module.getNamedValue(CalledFunctionName);
@@ -221,7 +221,10 @@
     }
 
     // Perform the import now.
-    FunctionImporter Importer(M.getContext(), *Index, diagnosticHandler);
+    ModuleLazyLoaderCache Loader(M.getContext());
+    FunctionImporter Importer(*Index, diagnosticHandler,
+                              [&](StringRef Name)
+                                  -> Module &{ return Loader(Name); });
     return Importer.importFunctions(M);
 
     return false;
Index: include/llvm/Transforms/IPO/FunctionImport.h
===================================================================
--- include/llvm/Transforms/IPO/FunctionImport.h
+++ include/llvm/Transforms/IPO/FunctionImport.h
@@ -18,30 +18,43 @@
 class Module;
 class FunctionInfoIndex;
 
-/// The function importer is automatically importing function from other modules
-/// based on the provided summary informations.
-class FunctionImporter {
+/// Helper to load on demand a Module from file and cache it for subsequent
+/// queries. It can be used with the FunctionImporter.
+class ModuleLazyLoaderCache {
+  /// The context that will be used for importing.
+  LLVMContext &Context;
 
   /// Cache of lazily loaded module for import.
   StringMap<std::unique_ptr<Module>> ModuleMap;
 
-  /// The context that will be used for importing.
-  LLVMContext &Context;
+public:
+  /// Create the loader, Module will be initialized in \p Context.
+  ModuleLazyLoaderCache(LLVMContext &Context) : Context(Context) {}
+
+  /// Retrieve a Module from the cache or lazily load it on demand.
+  Module &operator()(StringRef FileName);
+};
+
+/// The function importer is automatically importing function from other modules
+/// based on the provided summary informations.
+class FunctionImporter {
 
   /// The summaries index used to trigger importing.
   const FunctionInfoIndex &Index;
 
   /// Diagnostic will be sent to this handler.
   DiagnosticHandlerFunction DiagnosticHandler;
 
   /// Retrieve a Module from the cache or lazily load it on demand.
-  Module &getOrLoadModule(StringRef FileName);
+  std::function<Module &(StringRef FileName)> getLazyModule;
 
 public:
   /// Create a Function Importer.
-  FunctionImporter(LLVMContext &Context, const FunctionInfoIndex &Index,
-                   DiagnosticHandlerFunction DiagnosticHandler)
-      : Context(Context), Index(Index), DiagnosticHandler(DiagnosticHandler) {}
+  FunctionImporter(const FunctionInfoIndex &Index,
+                   DiagnosticHandlerFunction DiagnosticHandler,
+                   std::function<Module &(StringRef FileName)> ModuleLoader)
+      : Index(Index), DiagnosticHandler(DiagnosticHandler),
+        getLazyModule(ModuleLoader) {}
 
   /// Import functions in Module \p M based on the summary informations.
   bool importFunctions(Module &M);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15101.41462.patch
Type: text/x-patch
Size: 3889 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151201/d8463b11/attachment.bin>


More information about the llvm-commits mailing list