|
18 | 18 | #define GET_OP_CLASSES
|
19 | 19 | #include "polygeist/PolygeistOps.h.inc"
|
20 | 20 |
|
| 21 | +#include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 22 | +#include "mlir/Dialect/SCF/SCF.h" |
| 23 | +#include "mlir/IR/Matchers.h" |
| 24 | +#include "mlir/IR/PatternMatch.h" |
| 25 | +#include "llvm/Support/CommandLine.h" |
| 26 | + |
| 27 | +bool getEffectsBefore( |
| 28 | + mlir::Operation *op, |
| 29 | + llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &effects, |
| 30 | + bool stopAtBarrier); |
| 31 | + |
| 32 | +bool getEffectsAfter( |
| 33 | + mlir::Operation *op, |
| 34 | + llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &effects, |
| 35 | + bool stopAtBarrier); |
| 36 | + |
| 37 | +bool isReadOnly(mlir::Operation *); |
| 38 | +bool isReadNone(mlir::Operation *); |
| 39 | + |
| 40 | +bool mayReadFrom(mlir::Operation *, mlir::Value); |
| 41 | +bool mayWriteTo(mlir::Operation *, mlir::Value, bool ignoreBarrier = false); |
| 42 | + |
| 43 | +bool mayAlias(mlir::MemoryEffects::EffectInstance a, |
| 44 | + mlir::MemoryEffects::EffectInstance b); |
| 45 | + |
| 46 | +bool mayAlias(mlir::MemoryEffects::EffectInstance a, mlir::Value b); |
| 47 | + |
| 48 | +extern llvm::cl::opt<bool> BarrierOpt; |
| 49 | + |
| 50 | +template <bool NotTopLevel = false> |
| 51 | +class BarrierElim final |
| 52 | + : public mlir::OpRewritePattern<mlir::polygeist::BarrierOp> { |
| 53 | +public: |
| 54 | + using mlir::OpRewritePattern<mlir::polygeist::BarrierOp>::OpRewritePattern; |
| 55 | + mlir::LogicalResult |
| 56 | + matchAndRewrite(mlir::polygeist::BarrierOp barrier, |
| 57 | + mlir::PatternRewriter &rewriter) const override { |
| 58 | + using namespace mlir; |
| 59 | + using namespace polygeist; |
| 60 | + if (!BarrierOpt) |
| 61 | + return failure(); |
| 62 | + // Remove if it only sync's constant indices. |
| 63 | + if (llvm::all_of(barrier.getOperands(), [](mlir::Value v) { |
| 64 | + IntegerAttr constValue; |
| 65 | + return matchPattern(v, m_Constant(&constValue)); |
| 66 | + })) { |
| 67 | + rewriter.eraseOp(barrier); |
| 68 | + return success(); |
| 69 | + } |
| 70 | + |
| 71 | + Operation *op = barrier; |
| 72 | + if (NotTopLevel && isa<mlir::scf::ParallelOp, mlir::AffineParallelOp>( |
| 73 | + barrier->getParentOp())) |
| 74 | + return failure(); |
| 75 | + |
| 76 | + { |
| 77 | + SmallVector<MemoryEffects::EffectInstance> beforeEffects; |
| 78 | + getEffectsBefore(op, beforeEffects, /*stopAtBarrier*/ true); |
| 79 | + |
| 80 | + SmallVector<MemoryEffects::EffectInstance> afterEffects; |
| 81 | + getEffectsAfter(op, afterEffects, /*stopAtBarrier*/ false); |
| 82 | + |
| 83 | + bool conflict = false; |
| 84 | + for (auto before : beforeEffects) |
| 85 | + for (auto after : afterEffects) { |
| 86 | + if (mayAlias(before, after)) { |
| 87 | + // Read, read is okay |
| 88 | + if (isa<MemoryEffects::Read>(before.getEffect()) && |
| 89 | + isa<MemoryEffects::Read>(after.getEffect())) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + // Write, write is not okay because may be different offsets and the |
| 94 | + // later must subsume other conflicts are invalid. |
| 95 | + conflict = true; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (!conflict) { |
| 101 | + rewriter.eraseOp(barrier); |
| 102 | + return success(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + { |
| 107 | + SmallVector<MemoryEffects::EffectInstance> beforeEffects; |
| 108 | + getEffectsBefore(op, beforeEffects, /*stopAtBarrier*/ false); |
| 109 | + |
| 110 | + SmallVector<MemoryEffects::EffectInstance> afterEffects; |
| 111 | + getEffectsAfter(op, afterEffects, /*stopAtBarrier*/ true); |
| 112 | + |
| 113 | + bool conflict = false; |
| 114 | + for (auto before : beforeEffects) |
| 115 | + for (auto after : afterEffects) { |
| 116 | + if (mayAlias(before, after)) { |
| 117 | + // Read, read is okay |
| 118 | + if (isa<MemoryEffects::Read>(before.getEffect()) && |
| 119 | + isa<MemoryEffects::Read>(after.getEffect())) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + // Write, write is not okay because may be different offsets and the |
| 123 | + // later must subsume other conflicts are invalid. |
| 124 | + conflict = true; |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (!conflict) { |
| 130 | + rewriter.eraseOp(barrier); |
| 131 | + return success(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return failure(); |
| 136 | + } |
| 137 | +}; |
| 138 | + |
21 | 139 | #endif // BFV_BFVOPS_H
|
0 commit comments