Skip to content

Commit 18839be

Browse files
committed
[ADT] Remove StatisticBase and make NoopStatistic empty
In LLVM_ENABLE_STATS=0 builds, `llvm::Statistic` maps to `llvm::NoopStatistic` but has 3 mostly unused pointers. GlobalOpt considers that the pointers can potentially retain allocated objects, so GlobalOpt cannot optimize out the `NoopStatistic` variables (see D69428 for more context), wasting 23KiB for stage 2 clang. This patch makes `NoopStatistic` empty and thus reclaims the wasted space. The clang size is even smaller than applying D69428 (slightly smaller in both .bss and .text). ``` # This means the D69428 optimization on clang is mostly nullified by this patch. HEAD+D69428: size(.bss) = 0x0725a8 HEAD+D101211: size(.bss) = 0x072238 # bloaty - HEAD+D69428 vs HEAD+D101211 # With D101211, we also save a lot of string table space (.rodata). FILE SIZE VM SIZE -------------- -------------- -0.0% -32 -0.0% -24 .eh_frame -0.0% -336 [ = ] 0 .symtab -0.0% -360 [ = ] 0 .strtab [ = ] 0 -0.2% -880 .bss -0.0% -2.11Ki -0.0% -2.11Ki .rodata -0.0% -2.89Ki -0.0% -2.89Ki .text -0.0% -5.71Ki -0.0% -5.88Ki TOTAL ``` Note: LoopFuse is a disabled pass. For now this patch adds `#if LLVM_ENABLE_STATS` so `OptimizationRemarkMissed` is skipped in LLVM_ENABLE_STATS==0 builds. If these `OptimizationRemarkMissed` are useful in LLVM_ENABLE_STATS==0 builds, we can replace `llvm::Statistic` with `llvm::TrackingStatistic`, or use a different abstraction to keep track of the strings. Similarly, skip the code in `mlir/lib/Pass/PassStatistics.cpp` which calls `getName`/`getDesc`/`getValue`. Reviewed By: lattner Differential Revision: https://reviews.llvm.org/D101211
1 parent 8ede964 commit 18839be

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

llvm/include/llvm/ADT/Statistic.h

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,22 @@ class raw_ostream;
4646
class raw_fd_ostream;
4747
class StringRef;
4848

49-
class StatisticBase {
49+
class TrackingStatistic {
5050
public:
51-
const char *DebugType;
52-
const char *Name;
53-
const char *Desc;
51+
const char *const DebugType;
52+
const char *const Name;
53+
const char *const Desc;
5454

55-
StatisticBase(const char *DebugType, const char *Name, const char *Desc)
56-
: DebugType(DebugType), Name(Name), Desc(Desc) {}
57-
58-
const char *getDebugType() const { return DebugType; }
59-
const char *getName() const { return Name; }
60-
const char *getDesc() const { return Desc; }
61-
};
62-
63-
class TrackingStatistic : public StatisticBase {
64-
public:
6555
std::atomic<unsigned> Value;
6656
std::atomic<bool> Initialized;
6757

6858
TrackingStatistic(const char *DebugType, const char *Name, const char *Desc)
69-
: StatisticBase(DebugType, Name, Desc), Value(0), Initialized(false) {}
59+
: DebugType(DebugType), Name(Name), Desc(Desc), Value(0),
60+
Initialized(false) {}
61+
62+
const char *getDebugType() const { return DebugType; }
63+
const char *getName() const { return Name; }
64+
const char *getDesc() const { return Desc; }
7065

7166
unsigned getValue() const { return Value.load(std::memory_order_relaxed); }
7267

@@ -132,9 +127,10 @@ class TrackingStatistic : public StatisticBase {
132127
void RegisterStatistic();
133128
};
134129

135-
class NoopStatistic : public StatisticBase {
130+
class NoopStatistic {
136131
public:
137-
using StatisticBase::StatisticBase;
132+
NoopStatistic(const char * /*DebugType*/, const char * /*Name*/,
133+
const char * /*Desc*/) {}
138134

139135
unsigned getValue() const { return 0; }
140136

llvm/lib/Transforms/Scalar/LoopFuse.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,13 @@ struct FusionCandidate {
372372
bool reportInvalidCandidate(llvm::Statistic &Stat) const {
373373
using namespace ore;
374374
assert(L && Preheader && "Fusion candidate not initialized properly!");
375+
#if LLVM_ENABLE_STATS
375376
++Stat;
376377
ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, Stat.getName(),
377378
L->getStartLoc(), Preheader)
378379
<< "[" << Preheader->getParent()->getName() << "]: "
379380
<< "Loop is not a candidate for fusion: " << Stat.getDesc());
381+
#endif
380382
return false;
381383
}
382384
};
@@ -1533,13 +1535,15 @@ struct LoopFuser {
15331535
assert(FC0.Preheader && FC1.Preheader &&
15341536
"Expecting valid fusion candidates");
15351537
using namespace ore;
1538+
#if LLVM_ENABLE_STATS
15361539
++Stat;
15371540
ORE.emit(RemarkKind(DEBUG_TYPE, Stat.getName(), FC0.L->getStartLoc(),
15381541
FC0.Preheader)
15391542
<< "[" << FC0.Preheader->getParent()->getName()
15401543
<< "]: " << NV("Cand1", StringRef(FC0.Preheader->getName()))
15411544
<< " and " << NV("Cand2", StringRef(FC1.Preheader->getName()))
15421545
<< ": " << Stat.getDesc());
1546+
#endif
15431547
}
15441548

15451549
/// Fuse two guarded fusion candidates, creating a new fused loop.

mlir/include/mlir/Pass/Pass.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ class Pass {
139139

140140
/// Assign the statistic to the given value.
141141
Statistic &operator=(unsigned value);
142-
143-
private:
144-
/// Hide some of the details of llvm::Statistic that we don't use.
145-
using llvm::Statistic::getDebugType;
146142
};
147143

148144
/// Returns the main statistics for this pass instance.

mlir/lib/Pass/PassStatistics.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static void printResultsAsList(raw_ostream &os, OpPassManager &pm) {
6464

6565
// If this is not an adaptor, add the stats to the list if there are any.
6666
if (!adaptor) {
67+
#if LLVM_ENABLE_STATS
6768
auto statistics = pass->getStatistics();
6869
if (statistics.empty())
6970
return;
@@ -76,6 +77,7 @@ static void printResultsAsList(raw_ostream &os, OpPassManager &pm) {
7677
for (auto &it : llvm::enumerate(pass->getStatistics()))
7778
passEntry[it.index()].value += it.value()->getValue();
7879
}
80+
#endif
7981
return;
8082
}
8183

@@ -103,6 +105,7 @@ static void printResultsAsList(raw_ostream &os, OpPassManager &pm) {
103105
/// Print the results in pipeline mode that mirrors the internal pass manager
104106
/// structure.
105107
static void printResultsAsPipeline(raw_ostream &os, OpPassManager &pm) {
108+
#if LLVM_ENABLE_STATS
106109
std::function<void(unsigned, Pass *)> printPass = [&](unsigned indent,
107110
Pass *pass) {
108111
if (auto *adaptor = dyn_cast<OpToOpPassAdaptor>(pass)) {
@@ -132,6 +135,7 @@ static void printResultsAsPipeline(raw_ostream &os, OpPassManager &pm) {
132135
};
133136
for (Pass &pass : pm.getPasses())
134137
printPass(/*indent=*/0, &pass);
138+
#endif
135139
}
136140

137141
static void printStatistics(OpPassManager &pm, PassDisplayMode displayMode) {

mlir/test/Pass/pipeline-stats.mlir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// REQUIRES: asserts
12
// RUN: mlir-opt %s -verify-each=true -pass-pipeline='func(test-stats-pass,test-stats-pass)' -pass-statistics -pass-statistics-display=list 2>&1 | FileCheck -check-prefix=LIST %s
23
// RUN: mlir-opt %s -verify-each=true -pass-pipeline='func(test-stats-pass,test-stats-pass)' -pass-statistics -pass-statistics-display=pipeline 2>&1 | FileCheck -check-prefix=PIPELINE %s
34

0 commit comments

Comments
 (0)