[llvm] r344187 - Support for remapping profile data when symbols change, for sample-based
Richard Smith via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 10 14:31:01 PDT 2018
Author: rsmith
Date: Wed Oct 10 14:31:01 2018
New Revision: 344187
URL: http://llvm.org/viewvc/llvm-project?rev=344187&view=rev
Log:
Support for remapping profile data when symbols change, for sample-based
profiling.
Reviewers: davidxl, tejohnson, dlj, erik.pilkington
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D51248
Modified:
llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
llvm/trunk/lib/ProfileData/SampleProfReader.cpp
llvm/trunk/unittests/ProfileData/SampleProfTest.cpp
Modified: llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfReader.h?rev=344187&r1=344186&r2=344187&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/SampleProfReader.h (original)
+++ llvm/trunk/include/llvm/ProfileData/SampleProfReader.h Wed Oct 10 14:31:01 2018
@@ -222,6 +222,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SymbolRemappingReader.h"
#include <algorithm>
#include <cstdint>
#include <memory>
@@ -289,11 +290,16 @@ public:
// The function name may have been updated by adding suffix. In sample
// profile, the function names are all stripped, so we need to strip
// the function name suffix before matching with profile.
- StringRef Fname = F.getName().split('.').first;
+ return getSamplesFor(F.getName().split('.').first);
+ }
+
+ /// Return the samples collected for function \p F.
+ virtual FunctionSamples *getSamplesFor(StringRef Fname) {
std::string FGUID;
Fname = getRepInFormat(Fname, getFormat(), FGUID);
- if (Profiles.count(Fname))
- return &Profiles[Fname];
+ auto It = Profiles.find(Fname);
+ if (It != Profiles.end())
+ return &It->second;
return nullptr;
}
@@ -337,6 +343,12 @@ protected:
/// Profile summary information.
std::unique_ptr<ProfileSummary> Summary;
+ /// Take ownership of the summary of this reader.
+ static std::unique_ptr<ProfileSummary>
+ takeSummary(SampleProfileReader &Reader) {
+ return std::move(Reader.Summary);
+ }
+
/// Compute summary for this profile.
void computeSummary();
@@ -525,6 +537,40 @@ protected:
static const uint32_t GCOVTagAFDOFunction = 0xac000000;
};
+/// A profile data reader proxy that remaps the profile data from another
+/// sample profile data reader, by applying a provided set of equivalences
+/// between components of the symbol names in the profile.
+class SampleProfileReaderItaniumRemapper : public SampleProfileReader {
+public:
+ SampleProfileReaderItaniumRemapper(
+ std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
+ std::unique_ptr<SampleProfileReader> Underlying)
+ : SampleProfileReader(std::move(B), C, Underlying->getFormat()) {
+ Profiles = std::move(Underlying->getProfiles());
+ Summary = takeSummary(*Underlying);
+ }
+
+ /// Create a remapped sample profile from the given remapping file and
+ /// underlying samples.
+ static ErrorOr<std::unique_ptr<SampleProfileReader>>
+ create(const Twine &Filename, LLVMContext &C,
+ std::unique_ptr<SampleProfileReader> Underlying);
+
+ /// Read and validate the file header.
+ std::error_code readHeader() override { return sampleprof_error::success; }
+
+ /// Read remapping file and apply it to the sample profile.
+ std::error_code read() override;
+
+ /// Return the samples collected for function \p F.
+ FunctionSamples *getSamplesFor(StringRef FunctionName) override;
+ using SampleProfileReader::getSamplesFor;
+
+private:
+ SymbolRemappingReader Remappings;
+ DenseMap<SymbolRemappingReader::Key, FunctionSamples*> SampleMap;
+};
+
} // end namespace sampleprof
} // end namespace llvm
Modified: llvm/trunk/lib/ProfileData/SampleProfReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfReader.cpp?rev=344187&r1=344186&r2=344187&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/SampleProfReader.cpp (original)
+++ llvm/trunk/lib/ProfileData/SampleProfReader.cpp Wed Oct 10 14:31:01 2018
@@ -912,6 +912,40 @@ bool SampleProfileReaderGCC::hasFormat(c
return Magic == "adcg*704";
}
+std::error_code SampleProfileReaderItaniumRemapper::read() {
+ // If the underlying data is in compact format, we can't remap it because
+ // we don't know what the original function names were.
+ if (getFormat() == SPF_Compact_Binary) {
+ Ctx.diagnose(DiagnosticInfoSampleProfile(
+ Buffer->getBufferIdentifier(),
+ "Profile data remapping cannot be applied to profile data "
+ "in compact format (original mangled names are not available).",
+ DS_Warning));
+ return sampleprof_error::success;
+ }
+
+ if (Error E = Remappings.read(*Buffer)) {
+ handleAllErrors(
+ std::move(E), [&](const SymbolRemappingParseError &ParseError) {
+ reportError(ParseError.getLineNum(), ParseError.getMessage());
+ });
+ return sampleprof_error::malformed;
+ }
+
+ for (auto &Sample : getProfiles())
+ if (auto Key = Remappings.insert(Sample.first()))
+ SampleMap.insert({Key, &Sample.second});
+
+ return sampleprof_error::success;
+}
+
+FunctionSamples *
+SampleProfileReaderItaniumRemapper::getSamplesFor(StringRef Fname) {
+ if (auto Key = Remappings.lookup(Fname))
+ return SampleMap.lookup(Key);
+ return SampleProfileReader::getSamplesFor(Fname);
+}
+
/// Prepare a memory buffer for the contents of \p Filename.
///
/// \returns an error code indicating the status of the buffer.
@@ -944,6 +978,27 @@ SampleProfileReader::create(const Twine
return create(BufferOrError.get(), C);
}
+/// Create a sample profile remapper from the given input, to remap the
+/// function names in the given profile data.
+///
+/// \param Filename The file to open.
+///
+/// \param C The LLVM context to use to emit diagnostics.
+///
+/// \param Underlying The underlying profile data reader to remap.
+///
+/// \returns an error code indicating the status of the created reader.
+ErrorOr<std::unique_ptr<SampleProfileReader>>
+SampleProfileReaderItaniumRemapper::create(
+ const Twine &Filename, LLVMContext &C,
+ std::unique_ptr<SampleProfileReader> Underlying) {
+ auto BufferOrError = setupMemoryBuffer(Filename);
+ if (std::error_code EC = BufferOrError.getError())
+ return EC;
+ return llvm::make_unique<SampleProfileReaderItaniumRemapper>(
+ std::move(BufferOrError.get()), C, std::move(Underlying));
+}
+
/// Create a sample profile reader based on the format of the input data.
///
/// \param B The memory buffer to create the reader from (assumes ownership).
Modified: llvm/trunk/unittests/ProfileData/SampleProfTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/SampleProfTest.cpp?rev=344187&r1=344186&r2=344187&view=diff
==============================================================================
--- llvm/trunk/unittests/ProfileData/SampleProfTest.cpp (original)
+++ llvm/trunk/unittests/ProfileData/SampleProfTest.cpp Wed Oct 10 14:31:01 2018
@@ -58,7 +58,7 @@ struct SampleProfTest : ::testing::Test
Reader->collectFuncsToUse(M);
}
- void testRoundTrip(SampleProfileFormat Format) {
+ void testRoundTrip(SampleProfileFormat Format, bool Remap) {
SmallVector<char, 128> ProfilePath;
ASSERT_TRUE(NoError(llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath)));
StringRef Profile(ProfilePath.data(), ProfilePath.size());
@@ -108,22 +108,35 @@ struct SampleProfTest : ::testing::Test
EC = Reader->read();
ASSERT_TRUE(NoError(EC));
- StringMap<FunctionSamples> &ReadProfiles = Reader->getProfiles();
- ASSERT_EQ(2u, ReadProfiles.size());
-
- std::string FooGUID;
- StringRef FooRep = getRepInFormat(FooName, Format, FooGUID);
- FunctionSamples &ReadFooSamples = ReadProfiles[FooRep];
- ASSERT_EQ(7711u, ReadFooSamples.getTotalSamples());
- ASSERT_EQ(610u, ReadFooSamples.getHeadSamples());
-
- std::string BarGUID;
- StringRef BarRep = getRepInFormat(BarName, Format, BarGUID);
- FunctionSamples &ReadBarSamples = ReadProfiles[BarRep];
- ASSERT_EQ(20301u, ReadBarSamples.getTotalSamples());
- ASSERT_EQ(1437u, ReadBarSamples.getHeadSamples());
+ if (Remap) {
+ auto MemBuffer = llvm::MemoryBuffer::getMemBuffer(R"(
+ # Types 'int' and 'long' are equivalent
+ type i l
+ # Function names 'foo' and 'faux' are equivalent
+ name 3foo 4faux
+ )");
+ Reader.reset(new SampleProfileReaderItaniumRemapper(
+ std::move(MemBuffer), Context, std::move(Reader)));
+ FooName = "_Z4fauxi";
+ BarName = "_Z3barl";
+
+ EC = Reader->read();
+ ASSERT_TRUE(NoError(EC));
+ }
+
+ ASSERT_EQ(2u, Reader->getProfiles().size());
+
+ FunctionSamples *ReadFooSamples = Reader->getSamplesFor(FooName);
+ ASSERT_TRUE(ReadFooSamples != nullptr);
+ ASSERT_EQ(7711u, ReadFooSamples->getTotalSamples());
+ ASSERT_EQ(610u, ReadFooSamples->getHeadSamples());
+
+ FunctionSamples *ReadBarSamples = Reader->getSamplesFor(BarName);
+ ASSERT_TRUE(ReadBarSamples != nullptr);
+ ASSERT_EQ(20301u, ReadBarSamples->getTotalSamples());
+ ASSERT_EQ(1437u, ReadBarSamples->getHeadSamples());
ErrorOr<SampleRecord::CallTargetMap> CTMap =
- ReadBarSamples.findCallTargetMapAt(1, 0);
+ ReadBarSamples->findCallTargetMapAt(1, 0);
ASSERT_FALSE(CTMap.getError());
std::string MconstructGUID;
@@ -184,15 +197,23 @@ struct SampleProfTest : ::testing::Test
};
TEST_F(SampleProfTest, roundtrip_text_profile) {
- testRoundTrip(SampleProfileFormat::SPF_Text);
+ testRoundTrip(SampleProfileFormat::SPF_Text, false);
}
TEST_F(SampleProfTest, roundtrip_raw_binary_profile) {
- testRoundTrip(SampleProfileFormat::SPF_Binary);
+ testRoundTrip(SampleProfileFormat::SPF_Binary, false);
}
TEST_F(SampleProfTest, roundtrip_compact_binary_profile) {
- testRoundTrip(SampleProfileFormat::SPF_Compact_Binary);
+ testRoundTrip(SampleProfileFormat::SPF_Compact_Binary, false);
+}
+
+TEST_F(SampleProfTest, remap_text_profile) {
+ testRoundTrip(SampleProfileFormat::SPF_Text, true);
+}
+
+TEST_F(SampleProfTest, remap_raw_binary_profile) {
+ testRoundTrip(SampleProfileFormat::SPF_Binary, true);
}
TEST_F(SampleProfTest, sample_overflow_saturation) {
More information about the llvm-commits
mailing list