1// Verify the printed output can be parsed. 2// RUN: mlir-opt %s | mlir-opt | FileCheck %s 3// Verify the generic form can be parsed. 4// RUN: mlir-opt -mlir-print-op-generic %s | mlir-opt | FileCheck %s 5 6// CHECK-LABEL: shape_num_elements 7func.func @shape_num_elements(%shape : !shape.shape) -> !shape.size { 8 %init = shape.const_size 1 9 %num_elements = shape.reduce(%shape, %init) : !shape.shape -> !shape.size { 10 ^bb0(%index : index, %extent : !shape.size, %acc : !shape.size): 11 %acc_next = shape.mul %acc, %extent 12 : !shape.size, !shape.size -> !shape.size 13 shape.yield %acc_next : !shape.size 14 } 15 return %num_elements : !shape.size 16} 17 18// CHECK-LABEL: extent_tensor_num_elements 19func.func @extent_tensor_num_elements(%shape : tensor<?xindex>) -> index { 20 %init = arith.constant 1 : index 21 %num_elements = shape.reduce(%shape, %init) : tensor<?xindex> -> index { 22 ^bb0(%index : index, %extent : index, %acc : index): 23 %acc_next = shape.mul %acc, %extent : index, index -> index 24 shape.yield %acc_next : index 25 } 26 return %num_elements : index 27} 28 29func.func @test_shape_num_elements_unknown() { 30 %0 = "shape.unknown_shape"() : () -> !shape.shape 31 %1 = call @shape_num_elements(%0) : (!shape.shape) -> (!shape.size) 32 %2 = "shape.print"(%1) : (!shape.size) -> !shape.size 33 return 34} 35 36func.func @const_shape() { 37 %0 = shape.const_shape [1, 2, 3] : !shape.shape 38 %2 = shape.const_shape [4, 5, 6] : tensor<3xindex> 39 return 40} 41 42func.func @test_shape_num_elements_fixed() { 43 %0 = shape.const_shape [1, 57, 92] : !shape.shape 44 %1 = call @shape_num_elements(%0) : (!shape.shape) -> (!shape.size) 45 %3 = "shape.print"(%1) : (!shape.size) -> !shape.size 46 return 47} 48 49func.func @test_broadcast_fixed() { 50 %0 = shape.const_shape [10, 1, 57, 92] : !shape.shape 51 %1 = shape.const_shape [4, 57, 92] : !shape.shape 52 %2 = shape.broadcast %0, %1 : !shape.shape, !shape.shape -> !shape.shape 53 %3 = "shape.print"(%2) : (!shape.shape) -> !shape.shape 54 return 55} 56 57func.func @test_broadcast_extents() -> tensor<4xindex> { 58 %0 = shape.const_shape [10, 1, 57, 92] : tensor<4xindex> 59 %1 = shape.const_shape [4, 57, 92] : tensor<3xindex> 60 %2 = shape.broadcast %0, %1 : tensor<4xindex>, tensor<3xindex> -> tensor<4xindex> 61 return %2 : tensor<4xindex> 62} 63 64func.func @test_shape_any_fixed() { 65 %0 = shape.const_shape [4, 57, 92] : !shape.shape 66 %1 = shape.const_shape [4, 57, 92] : !shape.shape 67 %2 = "shape.meet"(%0, %1) : (!shape.shape, !shape.shape) -> !shape.shape 68 %3 = "shape.print"(%2) : (!shape.shape) -> !shape.shape 69 return 70} 71 72func.func @test_shape_any_unknown() { 73 %0 = shape.const_shape [4, -1, 92] : !shape.shape 74 %1 = shape.const_shape [-1, 57, 92] : !shape.shape 75 %2 = "shape.meet"(%0, %1) : (!shape.shape, !shape.shape) -> !shape.shape 76 %3 = "shape.print"(%2) : (!shape.shape) -> !shape.shape 77 return 78} 79 80func.func @test_shape_any_fixed_mismatch() { 81 %0 = shape.const_shape [4, 57, 92] : !shape.shape 82 %1 = shape.const_shape [2, 57, 92] : !shape.shape 83 %2 = "shape.meet"(%0, %1) : (!shape.shape, !shape.shape) -> !shape.shape 84 %3 = "shape.print"(%2) : (!shape.shape) -> !shape.shape 85 return 86} 87 88func.func @test_parse_const_shape() { 89 %0 = shape.const_shape [] : !shape.shape 90 %1 = shape.const_shape [1, 2, 3] : !shape.shape 91 %2 = shape.const_shape [1, 2, 3] : tensor<3xindex> 92 return 93} 94 95func.func @test_shape_of(%arg0: tensor<?xf32>) -> tensor<?xindex> { 96 %0 = shape.shape_of %arg0 : tensor<?xf32> -> tensor<?xindex> 97 return %0 : tensor<?xindex> 98} 99 100func.func @test_constraints() { 101 %0 = shape.const_shape [] : !shape.shape 102 %1 = shape.const_shape [1, 2, 3] : !shape.shape 103 %true = arith.constant true 104 %w0 = shape.cstr_broadcastable %0, %1 : !shape.shape, !shape.shape 105 %w1 = shape.cstr_eq %0, %1 : !shape.shape, !shape.shape 106 %w2 = shape.const_witness true 107 %w3 = shape.const_witness false 108 %w4 = shape.cstr_require %true, "msg" 109 %w_all = shape.assuming_all %w0, %w1, %w2, %w3, %w4 110 shape.assuming %w_all -> !shape.shape { 111 %2 = "shape.any"(%0, %1) : (!shape.shape, !shape.shape) -> !shape.shape 112 shape.assuming_yield %2 : !shape.shape 113 } 114 return 115} 116 117func.func @eq_on_extent_tensors(%lhs : tensor<?xindex>, 118 %rhs : tensor<?xindex>) { 119 %w0 = shape.cstr_eq %lhs, %rhs : tensor<?xindex>, tensor<?xindex> 120 return 121} 122 123func.func @broadcastable_on_extent_tensors(%lhs : tensor<?xindex>, 124 %rhs : tensor<?xindex>) { 125 %w0 = shape.cstr_broadcastable %lhs, %rhs : tensor<?xindex>, tensor<?xindex> 126 return 127} 128 129func.func @mul(%size_arg : !shape.size, %index_arg : index) { 130 %size_prod = shape.mul %size_arg, %size_arg 131 : !shape.size, !shape.size -> !shape.size 132 %index_prod = shape.mul %index_arg, %index_arg : index, index -> index 133 %mixed_prod = shape.mul %size_arg, %index_arg 134 : !shape.size, index -> !shape.size 135 return 136} 137 138func.func @div(%size_arg : !shape.size, %index_arg : index) { 139 %size_div = shape.div %size_arg, %size_arg 140 : !shape.size, !shape.size -> !shape.size 141 %index_div = shape.div %index_arg, %index_arg : index, index -> index 142 %mixed_div = shape.div %size_arg, %index_arg 143 : !shape.size, index -> !shape.size 144 return 145} 146 147func.func @add(%size_arg : !shape.size, %index_arg : index) { 148 %size_sum = shape.add %size_arg, %size_arg 149 : !shape.size, !shape.size -> !shape.size 150 %index_sum = shape.add %index_arg, %index_arg : index, index -> index 151 %mixed_sum = shape.add %size_arg, %index_arg 152 : !shape.size, index -> !shape.size 153 return 154} 155 156func.func @const_size() { 157 // CHECK: %c1 = shape.const_size 1 158 // CHECK: %c2 = shape.const_size 2 159 // CHECK: %c2_0 = shape.const_size 2 160 %0 = shape.const_size 1 161 %1 = shape.const_size 2 162 %2 = shape.const_size 2 163 return 164} 165 166func.func @test_to_extent_tensor(%arg: !shape.shape) -> tensor<3xindex> { 167 %0 = shape.to_extent_tensor %arg : !shape.shape -> tensor<3xindex> 168 return %0 : tensor<3xindex> 169} 170 171func.func @test_identity_to_extent_tensor(%arg: tensor<3xindex>) -> tensor<3xindex> { 172 %0 = shape.to_extent_tensor %arg : tensor<3xindex> -> tensor<3xindex> 173 return %0 : tensor<3xindex> 174} 175 176func.func @test_from_extent_tensor(%arg: tensor<?xindex>) -> !shape.shape { 177 %0 = shape.from_extent_tensor %arg : tensor<?xindex> 178 return %0 : !shape.shape 179} 180 181func.func @rank(%shape : !shape.shape) -> !shape.size { 182 %rank = shape.rank %shape : !shape.shape -> !shape.size 183 return %rank : !shape.size 184} 185 186func.func @rank_on_extent_tensor(%shape : tensor<?xindex>) -> index { 187 %rank = shape.rank %shape : tensor<?xindex> -> index 188 return %rank : index 189} 190 191func.func @shape_eq_on_shapes(%a : !shape.shape, %b : !shape.shape) -> i1 { 192 %result = shape.shape_eq %a, %b : !shape.shape, !shape.shape 193 return %result : i1 194} 195 196func.func @shape_eq_on_tensors(%a : tensor<?xindex>, %b : tensor<?xindex>) -> i1 { 197 %result = shape.shape_eq %a, %b : tensor<?xindex>, tensor<?xindex> 198 return %result : i1 199} 200 201func.func @shape_eq_on_mixed(%a : tensor<?xindex>, %b : !shape.shape) -> i1 { 202 %result = shape.shape_eq %a, %b : tensor<?xindex>, !shape.shape 203 return %result : i1 204} 205 206func.func @get_extent_on_shape(%arg : !shape.shape) -> !shape.size { 207 %c0 = shape.const_size 0 208 %result = shape.get_extent %arg, %c0 : 209 !shape.shape, !shape.size -> !shape.size 210 return %result : !shape.size 211} 212 213func.func @get_extent_on_extent_tensor(%arg : tensor<?xindex>) -> index { 214 %c0 = arith.constant 0 : index 215 %result = shape.get_extent %arg, %c0 : tensor<?xindex>, index -> index 216 return %result : index 217} 218 219func.func @get_extent_on_mixed_operands(%arg : tensor<?xindex>) -> !shape.size { 220 %c0 = shape.const_size 0 221 %result = shape.get_extent %arg, %c0 : tensor<?xindex>, !shape.size -> !shape.size 222 return %result : !shape.size 223} 224 225func.func @any() { 226 %0 = shape.const_shape [1, 2, 3] : !shape.shape 227 %1 = shape.const_shape [4, 5, 6] : !shape.shape 228 %2 = "shape.any"(%0, %1) : (!shape.shape, !shape.shape) -> !shape.shape 229 %3 = shape.const_shape [1, 2, 3] : tensor<3xindex> 230 %4 = shape.const_shape [4, 5, 6] : tensor<3xindex> 231 %5 = "shape.any"(%3, %4) : (tensor<3xindex>, tensor<3xindex>) -> tensor<3xindex> 232 return 233} 234 235func.func @num_elements_extent_tensor(%arg : tensor<?xindex>) -> index { 236 %result = shape.num_elements %arg : tensor<?xindex> -> index 237 return %result : index 238} 239 240func.func @num_elements_shape(%arg : !shape.shape) -> !shape.size { 241 %result = shape.num_elements %arg : !shape.shape -> !shape.size 242 return %result : !shape.size 243} 244 245// Testing invoking shape function from another. shape_equal_shapes is merely 246// a trivial helper function to invoke elsewhere. 247func.func @shape_equal_shapes(%a : !shape.value_shape, %b : !shape.value_shape) -> !shape.shape { 248 %0 = shape.shape_of %a : !shape.value_shape -> !shape.shape 249 %1 = shape.shape_of %b : !shape.value_shape -> !shape.shape 250 %2 = "shape.meet"(%0, %1) : (!shape.shape, !shape.shape) -> !shape.shape 251 return %2 : !shape.shape 252} 253func.func @shape_with_shape(%a : !shape.value_shape, %b : !shape.value_shape) -> !shape.shape { 254 %0 = shape.shape_of %a : !shape.value_shape -> !shape.shape 255 %1 = shape.with_shape %b, %0 : !shape.value_shape, !shape.shape 256 %2 = call @shape_equal_shapes(%a, %1) : (!shape.value_shape, !shape.value_shape) -> !shape.shape 257 return %2 : !shape.shape 258} 259 260func.func @any_on_shape(%a : !shape.shape, %b : !shape.shape, %c : !shape.shape) 261 -> !shape.shape { 262 %result = shape.any %a, %b, %c 263 : !shape.shape, !shape.shape, !shape.shape -> !shape.shape 264 return %result : !shape.shape 265} 266 267func.func @any_on_mixed(%a : tensor<?xindex>, 268 %b : tensor<?xindex>, 269 %c : !shape.shape) -> !shape.shape { 270 %result = shape.any %a, %b, %c 271 : tensor<?xindex>, tensor<?xindex>, !shape.shape -> !shape.shape 272 return %result : !shape.shape 273} 274 275func.func @any_on_extent_tensors(%a : tensor<?xindex>, 276 %b : tensor<?xindex>, 277 %c : tensor<?xindex>) -> tensor<?xindex> { 278 %result = shape.any %a, %b, %c 279 : tensor<?xindex>, tensor<?xindex>, tensor<?xindex> -> tensor<?xindex> 280 return %result : tensor<?xindex> 281} 282 283func.func @is_broadcastable_on_extent_tensors(%a : tensor<?xindex>, 284 %b : tensor<?xindex>) -> i1 { 285 %result = shape.is_broadcastable %a, %b 286 : tensor<?xindex>, tensor<?xindex> 287 return %result : i1 288} 289 290func.func @is_broadcastable_on_shapes(%a : !shape.shape, 291 %b : !shape.shape) -> i1 { 292 %result = shape.is_broadcastable %a, %b 293 : !shape.shape, !shape.shape 294 return %result : i1 295} 296 297func.func @shape_upper_bounded_by_constant(%a: !shape.shape) -> !shape.shape { 298 %0 = shape.const_shape [4, 57, 92] : !shape.shape 299 %1 = shape.max %a, %0 : !shape.shape, !shape.shape -> !shape.shape 300 %2 = shape.meet %0, %1, error="exceeded element-wise upper bound" : 301 !shape.shape, !shape.shape -> !shape.shape 302 return %2 : !shape.shape 303} 304 305func.func @shape_lower_bounded_by_constant(%a: !shape.shape) -> !shape.shape { 306 %0 = shape.const_shape [4, 57, 92] : !shape.shape 307 %1 = shape.min %a, %0 : !shape.shape, !shape.shape -> !shape.shape 308 %2 = shape.meet %0, %1, error="lower bound element-wise exceeded" : 309 !shape.shape, !shape.shape -> !shape.shape 310 return %2 : !shape.shape 311} 312 313func.func @size_upper_bounded_by_constant(%a: !shape.size) -> !shape.size { 314 %0 = shape.const_size 5 315 %1 = shape.max %a, %0 : !shape.size, !shape.size -> !shape.size 316 %2 = shape.meet %0, %1, error="exceeded element-wise upper bound" : 317 !shape.size, !shape.size -> !shape.size 318 return %2 : !shape.size 319} 320 321func.func @size_lower_bounded_by_constant(%a: !shape.size) -> !shape.size { 322 %0 = shape.const_size 9 323 %1 = shape.min %a, %0 : !shape.size, !shape.size -> !shape.size 324 %2 = shape.meet %0, %1, error="lower bound element-wise exceeded" : 325 !shape.size, !shape.size -> !shape.size 326 return %2 : !shape.size 327} 328