1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00

[CSSPGO][llvm-profgen] Add brackets for context id to support extended binary format

To align with https://reviews.llvm.org/D95547, we need to add brackets for context id before initializing the `SampleContext`.

Also added test cases for extended binary format from llvm-profgen side.

Differential Revision: https://reviews.llvm.org/D95929
This commit is contained in:
wlei 2021-02-03 14:13:06 -08:00
parent 4b43882cd0
commit fafe9e7911
9 changed files with 105 additions and 77 deletions

View File

@ -360,10 +360,7 @@ std::error_code SampleProfileWriterCompactBinary::write(
/// it needs to be parsed by the SampleProfileReaderText class.
std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
auto &OS = *OutputStream;
if (FunctionSamples::ProfileIsCS)
OS << "[" << S.getNameWithContext() << "]:" << S.getTotalSamples();
else
OS << S.getName() << ":" << S.getTotalSamples();
OS << S.getNameWithContext(true) << ":" << S.getTotalSamples();
if (Indent == 0)
OS << ":" << S.getHeadSamples();
OS << "\n";

View File

@ -0,0 +1,14 @@
; test for dwarf-based cs profile
; RUN: llvm-profgen --format=extbinary --perfscript=%S/Inputs/recursion-compression-noprobe.perfscript --binary=%S/Inputs/recursion-compression-noprobe.perfbin --output=%t1 --csprof-cold-thres=0
; RUN: llvm-profdata merge --sample --text --output=%t2 %t1
; RUN: FileCheck %S/recursion-compression-noprobe.test --input-file %t2
; RUN: llvm-profdata merge --sample --extbinary --output=%t3 %t2 && llvm-profdata merge --sample --text --output=%t4 %t3
; RUN: diff -b %t2 %t4
; test for probe-based cs profile
; RUN: llvm-profgen --format=extbinary --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t5 --csprof-cold-thres=0
; RUN: llvm-profdata merge --sample --text --output=%t6 %t5
; RUN: FileCheck %S/recursion-compression-pseudoprobe.test --input-file %t6
; RUN: llvm-profdata merge --sample --extbinary --output=%t7 %t6 && llvm-profdata merge --sample --text --output=%t8 %t7
; RUN: diff -b %t6 %t8

View File

@ -2,11 +2,11 @@
; RUN: FileCheck %s --input-file %t
; CHECK:[main:1 @ foo]:44:0
; CHECK: 2.2: 14
; CHECK: 2.1: 14
; CHECK: 3: 15
; CHECK: 3.2: 14 bar:14
; CHECK: 3.4: 1
; CHECK:[main:1 @ foo:3.2 @ bar]:14:0
; CHECK: 3.1: 14 bar:14
; CHECK: 3.2: 1
; CHECK:[main:1 @ foo:3.1 @ bar]:14:0
; CHECK: 1: 14
; CHECK-UNWINDER: Binary(inline-cs-noprobe.perfbin)'s Range Counter:
@ -15,10 +15,9 @@
; CHECK-UNWINDER: (67e, 69b): 1
; CHECK-UNWINDER: (67e, 6ad): 13
; CHECK-UNWINDER: (6bd, 6c8): 14
; CHECK-UNWINDER: main:1 @ foo:3.2 @ bar
; CHECK-UNWINDER: main:1 @ foo:3.1 @ bar
; CHECK-UNWINDER: (6af, 6bb): 14
; CHECK-UNWINDER: Binary(inline-cs-noprobe.perfbin)'s Branch Counter:
; CHECK-UNWINDER: main:1 @ foo
; CHECK-UNWINDER: (69b, 670): 1

View File

@ -36,6 +36,8 @@
; original code:
; clang -O0 -g test.c -o a.out
#include <stdio.h>

View File

@ -10,16 +10,17 @@
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb]:12:0
; CHECK-UNCOMPRESS: 1: 11
; CHECK-UNCOMPRESS: 2: 1 fa:1
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa]:3:0
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 2: 2 fb:1
; CHECK-UNCOMPRESS:[main:1 @ foo]:3:0
; CHECK-UNCOMPRESS: 2: 1
; CHECK-UNCOMPRESS: 3: 2 fa:1
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa:2 @ fb:2 @ fa]:1:0
; CHECK-UNCOMPRESS: 4: 1
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa]:3:0
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 2: 2 fb:1
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa:2 @ fb]:1:0
; CHECK-UNCOMPRESS: 2: 1 fa:1
; CHECK-UNCOMPRESS:[main:1 @ foo:3 @ fa:2 @ fb:2 @ fa:2 @ fb:2 @ fa]:1:0
; CHECK-UNCOMPRESS: 4: 1
; CHECK: [main:1 @ foo:3 @ fa]:14:0
; CHECK: 1: 1

View File

@ -4,11 +4,11 @@
; RUN: llvm-profgen --perfscript=%S/Inputs/recursion-compression-pseudoprobe.perfscript --binary=%S/Inputs/recursion-compression-pseudoprobe.perfbin --output=%t --show-unwinder-output --csprof-cold-thres=0 | FileCheck %s --check-prefix=CHECK-UNWINDER
; RUN: FileCheck %s --input-file %t
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa]:4:1
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa]:4:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 4: 1
; CHECK-UNCOMPRESS: 7: 1 fb:1
; CHECK-UNCOMPRESS: 5: 1
; CHECK-UNCOMPRESS: 8: 1 fa:1
; CHECK-UNCOMPRESS: !CFGChecksum: 120515930909
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa]:4:1
; CHECK-UNCOMPRESS: 1: 1
@ -16,28 +16,13 @@
; CHECK-UNCOMPRESS: 4: 1
; CHECK-UNCOMPRESS: 7: 1 fb:1
; CHECK-UNCOMPRESS: !CFGChecksum: 120515930909
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa]:4:1
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa]:4:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 5: 1
; CHECK-UNCOMPRESS: 8: 1 fa:1
; CHECK-UNCOMPRESS: 4: 1
; CHECK-UNCOMPRESS: 7: 1 fb:1
; CHECK-UNCOMPRESS: !CFGChecksum: 120515930909
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 6: 1 fa:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 6: 1 fa:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 6: 1 fa:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 2: 1
; CHECK-UNCOMPRESS: 5: 1 fb:1
@ -47,11 +32,26 @@
; CHECK-UNCOMPRESS: 2: 1
; CHECK-UNCOMPRESS: 5: 1 fb:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 2: 1
; CHECK-UNCOMPRESS: 5: 1 fb:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 6: 1 fa:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 6: 1 fa:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
; CHECK-UNCOMPRESS: 6: 1 fa:1
; CHECK-UNCOMPRESS: !CFGChecksum: 72617220756
; CHECK-UNCOMPRESS: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa:7 @ fb:6 @ fa]:2:1
; CHECK-UNCOMPRESS: 1: 1
; CHECK-UNCOMPRESS: 3: 1
@ -74,28 +74,29 @@
; CHECK: 4: 1
; CHECK: 7: 1 fb:1
; CHECK: !CFGChecksum: 120515930909
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa]:4:1
; CHECK: 1: 1
; CHECK: 3: 1
; CHECK: 4: 1
; CHECK: 7: 1 fb:1
; CHECK: !CFGChecksum: 120515930909
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa]:4:1
; CHECK: 1: 1
; CHECK: 3: 1
; CHECK: 5: 1
; CHECK: 8: 1 fa:1
; CHECK: !CFGChecksum: 120515930909
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa]:4:1
; CHECK: 1: 1
; CHECK: 3: 1
; CHECK: 6: 1 fa:1
; CHECK: !CFGChecksum: 72617220756
; CHECK: 4: 1
; CHECK: 7: 1 fb:1
; CHECK: !CFGChecksum: 120515930909
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb]:3:1
; CHECK: 1: 1
; CHECK: 3: 1
; CHECK: 6: 1 fa:1
; CHECK: !CFGChecksum: 72617220756
; CHECK: [main:2 @ foo:5 @ fa:8 @ fa:7 @ fb:5 @ fb:6 @ fa:8 @ fa:7 @ fb:6 @ fa:7 @ fb]:3:1
; CHECK: 1: 1
; CHECK: 3: 1
; CHECK: 6: 1 fa:1
; CHECK: !CFGChecksum: 72617220756
; CHECK-UNWINDER: Binary(recursion-compression-pseudoprobe.perfbin)'s Range Counter:

View File

@ -76,13 +76,16 @@ ProfileGenerator::create(const BinarySampleCounterMap &BinarySampleCounters,
return ProfileGenerator;
}
void ProfileGenerator::write(std::unique_ptr<SampleProfileWriter> Writer,
StringMap<FunctionSamples> &ProfileMap) {
Writer->write(ProfileMap);
}
void ProfileGenerator::write() {
auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
if (std::error_code EC = WriterOrErr.getError())
exitWithError(EC, OutputFilename);
auto Writer = std::move(WriterOrErr.get());
mergeAndTrimColdProfile(ProfileMap);
Writer->write(ProfileMap);
write(std::move(WriterOrErr.get()), ProfileMap);
}
void ProfileGenerator::findDisjointRanges(RangeSample &DisjointRanges,
@ -188,7 +191,6 @@ CSProfileGenerator::getFunctionProfileForContext(StringRef ContextStr) {
if (Ret.second) {
SampleContext FContext(Ret.first->first(), RawContext);
FunctionSamples &FProfile = Ret.first->second;
FProfile.setName(FContext.getNameWithoutContext());
FProfile.setContext(FContext);
}
return Ret.first->second;
@ -268,16 +270,15 @@ void CSProfileGenerator::populateFunctionBoundarySamples(
CalleeName, Count);
// Record head sample for called target(callee)
// TODO: Cleanup ' @ '
std::string CalleeContextId =
getCallSite(LeafLoc) + " @ " + CalleeName.str();
std::ostringstream OCalleeCtxStr;
if (ContextId.find(" @ ") != StringRef::npos) {
CalleeContextId =
ContextId.rsplit(" @ ").first.str() + " @ " + CalleeContextId;
OCalleeCtxStr << ContextId.rsplit(" @ ").first.str();
OCalleeCtxStr << " @ ";
}
OCalleeCtxStr << getCallSite(LeafLoc) << " @ " << CalleeName.str();
FunctionSamples &CalleeProfile =
getFunctionProfileForContext(CalleeContextId);
getFunctionProfileForContext(OCalleeCtxStr.str());
assert(Count != 0 && "Unexpected zero weight branch");
CalleeProfile.addHeadSamples(Count);
}
@ -334,8 +335,8 @@ void CSProfileGenerator::populateInferredFunctionSamples() {
EstimatedCallCount = 1;
CallerProfile.addCalledTargetSamples(
CallerLeafFrameLoc.second.LineOffset,
CallerLeafFrameLoc.second.Discriminator, CalleeProfile.getName(),
EstimatedCallCount);
CallerLeafFrameLoc.second.Discriminator,
CalleeProfile.getContext().getNameWithoutContext(), EstimatedCallCount);
CallerProfile.addBodySamples(CallerLeafFrameLoc.second.LineOffset,
CallerLeafFrameLoc.second.Discriminator,
EstimatedCallCount);
@ -362,8 +363,8 @@ void CSProfileGenerator::mergeAndTrimColdProfile(
// Remove the code profile from ProfileMap and merge them into BaseProileMap
StringMap<FunctionSamples> BaseProfileMap;
for (const auto &I : ToRemoveVec) {
auto Ret =
BaseProfileMap.try_emplace(I.second->getName(), FunctionSamples());
auto Ret = BaseProfileMap.try_emplace(
I.second->getContext().getNameWithoutContext(), FunctionSamples());
FunctionSamples &BaseProfile = Ret.first->second;
BaseProfile.merge(*I.second);
ProfileMap.erase(I.first);
@ -378,14 +379,27 @@ void CSProfileGenerator::mergeAndTrimColdProfile(
// Merge the profile if the original profile exists, otherwise just insert
// as a new profile
FunctionSamples &OrigProfile = getFunctionProfileForContext(I.getKey());
StringRef TmpName = OrigProfile.getName();
OrigProfile.merge(I.second);
// Should use the name ref from ProfileMap's key to avoid name being freed
// from BaseProfileMap
OrigProfile.setName(TmpName);
}
}
void CSProfileGenerator::write(std::unique_ptr<SampleProfileWriter> Writer,
StringMap<FunctionSamples> &ProfileMap) {
mergeAndTrimColdProfile(ProfileMap);
// Add bracket for context key to support different profile binary format
StringMap<FunctionSamples> CxtWithBracketPMap;
for (const auto &Item : ProfileMap) {
std::string ContextWithBracket = "[" + Item.first().str() + "]";
auto Ret = CxtWithBracketPMap.try_emplace(ContextWithBracket, Item.second);
assert(Ret.second && "Must be a unique context");
SampleContext FContext(Ret.first->first(), RawContext);
FunctionSamples &FProfile = Ret.first->second;
FProfile.setName(FContext.getNameWithContext(true));
FProfile.setContext(FContext);
}
Writer->write(CxtWithBracketPMap);
}
// Helper function to extract context prefix string stack
// Extract context stack for reusing, leaf context stack will
// be added compressed while looking up function profile
@ -399,8 +413,7 @@ extractPrefixContextStack(SmallVectorImpl<std::string> &ContextStrStack,
}
void PseudoProbeCSProfileGenerator::generateProfile() {
// Enable CS and pseudo probe functionalities in SampleProf
FunctionSamples::ProfileIsCS = true;
// Enable pseudo probe functionalities in SampleProf
FunctionSamples::ProfileIsProbeBased = true;
for (const auto &BI : BinarySampleCounters) {
ProfiledBinary *Binary = BI.first;
@ -495,8 +508,9 @@ void PseudoProbeCSProfileGenerator::populateBodySamplesWithProbes(
CallerProfile.setFunctionHash(InlinerDesc->FuncHash);
CallerProfile.addBodySamples(CallerIndex, 0, Count);
CallerProfile.addTotalSamples(Count);
CallerProfile.addCalledTargetSamples(CallerIndex, 0,
FunctionProfile.getName(), Count);
CallerProfile.addCalledTargetSamples(
CallerIndex, 0,
FunctionProfile.getContext().getNameWithoutContext(), Count);
}
}
}

View File

@ -28,11 +28,9 @@ public:
create(const BinarySampleCounterMap &BinarySampleCounters,
enum PerfScriptType SampleType);
virtual void generateProfile() = 0;
// Merge and trim profile with cold context before serialization,
// only eligible for CS profile
virtual void
mergeAndTrimColdProfile(StringMap<FunctionSamples> &ProfileMap){};
// Use SampleProfileWriter to serialize profile map
virtual void write(std::unique_ptr<SampleProfileWriter> Writer,
StringMap<FunctionSamples> &ProfileMap);
void write();
protected:
@ -68,8 +66,6 @@ public:
public:
void generateProfile() override {
// Enable context-sensitive functionalities in SampleProf
FunctionSamples::ProfileIsCS = true;
for (const auto &BI : BinarySampleCounters) {
ProfiledBinary *Binary = BI.first;
for (const auto &CI : BI.second) {
@ -205,7 +201,9 @@ protected:
FunctionSamples &getFunctionProfileForContext(StringRef ContextId);
// Merge cold context profile whose total sample is below threshold
// into base profile.
void mergeAndTrimColdProfile(StringMap<FunctionSamples> &ProfileMap) override;
void mergeAndTrimColdProfile(StringMap<FunctionSamples> &ProfileMap);
void write(std::unique_ptr<SampleProfileWriter> Writer,
StringMap<FunctionSamples> &ProfileMap) override;
private:
// Helper function for updating body sample for a leaf location in

View File

@ -11,6 +11,7 @@
#include "ProfileGenerator.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/TargetRegistry.h"
@ -393,7 +394,8 @@ FrameLocationStack ProfiledBinary::symbolize(const InstructionPointer &IP,
if (UseCanonicalFnName)
FunctionName = FunctionSamples::getCanonicalFnName(FunctionName);
LineLocation Line(CallerFrame.Line - CallerFrame.StartLine,
CallerFrame.Discriminator);
DILocation::getBaseDiscriminatorFromDiscriminator(
CallerFrame.Discriminator));
FrameLocation Callsite(FunctionName.str(), Line);
CallStack.push_back(Callsite);
}