[Mlir-commits] [mlir] [mlir] Avoid crash in mlir-query by using dyn_cast instead of cast for FileLineColLoc (PR #145049)

Denzel-Brian Budii llvmlistbot at llvm.org
Tue Oct 7 02:50:19 PDT 2025


https://github.com/chios202 updated https://github.com/llvm/llvm-project/pull/145049

>From 1cbad922ccf7cc59c1ac50812f135b98a6d45f0c Mon Sep 17 00:00:00 2001
From: Denzel-Brian Budii <chio.star at yahoo.com>
Date: Fri, 20 Jun 2025 14:59:41 +0000
Subject: [PATCH 1/2] Avoid crash by using dyn_cast instead of cast for
 FileLineColLoc

---
 mlir/lib/Query/Matcher/MatchFinder.cpp | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/mlir/lib/Query/Matcher/MatchFinder.cpp b/mlir/lib/Query/Matcher/MatchFinder.cpp
index 1d4817e32417d..a4fe1cd549e25 100644
--- a/mlir/lib/Query/Matcher/MatchFinder.cpp
+++ b/mlir/lib/Query/Matcher/MatchFinder.cpp
@@ -38,21 +38,23 @@ MatchFinder::collectMatches(Operation *root, DynMatcher matcher) const {
 
 void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
                              Operation *op) const {
-  auto fileLoc = cast<FileLineColLoc>(op->getLoc());
-  SMLoc smloc = qs.getSourceManager().FindLocForLineAndColumn(
-      qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
-  llvm::SMDiagnostic diag =
-      qs.getSourceManager().GetMessage(smloc, llvm::SourceMgr::DK_Note, "");
-  diag.print("", os, true, false, true);
+  if (auto fileLoc = dyn_cast<FileLineColLoc>(op->getLoc())) {
+    SMLoc smloc = qs.getSourceManager().FindLocForLineAndColumn(
+        qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
+    llvm::SMDiagnostic diag =
+        qs.getSourceManager().GetMessage(smloc, llvm::SourceMgr::DK_Note, "");
+    diag.print("", os, true, false, true);
+  }
 }
 
 void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
                              Operation *op, const std::string &binding) const {
-  auto fileLoc = cast<FileLineColLoc>(op->getLoc());
-  auto smloc = qs.getSourceManager().FindLocForLineAndColumn(
-      qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
-  qs.getSourceManager().PrintMessage(os, smloc, llvm::SourceMgr::DK_Note,
-                                     "\"" + binding + "\" binds here");
+  if (auto fileLoc = dyn_cast<FileLineColLoc>(op->getLoc())) {
+    auto smloc = qs.getSourceManager().FindLocForLineAndColumn(
+        qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
+    qs.getSourceManager().PrintMessage(os, smloc, llvm::SourceMgr::DK_Note,
+                                       "\"" + binding + "\" binds here");
+  }
 }
 
 std::vector<Operation *>

>From 8754de15ef02083495081e071ec3ba177424cb9b Mon Sep 17 00:00:00 2001
From: Denzel-Brian Budii <chio.star at yahoo.com>
Date: Tue, 7 Oct 2025 09:53:04 +0000
Subject: [PATCH 2/2] replace dyn_cast with findInstanceOf

---
 mlir/lib/Query/Matcher/MatchFinder.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Query/Matcher/MatchFinder.cpp b/mlir/lib/Query/Matcher/MatchFinder.cpp
index a4fe1cd549e25..48a24b98f9a4e 100644
--- a/mlir/lib/Query/Matcher/MatchFinder.cpp
+++ b/mlir/lib/Query/Matcher/MatchFinder.cpp
@@ -38,7 +38,7 @@ MatchFinder::collectMatches(Operation *root, DynMatcher matcher) const {
 
 void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
                              Operation *op) const {
-  if (auto fileLoc = dyn_cast<FileLineColLoc>(op->getLoc())) {
+  if (auto fileLoc = op->getLoc()->findInstanceOf<FileLineColLoc>()) {
     SMLoc smloc = qs.getSourceManager().FindLocForLineAndColumn(
         qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
     llvm::SMDiagnostic diag =
@@ -49,7 +49,7 @@ void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
 
 void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
                              Operation *op, const std::string &binding) const {
-  if (auto fileLoc = dyn_cast<FileLineColLoc>(op->getLoc())) {
+  if (auto fileLoc = op->getLoc()->findInstanceOf<FileLineColLoc>()) {
     auto smloc = qs.getSourceManager().FindLocForLineAndColumn(
         qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
     qs.getSourceManager().PrintMessage(os, smloc, llvm::SourceMgr::DK_Note,



More information about the Mlir-commits mailing list