|
| 1 | +#include "mlir/IR/OperationSupport.h" |
| 2 | +#include "triton/Dialect/Gluon/Transforms/Passes.h" |
| 3 | + |
| 4 | +#include "triton/Dialect/TritonGPU/Transforms/Utility.h" |
| 5 | + |
| 6 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 7 | +#include "mlir/Dialect/ControlFlow/IR/ControlFlow.h" |
| 8 | +#include "mlir/Dialect/SCF/IR/SCF.h" |
| 9 | +#include "mlir/Pass/Pass.h" |
| 10 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 11 | + |
| 12 | +using namespace mlir; |
| 13 | +using namespace triton; |
| 14 | + |
| 15 | +namespace mlir::triton::gluon { |
| 16 | +#define GEN_PASS_DEF_GLUONSIMPLIFYCONTROLFLOW |
| 17 | +#include "triton/Dialect/Gluon/Transforms/Passes.h.inc" |
| 18 | +} // namespace mlir::triton::gluon |
| 19 | + |
| 20 | +namespace { |
| 21 | +struct SimplifyControlFlow |
| 22 | + : public gluon::impl::GluonSimplifyControlFlowBase<SimplifyControlFlow> { |
| 23 | + void runOnOperation() override; |
| 24 | +}; |
| 25 | +} // namespace |
| 26 | + |
| 27 | +void SimplifyControlFlow::runOnOperation() { |
| 28 | + MLIRContext *ctx = &getContext(); |
| 29 | + RewritePatternSet patterns(&getContext()); |
| 30 | + |
| 31 | + // Populate `scf` and `cf` canonicalizers. |
| 32 | + ctx->getLoadedDialect<scf::SCFDialect>()->getCanonicalizationPatterns( |
| 33 | + patterns); |
| 34 | + ctx->getLoadedDialect<cf::ControlFlowDialect>()->getCanonicalizationPatterns( |
| 35 | + patterns); |
| 36 | + for (mlir::RegisteredOperationName op : ctx->getRegisteredOperationsByDialect( |
| 37 | + scf::SCFDialect::getDialectNamespace())) |
| 38 | + op.getCanonicalizationPatterns(patterns, ctx); |
| 39 | + for (mlir::RegisteredOperationName op : ctx->getRegisteredOperationsByDialect( |
| 40 | + cf::ControlFlowDialect::getDialectNamespace())) |
| 41 | + op.getCanonicalizationPatterns(patterns, ctx); |
| 42 | + populateForOpDeadArgumentElimination(patterns); |
| 43 | + |
| 44 | + GreedyRewriteConfig config; |
| 45 | + // This is intended to run before AutoLayouts are resolved, in which case |
| 46 | + // CSEing constants can lead to additional layout conflicts. |
| 47 | + config.enableConstantCSE(false); |
| 48 | + (void)applyPatternsGreedily(getOperation(), std::move(patterns), config); |
| 49 | +} |
0 commit comments