[Mlir-commits] [mlir] [mlir][ArithToLLVM] Fix index lowering for addui_extended (PR #223265)

Bruce Xi llvmlistbot at llvm.org
Sun Sep 13 11:30:33 PDT 2026


https://github.com/brucexi999 created https://github.com/llvm/llvm-project/pull/223265

Lowering `arith.addui_extended` with `index` operands fails because the LLVM
result struct uses the unconverted sum type, even though the operands have
already been converted.

Convert the sum type before constructing the LLVM result struct. Add a
regression test covering both 32-bit and 64-bit index lowering.

This preserves the operation's existing `index` support and uses the converted
index width for the overflow intrinsic. Related discussion of `index` support
in extended arithmetic: https://github.com/llvm/llvm-project/pull/85973.

The regression test fails without the fix and passes with it. All six
ArithToLLVM test files pass with assertions enabled on upstream base
`4098f568c4`.

Addresses the lowering failure reported in
https://github.com/llvm/llvm-project/issues/84986.

Assisted-by: Codex


>From 573cb45df4cadd1cb107dddfac69fc1f23039142 Mon Sep 17 00:00:00 2001
From: Bruce Xi <91307696+brucexi999 at users.noreply.github.com>
Date: Sun, 13 Sep 2026 14:08:21 -0400
Subject: [PATCH] [mlir][ArithToLLVM] Fix index lowering for addui_extended

Lowering `arith.addui_extended` with `index` operands fails because the LLVM
result struct uses the unconverted sum type, even though the operands have
already been converted.

Convert the sum type before constructing the LLVM result struct. Add a
regression test covering both 32-bit and 64-bit index lowering.

This preserves the operation's existing `index` support and uses the converted
index width for the overflow intrinsic. Related discussion of `index` support
in extended arithmetic: https://github.com/llvm/llvm-project/pull/85973.

The regression test fails without the fix and passes with it. All six
ArithToLLVM test files pass with assertions enabled on upstream base
`4098f568c4`.

Addresses the lowering failure reported in
https://github.com/llvm/llvm-project/issues/84986.

Assisted-by: Codex
---
 mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp    |  3 ++-
 .../ArithToLLVM/arith-to-llvm-index.mlir           | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 mlir/test/Conversion/ArithToLLVM/arith-to-llvm-index.mlir

diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index 982b1e5e9a8d1..5cfa0a712ef53 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -559,9 +559,10 @@ LogicalResult AddUIExtendedOpLowering::matchAndRewrite(
 
   // Handle the scalar and 1D vector cases.
   if (!isa<LLVM::LLVMArrayType>(operandType)) {
+    Type newSumType = typeConverter->convertType(sumResultType);
     Type newOverflowType = typeConverter->convertType(overflowResultType);
     Type structType =
-        LLVM::LLVMStructType::getLiteral(ctx, {sumResultType, newOverflowType});
+        LLVM::LLVMStructType::getLiteral(ctx, {newSumType, newOverflowType});
     Value addOverflow = LLVM::UAddWithOverflowOp::create(
         rewriter, loc, structType, adaptor.getLhs(), adaptor.getRhs());
     Value sumExtracted =
diff --git a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm-index.mlir b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm-index.mlir
new file mode 100644
index 0000000000000..a5b24753979b9
--- /dev/null
+++ b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm-index.mlir
@@ -0,0 +1,14 @@
+// RUN: mlir-opt %s -convert-arith-to-llvm="index-bitwidth=32" | FileCheck %s --check-prefix=INDEX32
+// RUN: mlir-opt %s -convert-arith-to-llvm="index-bitwidth=64" | FileCheck %s --check-prefix=INDEX64
+
+// The sum's type must be converted to the configured index width when building
+// the LLVM struct that holds the sum and overflow indicator.
+
+// INDEX32-LABEL: func.func @addui_extended_index(
+// INDEX32: "llvm.intr.uadd.with.overflow"{{.*}} : (i32, i32) -> !llvm.struct<(i32, i1)>
+// INDEX64-LABEL: func.func @addui_extended_index(
+// INDEX64: "llvm.intr.uadd.with.overflow"{{.*}} : (i64, i64) -> !llvm.struct<(i64, i1)>
+func.func @addui_extended_index(%a: index, %b: index) -> (index, i1) {
+  %sum, %overflow = arith.addui_extended %a, %b : index, i1
+  return %sum, %overflow : index, i1
+}



More information about the Mlir-commits mailing list