Skip to content

[mlir][tosa] Add custom operand getters for select op #145921

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1490,9 +1490,9 @@ def Tosa_SelectOp : Tosa_ElementwiseOp<"select"> {
}];

let arguments = (ins
Tosa_I1Tensor:$input1,
Tosa_Tensor:$input2,
Tosa_Tensor:$input3
Tosa_I1Tensor:$input1, // pred
Tosa_Tensor:$input2, // on true
Tosa_Tensor:$input3 // on false
);

let results = (outs
Expand All @@ -1512,6 +1512,13 @@ def Tosa_SelectOp : Tosa_ElementwiseOp<"select"> {
operands attr-dict `:` `(` type($input1) `,` type($input2) `,` type($input3)
`)` `->` type($output)
}];

let extraClassDeclaration = [{
// Custom getters for readability
::mlir::TypedValue<::mlir::TensorType> getPred() { return getInput1(); }
::mlir::TypedValue<::mlir::TensorType> getOnTrue() { return getInput2(); }
::mlir::TypedValue<::mlir::TensorType> getOnFalse() { return getInput3(); }
}];
}

//===----------------------------------------------------------------------===//
Expand Down
10 changes: 5 additions & 5 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ LogicalResult SelectOp::canonicalize(SelectOp op, PatternRewriter &rewriter) {
return failure();
rewriter.modifyOpInPlace(op, [&]() {
op.getOperation()->setOperands(
{notOp.getInput1(), op.getInput3(), op.getInput2()});
{notOp.getInput1(), op.getOnFalse(), op.getOnTrue()});
});
return success();
}
Expand Down Expand Up @@ -1510,8 +1510,8 @@ OpFoldResult SliceOp::fold(FoldAdaptor adaptor) {
}

OpFoldResult tosa::SelectOp::fold(FoldAdaptor adaptor) {
if (getInput2() == getInput3())
return getInput2();
if (getOnTrue() == getOnFalse())
return getOnTrue();

auto predicate =
llvm::dyn_cast_if_present<DenseIntElementsAttr>(adaptor.getInput1());
Expand All @@ -1520,8 +1520,8 @@ OpFoldResult tosa::SelectOp::fold(FoldAdaptor adaptor) {

if (!predicate.isSplat())
return {};
return predicate.getSplatValue<APInt>().getBoolValue() ? getInput2()
: getInput3();
return predicate.getSplatValue<APInt>().getBoolValue() ? getOnTrue()
: getOnFalse();
}

OpFoldResult TileOp::fold(FoldAdaptor adaptor) {
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3819,16 +3819,16 @@ LogicalResult ReverseOp::verify() {

LogicalResult tosa::SelectOp::verify() {
// verify input2 and input3 have same element type as output
if (verifySameElementTypes(*this, /* inType = */ getInput2().getType(),
if (verifySameElementTypes(*this, /* inType = */ getOnTrue().getType(),
/* outType = */ getOutput().getType())
.failed() ||
verifySameElementTypes(*this, /* inType = */ getInput3().getType(),
verifySameElementTypes(*this, /* inType = */ getOnFalse().getType(),
/* outType = */ getOutput().getType())
.failed()) {
return failure();
}
// verify input1 has element type of bool
auto predicateType = llvm::dyn_cast<ShapedType>(getInput1().getType());
auto predicateType = llvm::dyn_cast<ShapedType>(getPred().getType());
if (!predicateType) {
return emitOpError("expect shaped tensor for input1, got ")
<< getInput1().getType();
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/Tosa/Transforms/TosaMakeBroadcastable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ struct ConvertTosaOp<tosa::SelectOp> : public OpRewritePattern<tosa::SelectOp> {
LogicalResult matchAndRewrite(tosa::SelectOp tosaOp,
PatternRewriter &rewriter) const override {

Value input1 = tosaOp.getInput1();
Value input2 = tosaOp.getInput2();
Value input3 = tosaOp.getInput3();
Value input1 = tosaOp.getPred();
Value input2 = tosaOp.getOnTrue();
Value input3 = tosaOp.getOnFalse();
Value output = tosaOp.getResult();

auto outputType = dyn_cast<RankedTensorType>(output.getType());
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::RFFT2dOp op) {

template <>
LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::SelectOp op) {
addValue(op.getInput2());
addValue(op.getInput3());
addValue(op.getOnTrue());
addValue(op.getOnFalse());
addValue(op.getOutput());
return success();
}
Expand Down