[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:25:42 PDT 2025


================
@@ -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())) {
----------------
chios202 wrote:

Printing logs with;

```cpp
void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
                             Operation *op) const {
  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);
  } else {
    os << "Cannot not retrieve FileLineColLoc.\n";
    op->print(os);
    os << "\n";
  }
}
```

mlir-query> match getUsersByPredicate(hasOpName("memref.alloc"),hasOpName("memref.dealloc"),true)

```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>)
  ^
Cannot not retrieve FileLineColLoc.
%0 = arith.mulf %arg3, %arg4 : f32
Cannot not retrieve FileLineColLoc.
%1 = arith.addf %arg5, %0 : f32
Cannot not retrieve FileLineColLoc.
linalg.yield %1 : f32
/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>)
  ^
Cannot not retrieve FileLineColLoc.
%0 = arith.mulf %arg3, %arg4 : f32
Cannot not retrieve FileLineColLoc.
%1 = arith.addf %arg5, %0 : f32
Cannot not retrieve FileLineColLoc.
linalg.yield %1 : f32
Match #2:
```

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


More information about the Mlir-commits mailing list