[Mlir-commits] [mlir] 48a0b89 - [mlir][LLVMIR] Fix a crash when `llvm.mlir.constant` fails to create an LLVM constant. (#224951)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Sep 24 00:11:53 PDT 2026


Author: Hamza Qureshi
Date: 2026-09-24T09:11:46+02:00
New Revision: 48a0b894efefb4f2f5dad224860c473e4d26e962

URL: https://github.com/llvm/llvm-project/commit/48a0b894efefb4f2f5dad224860c473e4d26e962
DIFF: https://github.com/llvm/llvm-project/commit/48a0b894efefb4f2f5dad224860c473e4d26e962.diff

LOG: [mlir][LLVMIR] Fix a crash when `llvm.mlir.constant` fails to create an LLVM constant. (#224951)

Fix the `llvm.mlir.constant` translation to handle a failed
`getLLVMConstant()` conversion. The current code can pass a null
`llvm::Value` to users of the constant, causing an assertion. Check the
result of `getLLVMConstant()` and return failure when the conversion
fails.

Fixes #224815

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
    mlir/test/Target/LLVMIR/llvmir-invalid.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index c1222224209fe..2df02fae26d20 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -2319,8 +2319,16 @@ def LLVM_ZeroOp
 
 def LLVM_ConstantOp
     : LLVM_Op<"mlir.constant", [Pure, ConstantLike]>,
-      LLVM_Builder<[{$res = getLLVMConstant($_resultType, $value, $_location,
-                                            moduleTranslation);}]>
+      LLVM_Builder<[{
+        llvm::Constant *cst = getLLVMConstant($_resultType, $value,
+                                              $_location, moduleTranslation);
+        // `getLLVMConstant` emits a diagnostic and returns null if the
+        // attribute cannot be converted, e.g. when it refers to a resource
+        // that is not present in the module.
+        if (!cst)
+          return failure();
+        $res = cst;
+      }]>
 {
   let summary = "Defines a constant of LLVM type.";
   let description = [{

diff  --git a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
index 47678734e81ac..c6bd08fad0585 100644
--- a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
@@ -450,3 +450,16 @@ llvm.func @invoke_branch_weights() -> i32 attributes {personality = @__gxx_perso
 ^bb2:  // 2 preds: ^bb0, ^bb1
   llvm.return %0 : i32
 }
+
+// -----
+
+// An in-function constant referring to a missing resource must fail the
+// translation rather than leaving a null value behind for its users.
+
+llvm.func @constant_resource_does_not_exist(%arg0: vector<4xi32>) {
+  // expected-error @below{{resource does not exist}}
+  // expected-error @below{{LLVM Translation failed for operation: llvm.mlir.constant}}
+  %0 = llvm.mlir.constant(dense_resource<missing> : vector<4xi32>) : vector<4xi32>
+  %1 = llvm.icmp "sgt" %arg0, %0 : vector<4xi32>
+  llvm.return
+}


        


More information about the Mlir-commits mailing list