Skip to content

[mlir][vector] Adds pattern rewrite for maskable Ops #83827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions mlir/include/mlir/Dialect/Vector/Utils/VectorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,64 @@ SmallVector<OpFoldResult> getMixedSizesXfer(bool hasTensorSemantics,
Operation *xfer,
RewriterBase &rewriter);

/// A pattern for ops that implement `MaskableOpInterface` and that _might_ be
/// masked (i.e. inside `vector.mask` Op region). In particular:
/// 1. Matches `SourceOp` operation, Op.
/// 2.1. If Op is masked, retrieves the masking Op, maskOp, and updates the
/// insertion point to avoid inserting new ops into the `vector.mask` Op
/// region (which only allows one Op).
/// 2.2 If Op is not masked, this step is skipped.
/// 3. Invokes `matchAndRewriteMaskableOp` on Op and optionally maskOp if
/// found in step 2.1.
///
/// This wrapper frees patterns from re-implementing the logic to update the
/// insertion point when a maskable Op is masked. Such patterns are still
/// responsible for providing an updated ("rewritten") version of:
/// a. the source Op when mask _is not_ present,
/// b. the source Op and the masking Op when mask _is_ present.
/// Note that the return value from `matchAndRewriteMaskableOp` depends on the
/// case above.
template <class SourceOp>
struct MaskableOpRewritePattern : OpRewritePattern<SourceOp> {
using OpRewritePattern<SourceOp>::OpRewritePattern;

private:
LogicalResult matchAndRewrite(SourceOp sourceOp,
PatternRewriter &rewriter) const final {
auto maskableOp = dyn_cast<MaskableOpInterface>(sourceOp.getOperation());
if (!maskableOp)
return failure();

Operation *rootOp = sourceOp;

// If this Op is masked, update the insertion point to avoid inserting into
// the vector.mask Op region.
OpBuilder::InsertionGuard guard(rewriter);
MaskingOpInterface maskOp;
if (maskableOp.isMasked()) {
maskOp = maskableOp.getMaskingOp();
rewriter.setInsertionPoint(maskOp);
rootOp = maskOp;
}

FailureOr<Value> newOp =
matchAndRewriteMaskableOp(sourceOp, maskOp, rewriter);
if (failed(newOp))
return failure();

rewriter.replaceOp(rootOp, *newOp);
return success();
}

public:
// Matches SourceOp that can potentially be masked with `maskingOp`. If the
// latter is present, returns an updated masking op (with a replacement for
// `sourceOp` nested inside). Otherwise, returns an updated `sourceOp`.
virtual FailureOr<Value>
matchAndRewriteMaskableOp(SourceOp sourceOp, MaskingOpInterface maskingOp,
PatternRewriter &rewriter) const = 0;
};

} // namespace vector

/// Constructs a permutation map of invariant memref indices to vector
Expand Down
Loading