Skip to content

Commit 2f44795

Browse files
committed
[TableGen] Factor out timer code into a new TGTimer class
Factor out the timer related functionality from `RecordKeeper` to a new `TGTimer` class in a new file.
1 parent 487686b commit 2f44795

File tree

14 files changed

+189
-121
lines changed

14 files changed

+189
-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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_TABLEGEN_TGTIMER_H
14+
#define LLVM_TABLEGEN_TGTIMER_H
15+
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Support/Timer.h"
18+
19+
namespace llvm {
20+
21+
// Timer related functionality f or TableGen backends.
22+
class TGTimer {
23+
private:
24+
TimerGroup *TimingGroup = nullptr;
25+
Timer *LastTimer = nullptr;
26+
bool BackendTimer = false; // Is last timer special backend overall timer?
27+
28+
public:
29+
TGTimer() = default;
30+
31+
/// Start phase timing; called if the --time-phases option is specified.
32+
void startPhaseTiming() {
33+
TimingGroup = new TimerGroup("TableGen", "TableGen Phase Timing");
34+
}
35+
36+
/// Start timing a phase. Automatically stops any previous phase timer.
37+
void startTimer(StringRef Name);
38+
39+
/// Stop timing a phase.
40+
void stopTimer();
41+
42+
/// Start timing the overall backend. If the backend itself starts a timer,
43+
/// then this timer is cleared.
44+
void startBackendTimer(StringRef Name);
45+
46+
/// Stop timing the overall backend.
47+
void stopBackendTimer();
48+
49+
/// Stop phase timing and print the report.
50+
void stopPhaseTiming() {
51+
delete TimingGroup;
52+
TimingGroup = nullptr;
53+
}
54+
};
55+
56+
} // end namespace llvm
57+
58+
#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>
@@ -3210,7 +3211,9 @@ void Record::checkUnusedTemplateArgs() {
32103211
}
32113212

32123213
RecordKeeper::RecordKeeper()
3213-
: Impl(std::make_unique<detail::RecordKeeperImpl>(*this)) {}
3214+
: Impl(std::make_unique<detail::RecordKeeperImpl>(*this)),
3215+
Timer(std::make_unique<TGTimer>()) {}
3216+
32143217
RecordKeeper::~RecordKeeper() = default;
32153218

32163219
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -3234,47 +3237,6 @@ Init *RecordKeeper::getNewAnonymousName() {
32343237
return AnonymousNameInit::get(*this, getImpl().AnonCounter++);
32353238
}
32363239

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

llvm/lib/TableGen/TGTimer.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#define DEBUG_TYPE "tblgen-timer"
17+
18+
// These functions implement the phase timing facility. Starting a timer
19+
// when one is already running stops the running one.
20+
void TGTimer::startTimer(StringRef Name) {
21+
if (TimingGroup) {
22+
if (LastTimer && LastTimer->isRunning()) {
23+
LastTimer->stopTimer();
24+
if (BackendTimer) {
25+
LastTimer->clear();
26+
BackendTimer = false;
27+
}
28+
}
29+
30+
LastTimer = new Timer("", Name, *TimingGroup);
31+
LastTimer->startTimer();
32+
}
33+
}
34+
35+
void TGTimer::stopTimer() {
36+
if (TimingGroup) {
37+
assert(LastTimer && "No phase timer was started");
38+
LastTimer->stopTimer();
39+
}
40+
}
41+
42+
void TGTimer::startBackendTimer(StringRef Name) {
43+
if (TimingGroup) {
44+
startTimer(Name);
45+
BackendTimer = true;
46+
}
47+
}
48+
49+
void TGTimer::stopBackendTimer() {
50+
if (TimingGroup) {
51+
if (BackendTimer) {
52+
stopTimer();
53+
BackendTimer = false;
54+
}
55+
}
56+
}

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)