Skip to content

Commit ef9971b

Browse files
[XLA][MLIR] Lower tf.Tan and tf.Sin to MLHLO
Add `tan` op and lowering to CHLO dialect, move CHLO lowerings to `chlo_legalize_to_hlo_patterns` and extend missing patterns. PiperOrigin-RevId: 331128170 Change-Id: Ib242db312b4c611d4a86ae4a565f714b7eaa703e
1 parent bc481f3 commit ef9971b

9 files changed

Lines changed: 17 additions & 132 deletions

File tree

‎tensorflow/compiler/mlir/hlo/BUILD‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,6 @@ cc_library(
746746
srcs = ["lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo.cc"],
747747
hdrs = ["include/mlir-hlo/Dialect/mhlo/transforms/rewriters.h"],
748748
deps = [
749-
":chlo_legalize_to_hlo_inc_gen",
750749
":hlo",
751750
"@llvm-project//mlir:IR",
752751
"@llvm-project//mlir:SCFDialect",
@@ -756,25 +755,6 @@ cc_library(
756755
],
757756
)
758757

759-
gentbl(
760-
name = "chlo_legalize_to_hlo_inc_gen",
761-
strip_include_prefix = "lib/Dialect/mhlo/transforms/",
762-
tbl_outs = [
763-
(
764-
"-gen-rewriters",
765-
"lib/Dialect/mhlo/transforms/generated_chlo_legalize_to_hlo.inc",
766-
),
767-
],
768-
tblgen = "@llvm-project//mlir:mlir-tblgen",
769-
td_file = "lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo_patterns.td",
770-
td_relative_includes = [
771-
"include",
772-
],
773-
td_srcs = [
774-
":hlo_ops_td_files",
775-
],
776-
)
777-
778758
cc_library(
779759
name = "test_passes",
780760
srcs = [

‎tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.td‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,13 @@ def HLOClient_BroadcastComplexOp : HLOClient_BroadcastBinaryElementwiseOp<
344344
//===----------------------------------------------------------------------===//
345345

346346
class HLOClient_UnaryElementwiseOp<string mnemonic, list<OpTrait> traits,
347-
Type TensorType> : HLOClient_Op<mnemonic,
347+
Type TensorType>: HLOClient_Op<mnemonic,
348348
!listconcat(traits, [InferFusibilityOpInterface])> {
349349
let arguments = (ins TensorType:$operand);
350350
let results = (outs TensorType);
351351
}
352352

353-
def HLOClient_AcosOp : HLOClient_UnaryElementwiseOp<"acos",
353+
def HLOClient_AcosOp: HLOClient_UnaryElementwiseOp<"acos",
354354
[NoSideEffect, SameOperandsAndResultType], HLO_FpOrComplexTensor> {
355355
let summary = "Acos operator";
356356

@@ -364,20 +364,7 @@ def HLOClient_AcosOp : HLOClient_UnaryElementwiseOp<"acos",
364364
}];
365365
}
366366

367-
def HLOClient_TanOp : HLOClient_UnaryElementwiseOp<"tan",
368-
[NoSideEffect, SameOperandsAndResultType], HLO_FpOrComplexTensor> {
369-
let summary = "Tan operation";
370-
371-
let description = [{
372-
Returns `Tan(operand)` element-wise.
373-
374-
$$
375-
\tan(x) = \sin(x) / \cos(x)
376-
$$
377-
}];
378-
}
379-
380-
def HLOClient_ConstantLikeOp : HLOClient_Op<"constant_like",
367+
def HLOClient_ConstantLikeOp: HLOClient_Op<"constant_like",
381368
[NoSideEffect, SameOperandsAndResultShape,
382369
InferTypeOpInterface,
383370
DeclareOpInterfaceMethods<InferShapedTypeOpInterface>,

‎tensorflow/compiler/mlir/hlo/lib/Dialect/mhlo/transforms/CMakeLists.txt‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,13 @@ set(LLVM_TARGET_DEFINITIONS legalize_to_standard_patterns.td)
2525
mlir_tablegen(generated_legalize_to_standard.inc -gen-rewriters)
2626
add_public_tablegen_target(MLIRMhloLegalizeToStandardIncGen)
2727

28-
set(LLVM_TARGET_DEFINITIONS chlo_legalize_to_hlo_patterns.td)
29-
mlir_tablegen(generated_chlo_legalize_to_hlo.inc -gen-rewriters)
30-
add_public_tablegen_target(MLIRChloLegalizeToHloIncGen)
31-
3228

3329
add_mlir_library(ChloPasses
3430
chlo_legalize_to_hlo.cc
3531
chlo_legalize_to_hlo_pass.cc
3632

3733
DEPENDS
3834
MLIRhlo_opsIncGen
39-
MLIRChloLegalizeToHloIncGen
4035

4136
LINK_COMPONENTS
4237
Core

‎tensorflow/compiler/mlir/hlo/lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo.cc‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,10 @@ struct HloCompareAdaptor {
469469
}
470470
};
471471

472-
#include "generated_chlo_legalize_to_hlo.inc"
473472
} // namespace
474473

475474
void PopulateLegalizeChloToHloPatterns(MLIRContext *context,
476475
OwningRewritePatternList *patterns) {
477-
populateWithGenerated(context, patterns);
478-
479476
// Instantiate conversion templates for conforming binary elementwise ops
480477
// that do not have different dtypes between operands and results and do
481478
// not have special attributes that need to be preserved.

‎tensorflow/compiler/mlir/hlo/lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo_patterns.td‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

‎tensorflow/compiler/mlir/hlo/lib/Dialect/mhlo/transforms/hlo_legalize_to_lhlo.cc‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,6 @@ void populateHLOToLHLOConversionPattern(
515515
HloToLhloOpConverter<mhlo::ReshapeOp>,
516516
HloToLhloOpConverter<mhlo::SelectOp>,
517517
HloToLhloOpConverter<mhlo::SignOp>,
518-
HloToLhloOpConverter<mhlo::SinOp>,
519518
HloToLhloOpConverter<mhlo::SliceOp>,
520519
HloToLhloOpConverter<mhlo::SqrtOp>,
521520
HloToLhloOpConverter<mhlo::SubOp>,

‎tensorflow/compiler/mlir/xla/tests/legalize-tf.mlir‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,32 +1985,6 @@ func @acos_dynamic(%arg0: tensor<*xf32>) -> tensor<*xf32> {
19851985
return %0 : tensor<*xf32>
19861986
}
19871987

1988-
// CHECK-LABEL: @tan
1989-
// CHECK-SAME: (%[[ARG:.*]]: tensor<2xf32>) -> tensor<2xf32>
1990-
// CHLO-LABEL: @tan
1991-
// CHLO-SAME: (%[[ARG:.*]]: tensor<2xf32>) -> tensor<2xf32>
1992-
func @tan(%arg : tensor<2xf32>) -> tensor<2xf32> {
1993-
// CHECK: "chlo.tan"(%[[ARG]]) : (tensor<2xf32>) -> tensor<2xf32>
1994-
// CHLO: %[[SINE:.*]] = "mhlo.sine"(%[[ARG]])
1995-
// CHLO %[[COSINE:.*]] = "mhlo.cosine"(%[[ARG]])
1996-
// CHLO %[[RESULT:.*]] = "mhlo.divide"(%[[SINE]], %[[COSINE]])
1997-
%result = "tf.Tan"(%arg) : (tensor<2xf32>) -> tensor<2xf32>
1998-
return %result : tensor<2xf32>
1999-
}
2000-
2001-
// CHECK-LABEL: @tan_unranked
2002-
// CHECK-SAME: (%[[ARG:.*]]: tensor<*xf32>) -> tensor<*xf32>
2003-
// CHLO-LABEL: @tan_unranked
2004-
// CHLO-SAME: (%[[ARG:.*]]: tensor<*xf32>) -> tensor<*xf32>
2005-
func @tan_unranked(%arg : tensor<*xf32>) -> tensor<*xf32> {
2006-
// CHECK: "chlo.tan"(%[[ARG]]) : (tensor<*xf32>) -> tensor<*xf32>
2007-
// CHLO: %[[SINE:.*]] = "mhlo.sine"(%[[ARG]])
2008-
// CHLO %[[COSINE:.*]] = "mhlo.cosine"(%[[ARG]])
2009-
// CHLO %[[RESULT:.*]] = "mhlo.divide"(%[[SINE]], %[[COSINE]])
2010-
%result = "tf.Tan"(%arg) : (tensor<*xf32>) -> tensor<*xf32>
2011-
return %result : tensor<*xf32>
2012-
}
2013-
20141988
// CHECK-LABEL: func @cast_dynamic_i2f
20151989
func @cast_dynamic_i2f(%arg0: tensor<?xi32>) -> tensor<?xf32> {
20161990
// CHECK: "mhlo.convert"(%arg0) : (tensor<?xi32>) -> tensor<?xf32>

‎tensorflow/compiler/mlir/xla/transforms/legalize_tf.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5832,7 +5832,7 @@ class ConvertQrOp : public OpRewritePattern<TF::QrOp> {
58325832
void EmitLegalizationErrors(Operation *op,
58335833
const DenseSet<Operation *> &nonlegalized_ops) {
58345834
// Track the legalization failures by mapping op name to information about
5835-
// that failure: the number of unlegalized occurrences of the op, and one
5835+
// that failure: the number of unlegalized occurances of the op, and one
58365836
// example operation that failed.
58375837
std::map<StringRef, std::pair<int, Operation *>> op_name_to_error_info;
58385838
DenseSet<Operation *> error_ops;

‎tensorflow/compiler/mlir/xla/transforms/legalize_tf_patterns.td‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,24 @@ foreach Mapping = [
589589
[TF_SinOp, HLO_SinOp],
590590
[TF_SqrtOp, HLO_SqrtOp],
591591
[TF_TanhOp, HLO_TanhOp],
592-
[TF_TanOp, HLOClient_TanOp],
593592
] in {
594593
def : Pat<(Mapping[0] HLO_Tensor:$input),
595594
(Mapping[1] $input)>;
596595
}
597596

597+
// Expand acos to MHLO dialect as follows:
598+
// acos(x) = 2 * atan(sqrt(1 - x^2) / (1 + x)) if x != -1
599+
// = pi if x == -1
600+
def : Pat<(HLOClient_AcosOp $input), (HLO_SelectOp
601+
(HLO_CompareOp $input, (HLO_ConstantLike<"0"> $input),
602+
HLO_COMPARISON_DIRECTION_NE),
603+
(HLO_MulOp (HLO_ConstantLike<"2.0f"> $input),
604+
(HLO_Atan2Op
605+
(HLO_SqrtOp (HLO_SubOp
606+
(HLO_ConstantLike<"1"> $input), (HLO_MulOp $input, $input))),
607+
(HLO_AddOp (HLO_ConstantLike<"1"> $input), $input))),
608+
(HLO_ConstantLike<"M_PI"> $input))>;
609+
598610
// TODO(bixia): Lower Cast with a Complex type source operand or with
599611
// Truncate=True for floating point value conversions.
600612
def : Pat<(TF_CastOp HLO_Tensor:$arg, ConstBoolAttrFalse),

0 commit comments

Comments
 (0)