Skip to content

Commit 830c0d4

Browse files
[WPD]Regard unreachable function as a possible devirtualizable target
1 parent e4c1419 commit 830c0d4

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ static cl::list<std::string>
167167
cl::desc("Prevent function(s) from being devirtualized"),
168168
cl::Hidden, cl::CommaSeparated);
169169

170+
/// A function is unreachable if its entry block ends with 'unreachable' IR
171+
/// instruction. In some cases, the program intends to run such functions and
172+
/// terminate, for instance, a unit test may run a death test. A non-test
173+
/// program might (or allowed to) invoke such functions to report failures
174+
/// (whether/when it's a good practice or not is a different topic). Regard
175+
/// unreachable function as possible devirtualize targets to keep the program
176+
/// behavior.
177+
static cl::opt<bool> WholeProgramDevirtKeepUnreachableFunction(
178+
"wholeprogramdevirt-keep-unreachable-function",
179+
cl::desc("Regard unreachable functions as possible devirtualize targets."),
180+
cl::Hidden, cl::init(true));
181+
170182
/// If explicitly specified, the devirt module pass will stop transformation
171183
/// once the total number of devirtualizations reach the cutoff value. Setting
172184
/// this option to 0 explicitly will do 0 devirtualization.
@@ -386,6 +398,9 @@ template <> struct DenseMapInfo<VTableSlotSummary> {
386398
// 2) All function summaries indicate it's unreachable
387399
// 3) There is no non-function with the same GUID (which is rare)
388400
static bool mustBeUnreachableFunction(ValueInfo TheFnVI) {
401+
if (WholeProgramDevirtKeepUnreachableFunction)
402+
return false;
403+
389404
if ((!TheFnVI) || TheFnVI.getSummaryList().empty()) {
390405
// Returns false if ValueInfo is absent, or the summary list is empty
391406
// (e.g., function declarations).
@@ -2241,6 +2256,8 @@ DevirtModule::lookUpFunctionValueInfo(Function *TheFn,
22412256

22422257
bool DevirtModule::mustBeUnreachableFunction(
22432258
Function *const F, ModuleSummaryIndex *ExportSummary) {
2259+
if (WholeProgramDevirtKeepUnreachableFunction)
2260+
return false;
22442261
// First, learn unreachability by analyzing function IR.
22452262
if (!F->isDeclaration()) {
22462263
// A function must be unreachable if its entry block ends with an

llvm/test/Transforms/WholeProgramDevirt/devirt_single_after_filtering_unreachable_function.ll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
; Test that static devirtualization doesn't happen because there are two
2+
; devirtualizable targets. Unreachable functions are kept in the devirtualizable
3+
; target set by default.
4+
; RUN: opt -S -passes=wholeprogramdevirt -whole-program-visibility -pass-remarks=wholeprogramdevirt %s 2>&1 | FileCheck %s --implicit-check-not="single-impl"
5+
16
; Test that regular LTO will analyze IR, detect unreachable functions and discard unreachable functions
27
; when finding virtual call targets.
38
; In this test case, the unreachable function is the virtual deleting destructor of an abstract class.
9+
; RUN: opt -S -passes=wholeprogramdevirt -whole-program-visibility -pass-remarks=wholeprogramdevirt -wholeprogramdevirt-keep-unreachable-function=false %s 2>&1 | FileCheck %s --check-prefix=DEVIRT
410

5-
; RUN: opt -S -passes=wholeprogramdevirt -whole-program-visibility -pass-remarks=wholeprogramdevirt %s 2>&1 | FileCheck %s
6-
7-
; CHECK: remark: tmp.cc:21:3: single-impl: devirtualized a call to _ZN7DerivedD0Ev
8-
; CHECK: remark: <unknown>:0:0: devirtualized _ZN7DerivedD0Ev
11+
; DEVIRT: remark: tmp.cc:21:3: single-impl: devirtualized a call to _ZN7DerivedD0Ev
12+
; DEVIRT: remark: <unknown>:0:0: devirtualized _ZN7DerivedD0Ev
913

1014
source_filename = "tmp.cc"
1115
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)