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"
37 static llvm::cl::opt<bool> help(
"h", llvm::cl::desc(
"Alias for -help"),
40 static llvm::cl::OptionCategory mlirQueryCategory(
"mlir-query options");
42 static llvm::cl::list<std::string> commands(
43 "c", llvm::cl::desc(
"Specify command to run"),
44 llvm::cl::value_desc(
"command"), llvm::cl::cat(mlirQueryCategory));
46 static llvm::cl::opt<std::string> inputFilename(
47 llvm::cl::Positional, llvm::cl::desc(
"<input file>"), llvm::cl::init(
"-"),
48 llvm::cl::cat(mlirQueryCategory));
50 static llvm::cl::opt<bool> noImplicitModule{
53 "Disable implicit addition of a top-level module op during parsing"),
54 llvm::cl::init(
false)};
56 static llvm::cl::opt<bool> allowUnregisteredDialects(
57 "allow-unregistered-dialect",
58 llvm::cl::desc(
"Allow operation with no registered dialects"),
59 llvm::cl::init(
false));
61 llvm::cl::HideUnrelatedOptions(mlirQueryCategory);
63 llvm::InitLLVM y(argc, argv);
65 llvm::cl::ParseCommandLineOptions(argc, argv,
"MLIR test case query tool.\n");
68 llvm::cl::PrintHelpMessage();
69 return mlir::success();
75 if (inputFilename ==
"-" &&
76 llvm::sys::Process::FileDescriptorIsDisplayed(fileno(stdin)))
77 llvm::errs() <<
"(processing input from stdin now, hit ctrl-c/ctrl-d to "
81 std::string errorMessage;
84 llvm::errs() << errorMessage <<
"\n";
85 return mlir::failure();
88 auto sourceMgr = llvm::SourceMgr();
89 auto bufferId = sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc());
96 : parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
98 return mlir::failure();
102 if (!commands.empty()) {
103 for (
auto &command : commands) {
106 return mlir::failure();
109 llvm::LineEditor le(
"mlir-query");
110 le.setListCompleter([&qs](llvm::StringRef line,
size_t pos) {
113 while (std::optional<std::string> line = le.readLine()) {
115 (void)queryRef->run(llvm::outs(), qs);
116 llvm::outs().flush();
122 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 ...