Skip to content

[mlir][linalg] Exclude non-convolutional ops from isaConvolutionOpInterface #102087

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 1 commit into from
Aug 26, 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
11 changes: 9 additions & 2 deletions mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ struct ConvolutionDimensions {
FailureOr<ConvolutionDimensions> inferConvolutionDims(LinalgOp linalgOp);

/// Checks whether `linalgOp` conforms to ConvolutionOpInterface.
/// By default, we require the `linalgOp` to have non-empty convolved dims
/// (implicitly non-empty `output_image` and `filter_loop`).
/// Users can loosen the constraint by setting `allowEmptyConvolvedDims` to true
// TODO: embed within `isa<ConvolutionOpInterface>` if possible / natural.
bool isaConvolutionOpInterface(LinalgOp linalgOp);
bool isaConvolutionOpInterface(LinalgOp linalgOp,
bool allowEmptyConvolvedDims = false);

/// Checks whether `linalgOp` is semantically equivalent to a `linalg.copyOp`.
bool isaCopyOpInterface(LinalgOp linalgOp);
Expand Down Expand Up @@ -175,9 +179,12 @@ enum class MatchConvolutionResult;
/// Checks whether `op` conforms to ConvolutionOpInterface and populates
/// `dimensions` with indexes of the different kinds of dimensions when
/// present.
/// If `allowEmptyConvolvedDims` is not set, we further checks whether the `op`
/// contains convolved dims.
MatchConvolutionResult
isConvolutionInterfaceImpl(Operation *op,
ConvolutionDimensions *dimensions = nullptr);
ConvolutionDimensions *dimensions = nullptr,
bool allowEmptyConvolvedDims = false);

/// Returns the error message corresponding to the convolution checking return
/// code.
Expand Down
20 changes: 13 additions & 7 deletions mlir/lib/Dialect/Linalg/IR/LinalgInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,15 @@ enum class MatchConvolutionResult {
NotProjectedPermutations,
NonConvolutionLoop,
OutputDimsNotParallel,
NonOutputDimNotReduction
NonOutputDimNotReduction,
EmptyConvolvedDims
};
} // namespace mlir::linalg::detail

mlir::linalg::detail::MatchConvolutionResult
mlir::linalg::detail::isConvolutionInterfaceImpl(
Operation *op, ConvolutionDimensions *dimensions) {
Operation *op, ConvolutionDimensions *dimensions,
bool allowEmptyConvolvedDims) {
auto linalgOp = dyn_cast<linalg::LinalgOp>(op);
if (!linalgOp)
return MatchConvolutionResult::NotLinalgOp;
Expand Down Expand Up @@ -886,10 +888,12 @@ mlir::linalg::detail::isConvolutionInterfaceImpl(
if (allLoopDims.size() != linalgOp.getNumLoops())
return MatchConvolutionResult::NonConvolutionLoop;

if (!allowEmptyConvolvedDims && inputExprWalker.convolvedDims.empty())
return MatchConvolutionResult::EmptyConvolvedDims;

if (dimensions) {
FailureOr<ConvolutionDimensions> res =
inferConvolutionDimsImpl(linalgOp, inputExprWalker,
/*allowEmptyConvolvedDims=*/true);
FailureOr<ConvolutionDimensions> res = inferConvolutionDimsImpl(
linalgOp, inputExprWalker, allowEmptyConvolvedDims);
assert(succeeded(res) && "unexpected failure to infer convolution dims");
*dimensions = *res;
}
Expand Down Expand Up @@ -920,8 +924,10 @@ mlir::linalg::detail::getMatchConvolutionMessage(MatchConvolutionResult res) {
llvm_unreachable("unhandled MatchConvolutionResult case");
}

bool mlir::linalg::isaConvolutionOpInterface(LinalgOp linalgOp) {
return linalg::detail::isConvolutionInterfaceImpl(linalgOp.getOperation()) ==
bool mlir::linalg::isaConvolutionOpInterface(LinalgOp linalgOp,
bool allowEmptyConvolvedDims) {
return linalg::detail::isConvolutionInterfaceImpl(
linalgOp.getOperation(), nullptr, allowEmptyConvolvedDims) ==
linalg::detail::MatchConvolutionResult::Success;
}

Expand Down
Loading