Skip to content

Commit 65ef4d4

Browse files
committed
[CodeGen] Part II of "Fine tune MachineFunctionSplitPass (MFS) for FSAFDO".
This CL adds a new discriminator pass. Also adds a new sample profile loading pass when MFS is enabled. Differential Revision: https://reviews.llvm.org/D152577
1 parent fa68972 commit 65ef4d4

File tree

4 files changed

+115
-14
lines changed

4 files changed

+115
-14
lines changed

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/Support/SaveAndRestore.h"
4444
#include "llvm/Support/Threading.h"
4545
#include "llvm/Support/VirtualFileSystem.h"
46+
#include "llvm/Support/WithColor.h"
4647
#include "llvm/Target/CGPassBuilderOption.h"
4748
#include "llvm/Target/TargetMachine.h"
4849
#include "llvm/Transforms/Scalar.h"
@@ -172,12 +173,6 @@ static cl::opt<GlobalISelAbortMode> EnableGlobalISelAbort(
172173
clEnumValN(GlobalISelAbortMode::DisableWithDiag, "2",
173174
"Disable the abort but emit a diagnostic on failure")));
174175

175-
// An option that disables inserting FS-AFDO discriminators before emit.
176-
// This is mainly for debugging and tuning purpose.
177-
static cl::opt<bool>
178-
FSNoFinalDiscrim("fs-no-final-discrim", cl::init(false), cl::Hidden,
179-
cl::desc("Do not insert FS-AFDO discriminators before "
180-
"emit."));
181176
// Disable MIRProfileLoader before RegAlloc. This is for for debugging and
182177
// tuning purpose.
183178
static cl::opt<bool> DisableRAFSProfileLoader(
@@ -1225,14 +1220,6 @@ void TargetPassConfig::addMachinePasses() {
12251220
addPass(&XRayInstrumentationID);
12261221
addPass(&PatchableFunctionID);
12271222

1228-
if (EnableFSDiscriminator && !FSNoFinalDiscrim)
1229-
// Add FS discriminators here so that all the instruction duplicates
1230-
// in different BBs get their own discriminators. With this, we can "sum"
1231-
// the SampleFDO counters instead of using MAX. This will improve the
1232-
// SampleFDO profile quality.
1233-
addPass(createMIRAddFSDiscriminatorsPass(
1234-
sampleprof::FSDiscriminatorPass::PassLast));
1235-
12361223
addPreEmitPass();
12371224

12381225
if (TM->Options.EnableIPRA)
@@ -1258,6 +1245,10 @@ void TargetPassConfig::addMachinePasses() {
12581245
addPass(createMachineOutlinerPass(RunOnAllFunctions));
12591246
}
12601247

1248+
if (EnableFSDiscriminator)
1249+
addPass(createMIRAddFSDiscriminatorsPass(
1250+
sampleprof::FSDiscriminatorPass::PassLast));
1251+
12611252
// Machine function splitter uses the basic block sections feature. Both
12621253
// cannot be enabled at the same time. Basic block sections takes precedence.
12631254
// FIXME: In principle, BasicBlockSection::Labels and splitting can used
@@ -1270,6 +1261,20 @@ void TargetPassConfig::addMachinePasses() {
12701261
addPass(llvm::createBasicBlockSectionsPass());
12711262
} else if (TM->Options.EnableMachineFunctionSplitter ||
12721263
EnableMachineFunctionSplitter) {
1264+
const std::string ProfileFile = getFSProfileFile(TM);
1265+
if (!ProfileFile.empty()) {
1266+
if (EnableFSDiscriminator) {
1267+
addPass(createMIRProfileLoaderPass(
1268+
ProfileFile, getFSRemappingFile(TM),
1269+
sampleprof::FSDiscriminatorPass::PassLast, nullptr));
1270+
} else {
1271+
// Sample profile is given, but FSDiscriminator is not
1272+
// enabled, this may result in performance regression.
1273+
WithColor::warning()
1274+
<< "Using AutoFDO without FSDiscriminator for MFS may regress "
1275+
"performance.";
1276+
}
1277+
}
12731278
addPass(createMachineFunctionSplitterPass());
12741279
}
12751280

135 Bytes
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;;; MFS with sample profile fails when no -enable-fs-discriminator=true.
2+
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -fs-profile-file=%S/Inputs/fsloader-mfs.afdo -split-machine-functions -enable-fs-discriminator=false 2>&1 | FileCheck %s --check-prefix=NODISCRIMINATOR
3+
; NODISCRIMINATOR: warning: Using AutoFDO without FSDiscriminator for MFS may regress performance.
4+
5+
define void @foo4(i1 zeroext %0, i1 zeroext %1) nounwind {
6+
br i1 %0, label %3, label %7
7+
8+
3:
9+
%4 = call i32 @bar()
10+
br label %7
11+
12+
5:
13+
%6 = call i32 @baz()
14+
br label %7
15+
16+
7:
17+
br i1 %1, label %8, label %10
18+
19+
8:
20+
%9 = call i32 @bam()
21+
br label %12
22+
23+
10:
24+
%11 = call i32 @baz()
25+
br label %12
26+
27+
12:
28+
%13 = tail call i32 @qux()
29+
ret void
30+
}
31+
32+
declare i32 @bar()
33+
declare i32 @baz()
34+
declare i32 @bam()
35+
declare i32 @qux()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -debug-pass=Structure -enable-fs-discriminator=true -improved-fs-discriminator=true 2>&1 | FileCheck %s --check-prefix=NOPROFILE
2+
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -debug-pass=Structure -fs-profile-file=%S/Inputs/fsloader-mfs.afdo -enable-fs-discriminator=true -improved-fs-discriminator=true 2>&1 | FileCheck %s --check-prefix=PROFILE-NOMFS
3+
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -debug-pass=Structure -fs-profile-file=%S/Inputs/fsloader-mfs.afdo -split-machine-functions -enable-fs-discriminator=true -improved-fs-discriminator=true 2>&1 | FileCheck %s --check-prefix=PROFILE-MFS
4+
5+
;; No profile is specified, no load passes.
6+
; NOPROFILE: Add FS discriminators in MIR
7+
; NO-NOPROFILE: SampleFDO loader in MIR
8+
; NOPROFILE: Add FS discriminators in MIR
9+
; NO-NOPROFILE: SampleFDO loader in MIR
10+
; NOPROFILE: Add FS discriminators in MIR
11+
; NO-NOPROFILE: SampleFDO loader in MIR
12+
13+
;; Profile is specified, so we have first 2 load passes.
14+
; PROFILE-NOMFS: Add FS discriminators in MIR
15+
; PROFILE-NOMFS: SampleFDO loader in MIR
16+
; PROFILE-NOMFS: Add FS discriminators in MIR
17+
; PROFILE-NOMFS: SampleFDO loader in MIR
18+
; PROFILE-NOMFS: Add FS discriminators in MIR
19+
;; But mfs is not specified, so no "SampleFDO loader should be created"
20+
; NO-PROFILE-NOMFS: SampleFDO loader in MIR
21+
22+
;; Profile is specified with mfs, so we have 3 load passes.
23+
; PROFILE-MFS: Add FS discriminators in MIR
24+
; PROFILE-MFS: SampleFDO loader in MIR
25+
; PROFILE-MFS: Add FS discriminators in MIR
26+
; PROFILE-MFS: SampleFDO loader in MIR
27+
; PROFILE-MFS: Add FS discriminators in MIR
28+
; PROFILE-MFS: SampleFDO loader in MIR
29+
; PROFILE-MFS: Machine Function Splitter Transformation
30+
31+
define void @foo4(i1 zeroext %0, i1 zeroext %1) nounwind {
32+
br i1 %0, label %3, label %7
33+
34+
3:
35+
%4 = call i32 @bar()
36+
br label %7
37+
38+
5:
39+
%6 = call i32 @baz()
40+
br label %7
41+
42+
7:
43+
br i1 %1, label %8, label %10
44+
45+
8:
46+
%9 = call i32 @bam()
47+
br label %12
48+
49+
10:
50+
%11 = call i32 @baz()
51+
br label %12
52+
53+
12:
54+
%13 = tail call i32 @qux()
55+
ret void
56+
}
57+
58+
declare i32 @bar()
59+
declare i32 @baz()
60+
declare i32 @bam()
61+
declare i32 @qux()

0 commit comments

Comments
 (0)