21 #include "llvm/LineEditor/LineEditor.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/SourceMgr.h"
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();
72 std::string errorMessage;
75 llvm::errs() << errorMessage <<
"\n";
76 return mlir::failure();
79 auto sourceMgr = llvm::SourceMgr();
80 auto bufferId = sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc());
87 : parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
89 return mlir::failure();
93 if (!commands.empty()) {
94 for (
auto &command : commands) {
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) {
104 while (std::optional<std::string> line = le.readLine()) {
106 (void)queryRef->run(llvm::outs(), qs);
107 llvm::outs().flush();
113 return mlir::success();
MLIRContext is the top-level object for a collection of MLIR operations.
void allowUnregisteredDialects(bool allow=true)
Enables creating operations in unregistered dialects.
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
OpTy get() const
Allow accessing the internal op.
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
llvm::IntrusiveRefCntPtr< Query > QueryRef
std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
std::unique_ptr< llvm::MemoryBuffer > openInputFile(llvm::StringRef inputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for reading.
LogicalResult mlirQueryMain(int argc, char **argv, MLIRContext &context, const mlir::query::matcher::Registry &matcherRegistry)
LogicalResult parseSourceFile(const llvm::SourceMgr &sourceMgr, Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc=nullptr)
This parses the file specified by the indicated SourceMgr and appends parsed operations to the given ...