<p dir="ltr">How hard would it be to change the callers that currently depend on this?</p>
<div class="gmail_quote">On May 27, 2016 12:48 PM, "Vedant Kumar" <<a href="mailto:vsk@apple.com">vsk@apple.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">vsk created this revision.<br>
vsk added reviewers: davide, rafael.<br>
vsk added a subscriber: llvm-commits.<br>
<br>
The old Timer implementation (before r256258) allowed nesting calls to {start,stop}Timer. Bring that behavior back and add a regression test for it.<br>
<br>
<a href="http://reviews.llvm.org/D20735" rel="noreferrer" target="_blank">http://reviews.llvm.org/D20735</a><br>
<br>
Files:<br>
include/llvm/Support/Timer.h<br>
lib/Support/Timer.cpp<br>
unittests/Support/TimerTest.cpp<br>
<br>
Index: unittests/Support/TimerTest.cpp<br>
===================================================================<br>
--- unittests/Support/TimerTest.cpp<br>
+++ unittests/Support/TimerTest.cpp<br>
@@ -62,4 +62,13 @@<br>
EXPECT_FALSE(T1.hasTriggered());<br>
}<br>
<br>
+TEST(Timer, Nesting) {<br>
+ Timer T1("T1");<br>
+ T1.startTimer();<br>
+ T1.startTimer();<br>
+ T1.stopTimer();<br>
+ T1.stopTimer();<br>
+ EXPECT_TRUE(T1.hasTriggered());<br>
+}<br>
+<br>
} // end anon namespace<br>
Index: lib/Support/Timer.cpp<br>
===================================================================<br>
--- lib/Support/Timer.cpp<br>
+++ lib/Support/Timer.cpp<br>
@@ -101,8 +101,10 @@<br>
<br>
void Timer::init(StringRef N, TimerGroup &tg) {<br>
assert(!TG && "Timer already initialized");<br>
+ Time = TimeRecord();<br>
Name.assign(N.begin(), N.end());<br>
- Running = Triggered = false;<br>
+ Triggered = false;<br>
+ RefCount = 0;<br>
TG = &tg;<br>
TG->addTimer(*this);<br>
}<br>
@@ -136,21 +138,23 @@<br>
}<br>
<br>
void Timer::startTimer() {<br>
- assert(!Running && "Cannot start a running timer");<br>
- Running = Triggered = true;<br>
- StartTime = TimeRecord::getCurrentTime(true);<br>
+ Triggered = true;<br>
+ if (!RefCount)<br>
+ Time -= TimeRecord::getCurrentTime(true);<br>
+ ++RefCount;<br>
}<br>
<br>
void Timer::stopTimer() {<br>
- assert(Running && "Cannot stop a paused timer");<br>
- Running = false;<br>
- Time += TimeRecord::getCurrentTime(false);<br>
- Time -= StartTime;<br>
+ assert(RefCount > 0 && "Cannot stop a paused timer");<br>
+ --RefCount;<br>
+ if (!RefCount)<br>
+ Time += TimeRecord::getCurrentTime(false);<br>
}<br>
<br>
void Timer::clear() {<br>
- Running = Triggered = false;<br>
- Time = StartTime = TimeRecord();<br>
+ RefCount = 0;<br>
+ Triggered = false;<br>
+ Time = TimeRecord();<br>
}<br>
<br>
static void printVal(double Val, double Total, raw_ostream &OS) {<br>
Index: include/llvm/Support/Timer.h<br>
===================================================================<br>
--- include/llvm/Support/Timer.h<br>
+++ include/llvm/Support/Timer.h<br>
@@ -77,9 +77,8 @@<br>
///<br>
class Timer {<br>
TimeRecord Time; // The total time captured<br>
- TimeRecord StartTime; // The time startTimer() was last called<br>
std::string Name; // The name of this time variable.<br>
- bool Running; // Is the timer currently running?<br>
+ unsigned RefCount; // How many times has this timer been started?<br>
bool Triggered; // Has the timer ever been triggered?<br>
TimerGroup *TG; // The TimerGroup this Timer is in.<br>
<br>
@@ -119,7 +118,10 @@<br>
void clear();<br>
<br>
/// Return the duration for which this timer has been running.<br>
- TimeRecord getTotalTime() const { return Time; }<br>
+ TimeRecord getTotalTime() const {<br>
+ assert(RefCount == 0 && "Cannot query active timer");<br>
+ return Time;<br>
+ }<br>
<br>
private:<br>
friend class TimerGroup;<br>
<br>
<br>
</blockquote></div>