[Mlir-commits] [mlir] [mlir] Improve mlir-query tool by implementing `getBackwardSlice` and `getForwardSlice` matchers (PR #115670)

Denzel-Brian Budii llvmlistbot at llvm.org
Tue Apr 22 05:47:33 PDT 2025


================
@@ -0,0 +1,69 @@
+//===- MatchFinder.cpp - -----------------------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the method definitions for the `MatchFinder` class
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Query/Matcher/MatchFinder.h"
+namespace mlir::query::matcher {
+
+MatchFinder::MatchResult::MatchResult(Operation *rootOp,
+                                      std::vector<Operation *> matchedOps)
+    : rootOp(rootOp), matchedOps(std::move(matchedOps)) {}
+
+std::vector<MatchFinder::MatchResult>
+MatchFinder::collectMatches(Operation *root, QueryOptions &options,
+                            DynMatcher matcher) const {
+  std::vector<MatchResult> results;
+  llvm::SetVector<Operation *> tempStorage;
+  root->walk([&](Operation *subOp) {
+    if (matcher.match(subOp)) {
+      MatchResult match;
+      match.rootOp = subOp;
+      match.matchedOps.push_back(subOp);
+      results.push_back(std::move(match));
+    } else if (matcher.match(subOp, tempStorage, options)) {
+      results.emplace_back(subOp, std::vector<Operation *>(tempStorage.begin(),
+                                                           tempStorage.end()));
+    }
+    tempStorage.clear();
+  });
+  return results;
+}
+
+void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
+                             Operation *op) const {
+  auto fileLoc = dyn_cast<FileLineColLoc>(op->getLoc());
+  SMLoc smloc = qs.getSourceManager().FindLocForLineAndColumn(
+      qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
----------------
chios202 wrote:

I did cast. The reasoning behind it being that, ideally this the file location should always be available? 

https://github.com/llvm/llvm-project/pull/115670


More information about the Mlir-commits mailing list