Skip to content

Commit

Permalink
[FS-AFDO] Do not load non-FS profile in MIR loader.
Browse files Browse the repository at this point in the history
I was seeing a regression when enabling FS discriminators on an non-FS CSSPGO build. This is because a probe can get a zero-valued discriminator at a specific pass and that could lead to accidentally loading the corresponding base counter in the non-FS profile, while a non-zeo discriminator would end up getting zero samples. This could in turn undo the sample distribution effort done by previous BFI maintenance work and the probe distribution factor work for pseudo probes specifically. To mitigate that I'm disabling loading a non-FS profile against FS discriminators. The problem should also exist with non-CS AutoFDO, so I'm doing this for it too.

Reviewed By: wenlei

Differential Revision: https://reviews.llvm.org/D149597
  • Loading branch information
htyu committed May 10, 2023
1 parent bcc2021 commit 958a3d8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions llvm/include/llvm/ProfileData/SampleProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ class SampleProfileReader {
/// Whether input profile contains ShouldBeInlined contexts.
bool profileIsPreInlined() const { return ProfileIsPreInlined; }

/// Whether input profile is flow-sensitive.
bool profileIsFS() const { return ProfileIsFS; }

virtual std::unique_ptr<ProfileSymbolList> getProfileSymbolList() {
return nullptr;
};
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/CodeGen/MIRSampleProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ bool MIRProfileLoader::doInitialization(Module &M) {
}

bool MIRProfileLoader::runOnFunction(MachineFunction &MF) {
// Do not load non-FS profiles. A line or probe can get a zero-valued
// discriminator at certain pass which could result in accidentally loading
// the corresponding base counter in the non-FS profile, while a non-zero
// discriminator would end up getting zero samples. This could in turn undo
// the sample distribution effort done by previous BFI maintenance and the
// probe distribution factor work for pseudo probes.
if (!Reader->profileIsFS())
return false;

Function &Func = MF.getFunction();
clearFunctionData(false);
Samples = Reader->getSamplesFor(Func);
Expand Down
6 changes: 5 additions & 1 deletion llvm/test/CodeGen/X86/fsafdo_test2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=true < %s | FileCheck %s --check-prefixes=V1,V01
; RUN: llvm-profdata merge --sample -profile-isfs -o %t1.afdo %S/Inputs/fsloader_v1.afdo
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=true -fs-profile-file=%t1.afdo -show-fs-branchprob -disable-ra-fsprofile-loader=false -disable-layout-fsprofile-loader=false < %s 2>&1 | FileCheck %s --check-prefixes=LOADERV1,LOADER
;
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=true -fs-profile-file=%S/Inputs/fsloader_v1.afdo -profile-isfs -show-fs-branchprob -disable-ra-fsprofile-loader=false -disable-layout-fsprofile-loader=false < %s 2>&1 | FileCheck %s --check-prefixes=LOADERV1,LOADER
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=true -fs-profile-file=%S/Inputs/fsloader_v1.afdo -show-fs-branchprob -disable-ra-fsprofile-loader=false -disable-layout-fsprofile-loader=false < %s 2>&1 | FileCheck %s --check-prefixes=NOLOAD
;;
;; C source code for the test (compiler at -O3):
;; // A test case for loop unroll.
Expand Down Expand Up @@ -82,6 +83,9 @@
; LOADER: Set branch fs prob: MBB (16 -> 18): unroll.c:24:11-->unroll.c:19:3 W=283590 0x30000000 / 0x80000000 = 37.50% --> 0x16588166 / 0x80000000 = 17.46%
; LOADER: Set branch fs prob: MBB (16 -> 17): unroll.c:24:11 W=283590 0x50000000 / 0x80000000 = 62.50% --> 0x69a77e9a / 0x80000000 = 82.54%

;; Check that the profile is not loaded since the reader doesn't know it is a FS profile.
; NOLOAD-NOT: Set branch fs prob

target triple = "x86_64-unknown-linux-gnu"

@sum = dso_local local_unnamed_addr global i32 0, align 4
Expand Down

0 comments on commit 958a3d8

Please sign in to comment.