diff --git a/llvm/include/llvm/ProfileData/SampleProfReader.h b/llvm/include/llvm/ProfileData/SampleProfReader.h --- a/llvm/include/llvm/ProfileData/SampleProfReader.h +++ b/llvm/include/llvm/ProfileData/SampleProfReader.h @@ -483,6 +483,9 @@ /// 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 getProfileSymbolList() { return nullptr; }; diff --git a/llvm/lib/CodeGen/MIRSampleProfile.cpp b/llvm/lib/CodeGen/MIRSampleProfile.cpp --- a/llvm/lib/CodeGen/MIRSampleProfile.cpp +++ b/llvm/lib/CodeGen/MIRSampleProfile.cpp @@ -308,6 +308,15 @@ } 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); diff --git a/llvm/test/CodeGen/X86/fsafdo_test2.ll b/llvm/test/CodeGen/X86/fsafdo_test2.ll --- a/llvm/test/CodeGen/X86/fsafdo_test2.ll +++ b/llvm/test/CodeGen/X86/fsafdo_test2.ll @@ -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. @@ -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