Index: include/llvm/Linker/Linker.h =================================================================== --- include/llvm/Linker/Linker.h +++ include/llvm/Linker/Linker.h @@ -83,7 +83,7 @@ /// Returns true on error. bool linkInModule(Module *Src, unsigned Flags = Flags::None, const FunctionInfoIndex *Index = nullptr, - Function *FuncToImport = nullptr); + DenseSet *FuncToImport = nullptr); /// \brief Set the composite to the passed-in module. void setModule(Module *Dst); Index: lib/Linker/LinkModules.cpp =================================================================== --- lib/Linker/LinkModules.cpp +++ lib/Linker/LinkModules.cpp @@ -432,7 +432,7 @@ /// 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 @@ -449,7 +449,7 @@ 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(TypeMap, DstM, LazilyLinkGlobalValues, this), DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index), @@ -669,7 +669,7 @@ 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; @@ -1057,7 +1057,7 @@ 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; } @@ -1419,7 +1419,7 @@ } else { // If the GV is to be lazily linked, don't create it just yet. // The ValueMaterializerTy will deal with creating it if it's used. - if (!DGV && !shouldOverrideFromSrc() && SGV != ImportFunction && + if (!DGV && !shouldOverrideFromSrc() && ImportFunction->count(SGV) && (SGV->hasLocalLinkage() || SGV->hasLinkOnceLinkage() || SGV->hasAvailableExternallyLinkage())) { DoNotLinkFromSource.insert(SGV); @@ -2090,7 +2090,7 @@ 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(); Index: lib/Transforms/IPO/FunctionImport.cpp =================================================================== --- lib/Transforms/IPO/FunctionImport.cpp +++ lib/Transforms/IPO/FunctionImport.cpp @@ -70,7 +70,6 @@ for (auto &BB : F) { for (auto &I : BB) { if (isa(I)) { - DEBUG(dbgs() << "Found a call: '" << I << "'\n"); auto CalledFunction = cast(I).getCalledFunction(); if (CalledFunction && CalledFunction->hasName() && CalledFunction->isDeclaration()) @@ -80,10 +79,10 @@ } } - /// Second step: for every call to an external function, try to import it. - - // Linker that will be used for importing function - Linker L(&M, DiagnosticHandler); + /// Second step: for every call to an external function, add it to the import + /// list. + DenseMap> + ModuleToFunctionsToImportMap; /// Insert initial called function set in a worklist, so that we can add /// transively called functions when importing. @@ -151,15 +150,21 @@ continue; } - // Link in the specified function. - if (L.linkInModule(&Module, Linker::Flags::None, &Index, F)) - report_fatal_error("Function Import: link error"); + // TODO: add callees from the newly imported function to the worklist. + } - // TODO: Process the newly imported function and add callees to the - // worklist. + // Link in the specified functions. - Changed = true; + // Linker that will be used for importing function + Linker L(&M, DiagnosticHandler); + + for (auto It : ModuleToFunctionsToImportMap) { + auto *Module = It.first; + auto &FunctionsToImport = It.second; + if (L.linkInModule(Module, Linker::Flags::None, &Index, &FunctionsToImport)) + report_fatal_error("Function Import: link error"); } + return Changed; } Index: tools/llvm-link/llvm-link.cpp =================================================================== --- tools/llvm-link/llvm-link.cpp +++ tools/llvm-link/llvm-link.cpp @@ -198,7 +198,10 @@ } // Link in the specified function. - if (L.linkInModule(M.get(), Linker::Flags::None, Index.get(), F)) + DenseSet FunctionToImport; + FunctionToImport.insert(F); + if (L.linkInModule(M.get(), Linker::Flags::None, Index.get(), + &FunctionToImport)) return false; } return true;