From af954e441a5170a75687699d91d85e0692929d43 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Sat, 25 Jan 2020 07:26:09 -0800 Subject: [PATCH] [WPD] Emit vcall_visibility metadata for MicrosoftCXXABI Summary: The MicrosoftCXXABI uses a separate mechanism for emitting vtable type metadata, and thus didn't pick up the change from D71907 to emit the vcall_visibility metadata under -fwhole-program-vtables. I believe this is the cause of a Windows bot failure when I committed follow on change D71913 that required a revert. The failure occurred in a CFI test that was expecting to not abort because it expected a devirtualization to occur, and without the necessary vcall_visibility metadata we would not get devirtualization. Note in the equivalent code in CodeGenModule::EmitVTableTypeMetadata (used by the ItaniumCXXABI), we also emit the vcall_visibility metadata when Virtual Function Elimination is enabled. Since I am not as familiar with the details of that optimization, I have marked that as a TODO and am only inserting under -fwhole-program-vtables. Reviewers: evgeny777 Subscribers: Prazek, ostannard, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D73418 --- clang/lib/CodeGen/MicrosoftCXXABI.cpp | 9 +++++++++ clang/test/CodeGenCXX/vcall-visibility-metadata.cpp | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp index aff4613..b083a58 100644 --- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -1621,6 +1621,15 @@ void MicrosoftCXXABI::emitVTableTypeMetadata(const VPtrInfo &Info, if (!CGM.getCodeGenOpts().LTOUnit) return; + // TODO: Should VirtualFunctionElimination also be supported here? + // See similar handling in CodeGenModule::EmitVTableTypeMetadata. + if (CGM.getCodeGenOpts().WholeProgramVTables) { + llvm::GlobalObject::VCallVisibility TypeVis = + CGM.GetVCallVisibilityLevel(RD); + if (TypeVis != llvm::GlobalObject::VCallVisibilityPublic) + VTable->setVCallVisibilityMetadata(TypeVis); + } + // The location of the first virtual function pointer in the virtual table, // aka the "address point" on Itanium. This is at offset 0 if RTTI is // disabled, or sizeof(void*) if RTTI is enabled. diff --git a/clang/test/CodeGenCXX/vcall-visibility-metadata.cpp b/clang/test/CodeGenCXX/vcall-visibility-metadata.cpp index b770ad7..84d9a21 100644 --- a/clang/test/CodeGenCXX/vcall-visibility-metadata.cpp +++ b/clang/test/CodeGenCXX/vcall-visibility-metadata.cpp @@ -1,5 +1,6 @@ // RUN: %clang_cc1 -flto -flto-unit -triple x86_64-unknown-linux -emit-llvm -fvirtual-function-elimination -fwhole-program-vtables -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-VFE // RUN: %clang_cc1 -flto -flto-unit -triple x86_64-unknown-linux -emit-llvm -fwhole-program-vtables -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NOVFE +// RUN: %clang_cc1 -flto -flto-unit -triple x86_64-pc-windows-msvc -emit-llvm -fwhole-program-vtables -o - %s | FileCheck %s --check-prefix=CHECK-MS --check-prefix=CHECK-NOVFE // Check that in ThinLTO we also get vcall_visibility summary entries in the bitcode // RUN: %clang_cc1 -flto=thin -flto-unit -triple x86_64-unknown-linux -emit-llvm-bc -fwhole-program-vtables -o - %s | llvm-dis -o - | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NOVFE --check-prefix=CHECK-SUMMARY @@ -8,6 +9,7 @@ // Anonymous namespace. namespace { // CHECK: @_ZTVN12_GLOBAL__N_11AE = {{.*}} !vcall_visibility [[VIS_TU:![0-9]+]] +// CHECK-MS: @anon.{{.*}} = private unnamed_addr constant {{.*}}struct.(anonymous namespace)::A{{.*}} !vcall_visibility [[VIS_TU:![0-9]+]] struct A { A() {} virtual int f() { return 1; } @@ -20,6 +22,7 @@ void *construct_A() { // Hidden visibility. // CHECK: @_ZTV1B = {{.*}} !vcall_visibility [[VIS_DSO:![0-9]+]] +// CHECK-MS: @anon.{{.*}} = private unnamed_addr constant {{.*}}struct.B{{.*}} !vcall_visibility [[VIS_DSO:![0-9]+]] struct __attribute__((visibility("hidden"))) B { B() {} virtual int f() { return 1; } @@ -31,6 +34,8 @@ B *construct_B() { // Default visibility. // CHECK-NOT: @_ZTV1C = {{.*}} !vcall_visibility +// On MS default is hidden +// CHECK-MS: @anon.{{.*}} = private unnamed_addr constant {{.*}}struct.C{{.*}} !vcall_visibility [[VIS_DSO]] struct __attribute__((visibility("default"))) C { C() {} virtual int f() { return 1; } @@ -42,6 +47,7 @@ C *construct_C() { // Hidden visibility, public LTO visibility. // CHECK-NOT: @_ZTV1D = {{.*}} !vcall_visibility +// CHECK-MS-NOT: @anon.{{.*}} = private unnamed_addr constant {{.*}}struct.D{{.*}} !vcall_visibility struct __attribute__((visibility("hidden"))) [[clang::lto_visibility_public]] D { D() {} virtual int f() { return 1; } @@ -53,6 +59,8 @@ D *construct_D() { // Hidden visibility, but inherits from class with default visibility. // CHECK-NOT: @_ZTV1E = {{.*}} !vcall_visibility +// On MS default is hidden +// CHECK-MS: @anon.{{.*}} = private unnamed_addr constant {{.*}}struct.E{{.*}} !vcall_visibility [[VIS_DSO]] struct __attribute__((visibility("hidden"))) E : C { E() {} virtual int f() { return 1; } @@ -64,6 +72,8 @@ E *construct_E() { // Anonymous namespace, but inherits from class with default visibility. // CHECK-NOT: @_ZTVN12_GLOBAL__N_11FE = {{.*}} !vcall_visibility +// On MS default is hidden +// CHECK-MS: @anon.{{.*}} = private unnamed_addr constant {{.*}}struct.(anonymous namespace)::F{{.*}} !vcall_visibility [[VIS_DSO]] namespace { struct __attribute__((visibility("hidden"))) F : C { F() {} @@ -77,6 +87,7 @@ void *construct_F() { // Anonymous namespace, but inherits from class with hidden visibility. // CHECK: @_ZTVN12_GLOBAL__N_11GE = {{.*}} !vcall_visibility [[VIS_DSO:![0-9]+]] +// CHECK-MS: @anon.{{.*}} = private unnamed_addr constant {{.*}}struct.(anonymous namespace)::G{{.*}} !vcall_visibility [[VIS_DSO]] namespace { struct __attribute__((visibility("hidden"))) G : B { G() {} @@ -87,6 +98,8 @@ void *construct_G() { return new G(); } +// CHECK-MS-DAG: [[VIS_DSO]] = !{i64 1} +// CHECK-MS-DAG: [[VIS_TU]] = !{i64 2} // CHECK-DAG: [[VIS_DSO]] = !{i64 1} // CHECK-DAG: [[VIS_TU]] = !{i64 2} // CHECK-VFE-DAG: !{i32 1, !"Virtual Function Elim", i32 1} -- 2.7.4