Skip to content

Commit 5b4a352

Browse files
committed
Minor cleanup
1 parent 0e719ea commit 5b4a352

4 files changed

Lines changed: 58 additions & 39 deletions

File tree

mlir/lib/Tools/mlir-query/Diagnostics.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_DIAGNOSTICS_H
14-
#define LLVM_CLANG_ASTMATCHERS_DYNAMIC_DIAGNOSTICS_H
13+
#ifndef MLIR_TOOLS_MLIRQUERY_MATCHERS_DIAGNOSTICS_H
14+
#define MLIR_TOOLS_MLIRQUERY_MATCHERS_DIAGNOSTICS_H
1515

1616
#include "VariantValue.h"
1717
#include "llvm/ADT/ArrayRef.h"
@@ -177,4 +177,4 @@ class Diagnostics {
177177
} // namespace query
178178
} // namespace mlir
179179

180-
#endif // LLVM_CLANG_ASTMATCHERS_DYNAMIC_DIAGNOSTICS_H
180+
#endif // MLIR_TOOLS_MLIRQUERY_MATCHERS_DIAGNOSTICS_H

mlir/lib/Tools/mlir-query/ExtraMatchers.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,21 @@
88
//
99
// This file provides extra matchers that are very useful for mlir-query. The
1010
// goal is to move this to include/mlir/IR/Matchers.h after the initial testing
11-
// phase.
11+
// phase. The matchers in this file are:
12+
//
13+
// - operation(args...): Matches an operation that matches all of the matchers
14+
// in the vector `matchers`.
15+
//
16+
// - argument(innerMatcher, index): Matches an operation argument that matches
17+
// `innerMatcher` at the given `index`.
18+
//
19+
// - usedBy(innerMatcher, hops, inclusive): Matches an operation that is used by
20+
// an operation that matches `innerMatcher` `hops` hops away. If `inclusive` is
21+
// true, also matches operations upto `hops` away.
22+
//
23+
// - definedBy(innerMatcher, hops, inclusive): Matches an operation that is
24+
// defined by an operation that matches `innerMatcher` `hops` hops away. If
25+
// `inclusive` is true, also matches operations upto `hops` away.
1226
//
1327
//===----------------------------------------------------------------------===//
1428

@@ -24,7 +38,9 @@ namespace query {
2438
namespace extramatcher {
2539

2640
namespace detail {
27-
// VariadicMatcher takes a vector of DynMatchers and returns true if all
41+
42+
// TODO: Rename to AllOf
43+
// OperationMatcher takes a vector of DynMatchers and returns true if all
2844
// DynMatchers match the given operation.
2945
struct OperationMatcher {
3046
OperationMatcher(std::vector<matcher::DynMatcher> matchers)
@@ -38,6 +54,7 @@ struct OperationMatcher {
3854
std::vector<matcher::DynMatcher> matchers;
3955
};
4056

57+
// ArgumentMatcher matches the operand of an operation at a specific index.
4158
struct ArgumentMatcher {
4259
ArgumentMatcher(matcher::DynMatcher innerMatcher, unsigned index)
4360
: innerMatcher(innerMatcher), index(index) {}
@@ -57,6 +74,9 @@ struct ArgumentMatcher {
5774
unsigned index;
5875
};
5976

77+
// This matcher checks if an operation is used by another operation that matches
78+
// the given inner matcher. It allows specifying the number of hops to follow in
79+
// the use-def chain, and whether the chain should be inclusive or not.
6080
struct UsedByMatcher {
6181
UsedByMatcher(matcher::DynMatcher innerMatcher, unsigned hops, bool inclusive)
6282
: innerMatcher(innerMatcher), hops(hops), inclusive(inclusive) {}
@@ -90,6 +110,10 @@ struct UsedByMatcher {
90110
bool inclusive;
91111
};
92112

113+
// This matcher checks if an operation is defined by another operation that
114+
// matches the given inner matcher. It allows specifying the number of hops to
115+
// follow in the def-use chain, and whether the chain should be inclusive or
116+
// not.
93117
struct DefinedByMatcher {
94118
DefinedByMatcher(matcher::DynMatcher innerMatcher, unsigned hops,
95119
bool inclusive)
@@ -121,6 +145,7 @@ struct DefinedByMatcher {
121145

122146
} // namespace detail
123147

148+
// TODO: Rename to allOf()
124149
inline detail::OperationMatcher operation(matcher::DynMatcher args...) {
125150
std::vector<matcher::DynMatcher> matchers({args});
126151
return detail::OperationMatcher(matchers);

mlir/lib/Tools/mlir-query/MLIRTypeTraits.h

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ namespace mlir {
2222
namespace query {
2323
namespace matcher {
2424

25-
/// MLIRNodeKind identifier.
26-
/// It can be constructed from any node kind and allows for runtime type
27-
/// hierarchy checks.
28-
/// Use getFromNodeKind<T>() to construct them.
25+
// MLIRNodeKind can be constructed from any node kind and allows for runtime type
26+
// hierarchy checks. Use getFromNodeKind<T>() to construct them.
2927
class MLIRNodeKind {
3028
public:
3129
// Empty identifier. It matches nothing.
@@ -53,8 +51,8 @@ class MLIRNodeKind {
5351
// Use getFromNodeKind<T>() to construct the kind.
5452
constexpr MLIRNodeKind(NodeKindId KindId) : KindId(KindId) {}
5553

56-
/// Helper meta-function to convert a kind T to its enum value.
57-
/// This struct is specialized below for all known kinds.
54+
// Helper meta-function to convert a kind T to its enum value.
55+
// This struct is specialized below for all known kinds.
5856
template <class T>
5957
struct KindToKindId {
6058
static const NodeKindId Id = NKI_None;
@@ -70,21 +68,21 @@ struct MLIRNodeKind::KindToKindId<Operation *> {
7068
static const NodeKindId Id = NKI_Operation;
7169
};
7270

73-
/// A dynamically typed MLIR node container.
74-
///
75-
/// Stores an MLIR node in a type safe way. This allows writing code that
76-
/// works with different kinds of MLIR nodes, despite the fact that they don't
77-
/// have a common base class.
78-
///
79-
/// Use create(Node) to create a DynTypedNode from an MLIR node,
80-
/// and get<T>() to retrieve the node as type T if the types match.
81-
///
82-
/// See MLIRNodeKind for which node base types are currently supported;
83-
/// You can create DynTypedNodes for all nodes in the inheritance hierarchy of
84-
/// the supported base types.
71+
// A dynamically typed MLIR node container.
72+
//
73+
// Stores an MLIR node in a type safe way. This allows writing code that
74+
// works with different kinds of MLIR nodes, despite the fact that they don't
75+
// have a common base class.
76+
//
77+
// Use create(Node) to create a DynTypedNode from an MLIR node,
78+
// and get<T>() to retrieve the node as type T if the types match.
79+
//
80+
// See MLIRNodeKind for which node base types are currently supported;
81+
// You can create DynTypedNodes for all nodes in the inheritance hierarchy of
82+
// the supported base types.
8583
class DynTypedNode {
8684
public:
87-
/// Creates a \c DynTypedNode from \c Node.
85+
// Creates a DynTypedNode from Node.
8886
template <typename T>
8987
static DynTypedNode create(T &Node) {
9088
return BaseConverter<T>::create(Node);

mlir/lib/Tools/mlir-query/Marshallers.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
#ifndef MLIR_TOOLS_MLIRQUERY_MATCHERS_MARSHALLERS_H
1919
#define MLIR_TOOLS_MLIRQUERY_MATCHERS_MARSHALLERS_H
2020

21-
#include <list>
22-
#include <string>
23-
#include <vector>
24-
2521
#include "MatchersInternal.h"
2622
#include "VariantValue.h"
2723
#include "mlir/IR/Matchers.h"
@@ -88,7 +84,7 @@ class MatcherCreateCallback {
8884
public:
8985
virtual ~MatcherCreateCallback() = default;
9086
virtual DynMatcher *run(const SourceRange &NameRange,
91-
ArrayRef<ParserValue> Args,
87+
const ArrayRef<ParserValue> &Args,
9288
Diagnostics *Error) const = 0;
9389
};
9490

@@ -104,7 +100,7 @@ class FixedArgCountMatcherCreateCallback : public MatcherCreateCallback {
104100
StringRef MatcherName)
105101
: Marshaller(Marshaller), Func(Func), MatcherName(MatcherName) {}
106102

107-
DynMatcher *run(const SourceRange &NameRange, ArrayRef<ParserValue> Args,
103+
DynMatcher *run(const SourceRange &NameRange, const ArrayRef<ParserValue> &Args,
108104
Diagnostics *Error) const override {
109105
return Marshaller(Func, MatcherName, NameRange, Args, Error);
110106
}
@@ -124,16 +120,16 @@ class VariadicMatcherCreateCallback : public MatcherCreateCallback {
124120

125121
typedef DynMatcher DerivedMatcherType;
126122

127-
DynMatcher *run(const SourceRange &NameRange, ArrayRef<ParserValue> Args,
123+
DynMatcher *run(const SourceRange &NameRange, const ArrayRef<ParserValue> &Args,
128124
Diagnostics *Error) const override {
129125
std::vector<DerivedMatcherType> References;
130126
std::vector<const DerivedMatcherType *> InnerArgs(Args.size());
131-
for (size_t i = 0, e = Args.size(); i != e; ++i) {
127+
for (std::size_t i = 0, e = Args.size(); i != e; ++i) {
132128

133129
if (!ArgTypeTraits<DerivedMatcherType>::is(Args[i].Value)) {
134130
Error->addError(Args[i].Range, Error->ET_RegistryWrongArgType)
135131
<< MatcherName << i + 1;
136-
return NULL;
132+
return nullptr;
137133
}
138134
References.push_back(
139135
ArgTypeTraits<DerivedMatcherType>::get(Args[i].Value));
@@ -170,7 +166,7 @@ DynMatcher *matcherMarshall0(ReturnType (*Func)(), StringRef MatcherName,
170166
if (Args.size() != 0) {
171167
Error->addError(NameRange, Error->ET_RegistryWrongArgCount)
172168
<< 0 << Args.size();
173-
return NULL;
169+
return nullptr;
174170
}
175171
ReturnType matcherFn = Func();
176172
MatcherInterface<T> *singleMatcher =
@@ -189,12 +185,12 @@ DynMatcher *matcherMarshall1(ReturnType (*Func)(InArgType1),
189185
if (Args.size() != 1) {
190186
Error->addError(NameRange, Error->ET_RegistryWrongArgCount)
191187
<< 1 << Args.size();
192-
return NULL;
188+
return nullptr;
193189
}
194190
if (!ArgTypeTraits<ArgType1>::is(Args[0].Value)) {
195191
Error->addError(Args[0].Range, Error->ET_RegistryWrongArgType)
196192
<< MatcherName << 1;
197-
return NULL;
193+
return nullptr;
198194
}
199195
ReturnType matcherFn = Func(ArgTypeTraits<ArgType1>::get(Args[0].Value));
200196
MatcherInterface<T> *singleMatcher =
@@ -215,17 +211,17 @@ DynMatcher *matcherMarshall1(ReturnType (*Func)(InArgType1, InArgType2),
215211
if (Args.size() != 2) {
216212
Error->addError(NameRange, Error->ET_RegistryWrongArgCount)
217213
<< 1 << Args.size();
218-
return NULL;
214+
return nullptr;
219215
}
220216
if (!ArgTypeTraits<ArgType1>::is(Args[0].Value)) {
221217
Error->addError(Args[0].Range, Error->ET_RegistryWrongArgType)
222218
<< MatcherName << 1;
223-
return NULL;
219+
return nullptr;
224220
}
225221
if (!ArgTypeTraits<ArgType2>::is(Args[1].Value)) {
226222
Error->addError(Args[1].Range, Error->ET_RegistryWrongArgType)
227223
<< MatcherName << 1;
228-
return NULL;
224+
return nullptr;
229225
}
230226
ReturnType matcherFn = Func(ArgTypeTraits<ArgType1>::get(Args[0].Value),
231227
ArgTypeTraits<ArgType2>::get(Args[1].Value));

0 commit comments

Comments
 (0)