This is an archive of the discontinued LLVM Phabricator instance.

[Statistics] Add a method to atomically update a statistic that contains a maximum
ClosedPublic

Authored by craig.topper on May 17 2017, 3:43 PM.

Details

Summary

There are several places in the codebase that try to calculate a maximum value in a Statistic object. We currently do this in one of two ways:

MaxNumFoo = std::max(MaxNumFoo, NumFoo);

or

MaxNumFoo = (MaxNumFoo > NumFoo) ? MaxNumFoo : NumFoo;

The first version reads from MaxNumFoo one time and uncontionally rwrites to it. The second rversion possibly reads it twice depending on the result of the first compare. But we have no way of knowing if the value was changed by another thread between the reads and the writes.

This patch adds a method to the Statistic object that can ensure that we only store if our value is the max and the previous max didn't change after we read it. If it changed we'll recheck if our value should still be the max or not and try again.

This spawned from an audit I'm trying to do of all places we uses the implicit conversion to unsigned on the Statistics objects. See my previous thread on llvm-dev https://groups.google.com/forum/#!topic/llvm-dev/yfvxiorKrDQ

Diff Detail

Event Timeline

craig.topper created this revision.May 17 2017, 3:43 PM
chandlerc accepted this revision.May 17 2017, 4:51 PM

LGTM

Ahh, "atomic" max. I remember when I first wrote this in some other project 8 years ago now and had to figure out if it really has adequate forward progress guarantees... =] (It does.)

This revision is now accepted and ready to land.May 17 2017, 4:51 PM
This revision was automatically updated to reflect the committed changes.