Files
clang-p2996/mlir/test/Transforms/test-canonicalize.mlir
Matthias Springer f10302e3fa [mlir] Require folders to produce Values of same type (#75887)
This commit adds extra assertions to `OperationFolder` and `OpBuilder`
to ensure that the types of the folded SSA values match with the result
types of the op. There used to be checks that discard the folded results
if the types do not match. This commit makes these checks stricter and
turns them into assertions.

Discarding folded results with the wrong type (without failing
explicitly) can hide bugs in op folders. Two such bugs became apparent
in MLIR (and some more in downstream projects) and are fixed with this
change.

Note: The existing type checks were introduced in
https://reviews.llvm.org/D95991.

Migration guide: If you see failing assertions (`folder produced value
of incorrect type`; make sure to run with assertions enabled!), run with
`-debug` or dump the operation right before the failing assertion. This
will point you to the op that has the broken folder. A common mistake is
a mismatch between static/dynamic dimensions (e.g., input has a static
dimension but folded result has a dynamic dimension).
2023-12-20 14:39:22 +09:00

92 lines
3.6 KiB
MLIR

// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(canonicalize))' | FileCheck %s
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(canonicalize{region-simplify=false}))' | FileCheck %s --check-prefixes=CHECK,NO-RS
// CHECK-LABEL: func @remove_op_with_inner_ops_pattern
func.func @remove_op_with_inner_ops_pattern() {
// CHECK-NEXT: return
"test.op_with_region_pattern"() ({
"test.op_with_region_terminator"() : () -> ()
}) : () -> ()
return
}
// CHECK-LABEL: func @remove_op_with_inner_ops_fold_no_side_effect
func.func @remove_op_with_inner_ops_fold_no_side_effect() {
// CHECK-NEXT: return
"test.op_with_region_fold_no_side_effect"() ({
"test.op_with_region_terminator"() : () -> ()
}) : () -> ()
return
}
// CHECK-LABEL: func @remove_op_with_inner_ops_fold
// CHECK-SAME: (%[[ARG_0:[a-z0-9]*]]: i32)
func.func @remove_op_with_inner_ops_fold(%arg0 : i32) -> (i32) {
// CHECK-NEXT: return %[[ARG_0]]
%0 = "test.op_with_region_fold"(%arg0) ({
"test.op_with_region_terminator"() : () -> ()
}) : (i32) -> (i32)
return %0 : i32
}
// CHECK-LABEL: func @remove_op_with_variadic_results_and_folder
// CHECK-SAME: (%[[ARG_0:[a-z0-9]*]]: i32, %[[ARG_1:[a-z0-9]*]]: i32)
func.func @remove_op_with_variadic_results_and_folder(%arg0 : i32, %arg1 : i32) -> (i32, i32) {
// CHECK-NEXT: return %[[ARG_0]], %[[ARG_1]]
%0, %1 = "test.op_with_variadic_results_and_folder"(%arg0, %arg1) : (i32, i32) -> (i32, i32)
return %0, %1 : i32, i32
}
// CHECK-LABEL: func @test_commutative_multi
// CHECK-SAME: (%[[ARG_0:[a-z0-9]*]]: i32, %[[ARG_1:[a-z0-9]*]]: i32)
func.func @test_commutative_multi(%arg0: i32, %arg1: i32) -> (i32, i32) {
// CHECK-DAG: %[[C42:.*]] = arith.constant 42 : i32
%c42_i32 = arith.constant 42 : i32
// CHECK-DAG: %[[C43:.*]] = arith.constant 43 : i32
%c43_i32 = arith.constant 43 : i32
// CHECK-NEXT: %[[O0:.*]] = "test.op_commutative"(%[[ARG_0]], %[[ARG_1]], %[[C42]], %[[C43]]) : (i32, i32, i32, i32) -> i32
%y = "test.op_commutative"(%c42_i32, %arg0, %arg1, %c43_i32) : (i32, i32, i32, i32) -> i32
// CHECK-NEXT: %[[O1:.*]] = "test.op_commutative"(%[[ARG_0]], %[[ARG_1]], %[[C42]], %[[C43]]) : (i32, i32, i32, i32) -> i32
%z = "test.op_commutative"(%arg0, %c42_i32, %c43_i32, %arg1): (i32, i32, i32, i32) -> i32
// CHECK-NEXT: return %[[O0]], %[[O1]]
return %y, %z: i32, i32
}
// CHECK-LABEL: func @test_commutative_multi_cst
func.func @test_commutative_multi_cst(%arg0: i32, %arg1: i32) -> (i32, i32) {
// CHECK-NEXT: %c42_i32 = arith.constant 42 : i32
%c42_i32 = arith.constant 42 : i32
%c42_i32_2 = arith.constant 42 : i32
// CHECK-NEXT: %[[O0:.*]] = "test.op_commutative"(%arg0, %arg1, %c42_i32, %c42_i32) : (i32, i32, i32, i32) -> i32
%y = "test.op_commutative"(%c42_i32, %arg0, %arg1, %c42_i32_2) : (i32, i32, i32, i32) -> i32
%c42_i32_3 = arith.constant 42 : i32
// CHECK-NEXT: %[[O1:.*]] = "test.op_commutative"(%arg0, %arg1, %c42_i32, %c42_i32) : (i32, i32, i32, i32) -> i32
%z = "test.op_commutative"(%arg0, %c42_i32_3, %c42_i32_2, %arg1): (i32, i32, i32, i32) -> i32
// CHECK-NEXT: return %[[O0]], %[[O1]]
return %y, %z: i32, i32
}
// CHECK-LABEL: test_dialect_canonicalizer
func.func @test_dialect_canonicalizer() -> (i32) {
%0 = "test.dialect_canonicalizable"() : () -> (i32)
// CHECK: %[[CST:.*]] = arith.constant 42 : i32
// CHECK: return %[[CST]]
return %0 : i32
}
// Check that the option to control region simplification actually works
// CHECK-LABEL: test_region_simplify
func.func @test_region_simplify() {
// CHECK-NEXT: return
// NO-RS-NEXT: ^bb1
// NO-RS-NEXT: return
// CHECK-NEXT: }
return
^bb1:
return
}