[Mlir-commits] [mlir] [mlir][LLVMIR] Fix a crash when `llvm.mlir.constant` fails to create an LLVM constant. (PR #224951)
Hamza Qureshi
llvmlistbot at llvm.org
Sun Sep 20 11:42:14 PDT 2026
https://github.com/hamzaqureshi5 created https://github.com/llvm/llvm-project/pull/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
>From e1302212ecf35f9d114f4a7507445baa2a1f4919 Mon Sep 17 00:00:00 2001
From: hamzaqureshi5 <hamza7771.861 at gmail.com>
Date: Sun, 20 Sep 2026 17:45:46 +0500
Subject: [PATCH] [mlir][LLVMIR] Fail translation when a constant cannot be
converted
getLLVMConstant() emits a diagnostic and returns null when it cannot convert
an attribute, for example when it refers to a dense_resource that is not
present in the module. The llvmBuilder for llvm.mlir.constant assigned that
result into the value map without checking it, so the null was handed to the
first user of the constant. Translating
%0 = llvm.mlir.constant(dense_resource<__elided__> : vector<128xi32>)
%2 = llvm.icmp "sgt" %1, %0 : vector<128xi32>
therefore passed a null llvm::Value to IRBuilder::CreateICmp and asserted in
dyn_cast rather than reporting the error.
Check the result and return failure, as the global path in
ModuleTranslation::convertGlobals() already does. A constant whose result is
unused was already reported cleanly; only a constant with a user crashed.
Fixes #224815
---
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td | 12 ++++++++++--
mlir/test/Target/LLVMIR/llvmir-invalid.mlir | 13 +++++++++++++
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 5998bd01f9bdf5..336ca06850720d 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 6e8301bb626e36..030f769b58200a 100644
--- a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
@@ -429,3 +429,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