forked from OSchip/llvm-project
Pass a std::unique_ptr to IRMover::move.
It was already the one "destroying" the source module, now the API reflects that. llvm-svn: 260989
This commit is contained in:
parent
aadc89c25d
commit
40358fb86f
|
@ -58,9 +58,9 @@ public:
|
|||
IRMover(Module &M);
|
||||
|
||||
typedef std::function<void(GlobalValue &)> ValueAdder;
|
||||
/// Move in the provide values. The source is destroyed.
|
||||
/// Move in the provide values.
|
||||
/// Returns true on error.
|
||||
bool move(Module &Src, ArrayRef<GlobalValue *> ValuesToLink,
|
||||
bool move(std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
|
||||
std::function<void(GlobalValue &GV, ValueAdder Add)> AddLazyFor,
|
||||
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr,
|
||||
bool IsMetadataLinkingPostpass = false);
|
||||
|
|
|
@ -54,12 +54,11 @@ public:
|
|||
static bool linkModules(Module &Dest, std::unique_ptr<Module> Src,
|
||||
unsigned Flags = Flags::None);
|
||||
|
||||
/// \brief Link metadata from \p Src into the composite. The source is
|
||||
/// destroyed.
|
||||
/// \brief Link metadata from \p Src into the composite.
|
||||
///
|
||||
/// The \p ValIDToTempMDMap sound have been populated earlier during function
|
||||
/// importing from \p Src.
|
||||
bool linkInMetadata(Module &Src,
|
||||
bool linkInMetadata(std::unique_ptr<Module> Src,
|
||||
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap);
|
||||
};
|
||||
|
||||
|
|
|
@ -374,7 +374,7 @@ public:
|
|||
/// from SrcM to DstM.
|
||||
class IRLinker {
|
||||
Module &DstM;
|
||||
Module &SrcM;
|
||||
std::unique_ptr<Module> SrcM;
|
||||
|
||||
std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
|
||||
|
||||
|
@ -433,13 +433,13 @@ class IRLinker {
|
|||
|
||||
/// Helper method for setting a message and returning an error code.
|
||||
bool emitError(const Twine &Message) {
|
||||
SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
|
||||
SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
|
||||
HasError = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void emitWarning(const Twine &Message) {
|
||||
SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
|
||||
SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
|
||||
}
|
||||
|
||||
/// Check whether we should be linking metadata from the source module.
|
||||
|
@ -512,12 +512,12 @@ class IRLinker {
|
|||
void stripNullSubprograms(DICompileUnit *CU);
|
||||
|
||||
public:
|
||||
IRLinker(Module &DstM, IRMover::IdentifiedStructTypeSet &Set, Module &SrcM,
|
||||
ArrayRef<GlobalValue *> ValuesToLink,
|
||||
IRLinker(Module &DstM, IRMover::IdentifiedStructTypeSet &Set,
|
||||
std::unique_ptr<Module> SrcM, ArrayRef<GlobalValue *> ValuesToLink,
|
||||
std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor,
|
||||
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr,
|
||||
bool IsMetadataLinkingPostpass = false)
|
||||
: DstM(DstM), SrcM(SrcM), AddLazyFor(AddLazyFor), TypeMap(Set),
|
||||
: DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(AddLazyFor), TypeMap(Set),
|
||||
GValMaterializer(this), LValMaterializer(this),
|
||||
IsMetadataLinkingPostpass(IsMetadataLinkingPostpass),
|
||||
ValIDToTempMDMap(ValIDToTempMDMap) {
|
||||
|
@ -796,7 +796,7 @@ GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
|
|||
/// types 'Foo' but one got renamed when the module was loaded into the same
|
||||
/// LLVMContext.
|
||||
void IRLinker::computeTypeMapping() {
|
||||
for (GlobalValue &SGV : SrcM.globals()) {
|
||||
for (GlobalValue &SGV : SrcM->globals()) {
|
||||
GlobalValue *DGV = getLinkedToGlobal(&SGV);
|
||||
if (!DGV)
|
||||
continue;
|
||||
|
@ -812,11 +812,11 @@ void IRLinker::computeTypeMapping() {
|
|||
TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
|
||||
}
|
||||
|
||||
for (GlobalValue &SGV : SrcM)
|
||||
for (GlobalValue &SGV : *SrcM)
|
||||
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
|
||||
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
|
||||
|
||||
for (GlobalValue &SGV : SrcM.aliases())
|
||||
for (GlobalValue &SGV : SrcM->aliases())
|
||||
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
|
||||
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
|
||||
|
||||
|
@ -824,7 +824,7 @@ void IRLinker::computeTypeMapping() {
|
|||
// At this point, the destination module may have a type "%foo = { i32 }" for
|
||||
// example. When the source module got loaded into the same LLVMContext, if
|
||||
// it had the same type, it would have been renamed to "%foo.42 = { i32 }".
|
||||
std::vector<StructType *> Types = SrcM.getIdentifiedStructTypes();
|
||||
std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
|
||||
for (StructType *ST : Types) {
|
||||
if (!ST->hasName())
|
||||
continue;
|
||||
|
@ -1126,8 +1126,8 @@ bool IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
|
|||
// a function and before remapping metadata on instructions below
|
||||
// in RemapInstruction, as the saved mapping is used to handle
|
||||
// the temporary metadata hanging off instructions.
|
||||
SrcM.getMaterializer()->saveMetadataList(MetadataToIDs,
|
||||
/* OnlyTempMD = */ true);
|
||||
SrcM->getMaterializer()->saveMetadataList(MetadataToIDs,
|
||||
/* OnlyTempMD = */ true);
|
||||
|
||||
// Link in the prefix data.
|
||||
if (Src.hasPrefixData())
|
||||
|
@ -1218,7 +1218,7 @@ void IRLinker::findReachedSubprograms(
|
|||
void IRLinker::findNeededSubprograms() {
|
||||
// Track unneeded nodes to make it simpler to handle the case
|
||||
// where we are checking if an already-mapped SP is needed.
|
||||
NamedMDNode *CompileUnits = SrcM.getNamedMetadata("llvm.dbg.cu");
|
||||
NamedMDNode *CompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");
|
||||
if (!CompileUnits)
|
||||
return;
|
||||
for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
|
||||
|
@ -1290,8 +1290,8 @@ void IRLinker::stripNullSubprograms(DICompileUnit *CU) {
|
|||
/// Insert all of the named MDNodes in Src into the Dest module.
|
||||
void IRLinker::linkNamedMDNodes() {
|
||||
findNeededSubprograms();
|
||||
const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
|
||||
for (const NamedMDNode &NMD : SrcM.named_metadata()) {
|
||||
const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
|
||||
for (const NamedMDNode &NMD : SrcM->named_metadata()) {
|
||||
// Don't link module flags here. Do them separately.
|
||||
if (&NMD == SrcModFlags)
|
||||
continue;
|
||||
|
@ -1314,7 +1314,7 @@ void IRLinker::linkNamedMDNodes() {
|
|||
/// Merge the linker flags in Src into the Dest module.
|
||||
bool IRLinker::linkModuleFlagsMetadata() {
|
||||
// If the source module has no module flags, we are done.
|
||||
const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
|
||||
const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
|
||||
if (!SrcModFlags)
|
||||
return false;
|
||||
|
||||
|
@ -1495,37 +1495,38 @@ bool IRLinker::run() {
|
|||
// Inherit the target data from the source module if the destination module
|
||||
// doesn't have one already.
|
||||
if (DstM.getDataLayout().isDefault())
|
||||
DstM.setDataLayout(SrcM.getDataLayout());
|
||||
DstM.setDataLayout(SrcM->getDataLayout());
|
||||
|
||||
if (SrcM.getDataLayout() != DstM.getDataLayout()) {
|
||||
if (SrcM->getDataLayout() != DstM.getDataLayout()) {
|
||||
emitWarning("Linking two modules of different data layouts: '" +
|
||||
SrcM.getModuleIdentifier() + "' is '" +
|
||||
SrcM.getDataLayoutStr() + "' whereas '" +
|
||||
SrcM->getModuleIdentifier() + "' is '" +
|
||||
SrcM->getDataLayoutStr() + "' whereas '" +
|
||||
DstM.getModuleIdentifier() + "' is '" +
|
||||
DstM.getDataLayoutStr() + "'\n");
|
||||
}
|
||||
|
||||
// Copy the target triple from the source to dest if the dest's is empty.
|
||||
if (DstM.getTargetTriple().empty() && !SrcM.getTargetTriple().empty())
|
||||
DstM.setTargetTriple(SrcM.getTargetTriple());
|
||||
if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
|
||||
DstM.setTargetTriple(SrcM->getTargetTriple());
|
||||
|
||||
Triple SrcTriple(SrcM.getTargetTriple()), DstTriple(DstM.getTargetTriple());
|
||||
Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
|
||||
|
||||
if (!SrcM.getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
|
||||
if (!SrcM->getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
|
||||
emitWarning("Linking two modules of different target triples: " +
|
||||
SrcM.getModuleIdentifier() + "' is '" + SrcM.getTargetTriple() +
|
||||
"' whereas '" + DstM.getModuleIdentifier() + "' is '" +
|
||||
DstM.getTargetTriple() + "'\n");
|
||||
SrcM->getModuleIdentifier() + "' is '" +
|
||||
SrcM->getTargetTriple() + "' whereas '" +
|
||||
DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +
|
||||
"'\n");
|
||||
|
||||
DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple));
|
||||
|
||||
// Append the module inline asm string.
|
||||
if (!SrcM.getModuleInlineAsm().empty()) {
|
||||
if (!SrcM->getModuleInlineAsm().empty()) {
|
||||
if (DstM.getModuleInlineAsm().empty())
|
||||
DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm());
|
||||
DstM.setModuleInlineAsm(SrcM->getModuleInlineAsm());
|
||||
else
|
||||
DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
|
||||
SrcM.getModuleInlineAsm());
|
||||
SrcM->getModuleInlineAsm());
|
||||
}
|
||||
|
||||
// Loop over all of the linked values to compute type mappings.
|
||||
|
@ -1560,10 +1561,10 @@ bool IRLinker::run() {
|
|||
// we don't actually link anything from source.
|
||||
if (IsMetadataLinkingPostpass) {
|
||||
// Ensure metadata materialized
|
||||
if (SrcM.getMaterializer()->materializeMetadata())
|
||||
if (SrcM->getMaterializer()->materializeMetadata())
|
||||
return true;
|
||||
SrcM.getMaterializer()->saveMetadataList(MetadataToIDs,
|
||||
/* OnlyTempMD = */ false);
|
||||
SrcM->getMaterializer()->saveMetadataList(MetadataToIDs,
|
||||
/* OnlyTempMD = */ false);
|
||||
}
|
||||
|
||||
linkNamedMDNodes();
|
||||
|
@ -1694,12 +1695,13 @@ IRMover::IRMover(Module &M) : Composite(M) {
|
|||
}
|
||||
|
||||
bool IRMover::move(
|
||||
Module &Src, ArrayRef<GlobalValue *> ValuesToLink,
|
||||
std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
|
||||
std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor,
|
||||
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap,
|
||||
bool IsMetadataLinkingPostpass) {
|
||||
IRLinker TheIRLinker(Composite, IdentifiedStructTypes, Src, ValuesToLink,
|
||||
AddLazyFor, ValIDToTempMDMap, IsMetadataLinkingPostpass);
|
||||
IRLinker TheIRLinker(Composite, IdentifiedStructTypes, std::move(Src),
|
||||
ValuesToLink, AddLazyFor, ValIDToTempMDMap,
|
||||
IsMetadataLinkingPostpass);
|
||||
bool RetCode = TheIRLinker.run();
|
||||
Composite.dropTriviallyDeadConstantArrays();
|
||||
return RetCode;
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace {
|
|||
/// entrypoint for this file.
|
||||
class ModuleLinker {
|
||||
IRMover &Mover;
|
||||
Module &SrcM;
|
||||
std::unique_ptr<Module> SrcM;
|
||||
|
||||
SetVector<GlobalValue *> ValuesToLink;
|
||||
StringSet<> Internalize;
|
||||
|
@ -71,7 +71,7 @@ class ModuleLinker {
|
|||
|
||||
/// Should we have mover and linker error diag info?
|
||||
bool emitError(const Twine &Message) {
|
||||
SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
|
||||
SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -123,11 +123,11 @@ class ModuleLinker {
|
|||
bool doImportAsDefinition(const GlobalValue *SGV);
|
||||
|
||||
public:
|
||||
ModuleLinker(IRMover &Mover, Module &SrcM, unsigned Flags,
|
||||
ModuleLinker(IRMover &Mover, std::unique_ptr<Module> SrcM, unsigned Flags,
|
||||
const FunctionInfoIndex *Index = nullptr,
|
||||
DenseSet<const GlobalValue *> *FunctionsToImport = nullptr,
|
||||
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr)
|
||||
: Mover(Mover), SrcM(SrcM), Flags(Flags), ImportIndex(Index),
|
||||
: Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags), ImportIndex(Index),
|
||||
FunctionsToImport(FunctionsToImport),
|
||||
ValIDToTempMDMap(ValIDToTempMDMap) {
|
||||
assert((ImportIndex || !FunctionsToImport) &&
|
||||
|
@ -137,7 +137,7 @@ public:
|
|||
// backend compilation, and we need to see if it has functions that
|
||||
// may be exported to another backend compilation.
|
||||
if (ImportIndex && !FunctionsToImport)
|
||||
HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
|
||||
HasExportedFunctions = ImportIndex->hasExportedFunctions(*this->SrcM);
|
||||
}
|
||||
|
||||
bool run();
|
||||
|
@ -221,11 +221,11 @@ bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
|
|||
const GlobalVariable *DstGV;
|
||||
const GlobalVariable *SrcGV;
|
||||
if (getComdatLeader(DstM, ComdatName, DstGV) ||
|
||||
getComdatLeader(SrcM, ComdatName, SrcGV))
|
||||
getComdatLeader(*SrcM, ComdatName, SrcGV))
|
||||
return true;
|
||||
|
||||
const DataLayout &DstDL = DstM.getDataLayout();
|
||||
const DataLayout &SrcDL = SrcM.getDataLayout();
|
||||
const DataLayout &SrcDL = SrcM->getDataLayout();
|
||||
uint64_t DstSize = DstDL.getTypeAllocSize(DstGV->getValueType());
|
||||
uint64_t SrcSize = SrcDL.getTypeAllocSize(SrcGV->getValueType());
|
||||
if (Result == Comdat::SelectionKind::ExactMatch) {
|
||||
|
@ -471,7 +471,7 @@ void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) {
|
|||
}
|
||||
|
||||
bool ModuleLinker::run() {
|
||||
for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
|
||||
for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
|
||||
const Comdat &C = SMEC.getValue();
|
||||
if (ComdatsChosen.count(&C))
|
||||
continue;
|
||||
|
@ -482,34 +482,34 @@ bool ModuleLinker::run() {
|
|||
ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
|
||||
}
|
||||
|
||||
for (GlobalVariable &GV : SrcM.globals())
|
||||
for (GlobalVariable &GV : SrcM->globals())
|
||||
if (const Comdat *SC = GV.getComdat())
|
||||
ComdatMembers[SC].push_back(&GV);
|
||||
|
||||
for (Function &SF : SrcM)
|
||||
for (Function &SF : *SrcM)
|
||||
if (const Comdat *SC = SF.getComdat())
|
||||
ComdatMembers[SC].push_back(&SF);
|
||||
|
||||
for (GlobalAlias &GA : SrcM.aliases())
|
||||
for (GlobalAlias &GA : SrcM->aliases())
|
||||
if (const Comdat *SC = GA.getComdat())
|
||||
ComdatMembers[SC].push_back(&GA);
|
||||
|
||||
// Insert all of the globals in src into the DstM module... without linking
|
||||
// initializers (which could refer to functions not yet mapped over).
|
||||
for (GlobalVariable &GV : SrcM.globals())
|
||||
for (GlobalVariable &GV : SrcM->globals())
|
||||
if (linkIfNeeded(GV))
|
||||
return true;
|
||||
|
||||
for (Function &SF : SrcM)
|
||||
for (Function &SF : *SrcM)
|
||||
if (linkIfNeeded(SF))
|
||||
return true;
|
||||
|
||||
for (GlobalAlias &GA : SrcM.aliases())
|
||||
for (GlobalAlias &GA : SrcM->aliases())
|
||||
if (linkIfNeeded(GA))
|
||||
return true;
|
||||
|
||||
if (ImportIndex) {
|
||||
FunctionImportGlobalProcessing ThinLTOProcessing(SrcM, ImportIndex,
|
||||
FunctionImportGlobalProcessing ThinLTOProcessing(*SrcM, ImportIndex,
|
||||
FunctionsToImport);
|
||||
if (ThinLTOProcessing.run())
|
||||
return true;
|
||||
|
@ -531,7 +531,7 @@ bool ModuleLinker::run() {
|
|||
Internalize.insert(GV->getName());
|
||||
}
|
||||
|
||||
if (Mover.move(SrcM, ValuesToLink.getArrayRef(),
|
||||
if (Mover.move(std::move(SrcM), ValuesToLink.getArrayRef(),
|
||||
[this](GlobalValue &GV, IRMover::ValueAdder Add) {
|
||||
addLazyFor(GV, Add);
|
||||
},
|
||||
|
@ -552,16 +552,16 @@ bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags,
|
|||
const FunctionInfoIndex *Index,
|
||||
DenseSet<const GlobalValue *> *FunctionsToImport,
|
||||
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
|
||||
ModuleLinker ModLinker(Mover, *Src, Flags, Index, FunctionsToImport,
|
||||
ModuleLinker ModLinker(Mover, std::move(Src), Flags, Index, FunctionsToImport,
|
||||
ValIDToTempMDMap);
|
||||
return ModLinker.run();
|
||||
}
|
||||
|
||||
bool Linker::linkInMetadata(Module &Src,
|
||||
bool Linker::linkInMetadata(std::unique_ptr<Module> Src,
|
||||
DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
|
||||
SetVector<GlobalValue *> ValuesToLink;
|
||||
if (Mover.move(
|
||||
Src, ValuesToLink.getArrayRef(),
|
||||
std::move(Src), ValuesToLink.getArrayRef(),
|
||||
[this](GlobalValue &GV, IRMover::ValueAdder Add) { assert(false); },
|
||||
ValIDToTempMDMap, true))
|
||||
return true;
|
||||
|
|
|
@ -906,7 +906,7 @@ static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
|
|||
else if (M->getTargetTriple().empty())
|
||||
M->setTargetTriple(DefaultTriple);
|
||||
|
||||
if (L.move(*M, Keep, [](GlobalValue &, IRMover::ValueAdder) {}))
|
||||
if (L.move(std::move(M), Keep, [](GlobalValue &, IRMover::ValueAdder) {}))
|
||||
message(LDPL_FATAL, "Failed to link module");
|
||||
}
|
||||
|
||||
|
|
|
@ -236,7 +236,7 @@ static bool importFunctions(const char *argv0, LLVMContext &Context,
|
|||
}
|
||||
|
||||
// Link in all necessary metadata from this module.
|
||||
if (L.linkInMetadata(*M, SME.getValue().get()))
|
||||
if (L.linkInMetadata(std::move(M), SME.getValue().get()))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue