//===- ExpandOps.cpp - Pass to legalize Arithmetic ops for LLVM lowering --===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "PassDetail.h" #include "mlir/Dialect/Arithmetic/Transforms/Passes.h" using namespace mlir; namespace { /// Expands CeilDivSIOp (n, m) into /// 1) x = (m > 0) ? -1 : 1 /// 2) (n*m>0) ? ((n+x) / m) + 1 : - (-n / m) struct CeilDivSIOpConverter : public OpRewritePattern { using OpRewritePattern::OpRewritePattern; LogicalResult matchAndRewrite(arith::CeilDivSIOp op, PatternRewriter &rewriter) const final { Location loc = op.getLoc(); auto signedCeilDivIOp = cast(op); Type type = signedCeilDivIOp.getType(); Value a = signedCeilDivIOp.getLhs(); Value b = signedCeilDivIOp.getRhs(); Value plusOne = rewriter.create( loc, rewriter.getIntegerAttr(type, 1)); Value zero = rewriter.create( loc, rewriter.getIntegerAttr(type, 0)); Value minusOne = rewriter.create( loc, rewriter.getIntegerAttr(type, -1)); // Compute x = (b>0) ? -1 : 1. Value compare = rewriter.create(loc, arith::CmpIPredicate::sgt, b, zero); Value x = rewriter.create(loc, compare, minusOne, plusOne); // Compute positive res: 1 + ((x+a)/b). Value xPlusA = rewriter.create(loc, x, a); Value xPlusADivB = rewriter.create(loc, xPlusA, b); Value posRes = rewriter.create(loc, plusOne, xPlusADivB); // Compute negative res: - ((-a)/b). Value minusA = rewriter.create(loc, zero, a); Value minusADivB = rewriter.create(loc, minusA, b); Value negRes = rewriter.create(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 aNeg = rewriter.create(loc, arith::CmpIPredicate::slt, a, zero); Value aPos = rewriter.create(loc, arith::CmpIPredicate::sgt, a, zero); Value bNeg = rewriter.create(loc, arith::CmpIPredicate::slt, b, zero); Value bPos = rewriter.create(loc, arith::CmpIPredicate::sgt, b, zero); Value firstTerm = rewriter.create(loc, aNeg, bNeg); Value secondTerm = rewriter.create(loc, aPos, bPos); Value compareRes = rewriter.create(loc, firstTerm, secondTerm); Value res = rewriter.create(loc, compareRes, posRes, negRes); // Perform substitution and return success. rewriter.replaceOp(op, {res}); return success(); } }; /// Expands FloorDivSIOp (n, m) into /// 1) x = (m<0) ? 1 : -1 /// 2) return (n*m<0) ? - ((-n+x) / m) -1 : n / m struct FloorDivSIOpConverter : public OpRewritePattern { using OpRewritePattern::OpRewritePattern; LogicalResult matchAndRewrite(arith::FloorDivSIOp op, PatternRewriter &rewriter) const final { Location loc = op.getLoc(); arith::FloorDivSIOp signedFloorDivIOp = cast(op); Type type = signedFloorDivIOp.getType(); Value a = signedFloorDivIOp.getLhs(); Value b = signedFloorDivIOp.getRhs(); Value plusOne = rewriter.create( loc, rewriter.getIntegerAttr(type, 1)); Value zero = rewriter.create( loc, rewriter.getIntegerAttr(type, 0)); Value minusOne = rewriter.create( loc, rewriter.getIntegerAttr(type, -1)); // Compute x = (b<0) ? 1 : -1. Value compare = rewriter.create(loc, arith::CmpIPredicate::slt, b, zero); Value x = rewriter.create(loc, compare, plusOne, minusOne); // Compute negative res: -1 - ((x-a)/b). Value xMinusA = rewriter.create(loc, x, a); Value xMinusADivB = rewriter.create(loc, xMinusA, b); Value negRes = rewriter.create(loc, minusOne, xMinusADivB); // Compute positive res: a/b. Value posRes = rewriter.create(loc, a, b); // Result is (a*b<0) ? negative result : positive 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 aNeg = rewriter.create(loc, arith::CmpIPredicate::slt, a, zero); Value aPos = rewriter.create(loc, arith::CmpIPredicate::sgt, a, zero); Value bNeg = rewriter.create(loc, arith::CmpIPredicate::slt, b, zero); Value bPos = rewriter.create(loc, arith::CmpIPredicate::sgt, b, zero); Value firstTerm = rewriter.create(loc, aNeg, bPos); Value secondTerm = rewriter.create(loc, aPos, bNeg); Value compareRes = rewriter.create(loc, firstTerm, secondTerm); Value res = rewriter.create(loc, compareRes, negRes, posRes); // Perform substitution and return success. rewriter.replaceOp(op, {res}); return success(); } }; struct ArithmeticExpandOpsPass : public ArithmeticExpandOpsBase { void runOnFunction() override { RewritePatternSet patterns(&getContext()); ConversionTarget target(getContext()); arith::populateArithmeticExpandOpsPatterns(patterns); target.addLegalDialect(); target.addIllegalOp(); if (failed( applyPartialConversion(getFunction(), target, std::move(patterns)))) signalPassFailure(); } }; } // end anonymous namespace void mlir::arith::populateArithmeticExpandOpsPatterns( RewritePatternSet &patterns) { patterns.add( patterns.getContext()); } std::unique_ptr mlir::arith::createArithmeticExpandOpsPass() { return std::make_unique(); }