22 #include "llvm/LineEditor/LineEditor.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/InitLLVM.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>"),
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();
73 std::string errorMessage;
76 llvm::errs() << errorMessage <<
"\n";
80 auto sourceMgr = llvm::SourceMgr();
81 auto bufferId = sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc());
88 : parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
94 if (!commands.empty()) {
95 for (
auto &command : commands) {
101 llvm::LineEditor le(
"mlir-query");
102 le.setListCompleter([&qs](llvm::StringRef line,
size_t pos) {
105 while (std::optional<std::string> line = le.readLine()) {
107 (void)queryRef->run(llvm::outs(), qs);
108 llvm::outs().flush();
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)
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
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 success(bool isSuccess=true)
Utility function to generate a LogicalResult.
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 ...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.