Skip to content

CloneModule: Map global initializers after mapping the function #134082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions llvm/lib/Transforms/Utils/CloneModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,6 @@ std::unique_ptr<Module> llvm::CloneModule(
VMap[&I] = GI;
}

// Now that all of the things that global variable initializer can refer to
// have been created, loop through and copy the global variable referrers
// over... We also set the attributes on the global now.
//
for (const GlobalVariable &G : M.globals()) {
GlobalVariable *GV = cast<GlobalVariable>(VMap[&G]);

SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
G.getAllMetadata(MDs);
for (auto MD : MDs)
GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap));

if (G.isDeclaration())
continue;

if (!ShouldCloneDefinition(&G)) {
// Skip after setting the correct linkage for an external reference.
GV->setLinkage(GlobalValue::ExternalLinkage);
continue;
}
if (G.hasInitializer())
GV->setInitializer(MapValue(G.getInitializer(), VMap));

copyComdat(GV, &G);
}

// Similarly, copy over function bodies now...
//
for (const Function &I : M) {
Expand Down Expand Up @@ -212,6 +186,32 @@ std::unique_ptr<Module> llvm::CloneModule(
NewNMD->addOperand(MapMetadata(N, VMap));
}

// Now that all of the things that global variable initializer can refer to
// have been created, loop through and copy the global variable referrers
// over... We also set the attributes on the global now.
//
for (const GlobalVariable &G : M.globals()) {
GlobalVariable *GV = cast<GlobalVariable>(VMap[&G]);

SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
G.getAllMetadata(MDs);
for (auto MD : MDs)
GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap));

if (G.isDeclaration())
continue;

if (!ShouldCloneDefinition(&G)) {
// Skip after setting the correct linkage for an external reference.
GV->setLinkage(GlobalValue::ExternalLinkage);
continue;
}
if (G.hasInitializer())
GV->setInitializer(MapValue(G.getInitializer(), VMap));

copyComdat(GV, &G);
}

return New;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=functions --test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck --check-prefixes=RESULT --input-file=%t %s

; FIXME: This testcase exhibits nonsensical behavior. The first
; function has blockaddress references. When the second function is
; deleted, it causes the blockreferences from the first to be replaced
; with inttoptr.

; INTERESTING: @blockaddr.table.other

; RESULT: @blockaddr.table.other = private unnamed_addr constant [2 x ptr] [ptr inttoptr (i32 1 to ptr), ptr inttoptr (i32 1 to ptr)]
; RESULT: @blockaddr.table.other = private unnamed_addr constant [2 x ptr] [ptr blockaddress(@bar, %L1), ptr blockaddress(@bar, %L2)]


@blockaddr.table.other = private unnamed_addr constant [2 x ptr] [ptr blockaddress(@bar, %L1), ptr blockaddress(@bar, %L2)]

Expand Down