@@ -1964,6 +1964,88 @@ func.func @dont_fold_pad_chains(%arg0: tensor<64x64xf32>,
19641964
19651965// -----
19661966
1967+ // CHECK-LABEL: func @merge_constant_padding
1968+ // CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: tensor<2x3xf32>
1969+ // CHECK-SAME: %[[PADVAL:[A-Za-z0-9]+]]: f32
1970+ // CHECK: %[[PAD:.+]] = tensor.pad %[[ARG0]] low[1, 3] high[4, 2]
1971+ // CHECK: tensor.yield %[[PADVAL]]
1972+ // CHECK: return %[[PAD]]
1973+ func.func @merge_constant_padding (%arg0: tensor <2 x3 xf32 >, %pad_value: f32 ) -> tensor <7 x8 xf32 > {
1974+ %pad0 = tensor.pad %arg0 low [1 , 1 ] high [1 , 0 ] {
1975+ ^bb0 (%b0: index , %b1 : index ):
1976+ tensor.yield %pad_value : f32
1977+ } : tensor <2 x3 xf32 > to tensor <4 x4 xf32 >
1978+ %pad1 = tensor.pad %pad0 low [0 , 2 ] high [3 , 2 ] {
1979+ ^bb0 (%b2: index , %b3 : index ):
1980+ tensor.yield %pad_value : f32
1981+ } : tensor <4 x4 xf32 > to tensor <7 x8 xf32 >
1982+ return %pad1 : tensor <7 x8 xf32 >
1983+ }
1984+
1985+ // -----
1986+
1987+ // CHECK: #[[$MAP:.*]] = affine_map<()[s0] -> (s0 + 1)>
1988+ // CHECK-LABEL: func @merge_constant_padding_dynamic
1989+ // CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: tensor<?x?xf32>
1990+ // CHECK-SAME: %[[IDX:[A-Za-z0-9]+]]: index
1991+ // CHECK-SAME: %[[PADVAL:[A-Za-z0-9]+]]: f32
1992+ // CHECK: %[[HIGH:.+]] = affine.apply #[[$MAP]]()[%[[IDX]]]
1993+ // CHECK: %[[PAD:.+]] = tensor.pad %[[ARG0]] low[%[[IDX]], 3] high[%[[HIGH]], 2]
1994+ // CHECK: tensor.yield %[[PADVAL]]
1995+ // CHECK: return %[[PAD]]
1996+ func.func @merge_constant_padding_dynamic (%arg0: tensor <?x?xf32 >, %idx: index , %pad_value: f32 ) -> tensor <?x?xf32 > {
1997+ %pad0 = tensor.pad %arg0 low [%idx , 1 ] high [1 , 0 ] {
1998+ ^bb0 (%b0: index , %b1 : index ):
1999+ tensor.yield %pad_value : f32
2000+ } : tensor <?x?xf32 > to tensor <?x?xf32 >
2001+ %pad1 = tensor.pad %pad0 low [0 , 2 ] high [%idx , 2 ] {
2002+ ^bb0 (%b2: index , %b3 : index ):
2003+ tensor.yield %pad_value : f32
2004+ } : tensor <?x?xf32 > to tensor <?x?xf32 >
2005+ return %pad1 : tensor <?x?xf32 >
2006+ }
2007+
2008+ // -----
2009+
2010+ // Verify that folding does not happen if it would drop a nofold attribute
2011+ // CHECK-LABEL: func @dont_merge_constant_padding_nofold
2012+ // CHECK: tensor.pad {{.*}} nofold
2013+ // CHECK: tensor.pad
2014+ func.func @dont_merge_constant_padding_nofold (%arg0: tensor <2 x3 xf32 >, %pad_value: f32 ) -> tensor <7 x8 xf32 > {
2015+ %pad0 = tensor.pad %arg0 nofold low [1 , 1 ] high [1 , 0 ] {
2016+ ^bb0 (%b0: index , %b1 : index ):
2017+ tensor.yield %pad_value : f32
2018+ } : tensor <2 x3 xf32 > to tensor <4 x4 xf32 >
2019+ %pad1 = tensor.pad %pad0 low [0 , 2 ] high [3 , 2 ] {
2020+ ^bb0 (%b2: index , %b3 : index ):
2021+ tensor.yield %pad_value : f32
2022+ } : tensor <4 x4 xf32 > to tensor <7 x8 xf32 >
2023+ return %pad1 : tensor <7 x8 xf32 >
2024+ }
2025+
2026+ // -----
2027+
2028+ // Verify that folding does not happen if it would drop a nofold attribute
2029+ // CHECK-LABEL: func @dont_merge_constant_padding_different_vals
2030+ // CHECK: tensor.pad
2031+ // CHECK: tensor.pad
2032+ func.func @dont_merge_constant_padding_different_vals (
2033+ %arg0: tensor <2 x3 xf32 >,
2034+ %pad_value0: f32 ,
2035+ %pad_value1: f32 ) -> tensor <7 x8 xf32 > {
2036+ %pad0 = tensor.pad %arg0 low [1 , 1 ] high [1 , 0 ] {
2037+ ^bb0 (%b0: index , %b1 : index ):
2038+ tensor.yield %pad_value0 : f32
2039+ } : tensor <2 x3 xf32 > to tensor <4 x4 xf32 >
2040+ %pad1 = tensor.pad %pad0 low [0 , 2 ] high [3 , 2 ] {
2041+ ^bb0 (%b2: index , %b3 : index ):
2042+ tensor.yield %pad_value1 : f32
2043+ } : tensor <4 x4 xf32 > to tensor <7 x8 xf32 >
2044+ return %pad1 : tensor <7 x8 xf32 >
2045+ }
2046+
2047+ // -----
2048+
19672049// CHECK-LABEL: func @fold_collapse_shape_from_elements
19682050func.func @fold_collapse_shape_from_elements (%arg0: i32 ) -> tensor <i32 > {
19692051 // CHECK: %[[FROM:.+]] = tensor.from_elements %arg0 : tensor<i32>
0 commit comments