[Mlir-commits] [mlir] 92df387 - [mlir][LLVM] Fix crash when dereferenceable instruction is folded (#221682)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Sep 16 23:23:24 PDT 2026


Author: Fangrui Song
Date: 2026-09-16T23:23:19-07:00
New Revision: 92df387cd28161612d3953e51aa29230350fb6ce

URL: https://github.com/llvm/llvm-project/commit/92df387cd28161612d3953e51aa29230350fb6ce
DIFF: https://github.com/llvm/llvm-project/commit/92df387cd28161612d3953e51aa29230350fb6ce.diff

LOG: [mlir][LLVM] Fix crash when dereferenceable instruction is folded (#221682)

`inst` is null when the builder folds the cast of a constant. Fix
nullptr dereference by dropping the metadata.

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
    mlir/test/Target/LLVMIR/attribute-dereferenceable.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index b769e3fd0e6f1..edd52db2a4a5b 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -629,8 +629,8 @@ class LLVM_DereferenceableCastOp<string mnemonic, string instName, Type type,
     auto *val = builder.Create}] # instName # [{($arg, $_resultType);
     $res = val;
     if ($dereferenceable) {
-      llvm::Instruction *inst = dyn_cast<llvm::Instruction>(val);
-      moduleTranslation.setDereferenceableMetadata(op, inst);
+      if (auto *inst = dyn_cast<llvm::Instruction>(val))
+        moduleTranslation.setDereferenceableMetadata(op, inst);
     }
   }];
   string mlirBuilder = [{

diff  --git a/mlir/test/Target/LLVMIR/attribute-dereferenceable.mlir b/mlir/test/Target/LLVMIR/attribute-dereferenceable.mlir
index 4beca410dd265..6b36f6f4cfe40 100644
--- a/mlir/test/Target/LLVMIR/attribute-dereferenceable.mlir
+++ b/mlir/test/Target/LLVMIR/attribute-dereferenceable.mlir
@@ -20,5 +20,14 @@ llvm.func @deref_or_null(%arg0: i64, %arg1: !llvm.ptr) {
   llvm.return
 }
 
+// Dereferenceable metadata is dropped if the cast is folded.
+llvm.func @deref_folded_away() -> !llvm.ptr {
+  // CHECK: define ptr @deref_folded_away()
+  // CHECK-NEXT: ret ptr inttoptr (i64 64 to ptr)
+  %0 = llvm.mlir.constant(64 : i64) : i64
+  %1 = llvm.inttoptr %0 dereferenceable<bytes = 4> : i64 to !llvm.ptr
+  llvm.return %1 : !llvm.ptr
+}
+
 // CHECK: [[D0]] = !{i64 4}
 // CHECK: [[D1]] = !{i64 8}


        


More information about the Mlir-commits mailing list