diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -277,7 +277,7 @@ if (config->relocatable) return binding; if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) || - (versionId == VER_NDX_LOCAL && isDefined())) + (versionId == VER_NDX_LOCAL && !isLazy())) return STB_LOCAL; if (!config->gnuUnique && binding == STB_GNU_UNIQUE) return STB_GLOBAL; diff --git a/lld/test/ELF/lto/Inputs/devirt_extern_vtable.ll b/lld/test/ELF/lto/Inputs/devirt_extern_vtable.ll new file mode 100644 --- /dev/null +++ b/lld/test/ELF/lto/Inputs/devirt_extern_vtable.ll @@ -0,0 +1,81 @@ +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.Cat = type { %struct.Animal } +%struct.Animal = type { i32 (...)** } + +$_ZN3CatC2Ev = comdat any + +$_ZN6AnimalC2Ev = comdat any + +$_ZTV6Animal = comdat any + +$_ZTS6Animal = comdat any + +$_ZTI6Animal = comdat any + +@_ZTV3Cat = external dso_local unnamed_addr constant { [3 x i8*] }, align 8 +@_ZTV6Animal = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8*, i8* }* @_ZTI6Animal to i8*), i8* bitcast (void ()* @__cxa_pure_virtual to i8*)] }, comdat, align 8, !type !0, !type !1 +@_ZTVN10__cxxabiv117__class_type_infoE = external dso_local global i8* +@_ZTS6Animal = linkonce_odr dso_local constant [8 x i8] c"6Animal\00", comdat, align 1 +@_ZTI6Animal = linkonce_odr dso_local constant { i8*, i8* } { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv117__class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @_ZTS6Animal, i32 0, i32 0) }, comdat, align 8 + +; Function Attrs: noinline optnone uwtable mustprogress +define dso_local void @_Z17useDoThingWithCatv() #0 { +entry: + %d = alloca %struct.Cat*, align 8 + %call = call noalias nonnull i8* @_Znwm(i64 8) #4 + %0 = bitcast i8* %call to %struct.Cat* + call void @_ZN3CatC2Ev(%struct.Cat* nonnull dereferenceable(8) %0) #5 + store %struct.Cat* %0, %struct.Cat** %d, align 8 + %1 = load %struct.Cat*, %struct.Cat** %d, align 8 + %2 = bitcast %struct.Cat* %1 to %struct.Animal* + call void @_Z14doThingWithCatP6Animal(%struct.Animal* %2) + ret void +} + +; Function Attrs: nobuiltin allocsize(0) +declare dso_local nonnull i8* @_Znwm(i64) #1 + +; Function Attrs: noinline nounwind optnone uwtable +define linkonce_odr dso_local void @_ZN3CatC2Ev(%struct.Cat* nonnull dereferenceable(8) %this) unnamed_addr #2 comdat align 2 { +entry: + %this.addr = alloca %struct.Cat*, align 8 + store %struct.Cat* %this, %struct.Cat** %this.addr, align 8 + %this1 = load %struct.Cat*, %struct.Cat** %this.addr, align 8 + %0 = bitcast %struct.Cat* %this1 to %struct.Animal* + call void @_ZN6AnimalC2Ev(%struct.Animal* nonnull dereferenceable(8) %0) #5 + %1 = bitcast %struct.Cat* %this1 to i32 (...)*** + store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV3Cat, i32 0, inrange i32 0, i32 2) to i32 (...)**), i32 (...)*** %1, align 8 + ret void +} + +declare dso_local void @_Z14doThingWithCatP6Animal(%struct.Animal*) #3 + +; Function Attrs: noinline nounwind optnone uwtable +define linkonce_odr dso_local void @_ZN6AnimalC2Ev(%struct.Animal* nonnull dereferenceable(8) %this) unnamed_addr #2 comdat align 2 { +entry: + %this.addr = alloca %struct.Animal*, align 8 + store %struct.Animal* %this, %struct.Animal** %this.addr, align 8 + %this1 = load %struct.Animal*, %struct.Animal** %this.addr, align 8 + %0 = bitcast %struct.Animal* %this1 to i32 (...)*** + store i32 (...)** bitcast (i8** getelementptr inbounds ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV6Animal, i32 0, inrange i32 0, i32 2) to i32 (...)**), i32 (...)*** %0, align 8 + ret void +} + +declare dso_local void @__cxa_pure_virtual() unnamed_addr + +attributes #0 = { noinline optnone uwtable mustprogress "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { nobuiltin allocsize(0) "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #2 = { noinline nounwind optnone uwtable "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #3 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #4 = { builtin allocsize(0) } +attributes #5 = { nounwind } + +!llvm.linker.options = !{} +!llvm.module.flags = !{!2, !3} + +!0 = !{i64 16, !"_ZTS6Animal"} +!1 = !{i64 16, !"_ZTSM6AnimalKFvvE.virtual"} +!2 = !{i32 1, !"wchar_size", i32 4} +!3 = !{i32 1, !"Virtual Function Elim", i32 0} diff --git a/lld/test/ELF/lto/devirt_extern_vtable.ll b/lld/test/ELF/lto/devirt_extern_vtable.ll new file mode 100644 --- /dev/null +++ b/lld/test/ELF/lto/devirt_extern_vtable.ll @@ -0,0 +1,96 @@ +; REQUIRES: x86 +;; Test that that a vtable defined locally in one module but external in another +;; does not prevent devirtualization. + +;; Hybrid WPD +; RUN: opt --thinlto-bc --thinlto-split-lto-unit -o %t1.o %s +; RUN: opt --thinlto-bc --thinlto-split-lto-unit %S/Inputs/devirt_extern_vtable.ll -o %t2.o +; RUN: echo '{ global: _Z17useDoThingWithCatv; local: *; };' > %t.ver + +; RUN: ld.lld %t1.o %t2.o -shared -o %t3 -save-temps --lto-whole-program-visibility \ +; RUN: -mllvm -pass-remarks=. --version-script %t.ver 2>&1 \ +; RUN: | FileCheck %s --check-prefix=REMARK + +; REMARK-DAG: :0:0: devirtualized _ZNK3Cat9makeNoiseEv +; REMARK-DAG: :0:0: single-impl: devirtualized a call to _ZNK3Cat9makeNoiseEv + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.Cat = type { %struct.Animal } +%struct.Animal = type { i32 (...)** } + +$_ZTS6Animal = comdat any + +$_ZTI6Animal = comdat any + +@.str = private unnamed_addr constant [5 x i8] c"Meow\00", align 1 +@_ZTV3Cat = dso_local unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI3Cat to i8*), i8* bitcast (void (%struct.Cat*)* @_ZNK3Cat9makeNoiseEv to i8*)] }, align 8, !type !0, !type !1, !type !2, !type !3 +@_ZTVN10__cxxabiv120__si_class_type_infoE = external dso_local global i8* +@_ZTS3Cat = dso_local constant [5 x i8] c"3Cat\00", align 1 +@_ZTVN10__cxxabiv117__class_type_infoE = external dso_local global i8* +@_ZTS6Animal = linkonce_odr dso_local constant [8 x i8] c"6Animal\00", comdat, align 1 +@_ZTI6Animal = linkonce_odr dso_local constant { i8*, i8* } { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv117__class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @_ZTS6Animal, i32 0, i32 0) }, comdat, align 8 +@_ZTI3Cat = dso_local constant { i8*, i8*, i8* } { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @_ZTS3Cat, i32 0, i32 0), i8* bitcast ({ i8*, i8* }* @_ZTI6Animal to i8*) }, align 8 + +; Function Attrs: noinline optnone uwtable mustprogress +define dso_local void @_ZNK3Cat9makeNoiseEv(%struct.Cat* nonnull dereferenceable(8) %this) unnamed_addr #0 align 2 { +entry: + %this.addr = alloca %struct.Cat*, align 8 + store %struct.Cat* %this, %struct.Cat** %this.addr, align 8 + %this1 = load %struct.Cat*, %struct.Cat** %this.addr, align 8 + %call = call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0)) + ret void +} + +declare dso_local i32 @puts(i8*) #1 + +; Function Attrs: noinline optnone uwtable mustprogress +define dso_local void @_Z14doThingWithCatP6Animal(%struct.Animal* %a) #0 { +entry: + %a.addr = alloca %struct.Animal*, align 8 + %d = alloca %struct.Cat*, align 8 + store %struct.Animal* %a, %struct.Animal** %a.addr, align 8 + %0 = load %struct.Animal*, %struct.Animal** %a.addr, align 8 + %1 = bitcast %struct.Animal* %0 to %struct.Cat* + store %struct.Cat* %1, %struct.Cat** %d, align 8 + %2 = load %struct.Cat*, %struct.Cat** %d, align 8 + %tobool = icmp ne %struct.Cat* %2, null + br i1 %tobool, label %if.then, label %if.end + +if.then: ; preds = %entry + %3 = load %struct.Cat*, %struct.Cat** %d, align 8 + %4 = bitcast %struct.Cat* %3 to void (%struct.Cat*)*** + %vtable = load void (%struct.Cat*)**, void (%struct.Cat*)*** %4, align 8 + %5 = bitcast void (%struct.Cat*)** %vtable to i8* + %6 = call i1 @llvm.type.test(i8* %5, metadata !"_ZTS3Cat") + call void @llvm.assume(i1 %6) + %vfn = getelementptr inbounds void (%struct.Cat*)*, void (%struct.Cat*)** %vtable, i64 0 + %7 = load void (%struct.Cat*)*, void (%struct.Cat*)** %vfn, align 8 + call void %7(%struct.Cat* nonnull dereferenceable(8) %3) + br label %if.end + +if.end: ; preds = %if.then, %entry + ret void +} + +; Function Attrs: nofree nosync nounwind readnone willreturn +declare i1 @llvm.type.test(i8*, metadata) #2 + +; Function Attrs: nofree nosync nounwind willreturn +declare void @llvm.assume(i1 noundef) #3 + +attributes #0 = { noinline optnone uwtable mustprogress "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #2 = { nofree nosync nounwind readnone willreturn } +attributes #3 = { nofree nosync nounwind willreturn } + +!llvm.linker.options = !{} +!llvm.module.flags = !{!4, !5} + +!0 = !{i64 16, !"_ZTS3Cat"} +!1 = !{i64 16, !"_ZTSM3CatKFvvE.virtual"} +!2 = !{i64 16, !"_ZTS6Animal"} +!3 = !{i64 16, !"_ZTSM6AnimalKFvvE.virtual"} +!4 = !{i32 1, !"wchar_size", i32 4} +!5 = !{i32 1, !"Virtual Function Elim", i32 0}