Skip to content

[mlir] [arith] Fix ceildivsi lowering in arith-expand #133774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 25 additions & 35 deletions mlir/lib/Dialect/Arith/Transforms/ExpandOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ struct CeilDivUIOpConverter : public OpRewritePattern<arith::CeilDivUIOp> {
}
};

/// Expands CeilDivSIOp (n, m) into
/// 1) x = (m > 0) ? -1 : 1
/// 2) (n*m>0) ? ((n+x) / m) + 1 : - (-n / m)
/// Expands CeilDivSIOp (a, b) into
/// z = a / b
/// if (z * b != a && (a < 0) == (b < 0)) {
/// return z + 1;
/// } else {
/// return z;
/// }
struct CeilDivSIOpConverter : public OpRewritePattern<arith::CeilDivSIOp> {
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(arith::CeilDivSIOp op,
Expand All @@ -69,43 +73,29 @@ struct CeilDivSIOpConverter : public OpRewritePattern<arith::CeilDivSIOp> {
Type type = op.getType();
Value a = op.getLhs();
Value b = op.getRhs();
Value plusOne = createConst(loc, type, 1, rewriter);

Value zero = createConst(loc, type, 0, rewriter);
Value minusOne = createConst(loc, type, -1, rewriter);
// Compute x = (b>0) ? -1 : 1.
Value compare =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::sgt, b, zero);
Value x = rewriter.create<arith::SelectOp>(loc, compare, minusOne, plusOne);
// Compute positive res: 1 + ((x+a)/b).
Value xPlusA = rewriter.create<arith::AddIOp>(loc, x, a);
Value xPlusADivB = rewriter.create<arith::DivSIOp>(loc, xPlusA, b);
Value posRes = rewriter.create<arith::AddIOp>(loc, plusOne, xPlusADivB);
// Compute negative res: - ((-a)/b).
Value minusA = rewriter.create<arith::SubIOp>(loc, zero, a);
Value minusADivB = rewriter.create<arith::DivSIOp>(loc, minusA, b);
Value negRes = rewriter.create<arith::SubIOp>(loc, zero, minusADivB);
// Result is (a*b>0) ? pos result : neg result.
// Note, we want to avoid using a*b because of possible overflow.
// The case that matters are a>0, a==0, a<0, b>0 and b<0. We do
// not particuliarly care if a*b<0 is true or false when b is zero
// as this will result in an illegal divide. So `a*b<0` can be reformulated
// as `(a<0 && b<0) || (a>0 && b>0)' or `(a<0 && b<0) || (a>0 && b>=0)'.
// We pick the first expression here.
Value one = createConst(loc, type, 1, rewriter);

Value quotient = rewriter.create<arith::DivSIOp>(loc, a, b);
Value product = rewriter.create<arith::MulIOp>(loc, quotient, b);
Value notEqualDivisor = rewriter.create<arith::CmpIOp>(
loc, arith::CmpIPredicate::ne, a, product);

Value aNeg =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt, a, zero);
Value aPos =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::sgt, a, zero);
Value bNeg =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt, b, zero);
Value bPos =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::sgt, b, zero);
Value firstTerm = rewriter.create<arith::AndIOp>(loc, aNeg, bNeg);
Value secondTerm = rewriter.create<arith::AndIOp>(loc, aPos, bPos);
Value compareRes =
rewriter.create<arith::OrIOp>(loc, firstTerm, secondTerm);
// Perform substitution and return success.
rewriter.replaceOpWithNewOp<arith::SelectOp>(op, compareRes, posRes,
negRes);

Value signEqual = rewriter.create<arith::CmpIOp>(
loc, arith::CmpIPredicate::eq, aNeg, bNeg);
Value cond =
rewriter.create<arith::AndIOp>(loc, notEqualDivisor, signEqual);

Value quotientPlusOne = rewriter.create<arith::AddIOp>(loc, quotient, one);

rewriter.replaceOpWithNewOp<arith::SelectOp>(op, cond, quotientPlusOne,
quotient);
return success();
}
};
Expand Down
36 changes: 14 additions & 22 deletions mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -600,28 +600,20 @@ func.func @select_complex(%arg0 : i1, %arg1 : complex<f32>, %arg2 : complex<f32>
// -----

// CHECK-LABEL: @ceildivsi
// CHECK-SAME: %[[ARG0:.*]]: i64) -> i64
func.func @ceildivsi(%arg0 : i64) -> i64 {
// CHECK: %[[CST0:.*]] = llvm.mlir.constant(1 : i64) : i64
// CHECK: %[[CST1:.*]] = llvm.mlir.constant(0 : i64) : i64
// CHECK: %[[CST2:.*]] = llvm.mlir.constant(-1 : i64) : i64
// CHECK: %[[CMP0:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[SEL0:.*]] = llvm.select %[[CMP0]], %[[CST2]], %[[CST0]] : i1, i64
// CHECK: %[[ADD0:.*]] = llvm.add %[[SEL0]], %[[ARG0]] : i64
// CHECK: %[[DIV0:.*]] = llvm.sdiv %[[ADD0]], %[[ARG0]] : i64
// CHECK: %[[ADD1:.*]] = llvm.add %[[DIV0]], %[[CST0]] : i64
// CHECK: %[[SUB0:.*]] = llvm.sub %[[CST1]], %[[ARG0]] : i64
// CHECK: %[[DIV1:.*]] = llvm.sdiv %[[SUB0]], %[[ARG0]] : i64
// CHECK: %[[SUB1:.*]] = llvm.sub %[[CST1]], %[[DIV1]] : i64
// CHECK: %[[CMP1:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[CMP2:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[CMP3:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[CMP4:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
// CHECK: %[[AND0:.*]] = llvm.and %[[CMP1]], %[[CMP3]] : i1
// CHECK: %[[AND1:.*]] = llvm.and %[[CMP2]], %[[CMP4]] : i1
// CHECK: %[[OR:.*]] = llvm.or %[[AND0]], %[[AND1]] : i1
// CHECK: %[[SEL1:.*]] = llvm.select %[[OR]], %[[ADD1]], %[[SUB1]] : i1, i64
%0 = arith.ceildivsi %arg0, %arg0 : i64
// CHECK-SAME: %[[ARG0:.*]]: i64, %[[ARG1:.*]]: i64) -> i64
func.func @ceildivsi(%arg0 : i64, %arg1 : i64) -> i64 {
// CHECK: %[[ZERO:.+]] = llvm.mlir.constant(0 : i64) : i64
// CHECK: %[[ONE:.+]] = llvm.mlir.constant(1 : i64) : i64
// CHECK: %[[DIV:.+]] = llvm.sdiv %[[ARG0]], %[[ARG1]] : i64
// CHECK: %[[MUL:.+]] = llvm.mul %[[DIV]], %[[ARG1]] : i64
// CHECK: %[[NEXACT:.+]] = llvm.icmp "ne" %[[ARG0]], %[[MUL]] : i64
// CHECK: %[[NNEG:.+]] = llvm.icmp "slt" %[[ARG0]], %[[ZERO]] : i64
// CHECK: %[[MNEG:.+]] = llvm.icmp "slt" %[[ARG1]], %[[ZERO]] : i64
// CHECK: %[[SAMESIGN:.+]] = llvm.icmp "eq" %[[NNEG]], %[[MNEG]] : i1
// CHECK: %[[SHOULDROUND:.+]] = llvm.and %[[NEXACT]], %[[SAMESIGN]] : i1
// CHECK: %[[CEIL:.+]] = llvm.add %[[DIV]], %[[ONE]] : i64
// CHECK: %[[RES:.+]] = llvm.select %[[SHOULDROUND]], %[[CEIL]], %[[DIV]] : i1, i64
%0 = arith.ceildivsi %arg0, %arg1 : i64
return %0: i64
}

Expand Down
57 changes: 21 additions & 36 deletions mlir/test/Dialect/Arith/expand-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@ func.func @ceildivi(%arg0: i32, %arg1: i32) -> (i32) {
%res = arith.ceildivsi %arg0, %arg1 : i32
return %res : i32

// CHECK: [[ONE:%.+]] = arith.constant 1 : i32
// CHECK: [[ZERO:%.+]] = arith.constant 0 : i32
// CHECK: [[MINONE:%.+]] = arith.constant -1 : i32
// CHECK: [[CMP1:%.+]] = arith.cmpi sgt, [[ARG1]], [[ZERO]] : i32
// CHECK: [[X:%.+]] = arith.select [[CMP1]], [[MINONE]], [[ONE]] : i32
// CHECK: [[TRUE1:%.+]] = arith.addi [[X]], [[ARG0]] : i32
// CHECK: [[TRUE2:%.+]] = arith.divsi [[TRUE1]], [[ARG1]] : i32
// CHECK: [[TRUE3:%.+]] = arith.addi [[ONE]], [[TRUE2]] : i32
// CHECK: [[FALSE1:%.+]] = arith.subi [[ZERO]], [[ARG0]] : i32
// CHECK: [[FALSE2:%.+]] = arith.divsi [[FALSE1]], [[ARG1]] : i32
// CHECK: [[FALSE3:%.+]] = arith.subi [[ZERO]], [[FALSE2]] : i32
// CHECK: [[NNEG:%.+]] = arith.cmpi slt, [[ARG0]], [[ZERO]] : i32
// CHECK: [[NPOS:%.+]] = arith.cmpi sgt, [[ARG0]], [[ZERO]] : i32
// CHECK: [[MNEG:%.+]] = arith.cmpi slt, [[ARG1]], [[ZERO]] : i32
// CHECK: [[MPOS:%.+]] = arith.cmpi sgt, [[ARG1]], [[ZERO]] : i32
// CHECK: [[TERM1:%.+]] = arith.andi [[NNEG]], [[MNEG]] : i1
// CHECK: [[TERM2:%.+]] = arith.andi [[NPOS]], [[MPOS]] : i1
// CHECK: [[CMP2:%.+]] = arith.ori [[TERM1]], [[TERM2]] : i1
// CHECK: [[RES:%.+]] = arith.select [[CMP2]], [[TRUE3]], [[FALSE3]] : i32
// CHECK: [[ONE:%.+]] = arith.constant 1 : i32
// CHECK: [[DIV:%.+]] = arith.divsi %arg0, %arg1 : i32
// CHECK: [[MUL:%.+]] = arith.muli [[DIV]], %arg1 : i32
// CHECK: [[NEXACT:%.+]] = arith.cmpi ne, %arg0, [[MUL]] : i32
// CHECK: [[NNEG:%.+]] = arith.cmpi slt, %arg0, [[ZERO]] : i32
// CHECK: [[MNEG:%.+]] = arith.cmpi slt, %arg1, [[ZERO]] : i32
// CHECK: [[SAMESIGN:%.+]] = arith.cmpi eq, [[NNEG]], [[MNEG]] : i1
// CHECK: [[SHOULDROUND:%.+]] = arith.andi [[NEXACT]], [[SAMESIGN]] : i1
// CHECK: [[CEIL:%.+]] = arith.addi [[DIV]], [[ONE]] : i32
// CHECK: [[RES:%.+]] = arith.select [[SHOULDROUND]], [[CEIL]], [[DIV]] : i32
}

// -----
Expand All @@ -37,25 +29,18 @@ func.func @ceildivi_index(%arg0: index, %arg1: index) -> (index) {
%res = arith.ceildivsi %arg0, %arg1 : index
return %res : index

// CHECK: [[ONE:%.+]] = arith.constant 1 : index
// CHECK: [[ZERO:%.+]] = arith.constant 0 : index
// CHECK: [[MINONE:%.+]] = arith.constant -1 : index
// CHECK: [[CMP1:%.+]] = arith.cmpi sgt, [[ARG1]], [[ZERO]] : index
// CHECK: [[X:%.+]] = arith.select [[CMP1]], [[MINONE]], [[ONE]] : index
// CHECK: [[TRUE1:%.+]] = arith.addi [[X]], [[ARG0]] : index
// CHECK: [[TRUE2:%.+]] = arith.divsi [[TRUE1]], [[ARG1]] : index
// CHECK: [[TRUE3:%.+]] = arith.addi [[ONE]], [[TRUE2]] : index
// CHECK: [[FALSE1:%.+]] = arith.subi [[ZERO]], [[ARG0]] : index
// CHECK: [[FALSE2:%.+]] = arith.divsi [[FALSE1]], [[ARG1]] : index
// CHECK: [[FALSE3:%.+]] = arith.subi [[ZERO]], [[FALSE2]] : index
// CHECK: [[NNEG:%.+]] = arith.cmpi slt, [[ARG0]], [[ZERO]] : index
// CHECK: [[NPOS:%.+]] = arith.cmpi sgt, [[ARG0]], [[ZERO]] : index
// CHECK: [[MNEG:%.+]] = arith.cmpi slt, [[ARG1]], [[ZERO]] : index
// CHECK: [[MPOS:%.+]] = arith.cmpi sgt, [[ARG1]], [[ZERO]] : index
// CHECK: [[TERM1:%.+]] = arith.andi [[NNEG]], [[MNEG]] : i1
// CHECK: [[TERM2:%.+]] = arith.andi [[NPOS]], [[MPOS]] : i1
// CHECK: [[CMP2:%.+]] = arith.ori [[TERM1]], [[TERM2]] : i1
// CHECK: [[RES:%.+]] = arith.select [[CMP2]], [[TRUE3]], [[FALSE3]] : index
// CHECK: [[ONE:%.+]] = arith.constant 1 : index
// CHECK: [[DIV:%.+]] = arith.divsi %arg0, %arg1 : index
// CHECK: [[MUL:%.+]] = arith.muli [[DIV]], %arg1 : index
// CHECK: [[NEXACT:%.+]] = arith.cmpi ne, %arg0, [[MUL]] : index
// CHECK: [[NNEG:%.+]] = arith.cmpi slt, %arg0, [[ZERO]] : index
// CHECK: [[MNEG:%.+]] = arith.cmpi slt, %arg1, [[ZERO]] : index
// CHECK: [[SAMESIGN:%.+]] = arith.cmpi eq, [[NNEG]], [[MNEG]] : i1
// CHECK: [[SHOULDROUND:%.+]] = arith.andi [[NEXACT]], [[SAMESIGN]] : i1
// CHECK: [[CEIL:%.+]] = arith.addi [[DIV]], [[ONE]] : index
// CHECK: [[RES:%.+]] = arith.select [[SHOULDROUND]], [[CEIL]], [[DIV]] : index

}

// -----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Check that the ceildivsi lowering is correct.
// We do not check any poison or UB values, as it is not possible to catch them.

// RUN: mlir-opt %s --convert-vector-to-llvm \
// RUN: --convert-func-to-llvm --convert-arith-to-llvm | \
// RUN: mlir-runner -e entry --entry-point-result=void \
// RUN: --shared-libs=%mlir_c_runner_utils | \
// RUN: FileCheck %s --match-full-lines

func.func @check_ceildivsi(%lhs : i32, %rhs : i32) -> () {
%res = arith.ceildivsi %lhs, %rhs : i32
vector.print %res : i32
return
}

func.func @entry() {
%int_min = arith.constant -2147483648 : i32
%int_max = arith.constant 2147483647 : i32
%minus_three = arith.constant -3 : i32
%minus_two = arith.constant -2 : i32
%minus_one = arith.constant -1 : i32
%zero = arith.constant 0 : i32
%one = arith.constant 1 : i32
%two = arith.constant 2 : i32
%three = arith.constant 3 : i32

// INT_MAX divided by values.
// CHECK: 1
func.call @check_ceildivsi(%int_max, %int_max) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%int_max, %int_min) : (i32, i32) -> ()
// CHECK-NEXT: -2147483647
func.call @check_ceildivsi(%int_max, %minus_one) : (i32, i32) -> ()
// CHECK-NEXT: -1073741823
func.call @check_ceildivsi(%int_max, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: 2147483647
func.call @check_ceildivsi(%int_max, %one) : (i32, i32) -> ()
// CHECK-NEXT: 1073741824
func.call @check_ceildivsi(%int_max, %two) : (i32, i32) -> ()

// INT_MIN divided by values.
// We do not check the result of INT_MIN divided by -1, as it is UB.
// CHECK-NEXT: 1
func.call @check_ceildivsi(%int_min, %int_min) : (i32, i32) -> ()
// CHECK-NEXT: -1
func.call @check_ceildivsi(%int_min, %int_max) : (i32, i32) -> ()
// CHECK-NEXT: 1073741824
func.call @check_ceildivsi(%int_min, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: -2147483648
func.call @check_ceildivsi(%int_min, %one) : (i32, i32) -> ()
// CHECK-NEXT: -1073741824
func.call @check_ceildivsi(%int_min, %two) : (i32, i32) -> ()

// Divide values by INT_MIN.
// CHECK-NEXT: 0
func.call @check_ceildivsi(%one, %int_min) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%two, %int_min) : (i32, i32) -> ()
// CHECK-NEXT: 1
func.call @check_ceildivsi(%minus_one, %int_min) : (i32, i32) -> ()
// CHECK-NEXT: 1
func.call @check_ceildivsi(%minus_two, %int_min) : (i32, i32) -> ()

// Divide values by INT_MAX.
// CHECK-NEXT: 1
func.call @check_ceildivsi(%one, %int_max) : (i32, i32) -> ()
// CHECK-NEXT: 1
func.call @check_ceildivsi(%two, %int_max) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%minus_one, %int_max) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%minus_two, %int_max) : (i32, i32) -> ()

// Check divisions by 2.
// CHECK-NEXT: -1
func.call @check_ceildivsi(%minus_three, %two) : (i32, i32) -> ()
// CHECK-NEXT: -1
func.call @check_ceildivsi(%minus_two, %two) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%minus_one, %two) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%zero, %two) : (i32, i32) -> ()
// CHECK-NEXT: 1
func.call @check_ceildivsi(%one, %two) : (i32, i32) -> ()
// CHECK-NEXT: 1
func.call @check_ceildivsi(%two, %two) : (i32, i32) -> ()
// CHECK-NEXT: 2
func.call @check_ceildivsi(%three, %two) : (i32, i32) -> ()

// Check divisions by -2.
// CHECK-NEXT: 2
func.call @check_ceildivsi(%minus_three, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: 1
func.call @check_ceildivsi(%minus_two, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: 1
func.call @check_ceildivsi(%minus_one, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%zero, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: 0
func.call @check_ceildivsi(%one, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: -1
func.call @check_ceildivsi(%two, %minus_two) : (i32, i32) -> ()
// CHECK-NEXT: -1
func.call @check_ceildivsi(%three, %minus_two) : (i32, i32) -> ()
return
}