37 LogicalResult matchAndRewrite(math::PowFOp op,
38 PatternRewriter &rewriter)
const final;
43PowFStrengthReduction::matchAndRewrite(math::PowFOp op,
45 Location loc = op.getLoc();
46 Value x = op.getLhs();
47 arith::FastMathFlags fmf = op.getFastmathAttr().getValue();
49 FloatAttr scalarExponent;
50 DenseFPElementsAttr vectorExponent;
56 auto isExponentValue = [&](
double value) ->
bool {
58 return scalarExponent.getValue().isExactlyValue(value);
60 if (isVector && vectorExponent.isSplat())
61 return vectorExponent.getSplatValue<FloatAttr>()
63 .isExactlyValue(value);
69 auto bcast = [&](Value value) -> Value {
70 if (
auto vec = dyn_cast<VectorType>(op.getType()))
71 return vector::BroadcastOp::create(rewriter, loc, vec, value);
76 if (isExponentValue(1.0)) {
82 if (isExponentValue(2.0)) {
88 if (isExponentValue(3.0)) {
89 Value square = arith::MulFOp::create(rewriter, loc, x, x, fmf);
95 if (isExponentValue(-1.0)) {
96 Value one = arith::ConstantOp::create(
104 if (isExponentValue(0.5)) {
110 if (isExponentValue(-0.5)) {
116 if (isExponentValue(0.75)) {
117 Value powHalf = math::SqrtOp::create(rewriter, loc, x, fmf);
118 Value powQuarter = math::SqrtOp::create(rewriter, loc, powHalf, fmf);
131template <
typename PowIOpTy,
typename DivOpTy,
typename MulOpTy>
132struct PowIStrengthReduction :
public OpRewritePattern<PowIOpTy> {
134 unsigned exponentThreshold;
137 PowIStrengthReduction(MLIRContext *context,
unsigned exponentThreshold = 3,
138 PatternBenefit benefit = 1,
139 ArrayRef<StringRef> generatedNames = {})
140 : OpRewritePattern<PowIOpTy>(context, benefit, generatedNames),
141 exponentThreshold(exponentThreshold) {}
143 LogicalResult matchAndRewrite(PowIOpTy op,
144 PatternRewriter &rewriter)
const final;
148template <
typename PowIOpTy,
typename DivOpTy,
typename MulOpTy>
150PowIStrengthReduction<PowIOpTy, DivOpTy, MulOpTy>::matchAndRewrite(
151 PowIOpTy op, PatternRewriter &rewriter)
const {
152 Location loc = op.getLoc();
153 Value base = op.getLhs();
161 if (!exponent.isSignedIntN(64))
163 int64_t exponentValue = exponent.getSExtValue();
167 bool exponentIsNegative =
false;
168 if (exponentValue < 0) {
169 exponentIsNegative =
true;
176 if constexpr (std::is_same_v<PowIOpTy, math::IPowIOp>)
182 if (exponentValue == std::numeric_limits<int64_t>::min())
188 if (exponentValue != 0 && exponentValue > exponentThreshold)
192 auto bcast = [&loc, &op, &rewriter](Value value) -> Value {
193 if (
auto vec = dyn_cast<VectorType>(op.getType()))
194 return vector::BroadcastOp::create(rewriter, loc, vec, value);
200 if constexpr (std::is_same_v<PowIOpTy, math::FPowIOp>) {
201 one = arith::ConstantOp::create(rewriter, loc,
203 }
else if constexpr (std::is_same_v<PowIOpTy, complex::PowiOp>) {
204 auto complexTy = cast<ComplexType>(opType);
205 Type elementType = complexTy.getElementType();
206 auto realPart = rewriter.
getFloatAttr(elementType, 1.0);
207 auto imagPart = rewriter.
getFloatAttr(elementType, 0.0);
208 one = complex::ConstantOp::create(
209 rewriter, loc, complexTy, rewriter.
getArrayAttr({realPart, imagPart}));
211 one = arith::ConstantOp::create(rewriter, loc,
216 if (exponentValue == 0) {
231 auto buildMul = [&](Value
lhs, Value
rhs) {
232 if constexpr (std::is_same_v<PowIOpTy, complex::PowiOp>)
233 return MulOpTy::create(rewriter, loc, op.getType(),
lhs,
rhs,
234 op.getFastmathAttr());
236 return MulOpTy::create(rewriter, loc,
lhs,
rhs);
238 for (
unsigned i = 1; i < exponentValue; ++i)
243 if (exponentIsNegative) {
244 if constexpr (std::is_same_v<PowIOpTy, complex::PowiOp>)
245 result = DivOpTy::create(rewriter, loc, op.getType(), bcast(one),
result,
246 op.getFastmathAttr());
248 result = DivOpTy::create(rewriter, loc, bcast(one),
result);
262template <
typename ExpOpTy>
263struct ExpQuotientStrengthReduction :
public OpRewritePattern<arith::DivFOp> {
267 LogicalResult matchAndRewrite(arith::DivFOp op,
268 PatternRewriter &rewriter)
const final {
269 auto numerator = op.getLhs().getDefiningOp<ExpOpTy>();
270 auto denominator = op.getRhs().getDefiningOp<ExpOpTy>();
271 if (!numerator || !denominator)
282 arith::FastMathFlags fmf = op.getFastmath();
283 if (!bitEnumContainsAll(fmf, arith::FastMathFlags::arcp |
284 arith::FastMathFlags::reassoc))
292 Operation *divOp = op;
293 auto diesWithDivision = [divOp](Operation *exp) {
294 return llvm::all_of(exp->getUsers(),
295 [divOp](Operation *user) { return user == divOp; });
297 if (!diesWithDivision(numerator) && !diesWithDivision(denominator))
311 constexpr arith::FastMathFlags valueAssumptions =
312 arith::FastMathFlags::nnan | arith::FastMathFlags::ninf;
315 arith::FastMathFlags subFmf = bitEnumClear(fmf, valueAssumptions);
319 arith::FastMathFlags expFmf = bitEnumClear(
320 numerator.getFastmath() & denominator.getFastmath(), valueAssumptions);
323 arith::SubFOp::create(rewriter, op.getLoc(), numerator.getOperand(),
324 denominator.getOperand(), subFmf);
336 PowFStrengthReduction,
337 PowIStrengthReduction<math::IPowIOp, arith::DivSIOp, arith::MulIOp>,
338 PowIStrengthReduction<math::FPowIOp, arith::DivFOp, arith::MulFOp>,
339 PowIStrengthReduction<complex::PowiOp, complex::DivOp, complex::MulOp>>(
341 patterns.
add<ExpQuotientStrengthReduction<math::ExpOp>,
342 ExpQuotientStrengthReduction<math::Exp2Op>>(
IntegerAttr getIntegerAttr(Type type, int64_t value)
FloatAttr getFloatAttr(Type type, double value)
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
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.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
void populateMathAlgebraicSimplificationPatterns(RewritePatternSet &patterns)
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...