|
| 1 | +//===- ExtraMatchers.h - Various common matchers ---------------------*- C++ |
| 2 | +//-*-===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file provides extra matchers that are very useful for mlir-query |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef MLIR_IR_EXTRAMATCHERS_H |
| 15 | +#define MLIR_IR_EXTRAMATCHERS_H |
| 16 | + |
| 17 | +#include "MatchFinder.h" |
| 18 | +#include "MatchersInternal.h" |
| 19 | +#include "mlir/IR/Region.h" |
| 20 | +#include "mlir/Query/Query.h" |
| 21 | +#include "llvm/Support/raw_ostream.h" |
| 22 | + |
| 23 | +namespace mlir { |
| 24 | + |
| 25 | +namespace query { |
| 26 | + |
| 27 | +namespace extramatcher { |
| 28 | + |
| 29 | +namespace detail { |
| 30 | + |
| 31 | +class BackwardSliceMatcher { |
| 32 | +public: |
| 33 | + BackwardSliceMatcher(matcher::DynMatcher &&innerMatcher, unsigned hops) |
| 34 | + : innerMatcher(std::move(innerMatcher)), hops(hops) {} |
| 35 | + |
| 36 | +private: |
| 37 | + bool matches(Operation *op, SetVector<Operation *> &backwardSlice, |
| 38 | + QueryOptions &options, unsigned tempHops) { |
| 39 | + |
| 40 | + if (op->hasTrait<OpTrait::IsIsolatedFromAbove>()) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + auto processValue = [&](Value value) { |
| 45 | + if (tempHops == 0) { |
| 46 | + return; |
| 47 | + } |
| 48 | + if (auto *definingOp = value.getDefiningOp()) { |
| 49 | + if (backwardSlice.count(definingOp) == 0) |
| 50 | + matches(definingOp, backwardSlice, options, tempHops - 1); |
| 51 | + } else if (auto blockArg = dyn_cast<BlockArgument>(value)) { |
| 52 | + if (options.omitBlockArguments) |
| 53 | + return; |
| 54 | + Block *block = blockArg.getOwner(); |
| 55 | + |
| 56 | + Operation *parentOp = block->getParentOp(); |
| 57 | + |
| 58 | + if (parentOp && backwardSlice.count(parentOp) == 0) { |
| 59 | + if (parentOp->getNumRegions() != 1 && |
| 60 | + parentOp->getRegion(0).getBlocks().size() != 1) { |
| 61 | + llvm::errs() |
| 62 | + << "Error: Expected parentOp to have exactly one region and " |
| 63 | + << "exactly one block, but found " << parentOp->getNumRegions() |
| 64 | + << " regions and " |
| 65 | + << (parentOp->getRegion(0).getBlocks().size()) << " blocks.\n"; |
| 66 | + }; |
| 67 | + matches(parentOp, backwardSlice, options, tempHops - 1); |
| 68 | + } |
| 69 | + } else { |
| 70 | + llvm::errs() << "No definingOp and not a block argument\n"; |
| 71 | + return; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + if (!options.omitUsesFromAbove) { |
| 76 | + llvm::for_each(op->getRegions(), [&](Region ®ion) { |
| 77 | + SmallPtrSet<Region *, 4> descendents; |
| 78 | + region.walk( |
| 79 | + [&](Region *childRegion) { descendents.insert(childRegion); }); |
| 80 | + region.walk([&](Operation *op) { |
| 81 | + for (OpOperand &operand : op->getOpOperands()) { |
| 82 | + if (!descendents.contains(operand.get().getParentRegion())) |
| 83 | + processValue(operand.get()); |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + llvm::for_each(op->getOperands(), processValue); |
| 90 | + backwardSlice.insert(op); |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | +public: |
| 95 | + bool match(Operation *op, SetVector<Operation *> &backwardSlice, |
| 96 | + QueryOptions &options) { |
| 97 | + |
| 98 | + if (innerMatcher.match(op) && matches(op, backwardSlice, options, hops)) { |
| 99 | + if (!options.inclusive) { |
| 100 | + backwardSlice.remove(op); |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | +private: |
| 108 | + matcher::DynMatcher innerMatcher; |
| 109 | + unsigned hops; |
| 110 | +}; |
| 111 | + |
| 112 | +class ForwardSliceMatcher { |
| 113 | +public: |
| 114 | + ForwardSliceMatcher(matcher::DynMatcher &&innerMatcher, unsigned hops) |
| 115 | + : innerMatcher(std::move(innerMatcher)), hops(hops) {} |
| 116 | + |
| 117 | +private: |
| 118 | + bool matches(Operation *op, SetVector<Operation *> &forwardSlice, |
| 119 | + QueryOptions &options, unsigned tempHops) { |
| 120 | + |
| 121 | + if (tempHops == 0) { |
| 122 | + forwardSlice.insert(op); |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + for (Region ®ion : op->getRegions()) |
| 127 | + for (Block &block : region) |
| 128 | + for (Operation &blockOp : block) |
| 129 | + if (forwardSlice.count(&blockOp) == 0) |
| 130 | + matches(&blockOp, forwardSlice, options, tempHops - 1); |
| 131 | + for (Value result : op->getResults()) { |
| 132 | + for (Operation *userOp : result.getUsers()) |
| 133 | + if (forwardSlice.count(userOp) == 0) |
| 134 | + matches(userOp, forwardSlice, options, tempHops - 1); |
| 135 | + } |
| 136 | + |
| 137 | + forwardSlice.insert(op); |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | +public: |
| 142 | + bool match(Operation *op, SetVector<Operation *> &forwardSlice, |
| 143 | + QueryOptions &options) { |
| 144 | + if (innerMatcher.match(op) && matches(op, forwardSlice, options, hops)) { |
| 145 | + if (!options.inclusive) { |
| 146 | + forwardSlice.remove(op); |
| 147 | + } |
| 148 | + SmallVector<Operation *, 0> v(forwardSlice.takeVector()); |
| 149 | + forwardSlice.insert(v.rbegin(), v.rend()); |
| 150 | + return true; |
| 151 | + } |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | +private: |
| 156 | + matcher::DynMatcher innerMatcher; |
| 157 | + unsigned hops; |
| 158 | +}; |
| 159 | + |
| 160 | +} // namespace detail |
| 161 | + |
| 162 | +inline detail::BackwardSliceMatcher |
| 163 | +definedBy(mlir::query::matcher::DynMatcher innerMatcher) { |
| 164 | + return detail::BackwardSliceMatcher(std::move(innerMatcher), 1); |
| 165 | +} |
| 166 | + |
| 167 | +inline detail::BackwardSliceMatcher |
| 168 | +getDefinitions(mlir::query::matcher::DynMatcher innerMatcher, unsigned hops) { |
| 169 | + return detail::BackwardSliceMatcher(std::move(innerMatcher), hops); |
| 170 | +} |
| 171 | + |
| 172 | +inline detail::ForwardSliceMatcher |
| 173 | +usedBy(mlir::query::matcher::DynMatcher innerMatcher) { |
| 174 | + return detail::ForwardSliceMatcher(std::move(innerMatcher), 1); |
| 175 | +} |
| 176 | + |
| 177 | +inline detail::ForwardSliceMatcher |
| 178 | +getUses(mlir::query::matcher::DynMatcher innerMatcher, unsigned hops) { |
| 179 | + return detail::ForwardSliceMatcher(std::move(innerMatcher), hops); |
| 180 | +} |
| 181 | + |
| 182 | +} // namespace extramatcher |
| 183 | + |
| 184 | +} // namespace query |
| 185 | + |
| 186 | +} // namespace mlir |
| 187 | + |
| 188 | +#endif // MLIR_IR_EXTRAMATCHERS_H |
0 commit comments