Skip to content

Commit 458676d

Browse files
committed
[WPD/VFE] Always emit vcall_visibility metadata for -fwhole-program-vtables
Summary: First patch to support Safe Whole Program Devirtualization Enablement, see RFC here: http://lists.llvm.org/pipermail/llvm-dev/2019-December/137543.html Always emit !vcall_visibility metadata under -fwhole-program-vtables, and not just for -fvirtual-function-elimination. The vcall visibility metadata will (in a subsequent patch) be used to communicate to WPD which vtables are safe to devirtualize, and we will optionally convert the metadata to hidden visibility at link time. Subsequent follow on patches will help enable this by adding vcall_visibility metadata to the ThinLTO summaries, and always emit type test intrinsics under -fwhole-program-vtables (and not just for vtables with hidden visibility). In order to do this safely with VFE, since for VFE all vtable loads must be type checked loads which will no longer be the case, this patch adds a new "Virtual Function Elim" module flag to communicate to GlobalDCE whether to perform VFE using the vcall_visibility metadata. One additional advantage of using the vcall_visibility metadata to drive more WPD at LTO link time is that we can use the same mechanism to enable more aggressive VFE at LTO link time as well. The link time option proposed in the RFC will convert vcall_visibility metadata to hidden (aka linkage unit visibility), which combined with -fvirtual-function-elimination will allow it to be done more aggressively at LTO link time under the same conditions. Reviewers: pcc, ostannard, evgeny777, steven_wu Subscribers: mehdi_amini, Prazek, hiraditya, dexonsmith, davidxl, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D71907
1 parent 6770de9 commit 458676d

17 files changed

+117
-12
lines changed

clang/lib/CodeGen/CGVTables.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,9 +1131,10 @@ void CodeGenModule::EmitVTableTypeMetadata(const CXXRecordDecl *RD,
11311131
}
11321132
}
11331133

1134-
if (getCodeGenOpts().VirtualFunctionElimination) {
1134+
if (getCodeGenOpts().VirtualFunctionElimination ||
1135+
getCodeGenOpts().WholeProgramVTables) {
11351136
llvm::GlobalObject::VCallVisibility TypeVis = GetVCallVisibilityLevel(RD);
11361137
if (TypeVis != llvm::GlobalObject::VCallVisibilityPublic)
1137-
VTable->addVCallVisibilityMetadata(TypeVis);
1138+
VTable->setVCallVisibilityMetadata(TypeVis);
11381139
}
11391140
}

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,14 @@ void CodeGenModule::Release() {
549549
getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
550550
}
551551

552+
if (CodeGenOpts.WholeProgramVTables) {
553+
// Indicate whether VFE was enabled for this module, so that the
554+
// vcall_visibility metadata added under whole program vtables is handled
555+
// appropriately in the optimizer.
556+
getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
557+
CodeGenOpts.VirtualFunctionElimination);
558+
}
559+
552560
if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
553561
getModule().addModuleFlag(llvm::Module::Override,
554562
"CFI Canonical Jump Tables",

clang/test/CodeGenCXX/vcall-visibility-metadata.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %clang_cc1 -flto -flto-unit -triple x86_64-unknown-linux -emit-llvm -fvirtual-function-elimination -fwhole-program-vtables -o - %s | FileCheck %s
1+
// 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
2+
// 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
23

34

45
// Anonymous namespace.
@@ -83,6 +84,7 @@ void *construct_G() {
8384
return new G();
8485
}
8586

86-
8787
// CHECK-DAG: [[VIS_DSO]] = !{i64 1}
8888
// CHECK-DAG: [[VIS_TU]] = !{i64 2}
89+
// CHECK-VFE-DAG: !{i32 1, !"Virtual Function Elim", i32 1}
90+
// CHECK-NOVFE-DAG: !{i32 1, !"Virtual Function Elim", i32 0}

llvm/include/llvm/IR/GlobalObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class GlobalObject : public GlobalValue {
183183
void copyMetadata(const GlobalObject *Src, unsigned Offset);
184184

185185
void addTypeMetadata(unsigned Offset, Metadata *TypeID);
186-
void addVCallVisibilityMetadata(VCallVisibility Visibility);
186+
void setVCallVisibilityMetadata(VCallVisibility Visibility);
187187
VCallVisibility getVCallVisibility() const;
188188

189189
protected:

llvm/lib/IR/Metadata.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,10 @@ void GlobalObject::addTypeMetadata(unsigned Offset, Metadata *TypeID) {
15001500
TypeID}));
15011501
}
15021502

1503-
void GlobalObject::addVCallVisibilityMetadata(VCallVisibility Visibility) {
1503+
void GlobalObject::setVCallVisibilityMetadata(VCallVisibility Visibility) {
1504+
// Remove any existing vcall visibility metadata first in case we are
1505+
// updating.
1506+
eraseMetadata(LLVMContext::MD_vcall_visibility);
15041507
addMetadata(LLVMContext::MD_vcall_visibility,
15051508
*MDNode::get(getContext(),
15061509
{ConstantAsMetadata::get(ConstantInt::get(

llvm/lib/Transforms/IPO/GlobalDCE.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ void GlobalDCEPass::AddVirtualFunctionDependencies(Module &M) {
263263
if (!ClEnableVFE)
264264
return;
265265

266+
// If the Virtual Function Elim module flag is present and set to zero, then
267+
// the vcall_visibility metadata was inserted for another optimization (WPD)
268+
// and we may not have type checked loads on all accesses to the vtable.
269+
// Don't attempt VFE in that case.
270+
auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
271+
M.getModuleFlag("Virtual Function Elim"));
272+
if (!Val || Val->getZExtValue() == 0)
273+
return;
274+
266275
ScanVTables(M);
267276

268277
if (VFESafeVTables.empty())

llvm/lib/Transforms/IPO/GlobalSplit.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ static bool splitGlobal(GlobalVariable &GV) {
111111
ConstantInt::get(Int32Ty, ByteOffset - SplitBegin)),
112112
Type->getOperand(1)}));
113113
}
114+
115+
if (GV.hasMetadata(LLVMContext::MD_vcall_visibility))
116+
SplitGV->setVCallVisibilityMetadata(GV.getVCallVisibility());
114117
}
115118

116119
for (User *U : GV.users()) {

llvm/test/Transforms/GlobalDCE/virtual-functions-base-call.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ entry:
7070

7171
declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata) #2
7272

73+
!llvm.module.flags = !{!5}
74+
7375
!0 = !{i64 16, !"_ZTS1A"}
7476
!1 = !{i64 16, !"_ZTSM1AFivE.virtual"}
7577
!2 = !{i64 2}
7678
!3 = !{i64 16, !"_ZTS1B"}
7779
!4 = !{i64 16, !"_ZTSM1BFivE.virtual"}
80+
!5 = !{i32 1, !"Virtual Function Elim", i32 1}
7881
!10 = !{}

llvm/test/Transforms/GlobalDCE/virtual-functions-base-pointer-call.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,14 @@ memptr.end: ; preds = %memptr.nonvirtual,
108108

109109
declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata)
110110

111+
!llvm.module.flags = !{!7}
112+
111113
!0 = !{i64 16, !"_ZTS1A"}
112114
!1 = !{i64 16, !"_ZTSM1AFiiE.virtual"}
113115
!2 = !{i64 24, !"_ZTSM1AFifE.virtual"}
114116
!3 = !{i64 2}
115117
!4 = !{i64 16, !"_ZTS1B"}
116118
!5 = !{i64 16, !"_ZTSM1BFiiE.virtual"}
117119
!6 = !{i64 24, !"_ZTSM1BFifE.virtual"}
120+
!7 = !{i32 1, !"Virtual Function Elim", i32 1}
118121
!12 = !{}

llvm/test/Transforms/GlobalDCE/virtual-functions-derived-call.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ entry:
7070

7171
declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata) #2
7272

73+
!llvm.module.flags = !{!5}
74+
7375
!0 = !{i64 16, !"_ZTS1A"}
7476
!1 = !{i64 16, !"_ZTSM1AFivE.virtual"}
7577
!2 = !{i64 2}
7678
!3 = !{i64 16, !"_ZTS1B"}
7779
!4 = !{i64 16, !"_ZTSM1BFivE.virtual"}
80+
!5 = !{i32 1, !"Virtual Function Elim", i32 1}
7881
!10 = !{}

0 commit comments

Comments
 (0)