1// RUN: mlir-opt -test-patterns='top-down=false' %s | FileCheck %s
2// RUN: mlir-opt -test-patterns='top-down=true' %s | FileCheck %s
3
4func.func @foo() -> i32 {
5  %c42 = arith.constant 42 : i32
6
7  // The new operation should be present in the output and contain an attribute
8  // with value "42" that results from folding.
9
10  // CHECK: "test.op_in_place_fold"(%{{.*}}) {attr = 42 : i32}
11  %0 = "test.op_in_place_fold_anchor"(%c42) : (i32) -> (i32)
12  return %0 : i32
13}
14
15func.func @test_fold_before_previously_folded_op() -> (i32, i32) {
16  // When folding two constants will be generated and uniqued. Check that the
17  // uniqued constant properly dominates both uses.
18  // CHECK: %[[CST:.+]] = arith.constant true
19  // CHECK-NEXT: "test.cast"(%[[CST]]) : (i1) -> i32
20  // CHECK-NEXT: "test.cast"(%[[CST]]) : (i1) -> i32
21
22  %0 = "test.cast"() {test_fold_before_previously_folded_op} : () -> (i32)
23  %1 = "test.cast"() {test_fold_before_previously_folded_op} : () -> (i32)
24  return %0, %1 : i32, i32
25}
26
27func.func @test_dont_reorder_constants() -> (i32, i32, i32) {
28  // Test that we don't reorder existing constants during folding if it isn't necessary.
29  // CHECK: %[[CST:.+]] = arith.constant 1
30  // CHECK-NEXT: %[[CST:.+]] = arith.constant 2
31  // CHECK-NEXT: %[[CST:.+]] = arith.constant 3
32  %0 = arith.constant 1 : i32
33  %1 = arith.constant 2 : i32
34  %2 = arith.constant 3 : i32
35  return %0, %1, %2 : i32, i32, i32
36}
37