Skip to content

[mlir][transform] Add elementwise criteria to match.structured.body #79626

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 12 commits into from
Jan 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def MatchStructuredBodyOp : Op<Transform_Dialect, "match.structured.body", [
* `passthrough`: the body of the structured payload op only forwards
inputs to the outputs (copy or broadcast).

* `elementwise`: the body of the structured payload op represents an
elementwise operation.

* `contraction`: the body of the structured payload op is a contraction
of the form `<red>(<elem>(bbarg0, bbarg1), bbarg2)` where `<elem>` and
`<red>` are binary operations whose names are specified in the attribute
Expand All @@ -123,6 +126,7 @@ def MatchStructuredBodyOp : Op<Transform_Dialect, "match.structured.body", [
let arguments = (ins TransformHandleTypeInterface:$operand_handle,
OptionalAttr<I64Attr>:$reduction_position,
UnitAttr:$passthrough,
UnitAttr:$elementwise,
OptionalAttr<StrArrayAttr>:$contraction);
let assemblyFormat = "$operand_handle attr-dict `:` type($operand_handle)";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration;
Expand Down
9 changes: 8 additions & 1 deletion mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h"
#include "mlir/Dialect/Linalg/TransformOps/Syntax.h"
#include "mlir/Dialect/Linalg/Utils/Utils.h"
#include "mlir/Dialect/Transform/IR/MatchInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Interfaces/FunctionImplementation.h"
Expand Down Expand Up @@ -187,6 +188,11 @@ DiagnosedSilenceableFailure transform::MatchStructuredBodyOp::matchOperation(
}
return DiagnosedSilenceableFailure::success();
}
if (getElementwise()) {
if (!isElementwise(linalgOp))
return emitSilenceableError() << "not elementwise";
return DiagnosedSilenceableFailure::success();
}
if (std::optional<ArrayAttr> contractionOps = getContraction()) {
Block &body = linalgOp->getRegion(0).front();
std::string message;
Expand All @@ -209,13 +215,14 @@ DiagnosedSilenceableFailure transform::MatchStructuredBodyOp::matchOperation(

LogicalResult transform::MatchStructuredBodyOp::verify() {
int64_t numOptions = getReductionPosition().has_value() + getPassthrough() +
getContraction().has_value();
getElementwise() + getContraction().has_value();

if (numOptions > 1) {
std::string attributeNames;
llvm::raw_string_ostream os(attributeNames);
llvm::interleaveComma(ArrayRef<StringAttr>{getReductionPositionAttrName(),
getPassthroughAttrName(),
getElementwiseAttrName(),
getContractionAttrName()},
os);
return emitOpError() << "only one of {" << os.str() << "} is allowed";
Expand Down
57 changes: 57 additions & 0 deletions mlir/test/Dialect/Linalg/match-ops-interpreter.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,63 @@ module attributes { transform.with_named_sequence } {

// -----

module attributes { transform.with_named_sequence } {
transform.named_sequence @print_elementwise(%arg0: !transform.any_op {transform.readonly}) {
transform.debug.emit_remark_at %arg0, "elementwise" : !transform.any_op
transform.yield
}

transform.named_sequence @match_structured_body_elementwise(%arg0: !transform.any_op {transform.readonly}) -> !transform.any_op {
%0 = transform.match.structured failures(propagate) %arg0 : (!transform.any_op) -> !transform.any_op {
^bb0(%arg1: !transform.any_op):
transform.match.structured.body %arg1 { elementwise } : !transform.any_op
transform.match.structured.yield %arg1 : !transform.any_op
}
transform.yield %0 : !transform.any_op
}

transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.consumed}) {
transform.foreach_match in %arg0
@match_structured_body_elementwise -> @print_elementwise
: (!transform.any_op) -> !transform.any_op
transform.yield
}

func.func @payload(%in1: tensor<2xf32>, %in2: tensor<2xf32>, %in3: tensor<2x3xf32>, %out: tensor<2xf32>, %out2: tensor<2x3xf32>) -> (tensor<2xf32>, tensor<2x3xf32>, tensor<2x3xf32>) attributes { transform.target_tag = "start_here" } {
%cst0 = arith.constant 0.0 : f32
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
// expected-remark @below {{elementwise}}
%fill = linalg.fill ins(%cst0: f32) outs(%out: tensor<2xf32>) -> tensor<2xf32>
// expected-remark @below {{elementwise}}
%add = linalg.map {arith.addf} ins(%in1, %in2: tensor<2xf32>, tensor<2xf32>) outs(%fill: tensor<2xf32>)
%non_elementwise = linalg.generic
{indexing_maps = [affine_map<(d0, d1) -> (d0)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>],
iterator_types = ["parallel", "parallel"]}
ins(%in1, %in3: tensor<2xf32>, tensor<2x3xf32>) outs(%out2: tensor<2x3xf32>) {
^bb0(%arg0: f32, %arg1: f32, %arg3: f32):
%0 = arith.addf %arg0, %arg1 : f32
%1 = tensor.dim %add, %c0 : tensor<2xf32>
%2 = arith.subi %1, %c1 : index
%3 = tensor.extract %add[%2] : tensor<2xf32>
%4 = arith.mulf %0, %3 : f32
linalg.yield %4 : f32
} -> tensor<2x3xf32>
// expected-remark @below {{elementwise}}
%add_bcast = linalg.generic
{indexing_maps = [affine_map<(d0, d1) -> (d0)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>],
iterator_types = ["parallel", "parallel"]}
ins(%in1, %in3: tensor<2xf32>, tensor<2x3xf32>) outs(%out2: tensor<2x3xf32>) {
^bb0(%arg0: f32, %arg1: f32, %arg3: f32):
%0 = arith.addf %arg0, %arg1 : f32
linalg.yield %0 : f32
} -> tensor<2x3xf32>
return %add, %add_bcast, %non_elementwise : tensor<2xf32>, tensor<2x3xf32>, tensor<2x3xf32>
}
}

// -----

module attributes { transform.with_named_sequence } {
transform.named_sequence @print_reduction(%arg0: !transform.any_op {transform.readonly}) {
transform.debug.emit_remark_at %arg0, "reduction" : !transform.any_op
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/Linalg/match-ops-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ transform.sequence failures(suppress) {
^bb0(%arg0: !transform.any_op):
transform.match.structured %arg0 : !transform.any_op {
^bb1(%arg1: !transform.any_op):
// expected-error @below {{only one of {"reduction_position", "passthrough", "contraction"} is allowed}}
// expected-error @below {{only one of {"reduction_position", "passthrough", "elementwise", "contraction"} is allowed}}
transform.match.structured.body %arg1 { passthrough, reduction_position = 0 } : !transform.any_op
transform.match.structured.yield
}
Expand Down