From d26200423ee818e54d4088bd0c499caf840d866d Mon Sep 17 00:00:00 2001 From: Manman Ren Date: Tue, 28 Aug 2012 22:21:25 +0000 Subject: Profile: set branch weight metadata with data generated from profiling. This patch implements ProfileDataLoader which loads profile data generated by -insert-edge-profiling and updates branch weight metadata accordingly. Patch by Alastair Murray. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162799 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ProfileDataLoader.cpp | 186 +++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 lib/Analysis/ProfileDataLoader.cpp (limited to 'lib/Analysis/ProfileDataLoader.cpp') diff --git a/lib/Analysis/ProfileDataLoader.cpp b/lib/Analysis/ProfileDataLoader.cpp new file mode 100644 index 0000000..0006492 --- /dev/null +++ b/lib/Analysis/ProfileDataLoader.cpp @@ -0,0 +1,186 @@ +//===- ProfileDataLoader.cpp - Load profile information from disk ---------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// The ProfileDataLoader class is used to load raw profiling data from the dump +// file. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/InstrTypes.h" +#include "llvm/Analysis/ProfileDataLoader.h" +#include "llvm/Analysis/ProfileDataTypes.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +using namespace llvm; + +namespace llvm { + +template<> +char ProfileDataT::ID = 0; + +raw_ostream& operator<<(raw_ostream &O, const Function *F) { + return O << F->getName(); +} + +raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) { + return O << BB->getName(); +} + +raw_ostream& operator<<(raw_ostream &O, std::pair E) { + O << "("; + + if (E.first) + O << E.first; + else + O << "0"; + + O << ","; + + if (E.second) + O << E.second; + else + O << "0"; + + return O << ")"; +} + +} // namespace llvm + +/// ByteSwap - Byteswap 'Var' if 'Really' is true. Required when the compiler +/// host and target have different endianness. +static inline unsigned ByteSwap(unsigned Var, bool Really) { + if (!Really) return Var; + return ((Var & (255U<< 0U)) << 24U) | + ((Var & (255U<< 8U)) << 8U) | + ((Var & (255U<<16U)) >> 8U) | + ((Var & (255U<<24U)) >> 24U); +} + +/// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one +/// (or both) may not be defined. +static unsigned AddCounts(unsigned A, unsigned B) { + // If either value is undefined, use the other. + // Undefined + undefined = undefined. + if (A == ProfileDataLoader::Uncounted) return B; + if (B == ProfileDataLoader::Uncounted) return A; + + // Saturate to the maximum storable value. This could change taken/nottaken + // ratios, but is presumably better than wrapping and thus potentially + // inverting ratios. + unsigned long long tmp = (unsigned long long)A + (unsigned long long)B; + if (tmp > (unsigned long long)ProfileDataLoader::MaxCount) + tmp = ProfileDataLoader::MaxCount; + return (unsigned)tmp; +} + +/// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F' +template +static void ReadProfilingData(const char *ToolName, FILE *F, + std::vector &Data, size_t NumEntries) { + // Read in the block of data... + if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) { + errs() << ToolName << ": profiling data truncated!\n"; + perror(0); + exit(1); + } +} + +/// ReadProfilingNumEntries - Read how many entries are in this profiling data +/// packet. +static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F, + bool ShouldByteSwap) { + std::vector NumEntries(1); + ReadProfilingData(ToolName, F, NumEntries, 1); + return ByteSwap(NumEntries[0], ShouldByteSwap); +} + +/// ReadProfilingBlock - Read the number of entries in the next profiling data +/// packet and then accumulate the entries into 'Data'. +static void ReadProfilingBlock(const char *ToolName, FILE *F, + bool ShouldByteSwap, + std::vector &Data) { + // Read the number of entries... + unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); + + // Read in the data. + std::vector TempSpace(NumEntries); + ReadProfilingData(ToolName, F, TempSpace, (size_t)NumEntries); + + // Make sure we have enough space ... + if (Data.size() < NumEntries) + Data.resize(NumEntries, ProfileDataLoader::Uncounted); + + // Accumulate the data we just read into the existing data. + for (unsigned i = 0; i < NumEntries; ++i) { + Data[i] = AddCounts(ByteSwap(TempSpace[i], ShouldByteSwap), Data[i]); + } +} + +/// ReadProfilingArgBlock - Read the command line arguments that the progam was +/// run with when the current profiling data packet(s) were generated. +static void ReadProfilingArgBlock(const char *ToolName, FILE *F, + bool ShouldByteSwap, + std::vector &CommandLines) { + // Read the number of bytes ... + unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); + + // Read in the arguments (if there are any to read). Round up the length to + // the nearest 4-byte multiple. + std::vector Args(ArgLength+4); + if (ArgLength) + ReadProfilingData(ToolName, F, Args, (ArgLength+3) & ~3); + + // Store the arguments. + CommandLines.push_back(std::string(&Args[0], &Args[ArgLength])); +} + +const unsigned ProfileDataLoader::Uncounted = ~0U; +const unsigned ProfileDataLoader::MaxCount = ~0U - 1U; + +/// ProfileDataLoader ctor - Read the specified profiling data file, exiting +/// the program if the file is invalid or broken. +ProfileDataLoader::ProfileDataLoader(const char *ToolName, + const std::string &Filename) + : Filename(Filename) { + FILE *F = fopen(Filename.c_str(), "rb"); + if (F == 0) { + errs() << ToolName << ": Error opening '" << Filename << "': "; + perror(0); + exit(1); + } + + // Keep reading packets until we run out of them. + unsigned PacketType; + while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) { + // If the low eight bits of the packet are zero, we must be dealing with an + // endianness mismatch. Byteswap all words read from the profiling + // information. This can happen when the compiler host and target have + // different endianness. + bool ShouldByteSwap = (char)PacketType == 0; + PacketType = ByteSwap(PacketType, ShouldByteSwap); + + switch (PacketType) { + case ArgumentInfo: + ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines); + break; + + case EdgeInfo: + ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts); + break; + + default: + errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n"; + exit(1); + } + } + + fclose(F); +} -- cgit v1.1 From f91e400b2125067e75e0f2159bca7ffd0cf109a0 Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Fri, 31 Aug 2012 05:18:31 +0000 Subject: Cleanups due to feedback. No functionality change. Patch by Alistair. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162979 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ProfileDataLoader.cpp | 68 +++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'lib/Analysis/ProfileDataLoader.cpp') diff --git a/lib/Analysis/ProfileDataLoader.cpp b/lib/Analysis/ProfileDataLoader.cpp index 0006492..01b5038 100644 --- a/lib/Analysis/ProfileDataLoader.cpp +++ b/lib/Analysis/ProfileDataLoader.cpp @@ -12,11 +12,14 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/OwningPtr.h" #include "llvm/Module.h" #include "llvm/InstrTypes.h" #include "llvm/Analysis/ProfileDataLoader.h" #include "llvm/Analysis/ProfileDataTypes.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" #include #include using namespace llvm; @@ -34,7 +37,8 @@ raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) { return O << BB->getName(); } -raw_ostream& operator<<(raw_ostream &O, std::pair E) { +raw_ostream& operator<<(raw_ostream &O, std::pair E) { O << "("; if (E.first) @@ -54,10 +58,9 @@ raw_ostream& operator<<(raw_ostream &O, std::pair> 8U) | @@ -75,21 +78,19 @@ static unsigned AddCounts(unsigned A, unsigned B) { // Saturate to the maximum storable value. This could change taken/nottaken // ratios, but is presumably better than wrapping and thus potentially // inverting ratios. - unsigned long long tmp = (unsigned long long)A + (unsigned long long)B; - if (tmp > (unsigned long long)ProfileDataLoader::MaxCount) + uint64_t tmp = (uint64_t)A + (uint64_t)B; + if (tmp > (uint64_t)ProfileDataLoader::MaxCount) tmp = ProfileDataLoader::MaxCount; return (unsigned)tmp; } /// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F' -template +template static void ReadProfilingData(const char *ToolName, FILE *F, - std::vector &Data, size_t NumEntries) { + SmallVector &Data, size_t NumEntries) { // Read in the block of data... if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) { - errs() << ToolName << ": profiling data truncated!\n"; - perror(0); - exit(1); + report_fatal_error(std::string(ToolName) + ": Profiling data truncated"); } } @@ -97,46 +98,46 @@ static void ReadProfilingData(const char *ToolName, FILE *F, /// packet. static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F, bool ShouldByteSwap) { - std::vector NumEntries(1); - ReadProfilingData(ToolName, F, NumEntries, 1); - return ByteSwap(NumEntries[0], ShouldByteSwap); + SmallVector NumEntries(1); + ReadProfilingData(ToolName, F, NumEntries, 1); + return ShouldByteSwap ? ByteSwap(NumEntries[0]) : NumEntries[0]; } /// ReadProfilingBlock - Read the number of entries in the next profiling data /// packet and then accumulate the entries into 'Data'. static void ReadProfilingBlock(const char *ToolName, FILE *F, bool ShouldByteSwap, - std::vector &Data) { + SmallVector &Data) { // Read the number of entries... unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); // Read in the data. - std::vector TempSpace(NumEntries); - ReadProfilingData(ToolName, F, TempSpace, (size_t)NumEntries); + SmallVector TempSpace(NumEntries); + ReadProfilingData(ToolName, F, TempSpace, (size_t)NumEntries); // Make sure we have enough space ... if (Data.size() < NumEntries) Data.resize(NumEntries, ProfileDataLoader::Uncounted); // Accumulate the data we just read into the existing data. - for (unsigned i = 0; i < NumEntries; ++i) { - Data[i] = AddCounts(ByteSwap(TempSpace[i], ShouldByteSwap), Data[i]); - } + for (unsigned i = 0; i < NumEntries; ++i) + Data[i] = AddCounts(ShouldByteSwap ? ByteSwap(TempSpace[i]) : TempSpace[i], + Data[i]); } /// ReadProfilingArgBlock - Read the command line arguments that the progam was /// run with when the current profiling data packet(s) were generated. static void ReadProfilingArgBlock(const char *ToolName, FILE *F, bool ShouldByteSwap, - std::vector &CommandLines) { + SmallVector &CommandLines) { // Read the number of bytes ... unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); // Read in the arguments (if there are any to read). Round up the length to // the nearest 4-byte multiple. - std::vector Args(ArgLength+4); + SmallVector Args(ArgLength+4); if (ArgLength) - ReadProfilingData(ToolName, F, Args, (ArgLength+3) & ~3); + ReadProfilingData(ToolName, F, Args, (ArgLength+3) & ~3); // Store the arguments. CommandLines.push_back(std::string(&Args[0], &Args[ArgLength])); @@ -145,17 +146,15 @@ static void ReadProfilingArgBlock(const char *ToolName, FILE *F, const unsigned ProfileDataLoader::Uncounted = ~0U; const unsigned ProfileDataLoader::MaxCount = ~0U - 1U; -/// ProfileDataLoader ctor - Read the specified profiling data file, exiting -/// the program if the file is invalid or broken. +/// ProfileDataLoader ctor - Read the specified profiling data file, reporting +/// a fatal error if the file is invalid or broken. ProfileDataLoader::ProfileDataLoader(const char *ToolName, const std::string &Filename) : Filename(Filename) { FILE *F = fopen(Filename.c_str(), "rb"); - if (F == 0) { - errs() << ToolName << ": Error opening '" << Filename << "': "; - perror(0); - exit(1); - } + if (F == 0) + report_fatal_error(std::string(ToolName) + ": Error opening '" + + Filename + "': "); // Keep reading packets until we run out of them. unsigned PacketType; @@ -165,7 +164,7 @@ ProfileDataLoader::ProfileDataLoader(const char *ToolName, // information. This can happen when the compiler host and target have // different endianness. bool ShouldByteSwap = (char)PacketType == 0; - PacketType = ByteSwap(PacketType, ShouldByteSwap); + PacketType = ShouldByteSwap ? ByteSwap(PacketType) : PacketType; switch (PacketType) { case ArgumentInfo: @@ -177,8 +176,9 @@ ProfileDataLoader::ProfileDataLoader(const char *ToolName, break; default: - errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n"; - exit(1); + report_fatal_error(std::string(ToolName) + + ": Unknown profiling packet type"); + break; } } -- cgit v1.1 From cb5f63d7fa717b67a666712a3a0d7eebd4d8fb8b Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Fri, 31 Aug 2012 12:43:07 +0000 Subject: Clean up ProfileDataLoader a bit. - Overloading operator<< for raw_ostream and pointers is dangerous, it alters the behavior of code that includes the header. - Remove unused ID. - Use LLVM's byte swapping helpers instead of a hand-coded. - Make ReadProfilingData work directly on a pointer. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162992 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ProfileDataLoader.cpp | 62 ++++++++++++-------------------------- 1 file changed, 19 insertions(+), 43 deletions(-) (limited to 'lib/Analysis/ProfileDataLoader.cpp') diff --git a/lib/Analysis/ProfileDataLoader.cpp b/lib/Analysis/ProfileDataLoader.cpp index 01b5038..69286ef 100644 --- a/lib/Analysis/ProfileDataLoader.cpp +++ b/lib/Analysis/ProfileDataLoader.cpp @@ -24,49 +24,25 @@ #include using namespace llvm; -namespace llvm { - -template<> -char ProfileDataT::ID = 0; - -raw_ostream& operator<<(raw_ostream &O, const Function *F) { - return O << F->getName(); -} - -raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) { - return O << BB->getName(); -} - -raw_ostream& operator<<(raw_ostream &O, std::pair E) { +raw_ostream &llvm::operator<<(raw_ostream &O, std::pair E) { O << "("; if (E.first) - O << E.first; + O << E.first->getName(); else O << "0"; O << ","; if (E.second) - O << E.second; + O << E.second->getName(); else O << "0"; return O << ")"; } -} // namespace llvm - -/// ByteSwap - Byteswap 'Var'. Required when the compiler host and target have -/// different endianness. -static inline unsigned ByteSwap(unsigned Var) { - return ((Var & (255U<< 0U)) << 24U) | - ((Var & (255U<< 8U)) << 8U) | - ((Var & (255U<<16U)) >> 8U) | - ((Var & (255U<<24U)) >> 24U); -} - /// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one /// (or both) may not be defined. static unsigned AddCounts(unsigned A, unsigned B) { @@ -85,22 +61,21 @@ static unsigned AddCounts(unsigned A, unsigned B) { } /// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F' -template +template static void ReadProfilingData(const char *ToolName, FILE *F, - SmallVector &Data, size_t NumEntries) { + T *Data, size_t NumEntries) { // Read in the block of data... - if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) { - report_fatal_error(std::string(ToolName) + ": Profiling data truncated"); - } + if (fread(Data, sizeof(T), NumEntries, F) != NumEntries) + report_fatal_error(Twine(ToolName) + ": Profiling data truncated"); } /// ReadProfilingNumEntries - Read how many entries are in this profiling data /// packet. static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F, bool ShouldByteSwap) { - SmallVector NumEntries(1); - ReadProfilingData(ToolName, F, NumEntries, 1); - return ShouldByteSwap ? ByteSwap(NumEntries[0]) : NumEntries[0]; + unsigned Entry; + ReadProfilingData(ToolName, F, &Entry, 1); + return ShouldByteSwap ? ByteSwap_32(Entry) : Entry; } /// ReadProfilingBlock - Read the number of entries in the next profiling data @@ -113,16 +88,17 @@ static void ReadProfilingBlock(const char *ToolName, FILE *F, // Read in the data. SmallVector TempSpace(NumEntries); - ReadProfilingData(ToolName, F, TempSpace, (size_t)NumEntries); + ReadProfilingData(ToolName, F, TempSpace.data(), NumEntries); // Make sure we have enough space ... if (Data.size() < NumEntries) Data.resize(NumEntries, ProfileDataLoader::Uncounted); // Accumulate the data we just read into the existing data. - for (unsigned i = 0; i < NumEntries; ++i) - Data[i] = AddCounts(ShouldByteSwap ? ByteSwap(TempSpace[i]) : TempSpace[i], - Data[i]); + for (unsigned i = 0; i < NumEntries; ++i) { + unsigned Entry = ShouldByteSwap ? ByteSwap_32(TempSpace[i]) : TempSpace[i]; + Data[i] = AddCounts(Entry, Data[i]); + } } /// ReadProfilingArgBlock - Read the command line arguments that the progam was @@ -137,7 +113,7 @@ static void ReadProfilingArgBlock(const char *ToolName, FILE *F, // the nearest 4-byte multiple. SmallVector Args(ArgLength+4); if (ArgLength) - ReadProfilingData(ToolName, F, Args, (ArgLength+3) & ~3); + ReadProfilingData(ToolName, F, Args.data(), (ArgLength+3) & ~3); // Store the arguments. CommandLines.push_back(std::string(&Args[0], &Args[ArgLength])); @@ -153,7 +129,7 @@ ProfileDataLoader::ProfileDataLoader(const char *ToolName, : Filename(Filename) { FILE *F = fopen(Filename.c_str(), "rb"); if (F == 0) - report_fatal_error(std::string(ToolName) + ": Error opening '" + + report_fatal_error(Twine(ToolName) + ": Error opening '" + Filename + "': "); // Keep reading packets until we run out of them. @@ -164,7 +140,7 @@ ProfileDataLoader::ProfileDataLoader(const char *ToolName, // information. This can happen when the compiler host and target have // different endianness. bool ShouldByteSwap = (char)PacketType == 0; - PacketType = ShouldByteSwap ? ByteSwap(PacketType) : PacketType; + PacketType = ShouldByteSwap ? ByteSwap_32(PacketType) : PacketType; switch (PacketType) { case ArgumentInfo: -- cgit v1.1