[Mlir-commits] [mlir] [mlir][LLVMIR] Fix a crash when `llvm.mlir.constant` fails to create an LLVM constant. (PR #224951)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sun Sep 20 11:42:52 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Hamza Qureshi (hamzaqureshi5)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/224951.diff
2 Files Affected:
- (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td (+10-2)
- (modified) mlir/test/Target/LLVMIR/llvmir-invalid.mlir (+13)
``````````diff
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 5998bd01f9bdf..336ca06850720 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 6e8301bb626e3..030f769b58200 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
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/224951
More information about the Mlir-commits
mailing list