MLIR  19.0.0git
Query.cpp
Go to the documentation of this file.
1 //===---- Query.cpp - -----------------------------------------------------===//
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 #include "mlir/Query/Query.h"
10 #include "QueryParser.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "llvm/Support/raw_ostream.h"
16 
17 namespace mlir::query {
18 
19 QueryRef parse(llvm::StringRef line, const QuerySession &qs) {
20  return QueryParser::parse(line, qs);
21 }
22 
23 std::vector<llvm::LineEditor::Completion>
24 complete(llvm::StringRef line, size_t pos, const QuerySession &qs) {
25  return QueryParser::complete(line, pos, qs);
26 }
27 
28 static void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op,
29  const std::string &binding) {
30  auto fileLoc = op->getLoc()->findInstanceOf<FileLineColLoc>();
31  auto smloc = qs.getSourceManager().FindLocForLineAndColumn(
32  qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
33  qs.getSourceManager().PrintMessage(os, smloc, llvm::SourceMgr::DK_Note,
34  "\"" + binding + "\" binds here");
35 }
36 
37 Query::~Query() = default;
38 
39 mlir::LogicalResult InvalidQuery::run(llvm::raw_ostream &os,
40  QuerySession &qs) const {
41  os << errStr << "\n";
42  return mlir::failure();
43 }
44 
45 mlir::LogicalResult NoOpQuery::run(llvm::raw_ostream &os,
46  QuerySession &qs) const {
47  return mlir::success();
48 }
49 
50 mlir::LogicalResult HelpQuery::run(llvm::raw_ostream &os,
51  QuerySession &qs) const {
52  os << "Available commands:\n\n"
53  " match MATCHER, m MATCHER "
54  "Match the mlir against the given matcher.\n"
55  " quit "
56  "Terminates the query session.\n\n";
57  return mlir::success();
58 }
59 
60 mlir::LogicalResult QuitQuery::run(llvm::raw_ostream &os,
61  QuerySession &qs) const {
62  qs.terminate = true;
63  return mlir::success();
64 }
65 
66 mlir::LogicalResult MatchQuery::run(llvm::raw_ostream &os,
67  QuerySession &qs) const {
68  int matchCount = 0;
69  std::vector<Operation *> matches =
71  os << "\n";
72  for (Operation *op : matches) {
73  os << "Match #" << ++matchCount << ":\n\n";
74  // Placeholder "root" binding for the initial draft.
75  printMatch(os, qs, op, "root");
76  }
77  os << matchCount << (matchCount == 1 ? " match.\n\n" : " matches.\n\n");
78 
79  return mlir::success();
80 }
81 
82 } // namespace mlir::query
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
static QueryRef parse(llvm::StringRef line, const QuerySession &qs)
static std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
llvm::SourceMgr & getSourceManager() const
Definition: QuerySession.h:26
static std::vector< Operation * > getMatches(Operation *root, DynMatcher matcher)
Definition: MatchFinder.h:25
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Definition: Query.cpp:19
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:37
std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
Definition: Query.cpp:24
static void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op, const std::string &binding)
Definition: Query.cpp:28
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
mlir::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:50
std::string errStr
Definition: Query.h:51
mlir::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:39
mlir::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:66
const matcher::DynMatcher matcher
Definition: Query.h:98
mlir::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:45
mlir::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:60