83 lines · plain
1// RUN: mlir-opt -test-greedy-patterns='top-down=false' %s | FileCheck %s2// RUN: mlir-opt -test-greedy-patterns='top-down=true' %s | FileCheck %s3// RUN: mlir-opt -test-greedy-patterns='cse-constants=false' %s | FileCheck %s --check-prefix=NOCSE4// RUN: mlir-opt -test-greedy-patterns='fold=false' %s | FileCheck %s --check-prefix=NOFOLD5 6func.func @foo() -> i32 {7 %c42 = arith.constant 42 : i328 9 // The new operation should be present in the output and contain an attribute10 // with value "42" that results from folding.11 12 // CHECK: "test.op_in_place_fold"(%{{.*}}) <{attr = 42 : i32}13 %0 = "test.op_in_place_fold_anchor"(%c42) : (i32) -> (i32)14 return %0 : i3215}16 17func.func @test_fold_before_previously_folded_op() -> (i32, i32) {18 // When folding two constants will be generated and uniqued. Check that the19 // uniqued constant properly dominates both uses.20 // CHECK: %[[CST:.+]] = arith.constant true21 // CHECK-NEXT: "test.cast"(%[[CST]]) : (i1) -> i3222 // CHECK-NEXT: "test.cast"(%[[CST]]) : (i1) -> i3223 24 %0 = "test.cast"() {test_fold_before_previously_folded_op} : () -> (i32)25 %1 = "test.cast"() {test_fold_before_previously_folded_op} : () -> (i32)26 return %0, %1 : i32, i3227}28 29func.func @test_dont_reorder_constants() -> (i32, i32, i32) {30 // Test that we don't reorder existing constants during folding if it isn't31 // necessary.32 // CHECK: %[[CST:.+]] = arith.constant 133 // CHECK-NEXT: %[[CST:.+]] = arith.constant 234 // CHECK-NEXT: %[[CST:.+]] = arith.constant 335 %0 = arith.constant 1 : i3236 %1 = arith.constant 2 : i3237 %2 = arith.constant 3 : i3238 return %0, %1, %2 : i32, i32, i3239}40 41// CHECK-LABEL: test_fold_nofold_nocse42// NOCSE-LABEL: test_fold_nofold_nocse43// NOFOLD-LABEL: test_fold_nofold_nocse44func.func @test_fold_nofold_nocse() -> (i32, i32, i32, i32, i32, i32) {45 // Test either not folding or deduping constants.46 47 // Testing folding. There should be only 4 constants here.48 // CHECK-NOT: arith.constant49 // CHECK-DAG: %[[CST:.+]] = arith.constant 050 // CHECK-DAG: %[[CST:.+]] = arith.constant 151 // CHECK-DAG: %[[CST:.+]] = arith.constant 252 // CHECK-DAG: %[[CST:.+]] = arith.constant 353 // CHECK-NOT: arith.constant54 // CHECK-NEXT: return55 56 // Testing not-CSE'ing. In this case we have the 3 original constants and 357 // produced by folding.58 // NOCSE-DAG: arith.constant 0 : i3259 // NOCSE-DAG: arith.constant 1 : i3260 // NOCSE-DAG: arith.constant 2 : i3261 // NOCSE-DAG: arith.constant 1 : i3262 // NOCSE-DAG: arith.constant 2 : i3263 // NOCSE-DAG: arith.constant 3 : i3264 // NOCSE-NEXT: return65 66 // Testing not folding. In this case we just have the original constants.67 // NOFOLD-DAG: %[[CST:.+]] = arith.constant 068 // NOFOLD-DAG: %[[CST:.+]] = arith.constant 169 // NOFOLD-DAG: %[[CST:.+]] = arith.constant 270 // NOFOLD: arith.addi71 // NOFOLD: arith.addi72 // NOFOLD: arith.addi73 74 %c0 = arith.constant 0 : i3275 %c1 = arith.constant 1 : i3276 %c2 = arith.constant 2 : i3277 %0 = arith.addi %c0, %c1 : i3278 %1 = arith.addi %0, %c1 : i3279 %2 = arith.addi %c2, %c1 : i3280 return %0, %1, %2, %c0, %c1, %c2 : i32, i32, i32, i32, i32, i3281}82 83