#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/FunctionExtras.h"
#include <functional>
namespace llvm {
IRMover(Module &M);
typedef std::function<void(GlobalValue &)> ValueAdder;
+ using LazyCallback =
+ llvm::unique_function<void(GlobalValue &GV, ValueAdder Add)>;
/// Move in the provide values in \p ValuesToLink from \p Src.
///
/// 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<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
- std::function<void(GlobalValue &GV, ValueAdder Add)> AddLazyFor,
- bool IsPerformingImport);
+ LazyCallback AddLazyFor, bool IsPerformingImport);
Module &getModule() { return Composite; }
private:
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);
}
std::unique_ptr<Module> SrcM;
/// See IRMover::move().
- std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
+ IRMover::LazyCallback AddLazyFor;
TypeMapTy TypeMap;
GlobalValueMaterializer GValMaterializer;
IRLinker(Module &DstM, MDMapT &SharedMDs,
IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
ArrayRef<GlobalValue *> ValuesToLink,
- std::function<void(GlobalValue &, IRMover::ValueAdder)> 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),
// 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;
}
}
}
-Error IRMover::move(
- std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
- std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor,
- bool IsPerformingImport) {
+Error IRMover::move(std::unique_ptr<Module> Src,
+ ArrayRef<GlobalValue *> ValuesToLink,
+ LazyCallback AddLazyFor, bool IsPerformingImport) {
IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
std::move(Src), ValuesToLink, std::move(AddLazyFor),
IsPerformingImport);
// 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;
<< " 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)));