[Mlir-commits] [mlir] [mlir][tosa-to-linalg] fix arithmetic_right_shift conversion with round (PR #159930)
Congcong Cai
llvmlistbot at llvm.org
Sat Sep 20 08:51:37 PDT 2025
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/159930
None
>From 6bfa2cb630c6576cceb18401a62ad75bc928cbf9 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 20 Sep 2025 23:45:36 +0800
Subject: [PATCH] [mlir][tosa-to-linalg] fix arithmetic_right_shift conversion
with round
---
mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp | 8 +++++---
mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir | 3 ++-
2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
index 1955eec9964eb..1e0aefded19c1 100644
--- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
+++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
@@ -298,6 +298,8 @@ static Value createLinalgBodyCalculationForElementwiseOp(
IntegerAttr::get(elementTy, 1));
auto zero = arith::ConstantOp::create(rewriter, loc,
IntegerAttr::get(elementTy, 0));
+ auto i1zero =
+ arith::ConstantOp::create(rewriter, loc, IntegerAttr::get(i1Ty, 0));
auto i1one =
arith::ConstantOp::create(rewriter, loc, IntegerAttr::get(i1Ty, 1));
@@ -315,9 +317,9 @@ static Value createLinalgBodyCalculationForElementwiseOp(
ArrayRef<NamedAttribute>());
auto isInputOdd =
arith::AndIOp::create(rewriter, loc, i1Ty, truncated, i1one);
-
- auto shouldRound = arith::AndIOp::create(
- rewriter, loc, i1Ty, shiftValueGreaterThanZero, isInputOdd);
+ // shifted, truncated, isInputOdd can be poison when input2 is 0.
+ auto shouldRound = arith::SelectOp::create(
+ rewriter, loc, i1Ty, shiftValueGreaterThanZero, isInputOdd, i1zero);
auto extended =
arith::ExtUIOp::create(rewriter, loc, resultTypes, shouldRound);
return arith::AddIOp::create(rewriter, loc, resultTypes, result, extended);
diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
index 37af8b8859852..e25b1565f39ee 100644
--- a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
+++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
@@ -698,13 +698,14 @@ func.func @test_simple_i32(%arg0: tensor<1xi32>, %unsigned: tensor<1xui32>, %uns
// CHECK: linalg.generic
// CHECK: arith.constant 1
// CHECK: arith.constant 0
+ // CHECK: arith.constant false
// CHECK: arith.constant true
// CHECK: arith.cmpi
// CHECK: arith.subi
// CHECK: arith.shrsi
// CHECK: arith.trunci
// CHECK: and
- // CHECK: and
+ // CHECK: arith.select
// CHECK: arith.extui
// CHECK: arith.addi
%12 = tosa.arithmetic_right_shift %arg0, %arg0 {round = 1 : i1} : (tensor<1xi32>, tensor<1xi32>) -> tensor<1xi32>
More information about the Mlir-commits
mailing list