diff options
| author | Justin Bogner <mail@justinbogner.com> | 2014-03-12 21:06:31 +0000 |
|---|---|---|
| committer | Justin Bogner <mail@justinbogner.com> | 2014-03-12 21:06:31 +0000 |
| commit | 07cdaf54b0678548b578063229ba37eb79200ee1 (patch) | |
| tree | ca9009b7b0b019896be9117e568b13fdf347710c /lib/CodeGen/CodeGenPGO.cpp | |
| parent | 7c9210c534a8fb77c17da943ea7c7871833b0699 (diff) | |
| download | clang-07cdaf54b0678548b578063229ba37eb79200ee1.tar.gz clang-07cdaf54b0678548b578063229ba37eb79200ee1.tar.bz2 clang-07cdaf54b0678548b578063229ba37eb79200ee1.tar.xz | |
Revert "CodeGen: Use a binary format for instrumentation based profiling"
I've clearly done something wrong with how to get this to link
correctly. Reverting for now.
This reverts commit r203711.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203712 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CodeGenPGO.cpp')
| -rw-r--r-- | lib/CodeGen/CodeGenPGO.cpp | 184 |
1 files changed, 149 insertions, 35 deletions
diff --git a/lib/CodeGen/CodeGenPGO.cpp b/lib/CodeGen/CodeGenPGO.cpp index abd47ccd2c..3206daa76a 100644 --- a/lib/CodeGen/CodeGenPGO.cpp +++ b/lib/CodeGen/CodeGenPGO.cpp @@ -17,12 +17,132 @@ #include "clang/AST/StmtVisitor.h" #include "llvm/Config/config.h" // for strtoull()/strtoll() define #include "llvm/IR/MDBuilder.h" -#include "llvm/Profile/ProfileDataReader.h" #include "llvm/Support/FileSystem.h" using namespace clang; using namespace CodeGen; +static void ReportBadPGOData(CodeGenModule &CGM, const char *Message) { + DiagnosticsEngine &Diags = CGM.getDiags(); + unsigned diagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0"); + Diags.Report(diagID) << Message; +} + +PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path) + : CGM(CGM) { + if (llvm::MemoryBuffer::getFile(Path, DataBuffer)) { + ReportBadPGOData(CGM, "failed to open pgo data file"); + return; + } + + if (DataBuffer->getBufferSize() > std::numeric_limits<unsigned>::max()) { + ReportBadPGOData(CGM, "pgo data file too big"); + return; + } + + // Scan through the data file and map each function to the corresponding + // file offset where its counts are stored. + const char *BufferStart = DataBuffer->getBufferStart(); + const char *BufferEnd = DataBuffer->getBufferEnd(); + const char *CurPtr = BufferStart; + uint64_t MaxCount = 0; + while (CurPtr < BufferEnd) { + // Read the function name. + const char *FuncStart = CurPtr; + // For Objective-C methods, the name may include whitespace, so search + // backward from the end of the line to find the space that separates the + // name from the number of counters. (This is a temporary hack since we are + // going to completely replace this file format in the near future.) + CurPtr = strchr(CurPtr, '\n'); + if (!CurPtr) { + ReportBadPGOData(CGM, "pgo data file has malformed function entry"); + return; + } + while (*--CurPtr != ' ') + ; + StringRef FuncName(FuncStart, CurPtr - FuncStart); + + // Read the number of counters. + char *EndPtr; + unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10); + if (EndPtr == CurPtr || *EndPtr != '\n' || NumCounters <= 0) { + ReportBadPGOData(CGM, "pgo data file has unexpected number of counters"); + return; + } + CurPtr = EndPtr; + + // Read function count. + uint64_t Count = strtoll(CurPtr, &EndPtr, 10); + if (EndPtr == CurPtr || *EndPtr != '\n') { + ReportBadPGOData(CGM, "pgo-data file has bad count value"); + return; + } + CurPtr = EndPtr; // Point to '\n'. + FunctionCounts[FuncName] = Count; + MaxCount = Count > MaxCount ? Count : MaxCount; + + // There is one line for each counter; skip over those lines. + // Since function count is already read, we start the loop from 1. + for (unsigned N = 1; N < NumCounters; ++N) { + CurPtr = strchr(++CurPtr, '\n'); + if (!CurPtr) { + ReportBadPGOData(CGM, "pgo data file is missing some counter info"); + return; + } + } + + // Skip over the blank line separating functions. + CurPtr += 2; + + DataOffsets[FuncName] = FuncStart - BufferStart; + } + MaxFunctionCount = MaxCount; +} + +bool PGOProfileData::getFunctionCounts(StringRef FuncName, + std::vector<uint64_t> &Counts) { + // Find the relevant section of the pgo-data file. + llvm::StringMap<unsigned>::const_iterator OffsetIter = + DataOffsets.find(FuncName); + if (OffsetIter == DataOffsets.end()) + return true; + const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue(); + + // Skip over the function name. + CurPtr = strchr(CurPtr, '\n'); + assert(CurPtr && "pgo-data has corrupted function entry"); + while (*--CurPtr != ' ') + ; + + // Read the number of counters. + char *EndPtr; + unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10); + assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 && + "pgo-data file has corrupted number of counters"); + CurPtr = EndPtr; + + Counts.reserve(NumCounters); + + for (unsigned N = 0; N < NumCounters; ++N) { + // Read the count value. + uint64_t Count = strtoll(CurPtr, &EndPtr, 10); + if (EndPtr == CurPtr || *EndPtr != '\n') { + ReportBadPGOData(CGM, "pgo-data file has bad count value"); + return true; + } + Counts.push_back(Count); + CurPtr = EndPtr + 1; + } + + // Make sure the number of counters matches up. + if (Counts.size() != NumCounters) { + ReportBadPGOData(CGM, "pgo-data file has inconsistent counters"); + return true; + } + + return false; +} + void CodeGenPGO::setFuncName(llvm::Function *Fn) { StringRef Func = Fn->getName(); @@ -57,23 +177,22 @@ void CodeGenPGO::emitWriteoutFunction() { llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx); - llvm::Function *AddFuncsF = - CGM.getModule().getFunction("__llvm_pgo_add_functions"); - if (!AddFuncsF) { - llvm::FunctionType *AddFuncsFTy = + llvm::Function *WriteoutF = + CGM.getModule().getFunction("__llvm_pgo_writeout"); + if (!WriteoutF) { + llvm::FunctionType *WriteoutFTy = llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx), false); - AddFuncsF = llvm::Function::Create(AddFuncsFTy, + WriteoutF = llvm::Function::Create(WriteoutFTy, llvm::GlobalValue::InternalLinkage, - "__llvm_pgo_add_functions", - &CGM.getModule()); + "__llvm_pgo_writeout", &CGM.getModule()); } - AddFuncsF->setUnnamedAddr(true); - AddFuncsF->addFnAttr(llvm::Attribute::NoInline); + WriteoutF->setUnnamedAddr(true); + WriteoutF->addFnAttr(llvm::Attribute::NoInline); if (CGM.getCodeGenOpts().DisableRedZone) - AddFuncsF->addFnAttr(llvm::Attribute::NoRedZone); + WriteoutF->addFnAttr(llvm::Attribute::NoRedZone); - llvm::BasicBlock *BB = AddFuncsF->empty() ? - llvm::BasicBlock::Create(Ctx, "", AddFuncsF) : &AddFuncsF->getEntryBlock(); + llvm::BasicBlock *BB = WriteoutF->empty() ? + llvm::BasicBlock::Create(Ctx, "", WriteoutF) : &WriteoutF->getEntryBlock(); CGBuilderTy PGOBuilder(BB); @@ -83,35 +202,32 @@ void CodeGenPGO::emitWriteoutFunction() { PGOBuilder.SetInsertPoint(I); llvm::Type *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx); - llvm::Type *Int64Ty = llvm::Type::getInt64Ty(Ctx); llvm::Type *Args[] = { Int8PtrTy, // const char *FuncName - Int64Ty, // uint64_t FunctionHash Int32Ty, // uint32_t NumCounters Int64PtrTy // uint64_t *Counters }; llvm::FunctionType *FTy = llvm::FunctionType::get(PGOBuilder.getVoidTy(), Args, false); llvm::Constant *EmitFunc = - CGM.getModule().getOrInsertFunction("llvm_pgo_add_function", FTy); + CGM.getModule().getOrInsertFunction("llvm_pgo_emit", FTy); llvm::Constant *NameString = CGM.GetAddrOfConstantCString(getFuncName(), "__llvm_pgo_name"); NameString = llvm::ConstantExpr::getBitCast(NameString, Int8PtrTy); - PGOBuilder.CreateCall4(EmitFunc, NameString, - // TODO: This should be a hash, not just the count! - PGOBuilder.getInt64(NumRegionCounters), + PGOBuilder.CreateCall3(EmitFunc, NameString, PGOBuilder.getInt32(NumRegionCounters), PGOBuilder.CreateBitCast(RegionCounters, Int64PtrTy)); } llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) { - llvm::Function *AddFuncsF = - CGM.getModule().getFunction("__llvm_pgo_add_functions"); - if (!AddFuncsF) + llvm::Function *WriteoutF = + CGM.getModule().getFunction("__llvm_pgo_writeout"); + if (!WriteoutF) return NULL; - // Create a small bit of code that initializes PGO at startup + // Create a small bit of code that registers the "__llvm_pgo_writeout" to + // be executed at exit. llvm::Function *F = CGM.getModule().getFunction("__llvm_pgo_init"); if (F) return NULL; @@ -139,7 +255,7 @@ llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) { // Inialize the environment and register the local writeout function. llvm::Constant *PGOInit = CGM.getModule().getOrInsertFunction("llvm_pgo_init", FTy); - PGOBuilder.CreateCall(PGOInit, AddFuncsF); + PGOBuilder.CreateCall(PGOInit, WriteoutF); PGOBuilder.CreateRetVoid(); return F; @@ -642,8 +758,8 @@ namespace { void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; - llvm::ProfileDataReader *PGOReader = CGM.getPGOReader(); - if (!InstrumentRegions && !PGOReader) + PGOProfileData *PGOData = CGM.getPGOData(); + if (!InstrumentRegions && !PGOData) return; if (!D) return; @@ -651,10 +767,10 @@ void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { mapRegionCounters(D); if (InstrumentRegions) emitCounterVariables(); - if (PGOReader) { - loadRegionCounts(PGOReader); + if (PGOData) { + loadRegionCounts(PGOData); computeRegionCounts(D); - applyFunctionAttributes(PGOReader, Fn); + applyFunctionAttributes(PGOData, Fn); } } @@ -681,12 +797,12 @@ void CodeGenPGO::computeRegionCounts(const Decl *D) { Walker.VisitBlockDecl(BD); } -void CodeGenPGO::applyFunctionAttributes(llvm::ProfileDataReader *PGOReader, +void CodeGenPGO::applyFunctionAttributes(PGOProfileData *PGOData, llvm::Function *Fn) { if (!haveRegionCounts()) return; - uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount(); + uint64_t MaxFunctionCount = PGOData->getMaximumFunctionCount(); uint64_t FunctionCount = getRegionCount(0); if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount)) // Turn on InlineHint attribute for hot functions. @@ -719,15 +835,13 @@ void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) { Builder.CreateStore(Count, Addr); } -void CodeGenPGO::loadRegionCounts(llvm::ProfileDataReader *PGOReader) { +void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) { // For now, ignore the counts from the PGO data file only if the number of // counters does not match. This could be tightened down in the future to // ignore counts when the input changes in various ways, e.g., by comparing a // hash value based on some characteristics of the input. RegionCounts = new std::vector<uint64_t>(); - uint64_t Hash; - // TODO: Check for hash mismatch - if (PGOReader->getFunctionCounts(getFuncName(), Hash, *RegionCounts) || + if (PGOData->getFunctionCounts(getFuncName(), *RegionCounts) || RegionCounts->size() != NumRegionCounters) { delete RegionCounts; RegionCounts = 0; |
