[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
Fri Jun 20 08:08:27 PDT 2025


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

If the following query is run: 

`match getUsersByPredicate(hasOpName("memref.alloc"), hasOpName("memref.dealloc"), true)`

on the IR illustrated below, it causes the program to crash.  Upon debugging, I think the underneath cause may be because traverses `linalg.matmul` facilitates `arith.addf`  and `getForwardSlice`  traverses the op's regions and blocks.

``` mlir
func.func @slicing_linalg_op(%arg0 : index, %arg1 : index, %arg2 : index) {
  %a = memref.alloc(%arg0, %arg2) : memref<?x?xf32>
  %b = memref.alloc(%arg2, %arg1) : memref<?x?xf32>
  %c = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
  %d = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
               outs(%c : memref<?x?xf32>)
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
               outs(%d : memref<?x?xf32>)
  memref.dealloc %c : memref<?x?xf32>
  memref.dealloc %b : memref<?x?xf32>
  memref.dealloc %a : memref<?x?xf32>
  memref.dealloc %d : memref<?x?xf32>
  return
}
```

If changed to `dyn_cast` the query succeeds:
```mlir
Match #1:

/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:4:8: note: "root" binds here
  %a = memref.alloc(%arg0, %arg2) : memref<?x?xf32>
       ^
/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:8:3:
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
  ^
/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:10:3:
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
  ^
Match #2:

/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:5:8: note: "root" binds here
  %b = memref.alloc(%arg2, %arg1) : memref<?x?xf32>
       ^
/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:8:3:
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
  ^
/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:10:3:
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
  ^
Match #3:

/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:6:8: note: "root" binds here
  %c = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
       ^
/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:8:3:
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
  ^
Match #4:

/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:7:8: note: "root" binds here
  %d = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
       ^
/home/dbudii/personal/llvm-project/mlir/test/mlir-query/backward-slice-union.mlir:10:3:
  linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
  ^
4 matches.

```


>From a5bee883dbd61d6616e52a6644f5740d21848d3d 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] 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 *>



More information about the Mlir-commits mailing list