|
| 1 | +//===- SliceMatchers.h - Matchers for slicing analysis ----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file provides matchers for MLIRQuery that peform slicing analysis |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef MLIR_TOOLS_MLIRQUERY_MATCHERS_SLICEMATCHERS_H |
| 14 | +#define MLIR_TOOLS_MLIRQUERY_MATCHERS_SLICEMATCHERS_H |
| 15 | + |
| 16 | +#include "mlir/Analysis/SliceAnalysis.h" |
| 17 | + |
| 18 | +/// A matcher encapsulating `getBackwardSlice` method from SliceAnalysis.h. |
| 19 | +/// Additionally, it limits the slice computation to a certain depth level using |
| 20 | +/// a custom filter. |
| 21 | +/// |
| 22 | +/// Example: starting from node 9, assuming the matcher |
| 23 | +/// computes the slice for the first two depth levels: |
| 24 | +/// ============================ |
| 25 | +/// 1 2 3 4 |
| 26 | +/// |_______| |______| |
| 27 | +/// | | | |
| 28 | +/// | 5 6 |
| 29 | +/// |___|_____________| |
| 30 | +/// | | |
| 31 | +/// 7 8 |
| 32 | +/// |_______________| |
| 33 | +/// | |
| 34 | +/// 9 |
| 35 | +/// |
| 36 | +/// Assuming all local orders match the numbering order: |
| 37 | +/// {5, 7, 6, 8, 9} |
| 38 | +namespace mlir::query::matcher { |
| 39 | + |
| 40 | +template <typename Matcher> |
| 41 | +class BackwardSliceMatcher { |
| 42 | +public: |
| 43 | + BackwardSliceMatcher(Matcher innerMatcher, int64_t maxDepth, bool inclusive, |
| 44 | + bool omitBlockArguments, bool omitUsesFromAbove) |
| 45 | + : innerMatcher(std::move(innerMatcher)), maxDepth(maxDepth), |
| 46 | + inclusive(inclusive), omitBlockArguments(omitBlockArguments), |
| 47 | + omitUsesFromAbove(omitUsesFromAbove) {} |
| 48 | + |
| 49 | + bool match(Operation *rootOp, SetVector<Operation *> &backwardSlice) { |
| 50 | + BackwardSliceOptions options; |
| 51 | + options.inclusive = inclusive; |
| 52 | + options.omitUsesFromAbove = omitUsesFromAbove; |
| 53 | + options.omitBlockArguments = omitBlockArguments; |
| 54 | + return (innerMatcher.match(rootOp) && |
| 55 | + matches(rootOp, backwardSlice, options, maxDepth)); |
| 56 | + } |
| 57 | + |
| 58 | +private: |
| 59 | + bool matches(Operation *rootOp, llvm::SetVector<Operation *> &backwardSlice, |
| 60 | + BackwardSliceOptions &options, int64_t maxDepth); |
| 61 | + |
| 62 | +private: |
| 63 | + // The outer matcher (e.g., BackwardSliceMatcher) relies on the innerMatcher |
| 64 | + // to determine whether we want to traverse the IR or not. For example, we |
| 65 | + // want to explore the IR only if the top-level operation name is |
| 66 | + // `"arith.addf"`. |
| 67 | + Matcher innerMatcher; |
| 68 | + // `maxDepth` specifies the maximum depth that the matcher can traverse the |
| 69 | + // IR. For example, if `maxDepth` is 2, the matcher will explore the defining |
| 70 | + // operations of the top-level op up to 2 levels. |
| 71 | + int64_t maxDepth; |
| 72 | + bool inclusive; |
| 73 | + bool omitBlockArguments; |
| 74 | + bool omitUsesFromAbove; |
| 75 | +}; |
| 76 | + |
| 77 | +template <typename Matcher> |
| 78 | +bool BackwardSliceMatcher<Matcher>::matches( |
| 79 | + Operation *rootOp, llvm::SetVector<Operation *> &backwardSlice, |
| 80 | + BackwardSliceOptions &options, int64_t maxDepth) { |
| 81 | + backwardSlice.clear(); |
| 82 | + llvm::DenseMap<Operation *, int64_t> opDepths; |
| 83 | + // Initializing the root op with a depth of 0 |
| 84 | + opDepths[rootOp] = 0; |
| 85 | + options.filter = [&](Operation *subOp) { |
| 86 | + // If the subOp hasn't been recorded in opDepths, it is deeper than |
| 87 | + // maxDepth. |
| 88 | + if (!opDepths.contains(subOp)) |
| 89 | + return false; |
| 90 | + // Examine subOp's operands to compute depths of their defining operations. |
| 91 | + for (auto operand : subOp->getOperands()) { |
| 92 | + int64_t newDepth = opDepths[subOp] + 1; |
| 93 | + // If the newDepth is greater than maxDepth, further computation can be |
| 94 | + // skipped. |
| 95 | + if (newDepth > maxDepth) |
| 96 | + continue; |
| 97 | + |
| 98 | + if (auto definingOp = operand.getDefiningOp()) { |
| 99 | + // Registers the minimum depth |
| 100 | + if (!opDepths.contains(definingOp) || newDepth < opDepths[definingOp]) |
| 101 | + opDepths[definingOp] = newDepth; |
| 102 | + } else { |
| 103 | + auto blockArgument = cast<BlockArgument>(operand); |
| 104 | + Operation *parentOp = blockArgument.getOwner()->getParentOp(); |
| 105 | + if (!parentOp) |
| 106 | + continue; |
| 107 | + |
| 108 | + if (!opDepths.contains(parentOp) || newDepth < opDepths[parentOp]) |
| 109 | + opDepths[parentOp] = newDepth; |
| 110 | + } |
| 111 | + } |
| 112 | + return true; |
| 113 | + }; |
| 114 | + getBackwardSlice(rootOp, &backwardSlice, options); |
| 115 | + return options.inclusive ? backwardSlice.size() > 1 |
| 116 | + : backwardSlice.size() >= 1; |
| 117 | +} |
| 118 | + |
| 119 | +/// Matches transitive defs of a top-level operation up to N levels. |
| 120 | +template <typename Matcher> |
| 121 | +inline BackwardSliceMatcher<Matcher> |
| 122 | +m_GetDefinitions(Matcher innerMatcher, int64_t maxDepth, bool inclusive, |
| 123 | + bool omitBlockArguments, bool omitUsesFromAbove) { |
| 124 | + assert(maxDepth >= 0 && "maxDepth must be non-negative"); |
| 125 | + return BackwardSliceMatcher<Matcher>(std::move(innerMatcher), maxDepth, |
| 126 | + inclusive, omitBlockArguments, |
| 127 | + omitUsesFromAbove); |
| 128 | +} |
| 129 | + |
| 130 | +/// Matches all transitive defs of a top-level operation up to N levels |
| 131 | +template <typename Matcher> |
| 132 | +inline BackwardSliceMatcher<Matcher> m_GetAllDefinitions(Matcher innerMatcher, |
| 133 | + int64_t maxDepth) { |
| 134 | + assert(maxDepth >= 0 && "maxDepth must be non-negative"); |
| 135 | + return BackwardSliceMatcher<Matcher>(std::move(innerMatcher), maxDepth, true, |
| 136 | + false, false); |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace mlir::query::matcher |
| 140 | + |
| 141 | +#endif // MLIR_TOOLS_MLIRQUERY_MATCHERS_SLICEMATCHERS_H |
0 commit comments