Skip to content

Commit 74bece8

Browse files
committed
[WPD][ELF] Allow whole program devirtualization for version script localized symbols
A `local:` version node in a version script can change the effective symbol binding to STB_LOCAL. The linker needs to communicate the fact to enable WPD (otherwise LTO does not know that the `!vcall_visibility` metadata has effectively changed from VCallVisibilityPublic to VCallVisibilityLinkageUnit). Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D98220
1 parent 46bf25a commit 74bece8

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

lld/ELF/LTO.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ void BitcodeCompiler::add(BitcodeFile &f) {
249249
usedStartStop.count(objSym.getSectionName());
250250
// Identify symbols exported dynamically, and that therefore could be
251251
// referenced by a shared library not visible to the linker.
252-
r.ExportDynamic = sym->isExportDynamic(sym->kind(), sym->visibility) ||
253-
sym->exportDynamic || sym->inDynamicList;
252+
r.ExportDynamic = sym->computeBinding() != STB_LOCAL &&
253+
(sym->isExportDynamic(sym->kind(), sym->visibility) ||
254+
sym->exportDynamic || sym->inDynamicList);
254255
const auto *dr = dyn_cast<Defined>(sym);
255256
r.FinalDefinitionInLinkageUnit =
256257
(isExec || sym->visibility != STV_DEFAULT) && dr &&
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; REQUIRES: x86
2+
;; If the vtable symbols are localized by a version script, devirtualization
3+
;; can still happen.
4+
5+
; RUN: opt --thinlto-bc -o %t1.o %s
6+
; RUN: opt --thinlto-bc -o %t2.o %S/Inputs/devirt_vcall_vis_shared_def.ll
7+
; RUN: echo '{ global: _start; local: *; };' > %t.ver
8+
9+
; RUN: ld.lld %t1.o %t2.o -o %t.out --save-temps --lto-whole-program-visibility -shared \
10+
; RUN: -mllvm -pass-remarks=. 2>&1 | count 0
11+
12+
; RUN: ld.lld %t1.o %t2.o -o %t.out --save-temps --lto-whole-program-visibility -shared \
13+
; RUN: --version-script=%t.ver -mllvm -pass-remarks=. 2>&1 | FileCheck %s --check-prefix=REMARK
14+
; RUN: llvm-dis < %t1.o.4.opt.bc | FileCheck %s --check-prefix=CHECK-IR
15+
16+
; REMARK: single-impl: devirtualized a call to _ZN1A1nEi
17+
18+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
19+
target triple = "x86_64-grtev4-linux-gnu"
20+
21+
%struct.A = type { i32 (...)** }
22+
%struct.B = type { %struct.A }
23+
24+
@_ZTV1A = available_externally unnamed_addr constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !vcall_visibility !2
25+
@_ZTV1B = linkonce_odr unnamed_addr constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.B*, i32)* @_ZN1B1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !type !1, !vcall_visibility !2
26+
27+
;; Prevent the vtables from being dead code eliminated.
28+
@llvm.used = appending global [2 x i8*] [ i8* bitcast ( { [4 x i8*] }* @_ZTV1A to i8*), i8* bitcast ( { [4 x i8*] }* @_ZTV1B to i8*)]
29+
30+
; CHECK-IR-LABEL: @_start(
31+
define i32 @_start(%struct.A* %obj, i32 %a) {
32+
entry:
33+
%0 = bitcast %struct.A* %obj to i8***
34+
%vtable = load i8**, i8*** %0
35+
%1 = bitcast i8** %vtable to i8*
36+
%p = call i1 @llvm.type.test(i8* %1, metadata !"_ZTS1A")
37+
call void @llvm.assume(i1 %p)
38+
%fptrptr = getelementptr i8*, i8** %vtable, i32 1
39+
%2 = bitcast i8** %fptrptr to i32 (%struct.A*, i32)**
40+
%fptr1 = load i32 (%struct.A*, i32)*, i32 (%struct.A*, i32)** %2, align 8
41+
42+
;; Check that the call was devirtualized.
43+
; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
44+
%call = tail call i32 %fptr1(%struct.A* nonnull %obj, i32 %a)
45+
46+
ret i32 %call
47+
}
48+
; CHECK-IR-LABEL: ret i32
49+
; CHECK-IR-LABEL: }
50+
51+
declare i1 @llvm.type.test(i8*, metadata)
52+
declare void @llvm.assume(i1)
53+
54+
define available_externally i32 @_ZN1A1fEi(%struct.A* %this, i32 %a) #0 {
55+
ret i32 0
56+
}
57+
58+
define available_externally i32 @_ZN1A1nEi(%struct.A* %this, i32 %a) #0 {
59+
ret i32 0
60+
}
61+
62+
define linkonce_odr i32 @_ZN1B1fEi(%struct.B* %this, i32 %a) #0 {
63+
ret i32 0
64+
}
65+
66+
;; Make sure we don't inline or otherwise optimize out the direct calls.
67+
attributes #0 = { noinline optnone }
68+
69+
!0 = !{i64 16, !"_ZTS1A"}
70+
!1 = !{i64 16, !"_ZTS1B"}
71+
!2 = !{i64 0}

0 commit comments

Comments
 (0)