From 617c60013dc31e5b768912217bd354b133888796 Mon Sep 17 00:00:00 2001 From: Nathan Slingerland Date: Wed, 18 Nov 2015 20:40:41 +0000 Subject: [PATCH] [llvm-profdata] Add SaturatingAdd/SaturatingMultiply Helper Functions (2nd try) Summary: This change adds MathExtras helper functions for handling unsigned, saturating addition and multiplication. It also updates the instrumentation and sample profile merge implementations to use them. Reviewers: dnovillo, bogner, davidxl Subscribers: davidxl, llvm-commits Differential Revision: http://reviews.llvm.org/D14720 llvm-svn: 253497 --- llvm/include/llvm/ProfileData/InstrProf.h | 2 +- llvm/include/llvm/ProfileData/SampleProf.h | 10 ++---- llvm/include/llvm/Support/MathExtras.h | 26 ++++++++++++++ llvm/unittests/ProfileData/InstrProfTest.cpp | 32 +++++++++++++++++ llvm/unittests/Support/MathExtrasTest.cpp | 36 ++++++++++++++++++++ 5 files changed, 97 insertions(+), 9 deletions(-) diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index 59d0e1877a690..c31f4b5b589bd 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -226,7 +226,7 @@ struct InstrProfValueSiteRecord { while (I != IE && I->Value < J->Value) ++I; if (I != IE && I->Value == J->Value) { - I->Count += J->Count; + I->Count = SaturatingAdd(I->Count, J->Count); ++I; continue; } diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h index d7596d7b1b773..a8960cf7bc120 100644 --- a/llvm/include/llvm/ProfileData/SampleProf.h +++ b/llvm/include/llvm/ProfileData/SampleProf.h @@ -173,10 +173,7 @@ class SampleRecord { /// Sample counts accumulate using saturating arithmetic, to avoid wrapping /// around unsigned integers. void addSamples(uint64_t S) { - if (NumSamples <= std::numeric_limits::max() - S) - NumSamples += S; - else - NumSamples = std::numeric_limits::max(); + NumSamples = SaturatingAdd(NumSamples, S); } /// Add called function \p F with samples \p S. @@ -185,10 +182,7 @@ class SampleRecord { /// around unsigned integers. void addCalledTarget(StringRef F, uint64_t S) { uint64_t &TargetSamples = CallTargets[F]; - if (TargetSamples <= std::numeric_limits::max() - S) - TargetSamples += S; - else - TargetSamples = std::numeric_limits::max(); + TargetSamples = SaturatingAdd(TargetSamples, S); } /// Return true if this sample record contains function calls. diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index 2c515bbd242a1..b7b3c02dcfe76 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -653,6 +653,32 @@ inline int64_t SignExtend64(uint64_t X, unsigned B) { return int64_t(X << (64 - B)) >> (64 - B); } +/// \brief Add two unsigned integers, X and Y, of type T. +/// Clamp the result to the maximum representable value of T on overflow. +template +typename std::enable_if::value, T>::type +SaturatingAdd(T X, T Y) { + // Hacker's Delight, p. 29 + T Z = X + Y; + if (Z < X || Z < Y) + return std::numeric_limits::max(); + else + return Z; +} + +/// \brief Multiply two unsigned integers, X and Y, of type T. +/// Clamp the result to the maximum representable value of T on overflow. +template +typename std::enable_if::value, T>::type +SaturatingMultiply(T X, T Y) { + // Hacker's Delight, p. 30 + T Z = X * Y; + if (Y != 0 && Z / Y != X) + return std::numeric_limits::max(); + else + return Z; +} + extern const float huge_valf; } // End llvm namespace diff --git a/llvm/unittests/ProfileData/InstrProfTest.cpp b/llvm/unittests/ProfileData/InstrProfTest.cpp index a9257fe316e0c..6891931e4d22a 100644 --- a/llvm/unittests/ProfileData/InstrProfTest.cpp +++ b/llvm/unittests/ProfileData/InstrProfTest.cpp @@ -350,6 +350,38 @@ TEST_F(InstrProfTest, get_icall_data_merge1) { ASSERT_EQ(2U, VD_4[2].Count); } +TEST_F(InstrProfTest, get_icall_data_merge1_saturation) { + const uint64_t Max = std::numeric_limits::max(); + + InstrProfRecord Record1("caller", 0x1234, {1}); + InstrProfRecord Record2("caller", 0x1234, {1}); + InstrProfRecord Record3("callee1", 0x1235, {3, 4}); + + Record1.reserveSites(IPVK_IndirectCallTarget, 1); + InstrProfValueData VD1[] = {{(uint64_t) "callee1", 1}}; + Record1.addValueData(IPVK_IndirectCallTarget, 0, VD1, 1, nullptr); + + Record2.reserveSites(IPVK_IndirectCallTarget, 1); + InstrProfValueData VD2[] = {{(uint64_t) "callee1", Max}}; + Record2.addValueData(IPVK_IndirectCallTarget, 0, VD2, 1, nullptr); + + Writer.addRecord(std::move(Record1)); + Writer.addRecord(std::move(Record2)); + Writer.addRecord(std::move(Record3)); + + auto Profile = Writer.writeBuffer(); + readProfile(std::move(Profile)); + + // Verify saturation of counts. + ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.getError())); + ASSERT_EQ(1U, R.get().getNumValueSites(IPVK_IndirectCallTarget)); + std::unique_ptr VD = + R.get().getValueForSite(IPVK_IndirectCallTarget, 0); + ASSERT_EQ(StringRef("callee1"), StringRef((const char *)VD[0].Value, 7)); + ASSERT_EQ(Max, VD[0].Count); +} + TEST_F(InstrProfTest, get_max_function_count) { InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2}); InstrProfRecord Record2("bar", 0, {1ULL << 63}); diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp index 8bd47e34c7ec1..781bce3fab055 100644 --- a/llvm/unittests/Support/MathExtrasTest.cpp +++ b/llvm/unittests/Support/MathExtrasTest.cpp @@ -190,4 +190,40 @@ TEST(MathExtras, RoundUpToAlignment) { EXPECT_EQ(552u, RoundUpToAlignment(321, 255, 42)); } +template +void SaturatingAddTestHelper() +{ + const T Max = std::numeric_limits::max(); + EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2))); + EXPECT_EQ(Max, SaturatingAdd(Max, T(1))); + EXPECT_EQ(Max, SaturatingAdd(T(1), Max)); + EXPECT_EQ(Max, SaturatingAdd(Max, Max)); +} + +TEST(MathExtras, SaturatingAdd) { + SaturatingAddTestHelper(); + SaturatingAddTestHelper(); + SaturatingAddTestHelper(); + SaturatingAddTestHelper(); +} + +template +void SaturatingMultiplyTestHelper() +{ + const T Max = std::numeric_limits::max(); + EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0))); + EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1))); + EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3))); + EXPECT_EQ(Max, SaturatingMultiply(Max, T(2))); + EXPECT_EQ(Max, SaturatingMultiply(T(2),Max)); + EXPECT_EQ(Max, SaturatingMultiply(Max, Max)); +} + +TEST(MathExtras, SaturatingMultiply) { + SaturatingMultiplyTestHelper(); + SaturatingMultiplyTestHelper(); + SaturatingMultiplyTestHelper(); + SaturatingMultiplyTestHelper(); +} + }