From 833c4754718c4f5326eda59926a8c06a271ebe76 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Mon, 18 Dec 2017 20:02:43 +0000 Subject: [PATCH] [PGO] Fix handling of cold entry count for instrumented PGO Summary: In r277849, getEntryCount was changed to return None when the entry count was 0, specifically for SamplePGO where it means no samples were recorded. However, for instrumentation PGO a 0 entry count should be returned directly, since it does mean that the function was completely cold. Otherwise we end up treating these functions conservatively in isFunctionEntryCold() and isColdBB(). Instead, for SamplePGO use -1 when there are no samples, and change getEntryCount to return None when the value is -1. Reviewers: danielcdh, davidxl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D41307 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321018 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/IR/Function.cpp | 4 +++- lib/Transforms/IPO/SampleProfile.cpp | 5 ++++- test/Transforms/SampleProfile/entry_counts.ll | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp index 1fff912ecf2..7063f6f40a3 100644 --- a/lib/IR/Function.cpp +++ b/lib/IR/Function.cpp @@ -1333,7 +1333,9 @@ Optional Function::getEntryCount() const { if (MDS->getString().equals("function_entry_count")) { ConstantInt *CI = mdconst::extract(MD->getOperand(1)); uint64_t Count = CI->getValue().getZExtValue(); - if (Count == 0) + // A value of -1 is used for SamplePGO when there were no samples. + // Treat this the same as unknown. + if (Count == (uint64_t)-1) return None; return Count; } diff --git a/lib/Transforms/IPO/SampleProfile.cpp b/lib/Transforms/IPO/SampleProfile.cpp index f0e781b9d92..7086c2eb52c 100644 --- a/lib/Transforms/IPO/SampleProfile.cpp +++ b/lib/Transforms/IPO/SampleProfile.cpp @@ -1583,7 +1583,10 @@ bool SampleProfileLoaderLegacyPass::runOnModule(Module &M) { } bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM) { - F.setEntryCount(0); + // Initialize the entry count to -1, which will be treated conservatively + // by getEntryCount as the same as unknown (None). If we have samples this + // will be overwritten in emitAnnotations. + F.setEntryCount(-1); std::unique_ptr OwnedORE; if (AM) { auto &FAM = diff --git a/test/Transforms/SampleProfile/entry_counts.ll b/test/Transforms/SampleProfile/entry_counts.ll index 6137a6908cf..cab7c87e049 100644 --- a/test/Transforms/SampleProfile/entry_counts.ll +++ b/test/Transforms/SampleProfile/entry_counts.ll @@ -9,8 +9,8 @@ entry: ret void, !dbg !9 } -; This function does not have profile, check if function_entry_count is 0 -; CHECK: {{.*}} = !{!"function_entry_count", i64 0} +; This function does not have profile, check if function_entry_count is -1 +; CHECK: {{.*}} = !{!"function_entry_count", i64 -1} define void @no_profile() { entry: ret void -- 2.11.0