|
| 1 | +#include <memory> |
| 2 | + |
| 3 | +#include "mlir/IR/BuiltinAttributes.h" |
| 4 | +#include "mlir/IR/Matchers.h" |
| 5 | +#include "mlir/IR/PatternMatch.h" |
| 6 | +#include "mlir/Pass/Pass.h" |
| 7 | +#include "mlir/Support/LLVM.h" |
| 8 | +#include "mlir/Support/LogicalResult.h" |
| 9 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 10 | +#include "triton/Analysis/Utility.h" |
| 11 | +#include "triton/Dialect/Triton/IR/Dialect.h" |
| 12 | +#include "triton/Dialect/Triton/Transforms/Passes.h" |
| 13 | + |
| 14 | +#define GEN_PASS_CLASSES |
| 15 | +#include "triton/Dialect/Triton/Transforms/Passes.h.inc" |
| 16 | + |
| 17 | +using namespace mlir; |
| 18 | +using llvm::ArrayRef; |
| 19 | +namespace mlir::triton { |
| 20 | + |
| 21 | +struct Div2Mul : public OpRewritePattern<arith::DivFOp> { |
| 22 | + using OpRewritePattern<arith::DivFOp>::OpRewritePattern; |
| 23 | + |
| 24 | + LogicalResult matchAndRewrite(arith::DivFOp op, |
| 25 | + PatternRewriter &rewriter) const override { |
| 26 | + Value result = op.getResult(); |
| 27 | + Value l = op.getLhs(); |
| 28 | + Value r = op.getRhs(); |
| 29 | + auto loc = op.getLoc(); |
| 30 | + |
| 31 | + if (!result.hasOneUse()) |
| 32 | + return failure(); |
| 33 | + for (auto &use : result.getUses()) { |
| 34 | + if (!dyn_cast<arith::DivFOp>(use.getOwner())) |
| 35 | + return failure(); |
| 36 | + auto DivUser = dyn_cast<arith::DivFOp>(use.getOwner()); |
| 37 | + if (DivUser.getLhs() != op.getResult()) |
| 38 | + return failure(); |
| 39 | + auto originalInsertionPoint = rewriter.saveInsertionPoint(); |
| 40 | + rewriter.setInsertionPointAfter(DivUser); |
| 41 | + auto loc_div = DivUser.getLoc(); |
| 42 | + auto product = |
| 43 | + rewriter.create<arith::MulFOp>(loc_div, r, DivUser.getRhs()); |
| 44 | + rewriter.setInsertionPointAfter(product); |
| 45 | + auto ResultEnd = |
| 46 | + rewriter.create<arith::DivFOp>(loc_div, l, product.getResult()); |
| 47 | + rewriter.restoreInsertionPoint(originalInsertionPoint); |
| 48 | + rewriter.replaceOp(op, product.getResult()); |
| 49 | + DivUser.replaceAllUsesWith(ResultEnd.getResult()); |
| 50 | + rewriter.eraseOp(DivUser); |
| 51 | + } |
| 52 | + return success(); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +struct Mul2Mul : public OpRewritePattern<arith::MulFOp> { |
| 57 | + using OpRewritePattern<arith::MulFOp>::OpRewritePattern; |
| 58 | + |
| 59 | + LogicalResult matchAndRewrite(arith::MulFOp op, |
| 60 | + PatternRewriter &rewriter) const override { |
| 61 | + Value result = op.getResult(); |
| 62 | + Value l = op.getLhs(); |
| 63 | + Value r = op.getRhs(); |
| 64 | + auto loc = op.getLoc(); |
| 65 | + |
| 66 | + if (!result.hasOneUse()) |
| 67 | + return failure(); |
| 68 | + for (auto &use : result.getUses()) { |
| 69 | + if (!dyn_cast<arith::MulFOp>(use.getOwner())) |
| 70 | + return failure(); |
| 71 | + auto MulUser = dyn_cast<arith::MulFOp>(use.getOwner()); |
| 72 | + if (!(MulUser.getLhs() == op.getResult() && |
| 73 | + ((MulUser.getRhs().getDefiningOp<arith::ConstantOp>() && |
| 74 | + r.getDefiningOp<arith::ConstantOp>()) || |
| 75 | + (r == MulUser.getRhs())))) |
| 76 | + return failure(); |
| 77 | + auto originalInsertionPoint = rewriter.saveInsertionPoint(); |
| 78 | + rewriter.setInsertionPointAfter(MulUser); |
| 79 | + auto loc_mul = MulUser.getLoc(); |
| 80 | + auto product = |
| 81 | + rewriter.create<arith::MulFOp>(loc_mul, r, MulUser.getRhs()); |
| 82 | + rewriter.setInsertionPointAfter(product); |
| 83 | + auto ResultEnd = |
| 84 | + rewriter.create<arith::MulFOp>(loc_mul, l, product.getResult()); |
| 85 | + rewriter.restoreInsertionPoint(originalInsertionPoint); |
| 86 | + rewriter.replaceOp(op, product.getResult()); |
| 87 | + MulUser.replaceAllUsesWith(ResultEnd.getResult()); |
| 88 | + rewriter.eraseOp(MulUser); |
| 89 | + } |
| 90 | + return success(); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +struct Add2Add : public OpRewritePattern<arith::AddFOp> { |
| 95 | + using OpRewritePattern<arith::AddFOp>::OpRewritePattern; |
| 96 | + |
| 97 | + LogicalResult matchAndRewrite(arith::AddFOp op, |
| 98 | + PatternRewriter &rewriter) const override { |
| 99 | + Value result = op.getResult(); |
| 100 | + Value l = op.getLhs(); |
| 101 | + Value r = op.getRhs(); |
| 102 | + auto loc = op.getLoc(); |
| 103 | + |
| 104 | + if (!result.hasOneUse()) |
| 105 | + return failure(); |
| 106 | + for (auto &use : result.getUses()) { |
| 107 | + if (!dyn_cast<arith::AddFOp>(use.getOwner())) |
| 108 | + return failure(); |
| 109 | + auto AddUser = dyn_cast<arith::AddFOp>(use.getOwner()); |
| 110 | + if (!(AddUser.getLhs() == op.getResult() && |
| 111 | + ((AddUser.getRhs().getDefiningOp<arith::ConstantOp>() && |
| 112 | + r.getDefiningOp<arith::ConstantOp>()) || |
| 113 | + (r == AddUser.getRhs())))) |
| 114 | + return failure(); |
| 115 | + auto originalInsertionPoint = rewriter.saveInsertionPoint(); |
| 116 | + rewriter.setInsertionPointAfter(AddUser); |
| 117 | + auto loc_add = AddUser.getLoc(); |
| 118 | + auto sum = rewriter.create<arith::AddFOp>(loc_add, r, AddUser.getRhs()); |
| 119 | + rewriter.setInsertionPointAfter(sum); |
| 120 | + auto ResultEnd = |
| 121 | + rewriter.create<arith::AddFOp>(loc_add, l, sum.getResult()); |
| 122 | + rewriter.restoreInsertionPoint(originalInsertionPoint); |
| 123 | + rewriter.replaceOp(op, sum.getResult()); |
| 124 | + AddUser.replaceAllUsesWith(ResultEnd.getResult()); |
| 125 | + rewriter.eraseOp(AddUser); |
| 126 | + } |
| 127 | + return success(); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +struct Sub2Add : public OpRewritePattern<arith::SubFOp> { |
| 132 | + using OpRewritePattern<arith::SubFOp>::OpRewritePattern; |
| 133 | + |
| 134 | + LogicalResult matchAndRewrite(arith::SubFOp op, |
| 135 | + PatternRewriter &rewriter) const override { |
| 136 | + Value result = op.getResult(); |
| 137 | + Value l = op.getLhs(); |
| 138 | + Value r = op.getRhs(); |
| 139 | + auto loc = op.getLoc(); |
| 140 | + |
| 141 | + if (!result.hasOneUse()) |
| 142 | + return failure(); |
| 143 | + for (auto &use : result.getUses()) { |
| 144 | + if (!dyn_cast<arith::SubFOp>(use.getOwner())) |
| 145 | + return failure(); |
| 146 | + auto SubUser = dyn_cast<arith::SubFOp>(use.getOwner()); |
| 147 | + if (!(SubUser.getLhs() == op.getResult() && |
| 148 | + ((SubUser.getRhs().getDefiningOp<arith::ConstantOp>() && |
| 149 | + r.getDefiningOp<arith::ConstantOp>()) || |
| 150 | + (r == SubUser.getRhs())))) |
| 151 | + return failure(); |
| 152 | + auto originalInsertionPoint = rewriter.saveInsertionPoint(); |
| 153 | + rewriter.setInsertionPointAfter(SubUser); |
| 154 | + auto loc_sub = SubUser.getLoc(); |
| 155 | + auto sum = rewriter.create<arith::AddFOp>(loc_sub, r, SubUser.getRhs()); |
| 156 | + rewriter.setInsertionPointAfter(sum); |
| 157 | + auto ResultEnd = |
| 158 | + rewriter.create<arith::SubFOp>(loc_sub, l, sum.getResult()); |
| 159 | + rewriter.restoreInsertionPoint(originalInsertionPoint); |
| 160 | + rewriter.replaceOp(op, sum.getResult()); |
| 161 | + SubUser.replaceAllUsesWith(ResultEnd.getResult()); |
| 162 | + rewriter.eraseOp(SubUser); |
| 163 | + } |
| 164 | + return success(); |
| 165 | + } |
| 166 | +}; |
| 167 | + |
| 168 | +class ExpressionRestructingPass |
| 169 | + : public TritonExpressionRestructingBase<ExpressionRestructingPass> { |
| 170 | +public: |
| 171 | + void runOnOperation() override { |
| 172 | + MLIRContext *context = &getContext(); |
| 173 | + RewritePatternSet patterns(context); |
| 174 | + ModuleOp m = getOperation(); |
| 175 | + patterns.add<Div2Mul>(context); |
| 176 | + patterns.add<Mul2Mul>(context); |
| 177 | + patterns.add<Add2Add>(context); |
| 178 | + patterns.add<Sub2Add>(context); |
| 179 | + |
| 180 | + if (applyPatternsAndFoldGreedily(m, std::move(patterns)).failed()) |
| 181 | + signalPassFailure(); |
| 182 | + } |
| 183 | +}; |
| 184 | + |
| 185 | +std::unique_ptr<mlir::Pass> createExpressionRestructingPass() { |
| 186 | + return std::make_unique<ExpressionRestructingPass>(); |
| 187 | +} |
| 188 | + |
| 189 | +} // namespace mlir::triton |
0 commit comments