[Mlir-commits] [mlir] [mlir] MLIR-QUERY slice-matchers implementation (PR #115670)

Jacques Pienaar llvmlistbot at llvm.org
Mon Apr 21 08:28:05 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());
----------------
jpienaar wrote:

You dyn\_cast but then query without checking if null. Either cast (so that at least one has an assert in non-opt builds) or check.

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


More information about the Mlir-commits mailing list