Skip to content
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

[MLIR] [XLA] lower xla_lhlo::slice to linalg dialect #36546

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions tensorflow/compiler/mlir/xla/tests/lhlo-legalize-to-linalg.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,21 @@ func @tanh(%input: memref<2x2xf32>,
// CHECK-NEXT: ^bb0(%[[OPERAND_IN:.*]]: f32, %[[RESULT_OUT:.*]]):
// CHECK-NEXT: %[[RESULT:.*]] = tanh %[[OPERAND_IN]] : f32
// CHECK-NEXT: linalg.yield %[[RESULT]] : f32


// -----

// CHECK: func @slice(%[[arg0:.*]]: memref<?x?xf32>, %[[arg1:.*]]: memref<?x?xf32>)
func @slice(%opearnd: memref<?x?xf32>, %result: memref<?x?xf32>) {
wyzero marked this conversation as resolved.
Show resolved Hide resolved
"xla_lhlo.slice"(%opearnd, %result)
{start_indices = dense<[0,0]> : tensor<2xi64>,
limit_indices = dense<[2,2]> : tensor<2xi64>,
strides = dense<[1,1]> : tensor<2xi64>}
: (memref<?x?xf32>, memref<?x?xf32>) -> ()
return
}

// CHECK: %[[LHS:.*]] = linalg.range
// CHECK: %[[RHS:.*]] = linalg.range
wyzero marked this conversation as resolved.
Show resolved Hide resolved
// CHECK: %[[RST:.*]] = linalg.slice %[[arg0]][%[[LHS]], %[[RHS]]]
// CHECK: linalg.copy(%[[RST]], %[[arg1]])
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,53 @@ class ConstConverter : public OpConversionPattern<xla_lhlo::ConstOp> {
}
};

class SliceConverter : public OpConversionPattern<xla_lhlo::SliceOp> {
public:
using OpConversionPattern<xla_lhlo::SliceOp>::OpConversionPattern;

PatternMatchResult matchAndRewrite(
xla_lhlo::SliceOp sliceOp, ArrayRef<Value> args,
ConversionPatternRewriter& rewriter) const final {
auto loc = sliceOp.getLoc();
auto argType =
sliceOp.getOperand(0).getType().template dyn_cast<ShapedType>();
if (!argType || !argType.hasRank()) {
emitError(loc,
"lhlo to linalg conversion expects statically shaped args");
wyzero marked this conversation as resolved.
Show resolved Hide resolved
return ConversionPattern::matchFailure();
}

int rank = argType.getRank();
wyzero marked this conversation as resolved.
Show resolved Hide resolved
SmallVector<mlir::ConstantOp, 3> start_indices;
SmallVector<mlir::ConstantOp, 3> limit_indices;
SmallVector<mlir::ConstantOp, 3> strides;

auto to_const_ops = [&] (const Attribute& attr,
SmallVector<mlir::ConstantOp, 3>& ops) {
for (auto value : attr.cast<DenseIntElementsAttr>().getIntValues()) {
ops.push_back(rewriter.create<ConstantIndexOp>(loc, value.getSExtValue()));
}
};

to_const_ops(sliceOp.start_indices(), start_indices);
to_const_ops(sliceOp.limit_indices(), limit_indices);
to_const_ops(sliceOp.strides(), strides);

SmallVector<Value, 3> ranges;
for (int i = 0; i < rank; ++i) {
ranges.push_back(rewriter.create<linalg::RangeOp>(
loc, start_indices[i], limit_indices[i], strides[i]).getResult());
}

auto linalg_slice = rewriter.create<linalg::SliceOp>(
loc, sliceOp.getOperand(0), ranges);
rewriter.create<linalg::CopyOp>(
loc, linalg_slice, sliceOp.getOperand(1));
rewriter.eraseOp(sliceOp);
return matchSuccess();
}
wyzero marked this conversation as resolved.
Show resolved Hide resolved
};

void populateLHLOToLinalgConversionPattern(MLIRContext* context,
OwningRewritePatternList* patterns) {
// clang-format off
Expand All @@ -364,7 +411,8 @@ void populateLHLOToLinalgConversionPattern(MLIRContext* context,
PointwiseToLinalgConverter<xla_lhlo::SignOp>,
PointwiseToLinalgConverter<xla_lhlo::SubOp>,
PointwiseToLinalgConverter<xla_lhlo::TanhOp>,
ScalarPointwiseToStandardConverter<xla_lhlo::AddOp>
ScalarPointwiseToStandardConverter<xla_lhlo::AddOp>,
SliceConverter
>(context);
// clang-format on
}
Expand Down