diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp --- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp +++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp @@ -677,9 +677,12 @@ if (auto sizeInterface = dyn_cast_or_null(definingOp)) { - assert(sizeInterface.isDynamicSize(unsignedIndex) && - "Expected dynamic subview size"); - return sizeInterface.getDynamicSize(unsignedIndex); + int nthDynamicIndex = 0; + for (unsigned idx = 0; idx <= unsignedIndex; ++idx) + if (memrefType.isDynamicDim(idx)) + ++nthDynamicIndex; + assert(nthDynamicIndex > 0 && "Expected at least one dynamic index"); + return sizeInterface.sizes()[nthDynamicIndex - 1]; } // dim(memrefcast) -> dim diff --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp --- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp +++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp @@ -274,10 +274,14 @@ // The size at the given index is now known to be a dynamic size. unsigned unsignedIndex = index.getValue().getZExtValue(); - if (auto sliceOp = dyn_cast_or_null(definingOp)) { - assert(sliceOp.isDynamicSize(unsignedIndex) && - "Expected dynamic slice size"); - return sliceOp.getDynamicSize(unsignedIndex); + if (auto sizeInterface = + dyn_cast_or_null(definingOp)) { + int nthDynamicIndex = 0; + for (unsigned idx = 0; idx <= unsignedIndex; ++idx) + if (tensorType.isDynamicDim(idx)) + ++nthDynamicIndex; + assert(nthDynamicIndex > 0 && "Expected at least one dynamic index"); + return sizeInterface.sizes()[nthDynamicIndex - 1]; } // dim(cast) -> dim diff --git a/mlir/test/Dialect/MemRef/canonicalize.mlir b/mlir/test/Dialect/MemRef/canonicalize.mlir --- a/mlir/test/Dialect/MemRef/canonicalize.mlir +++ b/mlir/test/Dialect/MemRef/canonicalize.mlir @@ -387,11 +387,32 @@ } // ----- + // CHECK-LABEL: func @allocator // CHECK: %[[alloc:.+]] = memref.alloc // CHECK: memref.store %[[alloc:.+]], %arg0 func @allocator(%arg0 : memref>, %arg1 : index) { %0 = memref.alloc(%arg1) : memref memref.store %0, %arg0[] : memref> - return + return +} + +// ----- + +#map0 = affine_map<(d0, d1)[s0, s1, s2] -> (d0 * s1 + s0 + d1 * s2)> + +// CHECK-LABEL: func @rank_reducing_subview_dim +// CHECK-SAME: %[[IDX_0:[0-9a-zA-Z]*]]: index +// CHECK-SAME: %[[IDX_1:[0-9a-zA-Z]*]]: index +func @rank_reducing_subview_dim(%arg0 : memref, %arg1 : index, + %arg2 : index) -> index +{ + %c0 = constant 0 : index + %c1 = constant 1 : index + %c4 = constant 4 : index + %0 = memref.subview %arg0[%c0, %arg1, %c1] [%c4, 1, %arg2] [%c1, %c1, %c1] : memref to memref + %1 = memref.dim %0, %c1 : memref + + // CHECK-NEXT: return %[[IDX_1]] : index + return %1 : index } diff --git a/mlir/test/Dialect/Tensor/canonicalize.mlir b/mlir/test/Dialect/Tensor/canonicalize.mlir --- a/mlir/test/Dialect/Tensor/canonicalize.mlir +++ b/mlir/test/Dialect/Tensor/canonicalize.mlir @@ -517,3 +517,21 @@ %2 = tensor.dim %0, %c1 : tensor return %1, %2: index, index } + +// ----- + +// CHECK-LABEL: func @rank_reducing_subview_dim +// CHECK-SAME: %[[IDX_0:[0-9a-zA-Z]*]]: index +// CHECK-SAME: %[[IDX_1:[0-9a-zA-Z]*]]: index +func @rank_reducing_subview_dim(%arg0 : tensor, %arg1 : index, + %arg2 : index) -> index +{ + %c0 = constant 0 : index + %c1 = constant 1 : index + %c4 = constant 4 : index + %0 = tensor.extract_slice %arg0[%c0, %arg1, %c1] [%c4, 1, %arg2] [%c1, %c1, %c1] : tensor to tensor + %1 = tensor.dim %0, %c1 : tensor + + // CHECK-NEXT: return %[[IDX_1]] : index + return %1 : index +}