Skip to content

Commit be0ef4b

Browse files
committed
[IRLinker] Fix mapping of declaration metadata
Ensure metadata for declarations copied during materialization is properly mapped if declarations do not become definitions. Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D145318
1 parent eed31bb commit be0ef4b

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

llvm/include/llvm/Transforms/Utils/ValueMapper.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
112112
/// There are a number of top-level entry points:
113113
/// - \a mapValue() (and \a mapConstant());
114114
/// - \a mapMetadata() (and \a mapMDNode());
115-
/// - \a remapInstruction(); and
116-
/// - \a remapFunction().
115+
/// - \a remapInstruction();
116+
/// - \a remapFunction(); and
117+
/// - \a remapGlobalObjectMetadata().
117118
///
118119
/// The \a ValueMaterializer can be used as a callback, but cannot invoke any
119120
/// of these top-level functions recursively. Instead, callbacks should use
@@ -175,6 +176,7 @@ class ValueMapper {
175176

176177
void remapInstruction(Instruction &I);
177178
void remapFunction(Function &F);
179+
void remapGlobalObjectMetadata(GlobalObject &GO);
178180

179181
void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
180182
unsigned MappingContextID = 0);

llvm/lib/Linker/IRMover.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ class IRLinker {
409409
std::vector<GlobalValue *> Worklist;
410410
std::vector<std::pair<GlobalValue *, Value*>> RAUWWorklist;
411411

412+
/// Set of globals with eagerly copied metadata that may require remapping.
413+
/// This remapping is performed after metadata linking.
414+
DenseSet<GlobalObject *> UnmappedMetadata;
415+
412416
void maybeAdd(GlobalValue *GV) {
413417
if (ValuesToLink.insert(GV).second)
414418
Worklist.push_back(GV);
@@ -750,8 +754,11 @@ GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
750754

751755
if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
752756
// Metadata for global variables and function declarations is copied eagerly.
753-
if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
757+
if (isa<GlobalVariable>(SGV) || SGV->isDeclaration()) {
754758
NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);
759+
if (SGV->isDeclaration())
760+
UnmappedMetadata.insert(NewGO);
761+
}
755762
}
756763

757764
// Remove these copied constants in case this stays a declaration, since
@@ -1651,6 +1658,13 @@ Error IRLinker::run() {
16511658
// are properly remapped.
16521659
linkNamedMDNodes();
16531660

1661+
// Clean up any global objects with potentially unmapped metadata.
1662+
// Specifically declarations which did not become definitions.
1663+
for (GlobalObject *NGO : UnmappedMetadata) {
1664+
if (NGO->isDeclaration())
1665+
Mapper.remapGlobalObjectMetadata(*NGO);
1666+
}
1667+
16541668
if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) {
16551669
// Append the module inline asm string.
16561670
DstM.appendModuleInlineAsm(adjustInlineAsm(SrcM->getModuleInlineAsm(),

llvm/lib/Transforms/Utils/ValueMapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,10 @@ void ValueMapper::remapFunction(Function &F) {
11811181
FlushingMapper(pImpl)->remapFunction(F);
11821182
}
11831183

1184+
void ValueMapper::remapGlobalObjectMetadata(GlobalObject &GO) {
1185+
FlushingMapper(pImpl)->remapGlobalObjectMetadata(GO);
1186+
}
1187+
11841188
void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
11851189
Constant &Init,
11861190
unsigned MCID) {

llvm/test/Linker/Inputs/metadata-function.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ define void @b() !b !0 {
1010
unreachable
1111
}
1212

13+
%AltHandle = type { i8* }
14+
declare !types !1 %AltHandle @init.AltHandle()
15+
16+
define void @uses.AltHandle() {
17+
%.res = call %AltHandle @init.AltHandle()
18+
unreachable
19+
}
20+
1321
!0 = !{!"b"}
22+
!1 = !{%AltHandle undef}

llvm/test/Linker/metadata-function.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ define void @a() !a !0 {
2121
unreachable
2222
}
2323

24+
; CHECK-DAG: define %[[HandleType:[A-Za-z]+]] @init.Handle() {
25+
; CHECK-DAG: declare !types ![[C:[0-9]+]] %[[HandleType]] @init.AltHandle()
26+
; CHECK-DAG: define void @uses.AltHandle() {
27+
%Handle = type { i8* }
28+
define %Handle @init.Handle() {
29+
unreachable
30+
}
31+
2432
; CHECK-DAG: ![[A]] = !{!"a"}
2533
; CHECK-DAG: ![[B]] = !{!"b"}
34+
; CHECK-DAG: ![[C]] = !{%[[HandleType]] undef}
2635
!0 = !{!"a"}

0 commit comments

Comments
 (0)