| // RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline="builtin.module(func.func(sccp))" -split-input-file | FileCheck %s | |
| /// Check simple forward constant propagation without any control flow. | |
| // CHECK-LABEL: func @no_control_flow | |
| func.func @no_control_flow(%arg0: i32) -> i32 { | |
| // CHECK: %[[CST:.*]] = arith.constant 1 : i32 | |
| // CHECK: return %[[CST]] : i32 | |
| %cond = arith.constant true | |
| %cst_1 = arith.constant 1 : i32 | |
| %select = arith.select %cond, %cst_1, %arg0 : i32 | |
| return %select : i32 | |
| } | |
| /// Check that a constant is properly propagated when only one edge of a branch | |
| /// is taken. | |
| // CHECK-LABEL: func @simple_control_flow | |
| func.func @simple_control_flow(%arg0 : i32) -> i32 { | |
| // CHECK: %[[CST:.*]] = arith.constant 1 : i32 | |
| %cond = arith.constant true | |
| %1 = arith.constant 1 : i32 | |
| cf.cond_br %cond, ^bb1, ^bb2(%arg0 : i32) | |
| ^bb1: | |
| cf.br ^bb2(%1 : i32) | |
| ^bb2(%arg : i32): | |
| // CHECK: ^bb2(%{{.*}}: i32): | |
| // CHECK: return %[[CST]] : i32 | |
| return %arg : i32 | |
| } | |
| /// Check that the arguments go to overdefined if the branch cannot detect when | |
| /// a specific successor is taken. | |
| // CHECK-LABEL: func @simple_control_flow_overdefined | |
| func.func @simple_control_flow_overdefined(%arg0 : i32, %arg1 : i1) -> i32 { | |
| %1 = arith.constant 1 : i32 | |
| cf.cond_br %arg1, ^bb1, ^bb2(%arg0 : i32) | |
| ^bb1: | |
| cf.br ^bb2(%1 : i32) | |
| ^bb2(%arg : i32): | |
| // CHECK: ^bb2(%[[ARG:.*]]: i32): | |
| // CHECK: return %[[ARG]] : i32 | |
| return %arg : i32 | |
| } | |
| /// Check that the arguments go to overdefined if there are conflicting | |
| /// constants. | |
| // CHECK-LABEL: func @simple_control_flow_constant_overdefined | |
| func.func @simple_control_flow_constant_overdefined(%arg0 : i32, %arg1 : i1) -> i32 { | |
| %1 = arith.constant 1 : i32 | |
| %2 = arith.constant 2 : i32 | |
| cf.cond_br %arg1, ^bb1, ^bb2(%arg0 : i32) | |
| ^bb1: | |
| cf.br ^bb2(%2 : i32) | |
| ^bb2(%arg : i32): | |
| // CHECK: ^bb2(%[[ARG:.*]]: i32): | |
| // CHECK: return %[[ARG]] : i32 | |
| return %arg : i32 | |
| } | |
| /// Check that the arguments go to overdefined if the branch is unknown. | |
| // CHECK-LABEL: func @unknown_terminator | |
| func.func @unknown_terminator(%arg0 : i32, %arg1 : i1) -> i32 { | |
| %1 = arith.constant 1 : i32 | |
| "foo.cond_br"() [^bb1, ^bb2] : () -> () | |
| ^bb1: | |
| cf.br ^bb2(%1 : i32) | |
| ^bb2(%arg : i32): | |
| // CHECK: ^bb2(%[[ARG:.*]]: i32): | |
| // CHECK: return %[[ARG]] : i32 | |
| return %arg : i32 | |
| } | |
| /// Check that arguments are properly merged across loop-like control flow. | |
| func.func private @ext_cond_fn() -> i1 | |
| // CHECK-LABEL: func @simple_loop | |
| func.func @simple_loop(%arg0 : i32, %cond1 : i1) -> i32 { | |
| // CHECK: %[[CST:.*]] = arith.constant 1 : i32 | |
| %cst_1 = arith.constant 1 : i32 | |
| cf.cond_br %cond1, ^bb1(%cst_1 : i32), ^bb2(%cst_1 : i32) | |
| ^bb1(%iv: i32): | |
| // CHECK: ^bb1(%{{.*}}: i32): | |
| // CHECK-NEXT: %[[COND:.*]] = call @ext_cond_fn() | |
| // CHECK-NEXT: cf.cond_br %[[COND]], ^bb1(%[[CST]] : i32), ^bb2(%[[CST]] : i32) | |
| %cst_0 = arith.constant 0 : i32 | |
| %res = arith.addi %iv, %cst_0 : i32 | |
| %cond2 = call @ext_cond_fn() : () -> i1 | |
| cf.cond_br %cond2, ^bb1(%res : i32), ^bb2(%res : i32) | |
| ^bb2(%arg : i32): | |
| // CHECK: ^bb2(%{{.*}}: i32): | |
| // CHECK: return %[[CST]] : i32 | |
| return %arg : i32 | |
| } | |
| /// Test that we can properly propagate within inner control, and in situations | |
| /// where the executable edges within the CFG are sensitive to the current state | |
| /// of the analysis. | |
| // CHECK-LABEL: func @simple_loop_inner_control_flow | |
| func.func @simple_loop_inner_control_flow(%arg0 : i32) -> i32 { | |
| // CHECK-DAG: %[[CST:.*]] = arith.constant 1 : i32 | |
| // CHECK-DAG: %[[TRUE:.*]] = arith.constant true | |
| %cst_1 = arith.constant 1 : i32 | |
| cf.br ^bb1(%cst_1 : i32) | |
| ^bb1(%iv: i32): | |
| %cond2 = call @ext_cond_fn() : () -> i1 | |
| cf.cond_br %cond2, ^bb5(%iv : i32), ^bb2 | |
| ^bb2: | |
| // CHECK: ^bb2: | |
| // CHECK: cf.cond_br %[[TRUE]], ^bb3, ^bb4 | |
| %cst_20 = arith.constant 20 : i32 | |
| %cond = arith.cmpi ult, %iv, %cst_20 : i32 | |
| cf.cond_br %cond, ^bb3, ^bb4 | |
| ^bb3: | |
| // CHECK: ^bb3: | |
| // CHECK: cf.br ^bb1(%[[CST]] : i32) | |
| %cst_1_2 = arith.constant 1 : i32 | |
| cf.br ^bb1(%cst_1_2 : i32) | |
| ^bb4: | |
| %iv_inc = arith.addi %iv, %cst_1 : i32 | |
| cf.br ^bb1(%iv_inc : i32) | |
| ^bb5(%result: i32): | |
| // CHECK: ^bb5(%{{.*}}: i32): | |
| // CHECK: return %[[CST]] : i32 | |
| return %result : i32 | |
| } | |
| /// Check that arguments go to overdefined when loop backedges produce a | |
| /// conflicting value. | |
| func.func private @ext_cond_and_value_fn() -> (i1, i32) | |
| // CHECK-LABEL: func @simple_loop_overdefined | |
| func.func @simple_loop_overdefined(%arg0 : i32, %cond1 : i1) -> i32 { | |
| %cst_1 = arith.constant 1 : i32 | |
| cf.cond_br %cond1, ^bb1(%cst_1 : i32), ^bb2(%cst_1 : i32) | |
| ^bb1(%iv: i32): | |
| %cond2, %res = call @ext_cond_and_value_fn() : () -> (i1, i32) | |
| cf.cond_br %cond2, ^bb1(%res : i32), ^bb2(%res : i32) | |
| ^bb2(%arg : i32): | |
| // CHECK: ^bb2(%[[ARG:.*]]: i32): | |
| // CHECK: return %[[ARG]] : i32 | |
| return %arg : i32 | |
| } | |
| // Check that we reprocess executable edges when information changes. | |
| // CHECK-LABEL: func @recheck_executable_edge | |
| func.func @recheck_executable_edge(%cond0: i1) -> (i1, i1) { | |
| %true = arith.constant true | |
| %false = arith.constant false | |
| cf.cond_br %cond0, ^bb_1a, ^bb2(%false : i1) | |
| ^bb_1a: | |
| cf.br ^bb2(%true : i1) | |
| ^bb2(%x: i1): | |
| // CHECK: ^bb2(%[[X:.*]]: i1): | |
| cf.br ^bb3(%x : i1) | |
| ^bb3(%y: i1): | |
| // CHECK: ^bb3(%[[Y:.*]]: i1): | |
| // CHECK: return %[[X]], %[[Y]] | |
| return %x, %y : i1, i1 | |
| } | |
| // CHECK-LABEL: func @simple_produced_operand | |
| func.func @simple_produced_operand() -> (i32, i32) { | |
| // CHECK: %[[ONE:.*]] = arith.constant 1 | |
| %1 = arith.constant 1 : i32 | |
| "test.internal_br"(%1) [^bb1, ^bb2] { | |
| operandSegmentSizes = array<i32: 0, 1> | |
| } : (i32) -> () | |
| ^bb1: | |
| cf.br ^bb2(%1, %1 : i32, i32) | |
| ^bb2(%arg1 : i32, %arg2 : i32): | |
| // CHECK: ^bb2(%[[ARG:.*]]: i32, %{{.*}}: i32): | |
| // CHECK: return %[[ARG]], %[[ONE]] : i32, i32 | |
| return %arg1, %arg2 : i32, i32 | |
| } | |
| // CHECK-LABEL: inplace_fold | |
| func.func @inplace_fold() -> (i32) { | |
| %0 = "test.op_in_place_fold_success"() : () -> i1 | |
| %1 = arith.constant 5 : i32 | |
| cf.cond_br %0, ^a, ^b | |
| ^a: | |
| // CHECK-NOT: addi | |
| %3 = arith.addi %1, %1 : i32 | |
| return %3 : i32 | |
| ^b: | |
| return %1 : i32 | |
| } | |
| // CHECK-LABEL: op_with_region | |
| func.func @op_with_region() -> (i32) { | |
| %0 = "test.op_with_region"() ({}) : () -> i1 | |
| %1 = arith.constant 5 : i32 | |
| cf.cond_br %0, ^a, ^b | |
| ^a: | |
| // CHECK-NOT: addi | |
| %3 = arith.addi %1, %1 : i32 | |
| return %3 : i32 | |
| ^b: | |
| return %1 : i32 | |
| } | |
| // `llvm.mlir.constant` allows a float attribute to be paired with an integer | |
| // type of the same width, so the folded attribute type differs from the source | |
| // type of the broadcast. Folding must bail out instead of crashing. | |
| // CHECK-LABEL: no_crash_with_different_source_type | |
| func.func @no_crash_with_different_source_type() { | |
| // CHECK: llvm.mlir.constant(0.000000e+00 : f32) : i32 | |
| %0 = llvm.mlir.constant(0.0 : f32) : i32 | |
| // CHECK: vector.broadcast %[[CST:.*]] : i32 to vector<128xi32> | |
| %1 = vector.broadcast %0 : i32 to vector<128xi32> | |
| llvm.return | |
| } | |
| // ----- | |
| // Regression test for https://github.com/llvm/llvm-project/issues/187972 | |
| // DeadCodeAnalysis::visitRegionBranchEdges must not dereference the front() | |
| // of an empty region (acc.serial with no blocks). | |
| // CHECK-LABEL: no_crash_acc_serial_empty_region | |
| func.func @no_crash_acc_serial_empty_region() { | |
| // CHECK: acc.serial | |
| acc.serial { | |
| } | |
| return | |
| } | |
| // ----- | |
| // Regression test for https://github.com/llvm/llvm-project/issues/188408 | |
| // isRegionOrCallableReturn must guard getTerminator() with mightHaveTerminator() | |
| // to avoid asserting on blocks in nested acc regions without terminators. | |
| // CHECK-LABEL: no_crash_acc_kernel_environment | |
| func.func @no_crash_acc_kernel_environment(%data: memref<8xi32>) { | |
| // CHECK: acc.kernel_environment | |
| acc.kernel_environment { | |
| acc.compute_region { | |
| acc.yield | |
| } <{origin = "acc.parallel"}> | |
| } | |
| return | |
| } | |
| // ----- | |
| // Regression test for https://github.com/llvm/llvm-project/issues/187973 | |
| // SwitchOp::getEntrySuccessorRegions must not call IntegerAttr::getInt() on | |
| // an unsigned integer type — that function asserts signless/index only. | |
| // CHECK-LABEL: no_crash_emitc_switch_unsigned_condition | |
| func.func @no_crash_emitc_switch_unsigned_condition() { | |
| // CHECK: emitc.constant | |
| %0 = "emitc.constant"() <{value = 1 : ui32}> : () -> ui32 | |
| // CHECK: emitc.switch | |
| emitc.switch %0 : ui32 | |
| case 2 { | |
| emitc.yield | |
| } | |
| default { | |
| emitc.yield | |
| } | |
| return | |
| } | |
| // ----- | |
| // Regression test for https://github.com/llvm/llvm-project/issues/137509 | |
| // | |
| // %a (the ^bb3 block arg) joins ^bb1's Constant %c1 with ^bb2's non-foldable | |
| // %v, so %a's lattice is overdefined. %cast2's fold collapses the round-trip | |
| // cast chain to %a - a Value that is not one of %cast2's own operands - so | |
| // constant propagation must read %a's lattice directly to compute %cast2's. | |
| // %a being overdefined, the cast chain is preserved. | |
| // CHECK-LABEL: func @fold_to_non_operand_value | |
| func.func @fold_to_non_operand_value(%x: i64, %cond: i1) -> i64 { | |
| %c1 = arith.constant 1 : i64 | |
| cf.cond_br %cond, ^bb1, ^bb2 | |
| ^bb1: | |
| cf.br ^bb3(%c1 : i64) | |
| ^bb2: | |
| %v = arith.addi %x, %c1 : i64 | |
| cf.br ^bb3(%v : i64) | |
| ^bb3(%a: i64): | |
| // CHECK: %[[CAST1:.*]] = builtin.unrealized_conversion_cast %{{.*}} : i64 to index | |
| // CHECK: %[[CAST2:.*]] = builtin.unrealized_conversion_cast %[[CAST1]] : index to i64 | |
| // CHECK: return %[[CAST2]] : i64 | |
| %cast1 = builtin.unrealized_conversion_cast %a : i64 to index | |
| %cast2 = builtin.unrealized_conversion_cast %cast1 : index to i64 | |
| return %cast2 : i64 | |
| } | |
| // ----- | |
| // Regression test: SCCP must revert in-place folds. The `vector.extract` | |
| // folder rewrites a constant dynamic position into a static one in place, but | |
| // the constants SCCP feeds into `fold` are speculative: on the first visit of | |
| // ^bb1 %iv is Constant 0, before the back edge widens it to overdefined. | |
| // Without the revert, the dynamic extract would permanently read element 0. | |
| // CHECK-LABEL: func @no_inplace_extract_fold_of_speculative_constant | |
| func.func @no_inplace_extract_fold_of_speculative_constant(%a: f32, %b: f32) -> f32 { | |
| %c0_i32 = arith.constant 0 : i32 | |
| %c1_i32 = arith.constant 1 : i32 | |
| %c2_i32 = arith.constant 2 : i32 | |
| %v = vector.from_elements %a, %b : vector<2xf32> | |
| cf.br ^bb1(%c0_i32 : i32) | |
| // CHECK: ^bb1(%[[IV:.*]]: i32): | |
| ^bb1(%iv: i32): | |
| // CHECK: %[[IDX:.*]] = arith.index_cast %[[IV]] : i32 to index | |
| %idx = arith.index_cast %iv : i32 to index | |
| // CHECK: vector.extract %{{.*}}[%[[IDX]]] : f32 from vector<2xf32> | |
| %e = vector.extract %v[%idx] : f32 from vector<2xf32> | |
| %next = arith.addi %iv, %c1_i32 : i32 | |
| %cond = arith.cmpi ne, %next, %c2_i32 : i32 | |
| cf.cond_br %cond, ^bb1(%next : i32), ^bb2 | |
| ^bb2: | |
| return %e : f32 | |
| } |