diff --git a/llvm/include/llvm/Linker/IRMover.h b/llvm/include/llvm/Linker/IRMover.h --- a/llvm/include/llvm/Linker/IRMover.h +++ b/llvm/include/llvm/Linker/IRMover.h @@ -11,6 +11,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/FunctionExtras.h" #include namespace llvm { @@ -62,6 +63,8 @@ IRMover(Module &M); typedef std::function ValueAdder; + using LazyCallback = + llvm::unique_function; /// Move in the provide values in \p ValuesToLink from \p Src. /// @@ -70,11 +73,11 @@ /// not present in ValuesToLink. The GlobalValue and a ValueAdder callback /// are passed as an argument, and the callback is expected to be called /// if the GlobalValue needs to be added to the \p ValuesToLink and linked. + /// Pass nullptr if there's no work to be done in such cases. /// - \p IsPerformingImport is true when this IR link is to perform ThinLTO /// function importing from Src. Error move(std::unique_ptr Src, ArrayRef ValuesToLink, - std::function AddLazyFor, - bool IsPerformingImport); + LazyCallback AddLazyFor, bool IsPerformingImport); Module &getModule() { return Composite; } private: diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -885,8 +885,7 @@ Keep.push_back(GV); } - return RegularLTO.Mover->move(std::move(Mod.M), Keep, - [](GlobalValue &, IRMover::ValueAdder) {}, + return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr, /* IsPerformingImport */ false); } diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -381,7 +381,7 @@ std::unique_ptr SrcM; /// See IRMover::move(). - std::function AddLazyFor; + IRMover::LazyCallback AddLazyFor; TypeMapTy TypeMap; GlobalValueMaterializer GValMaterializer; @@ -524,8 +524,7 @@ IRLinker(Module &DstM, MDMapT &SharedMDs, IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr SrcM, ArrayRef ValuesToLink, - std::function AddLazyFor, - bool IsPerformingImport) + IRMover::LazyCallback AddLazyFor, bool IsPerformingImport) : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)), TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this), SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport), @@ -987,10 +986,11 @@ // Callback to the client to give a chance to lazily add the Global to the // list of value to link. bool LazilyAdded = false; - AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) { - maybeAdd(&GV); - LazilyAdded = true; - }); + if (AddLazyFor) + AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) { + maybeAdd(&GV); + LazilyAdded = true; + }); return LazilyAdded; } @@ -1673,10 +1673,9 @@ } } -Error IRMover::move( - std::unique_ptr Src, ArrayRef ValuesToLink, - std::function AddLazyFor, - bool IsPerformingImport) { +Error IRMover::move(std::unique_ptr Src, + ArrayRef ValuesToLink, + LazyCallback AddLazyFor, bool IsPerformingImport) { IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes, std::move(Src), ValuesToLink, std::move(AddLazyFor), IsPerformingImport); diff --git a/llvm/lib/Linker/LinkModules.cpp b/llvm/lib/Linker/LinkModules.cpp --- a/llvm/lib/Linker/LinkModules.cpp +++ b/llvm/lib/Linker/LinkModules.cpp @@ -573,11 +573,13 @@ // FIXME: Propagate Errors through to the caller instead of emitting // diagnostics. bool HasErrors = false; - if (Error E = Mover.move(std::move(SrcM), ValuesToLink.getArrayRef(), - [this](GlobalValue &GV, IRMover::ValueAdder Add) { - addLazyFor(GV, Add); - }, - /* IsPerformingImport */ false)) { + if (Error E = + Mover.move(std::move(SrcM), ValuesToLink.getArrayRef(), + IRMover::LazyCallback( + [this](GlobalValue &GV, IRMover::ValueAdder Add) { + addLazyFor(GV, Add); + }), + /* IsPerformingImport */ false)) { handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { DstM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, EIB.message())); HasErrors = true; diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -1331,10 +1331,9 @@ << " from " << SrcModule->getSourceFileName() << "\n"; } - if (Error Err = Mover.move( - std::move(SrcModule), GlobalsToImport.getArrayRef(), - [](GlobalValue &, IRMover::ValueAdder) {}, - /*IsPerformingImport=*/true)) + if (Error Err = Mover.move(std::move(SrcModule), + GlobalsToImport.getArrayRef(), nullptr, + /*IsPerformingImport=*/true)) report_fatal_error(Twine("Function Import: link error: ") + toString(std::move(Err)));