Skip to content

Commit d883ef1

Browse files
authored
[TableGen] Factor out timer code into a new TGTimer class (#111054)
Factor out the timer related functionality from `RecordKeeper` to a new `TGTimer` class in a new file.
1 parent 99c05b2 commit d883ef1

File tree

14 files changed

+188
-121
lines changed

14 files changed

+188
-121
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class RecordVal;
5151
class Resolver;
5252
class StringInit;
5353
class TypedInit;
54+
class TGTimer;
5455

5556
//===----------------------------------------------------------------------===//
5657
// Type Classes
@@ -1785,11 +1786,13 @@ class Record {
17851786
}
17861787

17871788
RecordVal *getValue(const Init *Name) {
1788-
return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1789+
return const_cast<RecordVal *>(
1790+
static_cast<const Record *>(this)->getValue(Name));
17891791
}
17901792

17911793
RecordVal *getValue(StringRef Name) {
1792-
return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1794+
return const_cast<RecordVal *>(
1795+
static_cast<const Record *>(this)->getValue(Name));
17931796
}
17941797

17951798
void addTemplateArg(Init *Name) {
@@ -2033,29 +2036,7 @@ class RecordKeeper {
20332036

20342037
Init *getNewAnonymousName();
20352038

2036-
/// Start phase timing; called if the --time-phases option is specified.
2037-
void startPhaseTiming() {
2038-
TimingGroup = new TimerGroup("TableGen", "TableGen Phase Timing");
2039-
}
2040-
2041-
/// Start timing a phase. Automatically stops any previous phase timer.
2042-
void startTimer(StringRef Name) const;
2043-
2044-
/// Stop timing a phase.
2045-
void stopTimer();
2046-
2047-
/// Start timing the overall backend. If the backend itself starts a timer,
2048-
/// then this timer is cleared.
2049-
void startBackendTimer(StringRef Name);
2050-
2051-
/// Stop timing the overall backend.
2052-
void stopBackendTimer();
2053-
2054-
/// Stop phase timing and print the report.
2055-
void stopPhaseTiming() {
2056-
if (TimingGroup)
2057-
delete TimingGroup;
2058-
}
2039+
TGTimer &getTimer() const { return *Timer; }
20592040

20602041
//===--------------------------------------------------------------------===//
20612042
// High-level helper methods, useful for tablegen backends.
@@ -2089,16 +2070,9 @@ class RecordKeeper {
20892070
mutable std::map<std::string, std::vector<const Record *>> Cache;
20902071
GlobalMap ExtraGlobals;
20912072

2092-
// TODO: Move timing related code out of RecordKeeper.
2093-
// These members are for the phase timing feature. We need a timer group,
2094-
// the last timer started, and a flag to say whether the last timer
2095-
// is the special "backend overall timer."
2096-
mutable TimerGroup *TimingGroup = nullptr;
2097-
mutable Timer *LastTimer = nullptr;
2098-
mutable bool BackendTimer = false;
2099-
21002073
/// The internal uniquer implementation of the RecordKeeper.
21012074
std::unique_ptr<detail::RecordKeeperImpl> Impl;
2075+
std::unique_ptr<TGTimer> Timer;
21022076
};
21032077

21042078
/// Sorting predicate to sort record pointers by name.

llvm/include/llvm/TableGen/TGTimer.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===- llvm/TableGen/TGTimer.h - Class for TableGen Timer -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines the TableGen timer class. It's a thin wrapper around timer
10+
// support in llvm/Support/Timer.h.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TABLEGEN_TGTIMER_H
15+
#define LLVM_TABLEGEN_TGTIMER_H
16+
17+
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/Support/Timer.h"
19+
#include <memory>
20+
21+
namespace llvm {
22+
23+
// Timer related functionality f or TableGen backends.
24+
class TGTimer {
25+
private:
26+
std::unique_ptr<TimerGroup> TimingGroup;
27+
std::unique_ptr<Timer> LastTimer;
28+
bool BackendTimer = false; // Is last timer special backend overall timer?
29+
30+
public:
31+
TGTimer() = default;
32+
~TGTimer() = default;
33+
34+
/// Start phase timing; called if the --time-phases option is specified.
35+
void startPhaseTiming() {
36+
TimingGroup =
37+
std::make_unique<TimerGroup>("TableGen", "TableGen Phase Timing");
38+
}
39+
40+
/// Start timing a phase. Automatically stops any previous phase timer.
41+
void startTimer(StringRef Name);
42+
43+
/// Stop timing a phase.
44+
void stopTimer();
45+
46+
/// Start timing the overall backend. If the backend itself starts a timer,
47+
/// then this timer is cleared.
48+
void startBackendTimer(StringRef Name);
49+
50+
/// Stop timing the overall backend.
51+
void stopBackendTimer();
52+
53+
/// Stop phase timing and print the report.
54+
void stopPhaseTiming() { TimingGroup.reset(); }
55+
};
56+
57+
} // end namespace llvm
58+
59+
#endif // LLVM_TABLEGEN_TGTIMER_H

llvm/lib/TableGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_llvm_component_library(LLVMTableGen
1111
TableGenBackendSkeleton.cpp
1212
TGLexer.cpp
1313
TGParser.cpp
14+
TGTimer.cpp
1415

1516
ADDITIONAL_HEADER_DIRS
1617
${LLVM_MAIN_INCLUDE_DIR}/llvm/TableGen

llvm/lib/TableGen/Main.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/Support/raw_ostream.h"
3030
#include "llvm/TableGen/Error.h"
3131
#include "llvm/TableGen/Record.h"
32+
#include "llvm/TableGen/TGTimer.h"
3233
#include "llvm/TableGen/TableGenBackend.h"
3334
#include <memory>
3435
#include <string>
@@ -98,13 +99,14 @@ static int createDependencyFile(const TGParser &Parser, const char *argv0) {
9899
int llvm::TableGenMain(const char *argv0,
99100
std::function<TableGenMainFn> MainFn) {
100101
RecordKeeper Records;
102+
TGTimer &Timer = Records.getTimer();
101103

102104
if (TimePhases)
103-
Records.startPhaseTiming();
105+
Timer.startPhaseTiming();
104106

105107
// Parse the input file.
106108

107-
Records.startTimer("Parse, build records");
109+
Timer.startTimer("Parse, build records");
108110
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
109111
MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true);
110112
if (std::error_code EC = FileOrErr.getError())
@@ -124,18 +126,18 @@ int llvm::TableGenMain(const char *argv0,
124126

125127
if (Parser.ParseFile())
126128
return 1;
127-
Records.stopTimer();
129+
Timer.stopTimer();
128130

129131
// Write output to memory.
130-
Records.startBackendTimer("Backend overall");
132+
Timer.startBackendTimer("Backend overall");
131133
std::string OutString;
132134
raw_string_ostream Out(OutString);
133135
unsigned status = 0;
134136
// ApplyCallback will return true if it did not apply any callback. In that
135137
// case, attempt to apply the MainFn.
136138
if (TableGen::Emitter::ApplyCallback(Records, Out))
137139
status = MainFn ? MainFn(Out, Records) : 1;
138-
Records.stopBackendTimer();
140+
Timer.stopBackendTimer();
139141
if (status)
140142
return 1;
141143

@@ -148,7 +150,7 @@ int llvm::TableGenMain(const char *argv0,
148150
return Ret;
149151
}
150152

151-
Records.startTimer("Write output");
153+
Timer.startTimer("Write output");
152154
bool WriteFile = true;
153155
if (WriteIfChanged) {
154156
// Only updates the real output file if there are any differences.
@@ -169,9 +171,9 @@ int llvm::TableGenMain(const char *argv0,
169171
if (ErrorsPrinted == 0)
170172
OutFile.keep();
171173
}
172-
173-
Records.stopTimer();
174-
Records.stopPhaseTiming();
174+
175+
Timer.stopTimer();
176+
Timer.stopPhaseTiming();
175177

176178
if (ErrorsPrinted > 0)
177179
return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");

llvm/lib/TableGen/Record.cpp

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/Support/SMLoc.h"
2929
#include "llvm/Support/raw_ostream.h"
3030
#include "llvm/TableGen/Error.h"
31+
#include "llvm/TableGen/TGTimer.h"
3132
#include <cassert>
3233
#include <cstdint>
3334
#include <map>
@@ -3218,7 +3219,9 @@ void Record::checkUnusedTemplateArgs() {
32183219
}
32193220

32203221
RecordKeeper::RecordKeeper()
3221-
: Impl(std::make_unique<detail::RecordKeeperImpl>(*this)) {}
3222+
: Impl(std::make_unique<detail::RecordKeeperImpl>(*this)),
3223+
Timer(std::make_unique<TGTimer>()) {}
3224+
32223225
RecordKeeper::~RecordKeeper() = default;
32233226

32243227
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -3242,47 +3245,6 @@ Init *RecordKeeper::getNewAnonymousName() {
32423245
return AnonymousNameInit::get(*this, getImpl().AnonCounter++);
32433246
}
32443247

3245-
// These functions implement the phase timing facility. Starting a timer
3246-
// when one is already running stops the running one.
3247-
3248-
void RecordKeeper::startTimer(StringRef Name) const {
3249-
if (TimingGroup) {
3250-
if (LastTimer && LastTimer->isRunning()) {
3251-
LastTimer->stopTimer();
3252-
if (BackendTimer) {
3253-
LastTimer->clear();
3254-
BackendTimer = false;
3255-
}
3256-
}
3257-
3258-
LastTimer = new Timer("", Name, *TimingGroup);
3259-
LastTimer->startTimer();
3260-
}
3261-
}
3262-
3263-
void RecordKeeper::stopTimer() {
3264-
if (TimingGroup) {
3265-
assert(LastTimer && "No phase timer was started");
3266-
LastTimer->stopTimer();
3267-
}
3268-
}
3269-
3270-
void RecordKeeper::startBackendTimer(StringRef Name) {
3271-
if (TimingGroup) {
3272-
startTimer(Name);
3273-
BackendTimer = true;
3274-
}
3275-
}
3276-
3277-
void RecordKeeper::stopBackendTimer() {
3278-
if (TimingGroup) {
3279-
if (BackendTimer) {
3280-
stopTimer();
3281-
BackendTimer = false;
3282-
}
3283-
}
3284-
}
3285-
32863248
ArrayRef<const Record *>
32873249
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
32883250
// We cache the record vectors for single classes. Many backends request

llvm/lib/TableGen/TGTimer.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===- TGTimer.cpp - TableGen Timer implementation --------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Implement the tablegen timer class.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/TableGen/TGTimer.h"
14+
using namespace llvm;
15+
16+
// These functions implement the phase timing facility. Starting a timer
17+
// when one is already running stops the running one.
18+
void TGTimer::startTimer(StringRef Name) {
19+
if (!TimingGroup)
20+
return;
21+
if (LastTimer && LastTimer->isRunning()) {
22+
LastTimer->stopTimer();
23+
if (BackendTimer) {
24+
LastTimer->clear();
25+
BackendTimer = false;
26+
}
27+
}
28+
29+
LastTimer = std::make_unique<Timer>("", Name, *TimingGroup);
30+
LastTimer->startTimer();
31+
}
32+
33+
void TGTimer::stopTimer() {
34+
if (!TimingGroup)
35+
return;
36+
37+
assert(LastTimer && "No phase timer was started");
38+
LastTimer->stopTimer();
39+
}
40+
41+
void TGTimer::startBackendTimer(StringRef Name) {
42+
if (!TimingGroup)
43+
return;
44+
45+
startTimer(Name);
46+
BackendTimer = true;
47+
}
48+
49+
void TGTimer::stopBackendTimer() {
50+
if (!TimingGroup || !BackendTimer)
51+
return;
52+
stopTimer();
53+
BackendTimer = false;
54+
}

llvm/utils/TableGen/CallingConvEmitter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Common/CodeGenTarget.h"
1515
#include "llvm/TableGen/Error.h"
1616
#include "llvm/TableGen/Record.h"
17+
#include "llvm/TableGen/TGTimer.h"
1718
#include "llvm/TableGen/TableGenBackend.h"
1819
#include <deque>
1920
#include <set>
@@ -51,7 +52,7 @@ void CallingConvEmitter::run(raw_ostream &O) {
5152

5253
// Emit prototypes for all of the non-custom CC's so that they can forward ref
5354
// each other.
54-
Records.startTimer("Emit prototypes");
55+
Records.getTimer().startTimer("Emit prototypes");
5556
O << "#ifndef GET_CC_REGISTER_LISTS\n\n";
5657
for (const Record *CC : CCs) {
5758
if (!CC->getValueAsBit("Custom")) {
@@ -71,7 +72,7 @@ void CallingConvEmitter::run(raw_ostream &O) {
7172
}
7273

7374
// Emit each non-custom calling convention description in full.
74-
Records.startTimer("Emit full descriptions");
75+
Records.getTimer().startTimer("Emit full descriptions");
7576
for (const Record *CC : CCs) {
7677
if (!CC->getValueAsBit("Custom")) {
7778
EmitCallingConv(CC, O);

0 commit comments

Comments
 (0)