|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 4 | +#include "mlir/Dialect/SCF/IR/SCF.h" |
| 5 | +#include "mlir/IR/BlockAndValueMapping.h" |
| 6 | +#include "mlir/IR/IntegerSet.h" |
| 7 | + |
| 8 | +static inline mlir::scf::IfOp |
| 9 | +cloneWithResults(mlir::scf::IfOp op, mlir::OpBuilder &rewriter, |
| 10 | + mlir::BlockAndValueMapping mapping = {}) { |
| 11 | + using namespace mlir; |
| 12 | + return rewriter.create<scf::IfOp>(op.getLoc(), op.getResultTypes(), |
| 13 | + mapping.lookupOrDefault(op.getCondition()), |
| 14 | + true); |
| 15 | +} |
| 16 | +static inline mlir::AffineIfOp |
| 17 | +cloneWithResults(mlir::AffineIfOp op, mlir::OpBuilder &rewriter, |
| 18 | + mlir::BlockAndValueMapping mapping = {}) { |
| 19 | + using namespace mlir; |
| 20 | + SmallVector<mlir::Value> lower; |
| 21 | + for (auto o : op.getOperands()) |
| 22 | + lower.push_back(mapping.lookupOrDefault(o)); |
| 23 | + return rewriter.create<AffineIfOp>(op.getLoc(), op.getResultTypes(), |
| 24 | + op.getIntegerSet(), lower, true); |
| 25 | +} |
| 26 | + |
| 27 | +static inline mlir::scf::IfOp |
| 28 | +cloneWithoutResults(mlir::scf::IfOp op, mlir::OpBuilder &rewriter, |
| 29 | + mlir::BlockAndValueMapping mapping = {}, |
| 30 | + mlir::TypeRange types = {}) { |
| 31 | + using namespace mlir; |
| 32 | + return rewriter.create<scf::IfOp>( |
| 33 | + op.getLoc(), types, mapping.lookupOrDefault(op.getCondition()), true); |
| 34 | +} |
| 35 | +static inline mlir::AffineIfOp |
| 36 | +cloneWithoutResults(mlir::AffineIfOp op, mlir::OpBuilder &rewriter, |
| 37 | + mlir::BlockAndValueMapping mapping = {}, |
| 38 | + mlir::TypeRange types = {}) { |
| 39 | + using namespace mlir; |
| 40 | + SmallVector<mlir::Value> lower; |
| 41 | + for (auto o : op.getOperands()) |
| 42 | + lower.push_back(mapping.lookupOrDefault(o)); |
| 43 | + return rewriter.create<AffineIfOp>(op.getLoc(), types, op.getIntegerSet(), |
| 44 | + lower, true); |
| 45 | +} |
| 46 | + |
| 47 | +static inline mlir::scf::ForOp |
| 48 | +cloneWithoutResults(mlir::scf::ForOp op, mlir::PatternRewriter &rewriter, |
| 49 | + mlir::BlockAndValueMapping mapping = {}) { |
| 50 | + using namespace mlir; |
| 51 | + return rewriter.create<scf::ForOp>( |
| 52 | + op.getLoc(), mapping.lookupOrDefault(op.getLowerBound()), |
| 53 | + mapping.lookupOrDefault(op.getUpperBound()), |
| 54 | + mapping.lookupOrDefault(op.getStep())); |
| 55 | +} |
| 56 | +static inline mlir::AffineForOp |
| 57 | +cloneWithoutResults(mlir::AffineForOp op, mlir::PatternRewriter &rewriter, |
| 58 | + mlir::BlockAndValueMapping mapping = {}) { |
| 59 | + using namespace mlir; |
| 60 | + SmallVector<Value> lower; |
| 61 | + for (auto o : op.getLowerBoundOperands()) |
| 62 | + lower.push_back(mapping.lookupOrDefault(o)); |
| 63 | + SmallVector<Value> upper; |
| 64 | + for (auto o : op.getUpperBoundOperands()) |
| 65 | + upper.push_back(mapping.lookupOrDefault(o)); |
| 66 | + return rewriter.create<AffineForOp>(op.getLoc(), lower, op.getLowerBoundMap(), |
| 67 | + upper, op.getUpperBoundMap(), |
| 68 | + op.getStep()); |
| 69 | +} |
| 70 | + |
| 71 | +static inline void clearBlock(mlir::Block *block, |
| 72 | + mlir::PatternRewriter &rewriter) { |
| 73 | + for (auto &op : llvm::make_early_inc_range(llvm::reverse(*block))) { |
| 74 | + assert(op.use_empty() && "expected 'op' to have no uses"); |
| 75 | + rewriter.eraseOp(&op); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +static inline mlir::Block *getThenBlock(mlir::scf::IfOp op) { |
| 80 | + return op.thenBlock(); |
| 81 | +} |
| 82 | +static inline mlir::Block *getThenBlock(mlir::AffineIfOp op) { |
| 83 | + return op.getThenBlock(); |
| 84 | +} |
| 85 | +static inline mlir::Block *getElseBlock(mlir::scf::IfOp op) { |
| 86 | + return op.elseBlock(); |
| 87 | +} |
| 88 | +static inline mlir::Block *getElseBlock(mlir::AffineIfOp op) { |
| 89 | + if (op.hasElse()) |
| 90 | + return op.getElseBlock(); |
| 91 | + else |
| 92 | + return nullptr; |
| 93 | +} |
| 94 | + |
| 95 | +static inline mlir::Region &getThenRegion(mlir::scf::IfOp op) { |
| 96 | + return op.getThenRegion(); |
| 97 | +} |
| 98 | +static inline mlir::Region &getThenRegion(mlir::AffineIfOp op) { |
| 99 | + return op.getThenRegion(); |
| 100 | +} |
| 101 | +static inline mlir::Region &getElseRegion(mlir::scf::IfOp op) { |
| 102 | + return op.getElseRegion(); |
| 103 | +} |
| 104 | +static inline mlir::Region &getElseRegion(mlir::AffineIfOp op) { |
| 105 | + return op.getElseRegion(); |
| 106 | +} |
| 107 | + |
| 108 | +static inline mlir::scf::YieldOp getThenYield(mlir::scf::IfOp op) { |
| 109 | + return op.thenYield(); |
| 110 | +} |
| 111 | +static inline mlir::AffineYieldOp getThenYield(mlir::AffineIfOp op) { |
| 112 | + return llvm::cast<mlir::AffineYieldOp>(op.getThenBlock()->getTerminator()); |
| 113 | +} |
| 114 | +static inline mlir::scf::YieldOp getElseYield(mlir::scf::IfOp op) { |
| 115 | + return op.elseYield(); |
| 116 | +} |
| 117 | +static inline mlir::AffineYieldOp getElseYield(mlir::AffineIfOp op) { |
| 118 | + return llvm::cast<mlir::AffineYieldOp>(op.getElseBlock()->getTerminator()); |
| 119 | +} |
| 120 | + |
| 121 | +static inline bool inBound(mlir::scf::IfOp op, mlir::Value v) { |
| 122 | + return op.getCondition() == v; |
| 123 | +} |
| 124 | +static inline bool inBound(mlir::AffineIfOp op, mlir::Value v) { |
| 125 | + return llvm::any_of(op.getOperands(), [&](mlir::Value e) { return e == v; }); |
| 126 | +} |
| 127 | +static inline bool inBound(mlir::scf::ForOp op, mlir::Value v) { |
| 128 | + return op.getUpperBound() == v; |
| 129 | +} |
| 130 | +static inline bool inBound(mlir::AffineForOp op, mlir::Value v) { |
| 131 | + return llvm::any_of(op.getUpperBoundOperands(), |
| 132 | + [&](mlir::Value e) { return e == v; }); |
| 133 | +} |
| 134 | +static inline bool hasElse(mlir::scf::IfOp op) { |
| 135 | + return op.getElseRegion().getBlocks().size() > 0; |
| 136 | +} |
| 137 | +static inline bool hasElse(mlir::AffineIfOp op) { |
| 138 | + return op.getElseRegion().getBlocks().size() > 0; |
| 139 | +} |
0 commit comments