NFC: Clean up constant fold tests

Use variable captures to make constant folding tests less sensitive to printer/parser implementation details.

See guidelines at https://github.com/tensorflow/mlir/blob/master/g3doc/TestingGuide.md

PiperOrigin-RevId: 268780812
This commit is contained in:
Geoffrey Martin-Noble 2019-09-12 15:29:59 -07:00 committed by A. Unique TensorFlower
parent a84bc68acc
commit f39a599e46
1 changed files with 86 additions and 85 deletions

View File

@ -2,9 +2,10 @@
// -----
// CHECK-LABEL: @test(%arg0: memref<f32>) {
func @test(%p : memref<f32>) {
// CHECK: %cst = constant 6.000000e+00 : f32
// CHECK-LABEL: @affine_for
// CHECK-SAME: [[ARG:%[a-zA-Z0-9]+]]
func @affine_for(%p : memref<f32>) {
// CHECK: [[C:%.+]] = constant 6.{{0*}}e+00 : f32
affine.for %arg1 = 0 to 128 {
affine.for %arg2 = 0 to 8 { // CHECK: affine.for %{{.*}} = 0 to 8 {
%0 = constant 4.5 : f32
@ -12,7 +13,7 @@ func @test(%p : memref<f32>) {
%2 = addf %0, %1 : f32
// CHECK-NEXT: store %cst, %arg0[]
// CHECK-NEXT: store [[C]], [[ARG]][]
store %2, %p[] : memref<f32>
}
}
@ -26,10 +27,10 @@ func @simple_addf() -> f32 {
%0 = constant 4.5 : f32
%1 = constant 1.5 : f32
// CHECK-NEXT: %cst = constant 6.000000e+00 : f32
// CHECK-NEXT: [[C:%.+]] = constant 6.{{0*}}e+00 : f32
%2 = addf %0, %1 : f32
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : f32
}
@ -40,10 +41,10 @@ func @addf_splat_tensor() -> tensor<4xf32> {
%0 = constant dense<4.5> : tensor<4xf32>
%1 = constant dense<1.5> : tensor<4xf32>
// CHECK-NEXT: %cst = constant dense<6.000000e+00> : tensor<4xf32>
// CHECK-NEXT: [[C:%.+]] = constant dense<6.{{0*}}e+00> : tensor<4xf32>
%2 = addf %0, %1 : tensor<4xf32>
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : tensor<4xf32>
}
@ -54,10 +55,10 @@ func @simple_addi() -> i32 {
%0 = constant 1 : i32
%1 = constant 5 : i32
// CHECK-NEXT: %c6_i32 = constant 6 : i32
// CHECK-NEXT: [[C:%.+]] = constant 6 : i32
%2 = addi %0, %1 : i32
// CHECK-NEXT: return %c6_i32
// CHECK-NEXT: return [[C]]
return %2 : i32
}
@ -68,10 +69,10 @@ func @addi_splat_vector() -> vector<8xi32> {
%0 = constant dense<1> : vector<8xi32>
%1 = constant dense<5> : vector<8xi32>
// CHECK-NEXT: %cst = constant dense<6> : vector<8xi32>
// CHECK-NEXT: [[C:%.+]] = constant dense<6> : vector<8xi32>
%2 = addi %0, %1 : vector<8xi32>
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : vector<8xi32>
}
@ -82,10 +83,10 @@ func @simple_subf() -> f32 {
%0 = constant 4.5 : f32
%1 = constant 1.5 : f32
// CHECK-NEXT: %cst = constant 3.000000e+00 : f32
// CHECK-NEXT: [[C:%.+]] = constant 3.{{0*}}e+00 : f32
%2 = subf %0, %1 : f32
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : f32
}
@ -96,10 +97,10 @@ func @subf_splat_vector() -> vector<4xf32> {
%0 = constant dense<4.5> : vector<4xf32>
%1 = constant dense<1.5> : vector<4xf32>
// CHECK-NEXT: %cst = constant dense<3.000000e+00> : vector<4xf32>
// CHECK-NEXT: [[C:%.+]] = constant dense<3.{{0*}}e+00> : vector<4xf32>
%2 = subf %0, %1 : vector<4xf32>
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : vector<4xf32>
}
@ -110,10 +111,10 @@ func @simple_subi() -> i32 {
%0 = constant 4 : i32
%1 = constant 1 : i32
// CHECK-NEXT: %c3_i32 = constant 3 : i32
// CHECK-NEXT:[[C3:%.+]] = constant 3 : i32
%2 = subi %0, %1 : i32
// CHECK-NEXT: return %c3_i32
// CHECK-NEXT: return [[C3]]
return %2 : i32
}
@ -124,10 +125,10 @@ func @subi_splat_tensor() -> tensor<4xi32> {
%0 = constant dense<4> : tensor<4xi32>
%1 = constant dense<1> : tensor<4xi32>
// CHECK-NEXT: %cst = constant dense<3> : tensor<4xi32>
// CHECK-NEXT: [[C:%.+]] = constant dense<3> : tensor<4xi32>
%2 = subi %0, %1 : tensor<4xi32>
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : tensor<4xi32>
}
@ -139,17 +140,17 @@ func @affine_apply(%variable : index) -> (index, index, index) {
%c211 = constant 211 : index
%N = constant 1075 : index
// CHECK: %c1159 = constant 1159 : index
// CHECK: %c1152 = constant 1152 : index
// CHECK:[[C1159:%.+]] = constant 1159 : index
// CHECK:[[C1152:%.+]] = constant 1152 : index
%x0 = affine.apply (d0, d1)[S0] -> ( (d0 + 128 * S0) floordiv 128 + d1 mod 128)
(%c177, %c211)[%N]
%x1 = affine.apply (d0, d1)[S0] -> (128 * (S0 ceildiv 128))
(%c177, %c211)[%N]
// CHECK: %c42 = constant 42 : index
// CHECK:[[C42:%.+]] = constant 42 : index
%y = affine.apply (d0) -> (42) (%variable)
// CHECK: return %c1159, %c1152, %c42
// CHECK: return [[C1159]], [[C1152]], [[C42]]
return %x0, %x1, %y : index, index, index
}
@ -160,10 +161,10 @@ func @simple_mulf() -> f32 {
%0 = constant 4.5 : f32
%1 = constant 1.5 : f32
// CHECK-NEXT: %cst = constant 6.750000e+00 : f32
// CHECK-NEXT: [[C:%.+]] = constant 6.75{{0*}}e+00 : f32
%2 = mulf %0, %1 : f32
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : f32
}
@ -174,10 +175,10 @@ func @mulf_splat_tensor() -> tensor<4xf32> {
%0 = constant dense<4.5> : tensor<4xf32>
%1 = constant dense<1.5> : tensor<4xf32>
// CHECK-NEXT: %cst = constant dense<6.750000e+00> : tensor<4xf32>
// CHECK-NEXT: [[C:%.+]] = constant dense<6.75{{0*}}e+00> : tensor<4xf32>
%2 = mulf %0, %1 : tensor<4xf32>
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : tensor<4xf32>
}
@ -188,15 +189,15 @@ func @simple_divis() -> (i32, i32) {
%0 = constant 6 : i32
%1 = constant 2 : i32
// CHECK-NEXT: %c3_i32 = constant 3 : i32
// CHECK-NEXT:[[C3:%.+]] = constant 3 : i32
%2 = divis %0, %1 : i32
%3 = constant -2 : i32
// CHECK-NEXT: %c-3_i32 = constant -3 : i32
// CHECK-NEXT: [[CM3:%.+]] = constant -3 : i32
%4 = divis %0, %3 : i32
// CHECK-NEXT: return %c3_i32, %c-3_i32 : i32, i32
// CHECK-NEXT: return [[C3]], [[CM3]]
return %2, %4 : i32, i32
}
@ -207,16 +208,16 @@ func @simple_diviu() -> (i32, i32) {
%0 = constant 6 : i32
%1 = constant 2 : i32
// CHECK-NEXT: %c3_i32 = constant 3 : i32
// CHECK-NEXT:[[C3:%.+]] = constant 3 : i32
%2 = diviu %0, %1 : i32
%3 = constant -2 : i32
// Unsigned division interprets -2 as 2^32-2, so the result is 0.
// CHECK-NEXT: %c0_i32 = constant 0 : i32
// CHECK-NEXT:[[C0:%.+]] = constant 0 : i32
%4 = diviu %0, %3 : i32
// CHECK-NEXT: return %c3_i32, %c0_i32 : i32, i32
// CHECK-NEXT: return [[C3]], [[C0]]
return %2, %4 : i32, i32
}
@ -229,13 +230,13 @@ func @simple_remis(%a : i32) -> (i32, i32, i32) {
%2 = constant 1 : i32
%3 = constant -2 : i32
// CHECK-NEXT: %c1_i32 = constant 1 : i32
// CHECK-NEXT:[[C1:%.+]] = constant 1 : i32
%4 = remis %0, %1 : i32
%5 = remis %0, %3 : i32
// CHECK-NEXT: %c0_i32 = constant 0 : i32
// CHECK-NEXT:[[C0:%.+]] = constant 0 : i32
%6 = remis %a, %2 : i32
// CHECK-NEXT: return %c1_i32, %c1_i32, %c0_i32 : i32, i32, i32
// CHECK-NEXT: return [[C1]], [[C1]], [[C0]] : i32, i32, i32
return %4, %5, %6 : i32, i32, i32
}
@ -248,14 +249,14 @@ func @simple_remiu(%a : i32) -> (i32, i32, i32) {
%2 = constant 1 : i32
%3 = constant -2 : i32
// CHECK-DAG: %c1_i32 = constant 1 : i32
// CHECK-DAG:[[C1:%.+]] = constant 1 : i32
%4 = remiu %0, %1 : i32
// CHECK-DAG: %c5_i32 = constant 5 : i32
// CHECK-DAG:[[C5:%.+]] = constant 5 : i32
%5 = remiu %0, %3 : i32
// CHECK-DAG: %c0_i32 = constant 0 : i32
// CHECK-DAG:[[C0:%.+]] = constant 0 : i32
%6 = remiu %a, %2 : i32
// CHECK-NEXT: return %c1_i32, %c5_i32, %c0_i32 : i32, i32, i32
// CHECK-NEXT: return [[C1]], [[C5]], [[C0]] : i32, i32, i32
return %4, %5, %6 : i32, i32, i32
}
@ -266,10 +267,10 @@ func @muli() -> i32 {
%0 = constant 4 : i32
%1 = constant 2 : i32
// CHECK-NEXT: %c8_i32 = constant 8 : i32
// CHECK-NEXT:[[C8:%.+]] = constant 8 : i32
%2 = muli %0, %1 : i32
// CHECK-NEXT: return %c8_i32
// CHECK-NEXT: return [[C8]]
return %2 : i32
}
@ -280,20 +281,20 @@ func @muli_splat_vector() -> vector<4xi32> {
%0 = constant dense<4> : vector<4xi32>
%1 = constant dense<2> : vector<4xi32>
// CHECK-NEXT: %cst = constant dense<8> : vector<4xi32>
// CHECK-NEXT: [[C:%.+]] = constant dense<8> : vector<4xi32>
%2 = muli %0, %1 : vector<4xi32>
// CHECK-NEXT: return %cst
// CHECK-NEXT: return [[C]]
return %2 : vector<4xi32>
}
// CHECK-LABEL: func @dim
func @dim(%x : tensor<8x4xf32>) -> index {
// CHECK: %c4 = constant 4 : index
// CHECK:[[C4:%.+]] = constant 4 : index
%0 = dim %x, 1 : tensor<8x4xf32>
// CHECK-NEXT: return %c4
// CHECK-NEXT: return [[C4]]
return %0 : index
}
@ -303,27 +304,27 @@ func @dim(%x : tensor<8x4xf32>) -> index {
func @cmpi() -> (i1, i1, i1, i1, i1, i1, i1, i1, i1, i1) {
%c42 = constant 42 : i32
%cm1 = constant -1 : i32
// CHECK-DAG: %false = constant 0 : i1
// CHECK-DAG: %true = constant 1 : i1
// CHECK-NEXT: return %false,
// CHECK-DAG: [[F:%.+]] = constant 0 : i1
// CHECK-DAG: [[T:%.+]] = constant 1 : i1
// CHECK-NEXT: return [[F]],
%0 = cmpi "eq", %c42, %cm1 : i32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%1 = cmpi "ne", %c42, %cm1 : i32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%2 = cmpi "slt", %c42, %cm1 : i32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%3 = cmpi "sle", %c42, %cm1 : i32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%4 = cmpi "sgt", %c42, %cm1 : i32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%5 = cmpi "sge", %c42, %cm1 : i32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%6 = cmpi "ult", %c42, %cm1 : i32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%7 = cmpi "ule", %c42, %cm1 : i32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%8 = cmpi "ugt", %c42, %cm1 : i32
// CHECK-SAME: %false
// CHECK-SAME: [[F]]
%9 = cmpi "uge", %c42, %cm1 : i32
return %0, %1, %2, %3, %4, %5, %6, %7, %8, %9 : i1, i1, i1, i1, i1, i1, i1, i1, i1, i1
}
@ -334,39 +335,39 @@ func @cmpi() -> (i1, i1, i1, i1, i1, i1, i1, i1, i1, i1) {
func @cmpf_normal_numbers() -> (i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1) {
%c42 = constant 42. : f32
%cm1 = constant -1. : f32
// CHECK-DAG: %false = constant 0 : i1
// CHECK-DAG: %true = constant 1 : i1
// CHECK-NEXT: return %false,
// CHECK-DAG: [[F:%.+]] = constant 0 : i1
// CHECK-DAG: [[T:%.+]] = constant 1 : i1
// CHECK-NEXT: return [[F]],
%0 = cmpf "false", %c42, %cm1 : f32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%1 = cmpf "oeq", %c42, %cm1 : f32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%2 = cmpf "ogt", %c42, %cm1 : f32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%3 = cmpf "oge", %c42, %cm1 : f32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%4 = cmpf "olt", %c42, %cm1 : f32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%5 = cmpf "ole", %c42, %cm1 : f32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%6 = cmpf "one", %c42, %cm1 : f32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%7 = cmpf "ord", %c42, %cm1 : f32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%8 = cmpf "ueq", %c42, %cm1 : f32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%9 = cmpf "ugt", %c42, %cm1 : f32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%10 = cmpf "uge", %c42, %cm1 : f32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%11 = cmpf "ult", %c42, %cm1 : f32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%12 = cmpf "ule", %c42, %cm1 : f32
// CHECK-SAME: %true,
// CHECK-SAME: [[T]],
%13 = cmpf "une", %c42, %cm1 : f32
// CHECK-SAME: %false,
// CHECK-SAME: [[F]],
%14 = cmpf "uno", %c42, %cm1 : f32
// CHECK-SAME: %true
// CHECK-SAME: [[T]]
%15 = cmpf "true", %c42, %cm1 : f32
return %0, %1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15 : i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1
}
@ -398,26 +399,26 @@ func @fold_extract_element(%arg0 : index) -> (f32, f16, f16, i32) {
%const_3 = constant 3 : index
// Fold an extract into a splat.
// CHECK-NEXT: {{.*}} = constant 4.500000e+00 : f32
%0 = constant dense<4.5> : tensor<4xf32>
// CHECK-NEXT: [[C4:%.+]] = constant 4.{{0*}}e+00 : f32
%0 = constant dense<4.0> : tensor<4xf32>
%ext_1 = extract_element %0[%arg0] : tensor<4xf32>
// Fold an extract into a sparse with a sparse index.
// CHECK-NEXT: {{.*}} = constant -2.000000e+00 : f16
// CHECK-NEXT: [[CM2:%.+]] = constant -2.{{0*}}e+00 : f16
%1 = constant sparse<[[0, 0, 0], [1, 1, 1]], [-5.0, -2.0]> : vector<4x4x4xf16>
%ext_2 = extract_element %1[%const_1, %const_1, %const_1] : vector<4x4x4xf16>
// Fold an extract into a sparse with a non sparse index.
// CHECK-NEXT: {{.*}} = constant 0.000000e+00 : f16
// CHECK-NEXT: [[C0:%.+]] = constant 0.{{0*}}e+00 : f16
%2 = constant sparse<[[1, 1, 1]], [-2.0]> : vector<1x1x1xf16>
%ext_3 = extract_element %2[%const_0, %const_0, %const_0] : vector<1x1x1xf16>
// Fold an extract into a dense tensor.
// CHECK-NEXT: {{.*}} = constant 64 : i32
// CHECK-NEXT: [[C64:%.+]] = constant 64 : i32
%3 = constant dense<[[[1, -2, 1, 36]], [[0, 2, -1, 64]]]> : tensor<2x1x4xi32>
%ext_4 = extract_element %3[%const_1, %const_0, %const_3] : tensor<2x1x4xi32>
// CHECK-NEXT: return
// CHECK-NEXT: return [[C4]], [[CM2]], [[C0]], [[C64]]
return %ext_1, %ext_2, %ext_3, %ext_4 : f32, f16, f16, i32
}
@ -428,10 +429,10 @@ func @fold_rank() -> (index) {
%const_0 = constant dense<[[[1, -2, 1, 36]], [[0, 2, -1, 64]]]> : tensor<2x1x4xi32>
// Fold a rank into a constant
// CHECK-NEXT: {{.*}} = constant 3 : index
// CHECK-NEXT: [[C3:%.+]] = constant 3 : index
%rank_0 = rank %const_0 : tensor<2x1x4xi32>
// CHECK-NEXT: return
// CHECK-NEXT: return [[C3]]
return %rank_0 : index
}