Skip to content

Commit 6094417

Browse files
[WPD][ThinLTO]Add cutoff option for WPD (#113383)
This option applies for _import_ WPD (i.e., when `DevirtModule` pass de-virtualizes according to an imported summary, in ThinLTO backend pipeline). It's meant for debugging (e.g., bisection).
1 parent d1fae59 commit 6094417

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

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

171+
/// If explicitly specified, the devirt module pass will stop transformation
172+
/// once the total number of devirtualizations reach the cutoff value. Setting
173+
/// this option to 0 explicitly will do 0 devirtualization.
174+
static cl::opt<unsigned> WholeProgramDevirtCutoff(
175+
"wholeprogramdevirt-cutoff",
176+
cl::desc("Max number of devirtualizations for devirt module pass"),
177+
cl::init(0));
178+
171179
/// Mechanism to add runtime checking of devirtualization decisions, optionally
172180
/// trapping or falling back to indirect call on any that are not correct.
173181
/// Trapping mode is useful for debugging undefined behavior leading to failures
@@ -316,6 +324,9 @@ VirtualCallTarget::VirtualCallTarget(GlobalValue *Fn, const TypeMemberInfo *TM)
316324

317325
namespace {
318326

327+
// Tracks the number of devirted calls in the IR transformation.
328+
static unsigned NumDevirtCalls = 0;
329+
319330
// A slot in a set of virtual tables. The TypeID identifies the set of virtual
320331
// tables, and the ByteOffset is the offset in bytes from the address point to
321332
// the virtual function pointer.
@@ -1169,10 +1180,16 @@ void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
11691180
if (!OptimizedCalls.insert(&VCallSite.CB).second)
11701181
continue;
11711182

1183+
// Stop when the number of devirted calls reaches the cutoff.
1184+
if (WholeProgramDevirtCutoff.getNumOccurrences() > 0 &&
1185+
NumDevirtCalls >= WholeProgramDevirtCutoff)
1186+
return;
1187+
11721188
if (RemarksEnabled)
11731189
VCallSite.emitRemark("single-impl",
11741190
TheFn->stripPointerCasts()->getName(), OREGetter);
11751191
NumSingleImpl++;
1192+
NumDevirtCalls++;
11761193
auto &CB = VCallSite.CB;
11771194
assert(!CB.getCalledFunction() && "devirtualizing direct call?");
11781195
IRBuilder<> Builder(&CB);

llvm/test/Transforms/WholeProgramDevirt/import.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
; RUN: opt -S -passes=wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-vcp-branch-funnel.yaml < %s | FileCheck --check-prefixes=CHECK,VCP,VCP-X86,VCP64,BRANCH-FUNNEL %s
99
; RUN: opt -S -passes=wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-branch-funnel.yaml < %s | FileCheck --check-prefixes=CHECK,BRANCH-FUNNEL,BRANCH-FUNNEL-NOVCP %s
1010

11+
; Cutoff value is not explicitly set. Expect 3 remark messages.
12+
; RUN: opt -S -passes=wholeprogramdevirt -wholeprogramdevirt-summary-action=import -pass-remarks=wholeprogramdevirt -wholeprogramdevirt-read-summary=%S/Inputs/import-single-impl.yaml < %s 2>&1 | grep "single-impl" | count 3
13+
; Cutoff value is set to 1. Expect one remark messages.
14+
; RUN: opt -S -passes=wholeprogramdevirt -wholeprogramdevirt-summary-action=import -pass-remarks=wholeprogramdevirt -wholeprogramdevirt-cutoff=1 -wholeprogramdevirt-read-summary=%S/Inputs/import-single-impl.yaml < %s 2>&1 | grep "single-impl" | count 1
15+
; Cutoff value is explicitly set to zero. Expect no remark message.
16+
; RUN: opt -S -passes=wholeprogramdevirt -wholeprogramdevirt-summary-action=import -pass-remarks=wholeprogramdevirt -wholeprogramdevirt-cutoff=0 -wholeprogramdevirt-read-summary=%S/Inputs/import-single-impl.yaml < %s 2>&1 | FileCheck -implicit-check-not="remark" %s
1117
target datalayout = "e-p:64:64"
1218
target triple = "x86_64-unknown-linux-gnu"
1319

0 commit comments

Comments
 (0)