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

Enable stablehlo.concatenate to tensor.concat conversion #2552

Merged
merged 2 commits into from
Sep 18, 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
6 changes: 6 additions & 0 deletions stablehlo/conversions/linalg/tests/miscellaneous.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func.func @bitcast_convert_contract(%input: tensor<7x4xi8>) -> tensor<7xi32> {
// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]
// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]
// CHECK-SAME: %[[VAL_2:[a-zA-Z0-9_]*]]
// CHECK-PRIMTIVE-NOT: linalg.generic
// CHECK-PRIMITIVE: %[[CONCAT:.*]] = tensor.concat dim(1)
// CHECK-PRIMITIVE: return %[[CONCAT:.*]]
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
// CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_5:.*]] = tensor.dim %[[VAL_0]], %[[C0]] : tensor<?x?xi32>
Expand Down Expand Up @@ -139,6 +142,9 @@ func.func @concatenate(%a: tensor<?x?xi32>, %b: tensor<?x?xi32>, %c: tensor<?x?x
// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]
// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]
// CHECK-SAME: %[[VAL_2:[a-zA-Z0-9_]*]]
// CHECK-PRIMTIVE-NOT: linalg.generic
// CHECK-PRIMITIVE: %[[CONCAT:.*]] = tensor.concat dim(1)
// CHECK-PRIMITIVE: return %[[CONCAT:.*]]
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
// CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index
// CHECK-DAG: %[[VAL_5:.*]] = builtin.unrealized_conversion_cast %[[VAL_2]] : tensor<?x?xui32> to tensor<?x?xi32>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,10 @@ struct IotaToMapConverter final : OpConversionPattern<OpTy> {
/// Converts stablehlo.concatenate operation to a linalg.generic op.
struct ConcatenateConverter final
: OpConversionPattern<mlir::stablehlo::ConcatenateOp> {
using OpConversionPattern::OpConversionPattern;
explicit ConcatenateConverter(TypeConverter &converter, MLIRContext *context,
bool enablePrimitiveOps)
: OpConversionPattern<mlir::stablehlo::ConcatenateOp>(converter, context),
enablePrimitiveOps(enablePrimitiveOps) {}

LogicalResult matchAndRewrite(
mlir::stablehlo::ConcatenateOp op, OpAdaptor adaptor,
Expand Down Expand Up @@ -1327,6 +1330,13 @@ struct ConcatenateConverter final

uint64_t dim = op.getDimension();
Location loc = op.getLoc();

if (enablePrimitiveOps) {
auto concatOp =
rewriter.create<tensor::ConcatOp>(loc, dim, adaptor.getOperands());
rewriter.replaceOp(op, concatOp);
return success();
}
Value zero = rewriter.create<arith::ConstantIndexOp>(loc, 0);

// Allocate the output tensor with tensor.empty.
Expand Down Expand Up @@ -1394,6 +1404,8 @@ struct ConcatenateConverter final
linalg::getPrunedAttributeList(op));
return success();
}

bool enablePrimitiveOps = false;
};

/// Converts stablehlo.concatenate operation to a sparse_tensor.concatenate op.
Expand Down Expand Up @@ -2594,9 +2606,10 @@ static void populateConversionPatterns(MLIRContext *context,
bool enablePrimitiveOps,
bool enableSparseOps) {
// clang-format off
patterns->add<ConcatenateConverter>(typeConverter, context,
enablePrimitiveOps);
patterns->add<
BitcastConvertConverter,
ConcatenateConverter,
ConstConverterTensor,
EinsumToLinalgConverter,
GatherConversion,
Expand Down
Loading