1- // ===- MatchFinder.h - ------------------------------------------*- C++ -*-===//
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- //
71// ===----------------------------------------------------------------------===//
8- //
9- // This file contains the MatchFinder class, which is used to find operations
10- // that match a given matcher.
11- //
2+ // MatchFinder.h
123// ===----------------------------------------------------------------------===//
134
145#ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
156#define MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
167
178#include " MatchersInternal.h"
18- #include " mlir/IR/Operation.h"
9+ #include " mlir/Query/QuerySession.h"
10+ #include " llvm/ADT/SetVector.h"
11+ #include " llvm/Support/SourceMgr.h"
12+ #include " llvm/Support/raw_ostream.h"
1913
2014namespace mlir ::query::matcher {
2115
22- // MatchFinder is used to find all operations that match a given matcher.
2316class MatchFinder {
17+ private:
18+ // Base print function with binding text
19+ static void printMatch (llvm::raw_ostream &os, QuerySession &qs,
20+ mlir::Operation *op, const std::string &binding) {
21+ auto fileLoc = op->getLoc ()->findInstanceOf <FileLineColLoc>();
22+ auto smloc = qs.getSourceManager ().FindLocForLineAndColumn (
23+ qs.getBufferId (), fileLoc.getLine (), fileLoc.getColumn ());
24+ qs.getSourceManager ().PrintMessage (os, smloc, llvm::SourceMgr::DK_Note,
25+ " \" " + binding + " \" binds here" );
26+ };
27+
2428public:
25- // Returns all operations that match the given matcher.
2629 static SetVector<Operation *>
27- getMatches (Operation *root, QueryOptions &options, DynMatcher matcher) {
28- SetVector<Operation *> backwardSlice;
30+ getMatches (Operation *root, QueryOptions &options, DynMatcher matcher,
31+ llvm::raw_ostream &os, QuerySession &qs) {
32+ unsigned matchCount = 0 ;
33+ SetVector<Operation *> matchedOps;
34+ SetVector<Operation *> tempStorage;
35+
2936 root->walk ([&](Operation *subOp) {
3037 if (matcher.match (subOp)) {
31- backwardSlice.insert (subOp);
38+ matchedOps.insert (subOp);
39+ os << " Match #" << ++matchCount << " :\n\n " ;
40+ printMatch (os, qs, subOp, " root" );
3241 } else {
33- matcher.match (subOp, backwardSlice, options);
34- // //
42+ SmallVector<Operation *> printingOps;
43+ size_t sizeBefore = matchedOps.size ();
44+ if (matcher.match (subOp, tempStorage, options)) {
45+ os << " Match #" << ++matchCount << " :\n\n " ;
46+ SmallVector<Operation *> printingOps (tempStorage.takeVector ());
47+ for (auto op : printingOps) {
48+ printMatch (os, qs, op, " " ); // Using version without binding text
49+ matchedOps.insert (op);
50+ }
51+ printingOps.clear ();
52+ }
3553 }
3654 });
37- return backwardSlice ;
55+ return matchedOps ;
3856 }
3957};
4058
4159} // namespace mlir::query::matcher
4260
43- #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
61+ #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
0 commit comments