1//===- MlirQueryMain.cpp - MLIR Query main --------------------------------===//
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 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
11// results.
12//
13//===----------------------------------------------------------------------===//
14
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/Process.h"
25#include "llvm/Support/SourceMgr.h"
26
27//===----------------------------------------------------------------------===//
28// Query Parser
29//===----------------------------------------------------------------------===//
30
31llvm::LogicalResult
32mlir::mlirQueryMain(int argc, char **argv, MLIRContext &context,
33 const mlir::query::matcher::Registry &matcherRegistry) {
34
35 static llvm::cl::OptionCategory mlirQueryCategory("mlir-query options");
36
37 static llvm::cl::list<std::string> commands(
38 "c", llvm::cl::desc("Specify command to run"),
39 llvm::cl::value_desc("command"), llvm::cl::cat(mlirQueryCategory));
40
41 static llvm::cl::opt<std::string> inputFilename(
42 llvm::cl::Positional, llvm::cl::desc("<input file>"), llvm::cl::init(Val: "-"),
43 llvm::cl::cat(mlirQueryCategory));
44
45 static llvm::cl::opt<bool> noImplicitModule{
46 "no-implicit-module",
47 llvm::cl::desc(
48 "Disable implicit addition of a top-level module op during parsing"),
49 llvm::cl::init(Val: false)};
50
51 static llvm::cl::opt<bool> allowUnregisteredDialects(
52 "allow-unregistered-dialect",
53 llvm::cl::desc("Allow operation with no registered dialects"),
54 llvm::cl::init(Val: false));
55
56 llvm::cl::HideUnrelatedOptions(Category&: mlirQueryCategory);
57
58 llvm::InitLLVM y(argc, argv);
59
60 llvm::cl::ParseCommandLineOptions(argc, argv, Overview: "MLIR test case query tool.\n");
61
62 // When reading from stdin and the input is a tty, it is often a user mistake
63 // and the process "appears to be stuck". Print a message to let the user
64 // know!
65 if (inputFilename == "-" &&
66 llvm::sys::Process::FileDescriptorIsDisplayed(fd: fileno(stdin)))
67 llvm::errs() << "(processing input from stdin now, hit ctrl-c/ctrl-d to "
68 "interrupt)\n";
69
70 // Set up the input file.
71 std::string errorMessage;
72 auto file = openInputFile(inputFilename, errorMessage: &errorMessage);
73 if (!file) {
74 llvm::errs() << errorMessage << "\n";
75 return mlir::failure();
76 }
77
78 auto sourceMgr = llvm::SourceMgr();
79 auto bufferId = sourceMgr.AddNewSourceBuffer(F: std::move(file), IncludeLoc: SMLoc());
80
81 context.allowUnregisteredDialects(allow: allowUnregisteredDialects);
82
83 // Parse the input MLIR file.
84 OwningOpRef<Operation *> opRef =
85 noImplicitModule ? parseSourceFile(sourceMgr, config: &context)
86 : parseSourceFile<mlir::ModuleOp>(sourceMgr, config: &context);
87 if (!opRef)
88 return mlir::failure();
89
90 mlir::query::QuerySession qs(opRef.get(), sourceMgr, bufferId,
91 matcherRegistry);
92 if (!commands.empty()) {
93 for (auto &command : commands) {
94 mlir::query::QueryRef queryRef = mlir::query::parse(line: command, qs);
95 if (mlir::failed(Result: queryRef->run(os&: llvm::outs(), qs)))
96 return mlir::failure();
97 }
98 } else {
99 llvm::LineEditor le("mlir-query");
100 le.setListCompleter([&qs](llvm::StringRef line, size_t pos) {
101 return mlir::query::complete(line, pos, qs);
102 });
103 while (std::optional<std::string> line = le.readLine()) {
104 mlir::query::QueryRef queryRef = mlir::query::parse(line: *line, qs);
105 (void)queryRef->run(os&: llvm::outs(), qs);
106 llvm::outs().flush();
107 if (qs.terminate)
108 break;
109 }
110 }
111
112 return mlir::success();
113}
114