1 //===- MlirQueryMain.cpp - MLIR Query main --------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the general framework of the MLIR query tool. It
10 // parses the command line arguments, parses the MLIR file and outputs the query
13 //===----------------------------------------------------------------------===//
15 #include "mlir/Tools/mlir-query/MlirQueryMain.h"
16 #include "mlir/IR/BuiltinOps.h"
17 #include "mlir/Parser/Parser.h"
18 #include "mlir/Query/Query.h"
19 #include "mlir/Query/QuerySession.h"
20 #include "mlir/Support/FileUtilities.h"
21 #include "llvm/LineEditor/LineEditor.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/SourceMgr.h"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
31 mlir::mlirQueryMain(int argc
, char **argv
, MLIRContext
&context
,
32 const mlir::query::matcher::Registry
&matcherRegistry
) {
34 // Override the default '-h' and use the default PrintHelpMessage() which
35 // won't print options in categories.
36 static llvm::cl::opt
<bool> help("h", llvm::cl::desc("Alias for -help"),
39 static llvm::cl::OptionCategory
mlirQueryCategory("mlir-query options");
41 static llvm::cl::list
<std::string
> commands(
42 "c", llvm::cl::desc("Specify command to run"),
43 llvm::cl::value_desc("command"), llvm::cl::cat(mlirQueryCategory
));
45 static llvm::cl::opt
<std::string
> inputFilename(
46 llvm::cl::Positional
, llvm::cl::desc("<input file>"),
47 llvm::cl::cat(mlirQueryCategory
));
49 static llvm::cl::opt
<bool> noImplicitModule
{
52 "Disable implicit addition of a top-level module op during parsing"),
53 llvm::cl::init(false)};
55 static llvm::cl::opt
<bool> allowUnregisteredDialects(
56 "allow-unregistered-dialect",
57 llvm::cl::desc("Allow operation with no registered dialects"),
58 llvm::cl::init(false));
60 llvm::cl::HideUnrelatedOptions(mlirQueryCategory
);
62 llvm::InitLLVM
y(argc
, argv
);
64 llvm::cl::ParseCommandLineOptions(argc
, argv
, "MLIR test case query tool.\n");
67 llvm::cl::PrintHelpMessage();
68 return mlir::success();
71 // Set up the input file.
72 std::string errorMessage
;
73 auto file
= openInputFile(inputFilename
, &errorMessage
);
75 llvm::errs() << errorMessage
<< "\n";
76 return mlir::failure();
79 auto sourceMgr
= llvm::SourceMgr();
80 auto bufferId
= sourceMgr
.AddNewSourceBuffer(std::move(file
), SMLoc());
82 context
.allowUnregisteredDialects(allowUnregisteredDialects
);
84 // Parse the input MLIR file.
85 OwningOpRef
<Operation
*> opRef
=
86 noImplicitModule
? parseSourceFile(sourceMgr
, &context
)
87 : parseSourceFile
<mlir::ModuleOp
>(sourceMgr
, &context
);
89 return mlir::failure();
91 mlir::query::QuerySession
qs(opRef
.get(), sourceMgr
, bufferId
,
93 if (!commands
.empty()) {
94 for (auto &command
: commands
) {
95 mlir::query::QueryRef queryRef
= mlir::query::parse(command
, qs
);
96 if (mlir::failed(queryRef
->run(llvm::outs(), qs
)))
97 return mlir::failure();
100 llvm::LineEditor
le("mlir-query");
101 le
.setListCompleter([&qs
](llvm::StringRef line
, size_t pos
) {
102 return mlir::query::complete(line
, pos
, qs
);
104 while (std::optional
<std::string
> line
= le
.readLine()) {
105 mlir::query::QueryRef queryRef
= mlir::query::parse(*line
, qs
);
106 (void)queryRef
->run(llvm::outs(), qs
);
107 llvm::outs().flush();
113 return mlir::success();