[llvm] r185184 - Add a division operator to BlockFrequency.
Jakob Stoklund Olesen
stoklund at 2pi.dk
Fri Jun 28 14:13:08 PDT 2013
On Jun 28, 2013, at 12:38 PM, Bill Schmidt <wschmidt at linux.vnet.ibm.com> wrote:
> Hi Jakob,
>
> Seeing a failure with the new divide support on PPC64:
Thanks, Bill.
I can’t reproduce this failure on x86, but I removed some undefined behavior in r185218. Does that help?
Thanks,
/jakob
>
> FAIL: LLVM-Unit :: Support/Release
> +Asserts/SupportTests/BlockFrequency.Divide (14508 of 14699)
> ******************** TEST 'LLVM-Unit :: Support/Release
> +Asserts/SupportTests/BlockFrequency.Divide' FAILED ********************
> Note: Google Test filter = BlockFrequency.Divide
> [==========] Running 1 test from 1 test case.
> [----------] Global test environment set-up.
> [----------] 1 test from BlockFrequency
> [ RUN ] BlockFrequency.Divide
> /home/wschmidt/llvm/llvm-base/unittests/Support/BlockFrequencyTest.cpp:58: Failure
> Value of: 0x6666666666666666ULL
> Actual: 7378697629483820646
> Expected: Freq.getFrequency()
> Which is: 0
> [ FAILED ] BlockFrequency.Divide (0 ms)
> [----------] 1 test from BlockFrequency (0 ms total)
>
> [----------] Global test environment tear-down
> [==========] 1 test from 1 test case ran. (1 ms total)
> [ PASSED ] 0 tests.
> [ FAILED ] 1 test, listed below:
> [ FAILED ] BlockFrequency.Divide
>
> 1 FAILED TEST
>
>
> Thanks,
> Bill
>
> On Fri, 2013-06-28 at 18:23 +0000, Jakob Stoklund Olesen wrote:
>> Author: stoklund
>> Date: Fri Jun 28 13:23:42 2013
>> New Revision: 185184
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=185184&view=rev
>> Log:
>> Add a division operator to BlockFrequency.
>>
>> Allow a BlockFrequency to be divided by a non-zero BranchProbability
>> with saturating arithmetic. This will be used to compute the frequency
>> of a loop header given the probability of leaving the loop.
>>
>> Our long division algorithm already saturates on overflow, so that was a
>> freebie.
>>
>> Modified:
>> llvm/trunk/include/llvm/Support/BlockFrequency.h
>> llvm/trunk/lib/Support/BlockFrequency.cpp
>> llvm/trunk/unittests/Support/BlockFrequencyTest.cpp
>>
>> Modified: llvm/trunk/include/llvm/Support/BlockFrequency.h
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BlockFrequency.h?rev=185184&r1=185183&r2=185184&view=diff
>> ==============================================================================
>> --- llvm/trunk/include/llvm/Support/BlockFrequency.h (original)
>> +++ llvm/trunk/include/llvm/Support/BlockFrequency.h Fri Jun 28 13:23:42 2013
>> @@ -27,6 +27,9 @@ class BlockFrequency {
>> uint64_t Frequency;
>> static const int64_t ENTRY_FREQ = 1 << 14;
>>
>> + // Scale frequency by N/D, saturating on overflow.
>> + void scale(uint32_t N, uint32_t D);
>> +
>> public:
>> BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { }
>>
>> @@ -42,6 +45,11 @@ public:
>> BlockFrequency &operator*=(const BranchProbability &Prob);
>> const BlockFrequency operator*(const BranchProbability &Prob) const;
>>
>> + /// \brief Divide by a non-zero branch probability using saturating
>> + /// arithmetic.
>> + BlockFrequency &operator/=(const BranchProbability &Prob);
>> + BlockFrequency operator/(const BranchProbability &Prob) const;
>> +
>> /// \brief Adds another block frequency using saturating arithmetic.
>> BlockFrequency &operator+=(const BlockFrequency &Freq);
>> const BlockFrequency operator+(const BlockFrequency &Freq) const;
>>
>> Modified: llvm/trunk/lib/Support/BlockFrequency.cpp
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/BlockFrequency.cpp?rev=185184&r1=185183&r2=185184&view=diff
>> ==============================================================================
>> --- llvm/trunk/lib/Support/BlockFrequency.cpp (original)
>> +++ llvm/trunk/lib/Support/BlockFrequency.cpp Fri Jun 28 13:23:42 2013
>> @@ -42,12 +42,14 @@ void mult96bit(uint64_t freq, uint32_t N
>> }
>>
>>
>> -/// div96bit - Divide 96-bit value stored in W array by D. Return 64-bit frequency.
>> +/// div96bit - Divide 96-bit value stored in W array by D.
>> +/// Return 64-bit quotient, saturated to UINT64_MAX on overflow.
>> uint64_t div96bit(uint64_t W[2], uint32_t D) {
>> uint64_t y = W[0];
>> uint64_t x = W[1];
>> int i;
>>
>> + // This long division algorithm automatically saturates on overflow.
>> for (i = 1; i <= 64 && x; ++i) {
>> uint32_t t = (int)x >> 31;
>> x = (x << 1) | (y >> 63);
>> @@ -63,31 +65,30 @@ uint64_t div96bit(uint64_t W[2], uint32_
>>
>> }
>>
>> +void BlockFrequency::scale(uint32_t N, uint32_t D) {
>> + assert(D != 0 && "Division by zero");
>>
>> -BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
>> - uint32_t n = Prob.getNumerator();
>> - uint32_t d = Prob.getDenominator();
>> -
>> - assert(n <= d && "Probability must be less or equal to 1.");
>> -
>> - // Calculate Frequency * n.
>> - uint64_t mulLo = (Frequency & UINT32_MAX) * n;
>> - uint64_t mulHi = (Frequency >> 32) * n;
>> - uint64_t mulRes = (mulHi << 32) + mulLo;
>> -
>> - // If there was overflow use 96-bit operations.
>> - if (mulHi > UINT32_MAX || mulRes < mulLo) {
>> - // 96-bit value represented as W[1]:W[0].
>> - uint64_t W[2];
>> -
>> - // Probability is less or equal to 1 which means that results must fit
>> - // 64-bit.
>> - mult96bit(Frequency, n, W);
>> - Frequency = div96bit(W, d);
>> - return *this;
>> + // Calculate Frequency * N.
>> + uint64_t MulLo = (Frequency & UINT32_MAX) * N;
>> + uint64_t MulHi = (Frequency >> 32) * N;
>> + uint64_t MulRes = (MulHi << 32) + MulLo;
>> +
>> + // If the product fits in 64 bits, just use built-in division.
>> + if (MulHi <= UINT32_MAX && MulRes <= MulLo) {
>> + Frequency = MulRes / D;
>> + return;
>> }
>>
>> - Frequency = mulRes / d;
>> + // Product overflowed, use 96-bit operations.
>> + // 96-bit value represented as W[1]:W[0].
>> + uint64_t W[2];
>> + mult96bit(Frequency, N, W);
>> + Frequency = div96bit(W, D);
>> + return;
>> +}
>> +
>> +BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
>> + scale(Prob.getNumerator(), Prob.getDenominator());
>> return *this;
>> }
>>
>> @@ -98,6 +99,17 @@ BlockFrequency::operator*(const BranchPr
>> return Freq;
>> }
>>
>> +BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
>> + scale(Prob.getDenominator(), Prob.getNumerator());
>> + return *this;
>> +}
>> +
>> +BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
>> + BlockFrequency Freq(Frequency);
>> + Freq /= Prob;
>> + return Freq;
>> +}
>> +
>> BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
>> uint64_t Before = Freq.Frequency;
>> Frequency += Freq.Frequency;
>>
>> Modified: llvm/trunk/unittests/Support/BlockFrequencyTest.cpp
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/BlockFrequencyTest.cpp?rev=185184&r1=185183&r2=185184&view=diff
>> ==============================================================================
>> --- llvm/trunk/unittests/Support/BlockFrequencyTest.cpp (original)
>> +++ llvm/trunk/unittests/Support/BlockFrequencyTest.cpp Fri Jun 28 13:23:42 2013
>> @@ -52,6 +52,24 @@ TEST(BlockFrequencyTest, MaxToMax) {
>> EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
>> }
>>
>> +TEST(BlockFrequency, Divide) {
>> + BlockFrequency Freq(0x3333333333333333ULL);
>> + Freq /= BranchProbability(1, 2);
>> + EXPECT_EQ(Freq.getFrequency(), 0x6666666666666666ULL);
>> +}
>> +
>> +TEST(BlockFrequencyTest, Saturate) {
>> + BlockFrequency Freq(0x3333333333333333ULL);
>> + Freq /= BranchProbability(100, 300);
>> + EXPECT_EQ(Freq.getFrequency(), 0x9999999999999999ULL);
>> + Freq /= BranchProbability(1, 2);
>> + EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
>> +
>> + Freq = 0x1000000000000000ULL;
>> + Freq /= BranchProbability(10000, 160000);
>> + EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
>> +}
>> +
>> TEST(BlockFrequencyTest, ProbabilityCompare) {
>> BranchProbability A(4, 5);
>> BranchProbability B(4U << 29, 5U << 29);
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list