From ffe2e4aae021964c8d7f83a362c5b51d83e48d56 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Wed, 2 Dec 2015 04:34:28 +0000 Subject: [PATCH] Change ModuleLinker to take a set of GlobalValues to import instead of a single one For efficiency reason, when importing multiple functions for the same Module, we can avoid reparsing it every time. Differential Revision: http://reviews.llvm.org/D15102 From: Mehdi Amini llvm-svn: 254486 --- llvm/include/llvm/Linker/Linker.h | 2 +- llvm/lib/Linker/LinkModules.cpp | 10 +++++----- llvm/lib/Transforms/IPO/FunctionImport.cpp | 6 +++++- llvm/tools/llvm-link/llvm-link.cpp | 5 ++++- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/Linker/Linker.h b/llvm/include/llvm/Linker/Linker.h index 38fa5562f300..7c24eef74111 100644 --- a/llvm/include/llvm/Linker/Linker.h +++ b/llvm/include/llvm/Linker/Linker.h @@ -82,7 +82,7 @@ public: /// Returns true on error. bool linkInModule(Module &Src, unsigned Flags = Flags::None, const FunctionInfoIndex *Index = nullptr, - Function *FuncToImport = nullptr); + DenseSet *FuncToImport = nullptr); static bool linkModules(Module &Dest, Module &Src, DiagnosticHandlerFunction DiagnosticHandler, diff --git a/llvm/lib/Linker/LinkModules.cpp b/llvm/lib/Linker/LinkModules.cpp index 39ab5db8d6bd..fba231100ee3 100644 --- a/llvm/lib/Linker/LinkModules.cpp +++ b/llvm/lib/Linker/LinkModules.cpp @@ -406,7 +406,7 @@ class ModuleLinker { /// Function to import from source module, all other functions are /// imported as declarations instead of definitions. - Function *ImportFunction; + DenseSet *ImportFunction; /// Set to true if the given FunctionInfoIndex contains any functions /// from this source module, in which case we must conservatively assume @@ -425,7 +425,7 @@ public: ModuleLinker(Module &DstM, Linker::IdentifiedStructTypeSet &Set, Module &SrcM, DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags, const FunctionInfoIndex *Index = nullptr, - Function *FuncToImport = nullptr) + DenseSet *FuncToImport = nullptr) : DstM(DstM), SrcM(SrcM), TypeMap(Set), ValMaterializer(this), DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index), ImportFunction(FuncToImport) { @@ -632,7 +632,7 @@ bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) { return true; // Only import the function requested for importing. auto *SF = dyn_cast(SGV); - if (SF && SF == ImportFunction) + if (SF && ImportFunction->count(SF)) return true; // Otherwise no. return false; @@ -1058,7 +1058,7 @@ bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc, if (isa(&Src)) { // For functions, LinkFromSrc iff this is the function requested // for importing. For variables, decide below normally. - LinkFromSrc = (&Src == ImportFunction); + LinkFromSrc = ImportFunction->count(&Src); return false; } @@ -2033,7 +2033,7 @@ Linker::Linker(Module &M) bool Linker::linkInModule(Module &Src, unsigned Flags, const FunctionInfoIndex *Index, - Function *FuncToImport) { + DenseSet *FuncToImport) { ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, DiagnosticHandler, Flags, Index, FuncToImport); bool RetCode = TheLinker.run(); diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp index 92764c9e8c3f..8230d64026c2 100644 --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -182,7 +182,10 @@ bool FunctionImporter::importFunctions(Module &M) { } // Link in the specified function. - if (L.linkInModule(Module, Linker::Flags::None, &Index, F)) + DenseSet FunctionsToImport; + FunctionsToImport.insert(F); + if (L.linkInModule(Module, Linker::Flags::None, &Index, + &FunctionsToImport)) report_fatal_error("Function Import: link error"); // Process the newly imported function and add callees to the worklist. @@ -194,6 +197,7 @@ bool FunctionImporter::importFunctions(Module &M) { Changed = true; } + return Changed; } diff --git a/llvm/tools/llvm-link/llvm-link.cpp b/llvm/tools/llvm-link/llvm-link.cpp index 2b63649cec2e..9a373c25cc5c 100644 --- a/llvm/tools/llvm-link/llvm-link.cpp +++ b/llvm/tools/llvm-link/llvm-link.cpp @@ -198,7 +198,10 @@ static bool importFunctions(const char *argv0, LLVMContext &Context, } // Link in the specified function. - if (L.linkInModule(*M, Linker::Flags::None, Index.get(), F)) + DenseSet FunctionToImport; + FunctionToImport.insert(F); + if (L.linkInModule(*M, Linker::Flags::None, Index.get(), + &FunctionToImport)) return false; } return true;