Skip to content

Commit ee44ca3

Browse files
jurahulLukacma
authored andcommitted
[NFC][LLVM] Code cleanup in llvm-xray (llvm#164080)
- Use nested namespace definitions in header files. - Mark file local function static and enclods file local structs in anonymous namespace. - Drop some unnecessary namespace qualifiers.
1 parent fe925af commit ee44ca3

15 files changed

+64
-89
lines changed

llvm/tools/llvm-xray/func-id-helper.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include "llvm/DebugInfo/Symbolize/Symbolize.h"
1818
#include <unordered_map>
1919

20-
namespace llvm {
21-
namespace xray {
20+
namespace llvm::xray {
2221

2322
// This class consolidates common operations related to Function IDs.
2423
class FuncIdConversionHelper {
@@ -45,7 +44,6 @@ class FuncIdConversionHelper {
4544
std::string FileLineAndColumn(int32_t FuncId) const;
4645
};
4746

48-
} // namespace xray
49-
} // namespace llvm
47+
} // namespace llvm::xray
5048

5149
#endif // LLVM_TOOLS_LLVM_XRAY_FUNC_ID_HELPER_H

llvm/tools/llvm-xray/trie-node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/STLExtras.h"
2222
#include "llvm/ADT/SmallVector.h"
2323

24+
namespace llvm {
2425
/// A type to represent a trie of invocations. It is useful to construct a
2526
/// graph of these nodes from reading an XRay trace, such that each function
2627
/// call can be placed in a larger context.
@@ -87,5 +88,6 @@ mergeTrieNodes(const TrieNode<T> &Left, const TrieNode<T> &Right,
8788

8889
return Node;
8990
}
91+
} // namespace llvm
9092

9193
#endif // LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H

llvm/tools/llvm-xray/xray-account.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,16 @@ static cl::opt<std::string>
118118
static cl::alias AccountInstrMap2("m", cl::aliasopt(AccountInstrMap),
119119
cl::desc("Alias for -instr_map"));
120120

121-
namespace {
122-
123-
template <class T, class U> void setMinMax(std::pair<T, T> &MM, U &&V) {
121+
template <class T, class U> static void setMinMax(std::pair<T, T> &MM, U &&V) {
124122
if (MM.first == 0 || MM.second == 0)
125123
MM = std::make_pair(std::forward<U>(V), std::forward<U>(V));
126124
else
127125
MM = std::make_pair(std::min(MM.first, V), std::max(MM.second, V));
128126
}
129127

130-
template <class T> T diff(T L, T R) { return std::max(L, R) - std::min(L, R); }
131-
132-
} // namespace
128+
template <class T> static T diff(T L, T R) {
129+
return std::max(L, R) - std::min(L, R);
130+
}
133131

134132
using RecursionStatus = LatencyAccountant::FunctionStack::RecursionStatus;
135133
RecursionStatus &RecursionStatus::operator++() {
@@ -143,6 +141,7 @@ RecursionStatus &RecursionStatus::operator++() {
143141
true); // Storage |= INT_MIN
144142
return *this;
145143
}
144+
146145
RecursionStatus &RecursionStatus::operator--() {
147146
auto Depth = Bitfield::get<RecursionStatus::Depth>(Storage);
148147
assert(Depth > 0);
@@ -153,6 +152,7 @@ RecursionStatus &RecursionStatus::operator--() {
153152
Bitfield::set<RecursionStatus::IsRecursive>(Storage, false); // Storage = 0
154153
return *this;
155154
}
155+
156156
bool RecursionStatus::isRecursive() const {
157157
return Bitfield::get<RecursionStatus::IsRecursive>(Storage); // Storage s< 0
158158
}
@@ -270,8 +270,9 @@ struct ResultRow {
270270
std::string DebugInfo;
271271
std::string Function;
272272
};
273+
} // namespace
273274

274-
ResultRow getStats(MutableArrayRef<uint64_t> Timings) {
275+
static ResultRow getStats(MutableArrayRef<uint64_t> Timings) {
275276
assert(!Timings.empty());
276277
ResultRow R;
277278
R.Sum = std::accumulate(Timings.begin(), Timings.end(), 0.0);
@@ -296,8 +297,6 @@ ResultRow getStats(MutableArrayRef<uint64_t> Timings) {
296297
return R;
297298
}
298299

299-
} // namespace
300-
301300
using TupleType = std::tuple<int32_t, uint64_t, ResultRow>;
302301

303302
template <typename F>
@@ -417,11 +416,8 @@ void LatencyAccountant::exportStatsAsCSV(raw_ostream &OS,
417416
});
418417
}
419418

420-
using namespace llvm::xray;
421-
422-
namespace llvm {
423-
template <> struct format_provider<llvm::xray::RecordTypes> {
424-
static void format(const llvm::xray::RecordTypes &T, raw_ostream &Stream,
419+
template <> struct llvm::format_provider<RecordTypes> {
420+
static void format(const RecordTypes &T, raw_ostream &Stream,
425421
StringRef Style) {
426422
switch (T) {
427423
case RecordTypes::ENTER:
@@ -445,7 +441,6 @@ template <> struct format_provider<llvm::xray::RecordTypes> {
445441
}
446442
}
447443
};
448-
} // namespace llvm
449444

450445
static CommandRegistration Unused(&Account, []() -> Error {
451446
InstrumentationMap Map;
@@ -468,10 +463,10 @@ static CommandRegistration Unused(&Account, []() -> Error {
468463

469464
const auto &FunctionAddresses = Map.getFunctionAddresses();
470465
symbolize::LLVMSymbolizer Symbolizer;
471-
llvm::xray::FuncIdConversionHelper FuncIdHelper(AccountInstrMap, Symbolizer,
472-
FunctionAddresses);
473-
xray::LatencyAccountant FCA(FuncIdHelper, AccountRecursiveCallsOnly,
474-
AccountDeduceSiblingCalls);
466+
FuncIdConversionHelper FuncIdHelper(AccountInstrMap, Symbolizer,
467+
FunctionAddresses);
468+
LatencyAccountant FCA(FuncIdHelper, AccountRecursiveCallsOnly,
469+
AccountDeduceSiblingCalls);
475470
auto TraceOrErr = loadTraceFile(AccountInput);
476471
if (!TraceOrErr)
477472
return joinErrors(

llvm/tools/llvm-xray/xray-account.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#include "llvm/Support/raw_ostream.h"
2222
#include "llvm/XRay/XRayRecord.h"
2323

24-
namespace llvm {
25-
namespace xray {
24+
namespace llvm::xray {
2625

2726
class LatencyAccountant {
2827
public:
@@ -107,7 +106,6 @@ class LatencyAccountant {
107106
template <class F> void exportStats(const XRayFileHeader &Header, F fn) const;
108107
};
109108

110-
} // namespace xray
111-
} // namespace llvm
109+
} // namespace llvm::xray
112110

113111
#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_ACCOUNT_H

llvm/tools/llvm-xray/xray-color-helper.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
#include "llvm/ADT/ArrayRef.h"
1717
#include <tuple>
1818

19-
namespace llvm {
20-
namespace xray {
19+
namespace llvm::xray {
2120

2221
/// The color helper class it a healper class which allows you to easily get a
2322
/// color in a gradient. This is used to color-code edges in XRay-Graph tools.
@@ -82,6 +81,6 @@ class ColorHelper {
8281
// Convert a tuple to a string
8382
static std::string getColorString(std::tuple<uint8_t, uint8_t, uint8_t> t);
8483
};
85-
} // namespace xray
86-
} // namespace llvm
84+
} // namespace llvm::xray
85+
8786
#endif

llvm/tools/llvm-xray/xray-converter.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,14 @@ struct StackIdData {
176176
// unique ID.
177177
SmallVector<TrieNode<StackIdData> *, 4> siblings;
178178
};
179+
} // namespace
179180

180181
using StackTrieNode = TrieNode<StackIdData>;
181182

182183
// A helper function to find the sibling nodes for an encountered function in a
183184
// thread of execution. Relies on the invariant that each time a new node is
184185
// traversed in a thread, sibling bidirectional pointers are maintained.
185-
SmallVector<StackTrieNode *, 4>
186+
static SmallVector<StackTrieNode *, 4>
186187
findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
187188
const DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>>
188189
&StackRootsByThreadId) {
@@ -213,7 +214,7 @@ findSiblings(StackTrieNode *parent, int32_t FnId, uint32_t TId,
213214
// StackTrie representing the function call stack. If no node exists, creates
214215
// the node. Assigns unique IDs to stacks newly encountered among all threads
215216
// and keeps sibling links up to when creating new nodes.
216-
StackTrieNode *findOrCreateStackNode(
217+
static StackTrieNode *findOrCreateStackNode(
217218
StackTrieNode *Parent, int32_t FuncId, uint32_t TId,
218219
DenseMap<uint32_t, SmallVector<StackTrieNode *, 4>> &StackRootsByThreadId,
219220
DenseMap<unsigned, StackTrieNode *> &StacksByStackId, unsigned *id_counter,
@@ -244,12 +245,13 @@ StackTrieNode *findOrCreateStackNode(
244245
return CurrentStack;
245246
}
246247

247-
void writeTraceViewerRecord(uint16_t Version, raw_ostream &OS, int32_t FuncId,
248-
uint32_t TId, uint32_t PId, bool Symbolize,
249-
const FuncIdConversionHelper &FuncIdHelper,
250-
double EventTimestampUs,
251-
const StackTrieNode &StackCursor,
252-
StringRef FunctionPhenotype) {
248+
static void writeTraceViewerRecord(uint16_t Version, raw_ostream &OS,
249+
int32_t FuncId, uint32_t TId, uint32_t PId,
250+
bool Symbolize,
251+
const FuncIdConversionHelper &FuncIdHelper,
252+
double EventTimestampUs,
253+
const StackTrieNode &StackCursor,
254+
StringRef FunctionPhenotype) {
253255
OS << " ";
254256
if (Version >= 3) {
255257
OS << llvm::formatv(
@@ -269,8 +271,6 @@ void writeTraceViewerRecord(uint16_t Version, raw_ostream &OS, int32_t FuncId,
269271
}
270272
}
271273

272-
} // namespace
273-
274274
void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
275275
raw_ostream &OS) {
276276
const auto &FH = Records.getFileHeader();
@@ -364,9 +364,6 @@ void TraceConverter::exportAsChromeTraceEventFormat(const Trace &Records,
364364
OS << "}\n"; // Close the JSON entry.
365365
}
366366

367-
namespace llvm {
368-
namespace xray {
369-
370367
static CommandRegistration Unused(&Convert, []() -> Error {
371368
// FIXME: Support conversion to BINARY when upgrading XRay trace versions.
372369
InstrumentationMap Map;
@@ -386,9 +383,9 @@ static CommandRegistration Unused(&Convert, []() -> Error {
386383
if (Demangle.getPosition() < NoDemangle.getPosition())
387384
SymbolizerOpts.Demangle = false;
388385
symbolize::LLVMSymbolizer Symbolizer(SymbolizerOpts);
389-
llvm::xray::FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
390-
FunctionAddresses);
391-
llvm::xray::TraceConverter TC(FuncIdHelper, ConvertSymbolize);
386+
FuncIdConversionHelper FuncIdHelper(ConvertInstrMap, Symbolizer,
387+
FunctionAddresses);
388+
TraceConverter TC(FuncIdHelper, ConvertSymbolize);
392389
std::error_code EC;
393390
raw_fd_ostream OS(ConvertOutput, EC,
394391
ConvertOutputFormat == ConvertFormats::BINARY
@@ -420,6 +417,3 @@ static CommandRegistration Unused(&Convert, []() -> Error {
420417
}
421418
return Error::success();
422419
});
423-
424-
} // namespace xray
425-
} // namespace llvm

llvm/tools/llvm-xray/xray-converter.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include "llvm/XRay/Trace.h"
1818
#include "llvm/XRay/XRayRecord.h"
1919

20-
namespace llvm {
21-
namespace xray {
20+
namespace llvm::xray {
2221

2322
class TraceConverter {
2423
FuncIdConversionHelper &FuncIdHelper;
@@ -37,7 +36,6 @@ class TraceConverter {
3736
void exportAsChromeTraceEventFormat(const Trace &Records, raw_ostream &OS);
3837
};
3938

40-
} // namespace xray
41-
} // namespace llvm
39+
} // namespace llvm::xray
4240

4341
#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_CONVERTER_H

llvm/tools/llvm-xray/xray-extract.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ static cl::opt<bool> NoDemangle("no-demangle",
5252
cl::desc("don't demangle symbols"),
5353
cl::sub(Extract));
5454

55-
namespace {
56-
57-
void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
58-
FuncIdConversionHelper &FH) {
55+
static void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
56+
FuncIdConversionHelper &FH) {
5957
// First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
6058
std::vector<YAMLXRaySledEntry> YAMLSleds;
6159
auto Sleds = Map.sleds();
@@ -72,8 +70,6 @@ void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
7270
Out << YAMLSleds;
7371
}
7472

75-
} // namespace
76-
7773
static CommandRegistration Unused(&Extract, []() -> Error {
7874
auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
7975
if (!InstrumentationMapOrError)
@@ -94,8 +90,8 @@ static CommandRegistration Unused(&Extract, []() -> Error {
9490
if (Demangle.getPosition() < NoDemangle.getPosition())
9591
opts.Demangle = false;
9692
symbolize::LLVMSymbolizer Symbolizer(opts);
97-
llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
98-
FunctionAddresses);
93+
FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
94+
FunctionAddresses);
9995
exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
10096
return Error::success();
10197
});

llvm/tools/llvm-xray/xray-graph-diff.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ Expected<GraphDiffRenderer> GraphDiffRenderer::Factory::getGraphDiffRenderer() {
236236

237237
return R;
238238
}
239+
239240
// Returns the Relative change With respect to LeftStat between LeftStat
240241
// and RightStat.
241242
static double statRelDiff(const GraphDiffRenderer::TimeStat &LeftStat,
@@ -363,9 +364,8 @@ void GraphDiffRenderer::exportGraphAsDOT(raw_ostream &OS, StatType EdgeLabel,
363364
StringMap<int32_t> VertexNo;
364365

365366
int i = 0;
366-
for (const auto &V : G.vertices()) {
367+
for (const auto &V : G.vertices())
367368
VertexNo[V.first] = i++;
368-
}
369369

370370
ColorHelper H(ColorHelper::DivergingScheme::PiYG);
371371

llvm/tools/llvm-xray/xray-graph-diff.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include "xray-graph.h"
1818
#include "llvm/XRay/Graph.h"
1919

20-
namespace llvm {
21-
namespace xray {
20+
namespace llvm::xray {
2221

2322
// This class creates a graph representing the difference between two
2423
// xray-graphs And allows you to print it to a dot file, with optional color
@@ -66,7 +65,6 @@ class GraphDiffRenderer {
6665

6766
const GraphT &getGraph() { return G; }
6867
};
69-
} // namespace xray
70-
} // namespace llvm
68+
} // namespace llvm::xray
7169

7270
#endif

0 commit comments

Comments
 (0)