Skip to content

Commit d16860d

Browse files
timshen91TensorFlow MLIR Team
authored andcommitted
[MLIR] Change LMHLO Conditional and While to capture needed buffers, instead of passing them by operands.
This is consistent with the design of LMHLO FusionOp, and it simplifies the usage. Before the change, those redundant operands ended up unused as all sub-regions can already capture needed buffers. PiperOrigin-RevId: 362381155
1 parent 8066794 commit d16860d

2 files changed

Lines changed: 23 additions & 37 deletions

File tree

‎include/mlir-hlo/Dialect/mhlo/IR/lhlo_ops.td‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -232,34 +232,21 @@ def LHLO_ReduceWindowOp: LHLO_Op<"reduce_window", []>, BASE_HLO_ReduceWindowOp {
232232
let regions = (region SizedRegion<1>:$body);
233233
}
234234

235-
// TODO(timshen): Add a custom parser to hide operand_segment_sizes. For example,
236-
// A tuple-like pattern match syntax could work:
237-
// lmhlo.case %index, (%input0, %input1, %input2), (%output0, %output1) {
238-
// ...
239-
// }, {
240-
// ...
241-
// } : (type_input0, type_input1, type_input2, type_output0, type_output1) -> ()
235+
// TODO(timshen): Add a custom syntax for this.
242236
def LHLO_CaseOp: LHLO_Op<"case", [
243-
AttrSizedOperandSegments,
244237
SingleBlockImplicitTerminator<"TerminatorOp">
245238
]>, BASE_HLO_CaseOp {
246239

247-
let arguments = (ins
248-
Arg<LHLO_Buffer, "", [MemRead]>:$index,
249-
Arg<Variadic<LHLO_Buffer>, "", [MemRead]>:$branch_operands,
250-
Arg<Variadic<LHLO_Buffer>, "", [MemWrite]>:$out
251-
);
240+
let arguments = (ins Arg<LHLO_PredOrIntBuffer, "", [MemRead]>:$index);
252241

253242
let regions = (region VariadicRegion<SizedRegion<1>>:$branches);
254243
}
255244

256245
// TODO(timshen): Add a custom syntax for this.
257-
def LHLO_WhileOp: LHLO_Op<"while", [SameVariadicOperandSize]>,
258-
BASE_HLO_WhileOp {
246+
def LHLO_WhileOp: LHLO_Op<"while", []>, BASE_HLO_WhileOp {
259247
let arguments = (ins
260-
Arg<Variadic<LHLO_Buffer>, "", [MemRead]>:$val,
261-
Arg<Variadic<LHLO_Buffer>, "", [MemWrite]>:$output
262-
);
248+
Arg<Variadic<LHLO_PredBuffer>, "", [MemWrite]>:$cond_val,
249+
OptionalAttr<I64Attr>:$trip_count);
263250

264251
let regions = (region SizedRegion<1>:$cond, SizedRegion<1>:$body);
265252
}

‎tests/lhlo_ops.mlir‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,21 +472,20 @@ func @fusion_memref(%input1: memref<10xf32>, %input2: memref<10xf32>, %input3: m
472472

473473
// CHECK-LABEL: func @case_memref
474474
func @case_memref(%index: memref<i32>, %operand_1: memref<f32>, %operand_2: memref<f32>, %operand_3: memref<f32>, %out: memref<f32>) -> () {
475-
"lmhlo.case"(%index, %operand_1, %operand_2, %operand_3, %out) ( {
476-
^bb0(%arg0: memref<f32>):
477-
"lmhlo.negate"(%arg0, %out) : (memref<f32>, memref<f32>) -> ()
475+
"lmhlo.case"(%index) ( {
476+
^bb0:
477+
"lmhlo.negate"(%operand_1, %out) : (memref<f32>, memref<f32>) -> ()
478478
"lmhlo.terminator"() : () -> ()
479479
}, {
480-
^bb0(%arg0: memref<f32>):
481-
"lmhlo.copy"(%arg0, %out) : (memref<f32>, memref<f32>) -> ()
480+
^bb0:
481+
"lmhlo.copy"(%operand_2, %out) : (memref<f32>, memref<f32>) -> ()
482482
"lmhlo.terminator"() : () -> ()
483483
}, {
484-
^bb0(%arg0: memref<f32>):
485-
"lmhlo.add"(%arg0, %arg0, %out) : (memref<f32>, memref<f32>, memref<f32>) -> ()
484+
^bb0:
485+
"lmhlo.add"(%operand_3, %operand_3, %out) : (memref<f32>, memref<f32>, memref<f32>) -> ()
486486
"lmhlo.terminator"() : () -> ()
487487
}
488-
) {operand_segment_sizes = dense<[1, 3, 1]> : vector<3xi32>}
489-
: (memref<i32>, memref<f32>, memref<f32>, memref<f32>, memref<f32>) -> ()
488+
) : (memref<i32>) -> ()
490489
return
491490
}
492491

@@ -908,22 +907,22 @@ func @triangular_solve_memrefs(%arg0: memref<4x4xf32>, %arg1: memref<3x4xf32>, %
908907
// -----
909908

910909
// CHECK-LABEL: func @while_memrefs
911-
func @while_memrefs(%arg0: memref<i64>, %arg_out: memref<i64>) -> () {
912-
"lmhlo.while"(%arg0, %arg_out) (
913-
{ ^bb0(%arg: memref<i64>, %cond: memref<i1>): "lmhlo.terminator"() : () -> () },
914-
{ ^bb0(%arg: memref<i64>, %body_out: memref<i64>): "lmhlo.terminator"() : () -> () }
915-
) : (memref<i64>, memref<i64>) -> ()
910+
func @while_memrefs(%arg0: memref<i64>, %arg_out: memref<i64>, %cond: memref<i1>) -> () {
911+
"lmhlo.while"(%cond) (
912+
{ ^bb0: "lmhlo.terminator"() : () -> () },
913+
{ ^bb0: "lmhlo.terminator"() : () -> () }
914+
) : (memref<i1>) -> ()
916915
return
917916
}
918917

919918
// -----
920919

921920
// CHECK-LABEL: func @while_memrefs
922-
func @while_memrefs(%arg0: memref<i64>, %arg1: memref<5xf32>, %arg0_out: memref<i64>, %arg1_out: memref<5xf32>) -> () {
923-
"lmhlo.while"(%arg0, %arg1, %arg0_out, %arg1_out) (
924-
{ ^bb0(%cur0: memref<i64>, %cur1: memref<5xf32>, %cond: memref<i1>): "lmhlo.terminator"() : () -> () },
925-
{ ^bb0(%cur0: memref<i64>, %cur1: memref<5xf32>, %body_out0: memref<i64>, %body_out1: memref<5xf32>): "lmhlo.terminator"() : () -> () }
926-
) : (memref<i64>, memref<5xf32>, memref<i64>, memref<5xf32>) -> ()
921+
func @while_memrefs(%arg0: memref<i64>, %arg1: memref<5xf32>, %arg0_out: memref<i64>, %arg1_out: memref<5xf32>, %cond: memref<i1>) -> () {
922+
"lmhlo.while"(%cond) (
923+
{ ^bb0: "lmhlo.terminator"() : () -> () },
924+
{ ^bb0: "lmhlo.terminator"() : () -> () }
925+
) : (memref<i1>) -> ()
927926
return
928927
}
929928

0 commit comments

Comments
 (0)