MLIR 24.0.0git
MlirQueryMain.cpp
Go to the documentation of this file.
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
16#include "mlir/IR/BuiltinOps.h"
17#include "mlir/Parser/Parser.h"
18#include "mlir/Query/Query.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("-"),
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(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(false));
55
56 llvm::cl::HideUnrelatedOptions(mlirQueryCategory);
57
58 llvm::InitLLVM y(argc, argv);
59
60 llvm::cl::ParseCommandLineOptions(argc, argv, "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(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);
73 if (!file) {
74 llvm::errs() << errorMessage << "\n";
75 return mlir::failure();
76 }
77
78 auto sourceMgr = llvm::SourceMgr();
79 auto bufferId = sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc());
80
81 context.allowUnregisteredDialects(allowUnregisteredDialects);
82
83 // Parse the input MLIR file.
85 noImplicitModule ? parseSourceFile(sourceMgr, &context)
86 : parseSourceFile<mlir::ModuleOp>(sourceMgr, &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(command, qs);
95 if (mlir::failed(queryRef->run(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, qs);
105 (void)queryRef->run(llvm::outs(), qs);
106 llvm::outs().flush();
107 if (qs.terminate)
108 break;
109 }
110 }
111
112 return mlir::success();
113}
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
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...
Definition OwningOpRef.h:29
OpTy get() const
Allow accessing the internal op.
Definition OwningOpRef.h:51
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Definition Query.cpp:21
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition Query.h:36
std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
Definition Query.cpp:26
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 ...
Definition Parser.cpp:38