Skip to content

Commit 7e0c231

Browse files
[mlir][NFC] Wrap TimingManager components into namespace
TimingManager and its components (e.g. helper classes and utility functions) reside in mlir namespace. Certain components are generic enough that it makes sense to enclose them into an additional namespace.
1 parent 6ef174c commit 7e0c231

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

mlir/include/mlir/Support/Timing.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ class TimingScope {
320320
Timer timer;
321321
};
322322

323+
namespace timing {
323324
//===----------------------------------------------------------------------===//
324325
// OutputStrategy
325326
//===----------------------------------------------------------------------===//
@@ -366,6 +367,7 @@ class OutputStrategy {
366367

367368
raw_ostream &os;
368369
};
370+
} // namespace timing
369371

370372
//===----------------------------------------------------------------------===//
371373
// DefaultTimingManager
@@ -428,7 +430,7 @@ class DefaultTimingManager : public TimingManager {
428430
DisplayMode getDisplayMode() const;
429431

430432
/// Change the stream where the output will be printed to.
431-
void setOutput(std::unique_ptr<OutputStrategy> output);
433+
void setOutput(std::unique_ptr<timing::OutputStrategy> output);
432434

433435
/// Print and clear the timing results. Only call this when there are no more
434436
/// references to nested timers around, as printing post-processes and clears
@@ -461,7 +463,7 @@ class DefaultTimingManager : public TimingManager {
461463

462464
private:
463465
const std::unique_ptr<detail::DefaultTimingManagerImpl> impl;
464-
std::unique_ptr<OutputStrategy> out;
466+
std::unique_ptr<timing::OutputStrategy> out;
465467
};
466468

467469
/// Register a set of useful command-line options that can be used to configure
@@ -473,10 +475,12 @@ void registerDefaultTimingManagerCLOptions();
473475
/// 'registerDefaultTimingManagerOptions' to a `DefaultTimingManager`.
474476
void applyDefaultTimingManagerCLOptions(DefaultTimingManager &tm);
475477

478+
namespace timing {
476479
/// Create an output strategy for the specified format, to be passed to
477480
/// DefaultTimingManager::setOutput().
478481
std::unique_ptr<OutputStrategy>
479482
createOutputStrategy(DefaultTimingManager::OutputFormat fmt, raw_ostream &os);
483+
} // namespace timing
480484

481485
} // namespace mlir
482486

mlir/lib/Support/Timing.cpp

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ TimingIdentifier TimingIdentifier::get(StringRef str, TimingManager &tm) {
109109

110110
namespace {
111111

112-
class OutputTextStrategy : public OutputStrategy {
112+
class OutputTextStrategy : public mlir::timing::OutputStrategy {
113113
public:
114-
OutputTextStrategy(raw_ostream &os) : OutputStrategy(os) {}
114+
OutputTextStrategy(raw_ostream &os) : mlir::timing::OutputStrategy(os) {}
115115

116-
void printHeader(const TimeRecord &total) override {
116+
void printHeader(const mlir::timing::TimeRecord &total) override {
117117
// Figure out how many spaces to description name.
118118
unsigned padding = (80 - kTimingDescription.size()) / 2;
119119
os << "===" << std::string(73, '-') << "===\n";
@@ -129,7 +129,8 @@ class OutputTextStrategy : public OutputStrategy {
129129

130130
void printFooter() override { os.flush(); }
131131

132-
void printTime(const TimeRecord &time, const TimeRecord &total) override {
132+
void printTime(const mlir::timing::TimeRecord &time,
133+
const mlir::timing::TimeRecord &total) override {
133134
if (total.user != total.wall) {
134135
os << llvm::format(" %8.4f (%5.1f%%)", time.user,
135136
100.0 * time.user / total.user);
@@ -138,33 +139,38 @@ class OutputTextStrategy : public OutputStrategy {
138139
100.0 * time.wall / total.wall);
139140
}
140141

141-
void printListEntry(StringRef name, const TimeRecord &time,
142-
const TimeRecord &total, bool lastEntry) override {
142+
void printListEntry(StringRef name, const mlir::timing::TimeRecord &time,
143+
const mlir::timing::TimeRecord &total,
144+
bool lastEntry) override {
143145
printTime(time, total);
144146
os << name << "\n";
145147
}
146148

147-
void printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
148-
const TimeRecord &total) override {
149+
void printTreeEntry(unsigned indent, StringRef name,
150+
const mlir::timing::TimeRecord &time,
151+
const mlir::timing::TimeRecord &total) override {
149152
printTime(time, total);
150153
os.indent(indent) << name << "\n";
151154
}
152155

153156
void printTreeEntryEnd(unsigned indent, bool lastEntry) override {}
154157
};
155158

156-
class OutputJsonStrategy : public OutputStrategy {
159+
class OutputJsonStrategy : public mlir::timing::OutputStrategy {
157160
public:
158-
OutputJsonStrategy(raw_ostream &os) : OutputStrategy(os) {}
161+
OutputJsonStrategy(raw_ostream &os) : mlir::timing::OutputStrategy(os) {}
159162

160-
void printHeader(const TimeRecord &total) override { os << "[" << "\n"; }
163+
void printHeader(const mlir::timing::TimeRecord &total) override {
164+
os << "[" << "\n";
165+
}
161166

162167
void printFooter() override {
163168
os << "]" << "\n";
164169
os.flush();
165170
}
166171

167-
void printTime(const TimeRecord &time, const TimeRecord &total) override {
172+
void printTime(const mlir::timing::TimeRecord &time,
173+
const mlir::timing::TimeRecord &total) override {
168174
if (total.user != total.wall) {
169175
os << "\"user\": {";
170176
os << "\"duration\": " << llvm::format("%8.4f", time.user) << ", ";
@@ -179,8 +185,9 @@ class OutputJsonStrategy : public OutputStrategy {
179185
os << "}";
180186
}
181187

182-
void printListEntry(StringRef name, const TimeRecord &time,
183-
const TimeRecord &total, bool lastEntry) override {
188+
void printListEntry(StringRef name, const mlir::timing::TimeRecord &time,
189+
const mlir::timing::TimeRecord &total,
190+
bool lastEntry) override {
184191
os << "{";
185192
printTime(time, total);
186193
os << ", \"name\": " << "\"" << name << "\"";
@@ -190,8 +197,9 @@ class OutputJsonStrategy : public OutputStrategy {
190197
os << "\n";
191198
}
192199

193-
void printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
194-
const TimeRecord &total) override {
200+
void printTreeEntry(unsigned indent, StringRef name,
201+
const mlir::timing::TimeRecord &time,
202+
const mlir::timing::TimeRecord &total) override {
195203
os.indent(indent) << "{";
196204
printTime(time, total);
197205
os << ", \"name\": " << "\"" << name << "\"";
@@ -225,7 +233,8 @@ class TimerImpl {
225233
using ChildrenMap = llvm::MapVector<const void *, std::unique_ptr<TimerImpl>>;
226234
using AsyncChildrenMap = llvm::DenseMap<uint64_t, ChildrenMap>;
227235

228-
TimerImpl(std::string &&name, std::unique_ptr<OutputStrategy> &output)
236+
TimerImpl(std::string &&name,
237+
std::unique_ptr<mlir::timing::OutputStrategy> &output)
229238
: threadId(llvm::get_threadid()), name(name), output(output) {}
230239

231240
/// Start the timer.
@@ -360,18 +369,18 @@ class TimerImpl {
360369
}
361370

362371
/// Returns the time for this timer in seconds.
363-
TimeRecord getTimeRecord() {
364-
return TimeRecord(
372+
mlir::timing::TimeRecord getTimeRecord() {
373+
return mlir::timing::TimeRecord(
365374
std::chrono::duration_cast<std::chrono::duration<double>>(wallTime)
366375
.count(),
367376
std::chrono::duration_cast<std::chrono::duration<double>>(userTime)
368377
.count());
369378
}
370379

371380
/// Print the timing result in list mode.
372-
void printAsList(TimeRecord total) {
381+
void printAsList(mlir::timing::TimeRecord total) {
373382
// Flatten the leaf timers in the tree and merge them by name.
374-
llvm::StringMap<TimeRecord> mergedTimers;
383+
llvm::StringMap<mlir::timing::TimeRecord> mergedTimers;
375384
std::function<void(TimerImpl *)> addTimer = [&](TimerImpl *timer) {
376385
mergedTimers[timer->name] += timer->getTimeRecord();
377386
for (auto &children : timer->children)
@@ -380,23 +389,25 @@ class TimerImpl {
380389
addTimer(this);
381390

382391
// Sort the timing information by wall time.
383-
std::vector<std::pair<StringRef, TimeRecord>> timerNameAndTime;
392+
std::vector<std::pair<StringRef, mlir::timing::TimeRecord>>
393+
timerNameAndTime;
384394
for (auto &it : mergedTimers)
385395
timerNameAndTime.emplace_back(it.first(), it.second);
386-
llvm::array_pod_sort(timerNameAndTime.begin(), timerNameAndTime.end(),
387-
[](const std::pair<StringRef, TimeRecord> *lhs,
388-
const std::pair<StringRef, TimeRecord> *rhs) {
389-
return llvm::array_pod_sort_comparator<double>(
390-
&rhs->second.wall, &lhs->second.wall);
391-
});
396+
llvm::array_pod_sort(
397+
timerNameAndTime.begin(), timerNameAndTime.end(),
398+
[](const std::pair<StringRef, mlir::timing::TimeRecord> *lhs,
399+
const std::pair<StringRef, mlir::timing::TimeRecord> *rhs) {
400+
return llvm::array_pod_sort_comparator<double>(&rhs->second.wall,
401+
&lhs->second.wall);
402+
});
392403

393404
// Print the timing information sequentially.
394405
for (auto &timeData : timerNameAndTime)
395406
output->printListEntry(timeData.first, timeData.second, total);
396407
}
397408

398409
/// Print the timing result in tree mode.
399-
void printAsTree(TimeRecord total, unsigned indent = 0) {
410+
void printAsTree(mlir::timing::TimeRecord total, unsigned indent = 0) {
400411
unsigned childIndent = indent;
401412
if (!hidden) {
402413
output->printTreeEntry(indent, name, getTimeRecord(), total);
@@ -468,7 +479,7 @@ class TimerImpl {
468479
/// Mutex for the async children.
469480
std::mutex asyncMutex;
470481

471-
std::unique_ptr<OutputStrategy> &output;
482+
std::unique_ptr<mlir::timing::OutputStrategy> &output;
472483
};
473484

474485
} // namespace
@@ -521,7 +532,8 @@ DefaultTimingManager::DisplayMode DefaultTimingManager::getDisplayMode() const {
521532
}
522533

523534
/// Change the stream where the output will be printed to.
524-
void DefaultTimingManager::setOutput(std::unique_ptr<OutputStrategy> output) {
535+
void DefaultTimingManager::setOutput(
536+
std::unique_ptr<mlir::timing::OutputStrategy> output) {
525537
out = std::move(output);
526538
}
527539

@@ -619,12 +631,13 @@ void mlir::applyDefaultTimingManagerCLOptions(DefaultTimingManager &tm) {
619631
return;
620632
tm.setEnabled(options->timing);
621633
tm.setDisplayMode(options->displayMode);
622-
tm.setOutput(createOutputStrategy(options->outputFormat, llvm::errs()));
634+
tm.setOutput(
635+
mlir::timing::createOutputStrategy(options->outputFormat, llvm::errs()));
623636
}
624637

625-
std::unique_ptr<OutputStrategy>
626-
mlir::createOutputStrategy(DefaultTimingManager::OutputFormat fmt,
627-
raw_ostream &os) {
638+
std::unique_ptr<mlir::timing::OutputStrategy>
639+
mlir::timing::createOutputStrategy(DefaultTimingManager::OutputFormat fmt,
640+
raw_ostream &os) {
628641
switch (fmt) {
629642
case OutputFormat::Text:
630643
return std::make_unique<OutputTextStrategy>(os);

0 commit comments

Comments
 (0)