From 236695e70c41e3d649b2b8a4a72f58e4218f0aa9 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Mon, 14 Mar 2022 14:08:25 -0700 Subject: [PATCH] [IRLinker] make IRLinker::AddLazyFor optional (llvm::unique_function). NFC 2 of the 3 callsite of IRMover::move() pass empty lambda functions. Just make this parameter llvm::unique_function. Came about via discussion in D120781. Probably worth making this change regardless of the resolution of D120781. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D121630 --- llvm/include/llvm/Linker/IRMover.h | 7 +++++-- llvm/lib/LTO/LTO.cpp | 3 +-- llvm/lib/Linker/IRMover.cpp | 21 ++++++++++----------- llvm/lib/Linker/LinkModules.cpp | 12 +++++++----- llvm/lib/Transforms/IPO/FunctionImport.cpp | 7 +++---- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/llvm/include/llvm/Linker/IRMover.h b/llvm/include/llvm/Linker/IRMover.h index e5df83f..1e3c539 100644 --- 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 @@ public: 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 @@ public: /// 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 index 647b8b9..24ac938 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -885,8 +885,7 @@ Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod, 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 index b475ea8..a71a84c 100644 --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -381,7 +381,7 @@ class IRLinker { std::unique_ptr SrcM; /// See IRMover::move(). - std::function AddLazyFor; + IRMover::LazyCallback AddLazyFor; TypeMapTy TypeMap; GlobalValueMaterializer GValMaterializer; @@ -524,8 +524,7 @@ public: 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 @@ bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) { // 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 @@ IRMover::IRMover(Module &M) : Composite(M) { } } -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 index f9f51bf..436c3eb 100644 --- a/llvm/lib/Linker/LinkModules.cpp +++ b/llvm/lib/Linker/LinkModules.cpp @@ -573,11 +573,13 @@ bool ModuleLinker::run() { // 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 index 7f90c2c..326fe62 100644 --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -1331,10 +1331,9 @@ Expected FunctionImporter::importFunctions( << " 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))); -- 2.7.4