Skip to content

Commit a6e870d

Browse files
wenju-hearsenm
andauthored
[IRLinker] Don't add duplicate named MD node operand to dest module (#146020)
Fix llvm.ident exploding when linking many bitcode files in libclc. This should de-duplicate other named metadata as well, e.g. opencl.spir.version and opencl.ocl.version. This PR is a re-submit of https://reviews.llvm.org/D20582 (authored by Matt Arsenault) with update that only checks MD node pointer for duplication according to review comment in that PR. --------- Co-authored-by: Matt Arsenault <[email protected]>
1 parent d64e6b5 commit a6e870d

12 files changed

+88
-12
lines changed

llvm/lib/Linker/IRMover.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,11 @@ void IRLinker::linkNamedMDNodes() {
11331133

11341134
NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
11351135
// Add Src elements into Dest node.
1136-
for (const MDNode *Op : NMD.operands())
1137-
DestNMD->addOperand(Mapper.mapMDNode(*Op));
1136+
for (const MDNode *Op : NMD.operands()) {
1137+
MDNode *MD = Mapper.mapMDNode(*Op);
1138+
if (!is_contained(DestNMD->operands(), MD))
1139+
DestNMD->addOperand(MD);
1140+
}
11381141
}
11391142
}
11401143

llvm/test/Linker/Inputs/ident.b.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
!llvm.ident = !{!0}
2-
!0 = !{!"Compiler V3"}
1+
!llvm.ident = !{!0, !1}
2+
!0 = !{!"Compiler V2"}
3+
!1 = !{!"Compiler V3"}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
!llvm.module.flags = !{!0}
2+
!opencl.ocl.version = !{!1}
3+
!opencl.spir.version = !{!1}
4+
!opencl.used.extensions = !{!3, !4}
5+
!opencl.used.optional.core.features = !{!5}
6+
!llvm.ident = !{!2}
7+
8+
!0 = !{i32 1, !"wchar_size", i32 4}
9+
!1 = !{i32 3, i32 0}
10+
!2 = !{!"LLVM.org clang version 20.1.0"}
11+
!3 = !{!"cl_images", !"cl_khr_fp16"}
12+
!4 = !{!"cl_images", !"cl_doubles"}
13+
!5 = !{!"cl_images"}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
!llvm.module.flags = !{!0}
2+
!opencl.ocl.version = !{!1}
3+
!opencl.spir.version = !{!1}
4+
!opencl.used.extensions = !{!3, !4}
5+
!opencl.used.optional.core.features = !{!5}
6+
!llvm.ident = !{!2}
7+
8+
!0 = !{i32 1, !"wchar_size", i32 4}
9+
!1 = !{i32 3, i32 0}
10+
!2 = !{!"LLVM.org clang version 20.1.0"}
11+
!3 = !{!"cl_images", !"cl_khr_fp16"}
12+
!4 = !{!"cl_khr_fp16", !"cl_doubles"}
13+
!5 = !{!"cl_images"}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
!opencl.ocl.version = !{!0}
2+
!opencl.spir.version = !{!0}
3+
!llvm.ident = !{!1}
4+
5+
!0 = !{i32 2, i32 0}
6+
!1 = !{!"LLVM.org clang version 20.1.0"}

llvm/test/Linker/dicompositetype-unique.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
; Check that the type map will unique two DICompositeTypes.
1919

20-
; CHECK: !named = !{!0, !1, !2, !3, !0, !1, !2, !3}
21-
; NOMAP: !named = !{!0, !1, !2, !3, !0, !4, !5, !6}
20+
; CHECK: !named = !{!0, !1, !2, !3}
21+
; NOMAP: !named = !{!0, !1, !2, !3, !4, !5, !6}
2222
!named = !{!0, !1, !2, !3}
2323

2424
; Check both directions.

llvm/test/Linker/distinct.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
; Add an external reference to @global so that it gets linked in.
1010
@alias = alias i32, ptr @global
1111

12-
; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !0, !1, !2, !9, !10, !11, !12, !13, !14}
12+
; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14}
1313
!named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
1414

1515
; CHECK: !0 = !{}

llvm/test/Linker/ident.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s
22

3-
; Verify that multiple input llvm.ident metadata are linked together.
3+
; Verify that multiple input llvm.ident metadata are linked together, and
4+
; duplicate "Compiler V2" metadata from the two inputs are unified.
45

56
; CHECK-DAG: !llvm.ident = !{!0, !1, !2}
67
; CHECK-DAG: "Compiler V1"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: llvm-link %S/Inputs/opencl.md.a.ll %S/Inputs/opencl.md.c.ll -S | FileCheck %s
2+
3+
; OpenCL/SPIR version is different in input modules.
4+
; Verify that different OpenCL/SPIR version metadata nodes are kept in the
5+
; destination module.
6+
7+
; CHECK: !opencl.ocl.version = !{![[#MD0:]], ![[#MD1:]]}
8+
; CHECK: !opencl.spir.version = !{![[#MD0]], ![[#MD1]]}
9+
; CHECK: !opencl.used.extensions = !{![[#MD2:]], ![[#MD3:]]}
10+
; CHECK: !opencl.used.optional.core.features = !{![[#MD4:]]}
11+
; CHECK: !llvm.ident = !{![[#MD5:]]}
12+
; CHECK: !llvm.module.flags = !{![[#MD6:]]}
13+
14+
; CHECK: ![[#MD0]] = !{i32 3, i32 0}
15+
; CHECK: ![[#MD1]] = !{i32 2, i32 0}
16+
; CHECK: ![[#MD2]] = !{!"cl_images", !"cl_khr_fp16"}
17+
; CHECK: ![[#MD3]] = !{!"cl_images", !"cl_doubles"}
18+
; CHECK: ![[#MD4]] = !{!"cl_images"}
19+
; CHECK: ![[#MD5]] = !{!"LLVM.org clang version 20.1.0"}
20+
; CHECK: ![[#MD6]] = !{i32 1, !"wchar_size", i32 4}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: llvm-link %S/Inputs/opencl.md.a.ll %S/Inputs/opencl.md.b.ll -S | FileCheck %s
2+
3+
; Verify that duplicate named metadata node isn't added to the result module.
4+
5+
; CHECK: !opencl.ocl.version = !{![[#MD0:]]}
6+
; CHECK: !opencl.spir.version = !{![[#MD0]]}
7+
; CHECK: !opencl.used.extensions = !{![[#MD1:]], ![[#MD2:]], ![[#MD3:]]}
8+
; CHECK: !opencl.used.optional.core.features = !{![[#MD4:]]}
9+
; CHECK: !llvm.ident = !{![[#MD5:]]}
10+
; CHECK: !llvm.module.flags = !{![[#MD6:]]}
11+
12+
; CHECK: ![[#MD0]] = !{i32 3, i32 0}
13+
; CHECK: ![[#MD1]] = !{!"cl_images", !"cl_khr_fp16"}
14+
; CHECK: ![[#MD2]] = !{!"cl_images", !"cl_doubles"}
15+
; CHECK: ![[#MD3]] = !{!"cl_khr_fp16", !"cl_doubles"}
16+
; CHECK: ![[#MD4]] = !{!"cl_images"}
17+
; CHECK: ![[#MD5]] = !{!"LLVM.org clang version 20.1.0"}
18+
; CHECK: ![[#MD6]] = !{i32 1, !"wchar_size", i32 4}

0 commit comments

Comments
 (0)