20 if (
auto shapedTy = dyn_cast<ShapedType>(type)) {
21 return arith::ConstantOp::create(rewriter, loc,
24 return arith::ConstantOp::create(rewriter, loc, attr);
32 LogicalResult matchAndRewrite(arith::CeilDivUIOp op,
33 PatternRewriter &rewriter)
const final {
34 Location loc = op.getLoc();
35 Value a = op.getLhs();
36 Value
b = op.getRhs();
39 arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::eq, a, zero);
41 Value minusOne = arith::SubIOp::create(rewriter, loc, a, one);
42 Value quotient = arith::DivUIOp::create(rewriter, loc, minusOne,
b);
43 Value plusOne = arith::AddIOp::create(rewriter, loc, quotient, one);
44 rewriter.replaceOpWithNewOp<arith::SelectOp>(op,
compare, zero, plusOne);
58 LogicalResult matchAndRewrite(arith::CeilDivSIOp op,
59 PatternRewriter &rewriter)
const final {
60 Location loc = op.getLoc();
61 Type type = op.getType();
62 Value a = op.getLhs();
63 Value
b = op.getRhs();
68 Value quotient = arith::DivSIOp::create(rewriter, loc, a,
b);
69 Value
product = arith::MulIOp::create(rewriter, loc, quotient,
b);
70 Value notEqualDivisor = arith::CmpIOp::create(
71 rewriter, loc, arith::CmpIPredicate::ne, a,
product);
73 Value aNeg = arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::slt,
75 Value bNeg = arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::slt,
78 Value signEqual = arith::CmpIOp::create(
79 rewriter, loc, arith::CmpIPredicate::eq, aNeg, bNeg);
81 arith::AndIOp::create(rewriter, loc, notEqualDivisor, signEqual);
83 Value quotientPlusOne = arith::AddIOp::create(rewriter, loc, quotient, one);
85 rewriter.replaceOpWithNewOp<arith::SelectOp>(op, cond, quotientPlusOne,
100 LogicalResult matchAndRewrite(arith::FloorDivSIOp op,
101 PatternRewriter &rewriter)
const final {
102 Location loc = op.getLoc();
103 Type type = op.getType();
104 Value a = op.getLhs();
105 Value
b = op.getRhs();
107 Value quotient = arith::DivSIOp::create(rewriter, loc, a,
b);
108 Value
product = arith::MulIOp::create(rewriter, loc, quotient,
b);
109 Value notEqualDivisor = arith::CmpIOp::create(
110 rewriter, loc, arith::CmpIPredicate::ne, a,
product);
113 Value aNeg = arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::slt,
115 Value bNeg = arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::slt,
118 Value signOpposite = arith::CmpIOp::create(
119 rewriter, loc, arith::CmpIPredicate::ne, aNeg, bNeg);
121 arith::AndIOp::create(rewriter, loc, notEqualDivisor, signOpposite);
123 Value minusOne =
createConst(loc, type, -1, rewriter);
124 Value quotientMinusOne =
125 arith::AddIOp::create(rewriter, loc, quotient, minusOne);
127 rewriter.replaceOpWithNewOp<arith::SelectOp>(op, cond, quotientMinusOne,
137 .
add<CeilDivSIOpConverter, CeilDivUIOpConverter, FloorDivSIOpConverter>(
static Value createConst(Location loc, Type type, int value, PatternRewriter &rewriter)
Create an integer or index constant.
static int64_t product(ArrayRef< int64_t > vals)
IntegerAttr getIntegerAttr(Type type, int64_t value)
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
void populateCeilFloorDivExpandOpsPatterns(RewritePatternSet &patterns)
int compare(const Fraction &x, const Fraction &y)
Three-way comparison between two fractions.
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...