xref: /llvm-project/mlir/tools/mlir-query/mlir-query.cpp (revision 02d9f4d1f128e17e04ab6e602d3c9b9942612428)
1 //===- mlir-query.cpp - MLIR Query Driver ---------------------------------===//
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 is a command line utility that queries a file from/to MLIR using one
10 // of the registered queries.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/IR/Dialect.h"
15 #include "mlir/IR/MLIRContext.h"
16 #include "mlir/IR/Matchers.h"
17 #include "mlir/InitAllDialects.h"
18 #include "mlir/Query/Matcher/Registry.h"
19 #include "mlir/Tools/mlir-query/MlirQueryMain.h"
20 
21 using namespace mlir;
22 
23 // This is needed because these matchers are defined as overloaded functions.
24 using HasOpAttrName = detail::AttrOpMatcher(StringRef);
25 using HasOpName = detail::NameOpMatcher(StringRef);
26 using IsConstantOp = detail::constant_op_matcher();
27 
28 namespace test {
29 #ifdef MLIR_INCLUDE_TESTS
30 void registerTestDialect(DialectRegistry &);
31 #endif
32 } // namespace test
33 
main(int argc,char ** argv)34 int main(int argc, char **argv) {
35 
36   DialectRegistry dialectRegistry;
37   registerAllDialects(dialectRegistry);
38 
39   query::matcher::Registry matcherRegistry;
40 
41   // Matchers registered in alphabetical order for consistency:
42   matcherRegistry.registerMatcher("hasOpAttrName",
43                                   static_cast<HasOpAttrName *>(m_Attr));
44   matcherRegistry.registerMatcher("hasOpName", static_cast<HasOpName *>(m_Op));
45   matcherRegistry.registerMatcher("isConstantOp",
46                                   static_cast<IsConstantOp *>(m_Constant));
47   matcherRegistry.registerMatcher("isNegInfFloat", m_NegInfFloat);
48   matcherRegistry.registerMatcher("isNegZeroFloat", m_NegZeroFloat);
49   matcherRegistry.registerMatcher("isNonZero", m_NonZero);
50   matcherRegistry.registerMatcher("isOne", m_One);
51   matcherRegistry.registerMatcher("isOneFloat", m_OneFloat);
52   matcherRegistry.registerMatcher("isPosInfFloat", m_PosInfFloat);
53   matcherRegistry.registerMatcher("isPosZeroFloat", m_PosZeroFloat);
54   matcherRegistry.registerMatcher("isZero", m_Zero);
55   matcherRegistry.registerMatcher("isZeroFloat", m_AnyZeroFloat);
56 
57 #ifdef MLIR_INCLUDE_TESTS
58   test::registerTestDialect(dialectRegistry);
59 #endif
60   MLIRContext context(dialectRegistry);
61 
62   return failed(mlirQueryMain(argc, argv, context, matcherRegistry));
63 }
64