[llvm] r264179 - Add getBlockProfileCount method to BlockFrequencyInfo
Easwaran Raman via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 23 11:18:27 PDT 2016
Author: eraman
Date: Wed Mar 23 13:18:26 2016
New Revision: 264179
URL: http://llvm.org/viewvc/llvm-project?rev=264179&view=rev
Log:
Add getBlockProfileCount method to BlockFrequencyInfo
Differential Revision: http://reviews.llvm.org/D18233
Added:
llvm/trunk/unittests/Analysis/BlockFrequencyInfoTest.cpp
Modified:
llvm/trunk/include/llvm/Analysis/BlockFrequencyInfo.h
llvm/trunk/include/llvm/ProfileData/ProfileCommon.h
llvm/trunk/lib/Analysis/BlockFrequencyInfo.cpp
llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/trunk/unittests/Analysis/CMakeLists.txt
Modified: llvm/trunk/include/llvm/Analysis/BlockFrequencyInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/BlockFrequencyInfo.h?rev=264179&r1=264178&r2=264179&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/BlockFrequencyInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/BlockFrequencyInfo.h Wed Mar 23 13:18:26 2016
@@ -14,6 +14,7 @@
#ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
#define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
+#include "llvm/ADT/Optional.h"
#include "llvm/Pass.h"
#include "llvm/Support/BlockFrequency.h"
#include <climits>
@@ -45,6 +46,11 @@ public:
/// floating points.
BlockFrequency getBlockFreq(const BasicBlock *BB) const;
+ /// \brief Returns the estimated profile count of \p BB.
+ /// This computes the relative block frequency of \p BB and multiplies it by
+ /// the enclosing function's count (if available) and returns the value.
+ Optional<uint64_t> getBlockProfileCount(const BasicBlock *BB) const;
+
// Set the frequency of the given basic block.
void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
Modified: llvm/trunk/include/llvm/ProfileData/ProfileCommon.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/ProfileCommon.h?rev=264179&r1=264178&r2=264179&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/ProfileCommon.h (original)
+++ llvm/trunk/include/llvm/ProfileData/ProfileCommon.h Wed Mar 23 13:18:26 2016
@@ -185,18 +185,5 @@ SummaryEntryVector &ProfileSummary::getD
return DetailedSummary;
}
-/// Helper to compute the profile count for a block, based on the
-/// ratio of its frequency to the entry block frequency, multiplied
-/// by the entry block count.
-inline uint64_t getBlockProfileCount(uint64_t BlockFreq, uint64_t EntryFreq,
- uint64_t EntryCount) {
- APInt ScaledCount(128, EntryCount);
- APInt BlockFreqAPInt(128, BlockFreq);
- APInt EntryFreqAPInt(128, EntryFreq);
- ScaledCount *= BlockFreqAPInt;
- ScaledCount = ScaledCount.udiv(EntryFreqAPInt);
- return ScaledCount.getLimitedValue();
-}
-
} // end namespace llvm
#endif
Modified: llvm/trunk/lib/Analysis/BlockFrequencyInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BlockFrequencyInfo.cpp?rev=264179&r1=264178&r2=264179&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/BlockFrequencyInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/BlockFrequencyInfo.cpp Wed Mar 23 13:18:26 2016
@@ -129,6 +129,20 @@ BlockFrequency BlockFrequencyInfo::getBl
return BFI ? BFI->getBlockFreq(BB) : 0;
}
+Optional<uint64_t>
+BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
+ auto EntryCount = getFunction()->getEntryCount();
+ if (!EntryCount)
+ return None;
+ // Use 128 bit APInt to do the arithmetic to avoid overflow.
+ APInt BlockCount(128, EntryCount.getValue());
+ APInt BlockFreq(128, getBlockFreq(BB).getFrequency());
+ APInt EntryFreq(128, getEntryFreq());
+ BlockCount *= BlockFreq;
+ BlockCount = BlockCount.udiv(EntryFreq);
+ return BlockCount.getLimitedValue();
+}
+
void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB,
uint64_t Freq) {
assert(BFI && "Expected analysis to be available");
Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=264179&r1=264178&r2=264179&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Wed Mar 23 13:18:26 2016
@@ -34,7 +34,6 @@
#include "llvm/IR/Operator.h"
#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/ValueSymbolTable.h"
-#include "llvm/ProfileData/ProfileCommon.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
@@ -2575,14 +2574,11 @@ static void WriteFunction(
auto *CalledFunction = CS.getCalledFunction();
if (CalledFunction && CalledFunction->hasName() &&
!CalledFunction->isIntrinsic()) {
- uint64_t ScaledCount = 0;
- if (HasProfileData)
- ScaledCount = getBlockProfileCount(
- BFI->getBlockFreq(&(*BB)).getFrequency(), BFI->getEntryFreq(),
- F.getEntryCount().getValue());
+ auto ScaledCount = BFI ? BFI->getBlockProfileCount(&*BB) : None;
unsigned CalleeId = VE.getValueID(
M->getValueSymbolTable().lookup(CalledFunction->getName()));
- CallGraphEdges[CalleeId] += ScaledCount;
+ CallGraphEdges[CalleeId] +=
+ (ScaledCount ? ScaledCount.getValue() : 0);
}
}
findRefEdges(&*I, VE, RefEdges, Visited);
Added: llvm/trunk/unittests/Analysis/BlockFrequencyInfoTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/BlockFrequencyInfoTest.cpp?rev=264179&view=auto
==============================================================================
--- llvm/trunk/unittests/Analysis/BlockFrequencyInfoTest.cpp (added)
+++ llvm/trunk/unittests/Analysis/BlockFrequencyInfoTest.cpp Wed Mar 23 13:18:26 2016
@@ -0,0 +1,86 @@
+//===- BlockFrequencyInfoTest.cpp - BlockFrequencyInfo unit tests ---------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/BlockFrequencyInfo.h"
+#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
+#include "llvm/Analysis/BranchProbabilityInfo.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Dominators.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/DataTypes.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gtest/gtest.h"
+
+namespace llvm {
+namespace {
+
+class BlockFrequencyInfoTest : public testing::Test {
+protected:
+ std::unique_ptr<BranchProbabilityInfo> BPI;
+ std::unique_ptr<DominatorTree> DT;
+ std::unique_ptr<LoopInfo> LI;
+
+ BlockFrequencyInfo buildBFI(Function &F) {
+ DT.reset(new DominatorTree(F));
+ LI.reset(new LoopInfo(*DT));
+ BPI.reset(new BranchProbabilityInfo(F, *LI));
+ return BlockFrequencyInfo(F, *BPI, *LI);
+ }
+ std::unique_ptr<Module> makeLLVMModule() {
+ const char *ModuleStrig = "define i32 @f(i32 %x) {\n"
+ "bb0:\n"
+ " %y1 = icmp eq i32 %x, 0 \n"
+ " br i1 %y1, label %bb1, label %bb2 \n"
+ "bb1:\n"
+ " br label %bb3\n"
+ "bb2:\n"
+ " br label %bb3\n"
+ "bb3:\n"
+ " %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
+ " ret i32 %y2\n"
+ "}\n";
+ LLVMContext &C = getGlobalContext();
+ SMDiagnostic Err;
+ return parseAssemblyString(ModuleStrig, Err, C);
+ }
+};
+
+TEST_F(BlockFrequencyInfoTest, Basic) {
+ auto M = makeLLVMModule();
+ Function *F = M->getFunction("f");
+ F->setEntryCount(100);
+
+ BlockFrequencyInfo BFI = buildBFI(*F);
+ BasicBlock &BB0 = F->getEntryBlock();
+ BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
+ BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
+ BasicBlock *BB3 = BB1->getSingleSuccessor();
+
+ uint64_t BB0Freq = BFI.getBlockFreq(&BB0).getFrequency();
+ uint64_t BB1Freq = BFI.getBlockFreq(BB1).getFrequency();
+ uint64_t BB2Freq = BFI.getBlockFreq(BB2).getFrequency();
+ uint64_t BB3Freq = BFI.getBlockFreq(BB3).getFrequency();
+
+ EXPECT_EQ(BB0Freq, BB3Freq);
+ EXPECT_EQ(BB0Freq, BB1Freq + BB2Freq);
+ EXPECT_EQ(BB0Freq, BB3Freq);
+
+ EXPECT_EQ(BFI.getBlockProfileCount(&BB0).getValue(), UINT64_C(100));
+ EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(100));
+ EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(), 100 * BB1Freq / BB0Freq);
+ EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(), 100 * BB2Freq / BB0Freq);
+}
+
+} // end anonymous namespace
+} // end namespace llvm
Modified: llvm/trunk/unittests/Analysis/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/CMakeLists.txt?rev=264179&r1=264178&r2=264179&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Analysis/CMakeLists.txt Wed Mar 23 13:18:26 2016
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_unittest(AnalysisTests
AliasAnalysisTest.cpp
+ BlockFrequencyInfoTest.cpp
CallGraphTest.cpp
CFGTest.cpp
CGSCCPassManagerTest.cpp
More information about the llvm-commits
mailing list