After different attempts I can’t get the Linalg’s SliceOp to work properly. The task is simple: I’ve to extract a slice from a multi-dimensional array (e.g. I have memref<5x3xi32> and I want to create a view over only one of the rows, let’s say the second one, which should thus result in a memref<3xi32>).
mlir::Value memref = builder.create<AllocaOp>(location, MemRefType::get({5, 3}, builder.getI32Type()));
mlir::Value c0 = builder.create<ConstantOp>(location, builder.getIndexAttr(0));
mlir::Value c3 = builder.create<ConstantOp>(location, builder.getIndexAttr(3));
mlir::Value c1 = builder.create<ConstantOp>(location, builder.getIndexAttr(1));
mlir::Value range = builder.create<linalg::RangeOp>(location, c0, c3, c1);
SmallVector<mlir::Value, 3> indexes;
indexes.push_back(c1);
indexes.push_back(range);
mlir::Value slice = builder.create<linalg::SliceOp>(location, memref, indexes);
The code generates the following IR:
%0 = alloca() : memref<5x3xi32>
%c0 = constant 0 : index
%c3 = constant 0 : index
%c1 = constant 0 : index
%3 = linalg.range %c0_7 : %c3 : %c1_8 : !linalg.range
%4 = linalg.slice %0[%c1, %3] : memref<5x3xi32>, index, !linalg.range, memref<?x?xi32, affine_map<(d0, d1) -> (d0 * 3 + d1)>>
I can’t understand the point of the error:
'linalg.slice' op expected rank of the view(2) to be the number of ranges(1)
Indeed the number of the ranges is lower than the rank of the original view, given that I want to reduce its rank. This should be legit also accorging also to the operation examples.
The only way I found to avoid the error is to manually set the result type of the operation, but I don’t think it should be necessary at all:
mlir::Value slice = builder.create<linalg::SliceOp>(location, MemRefType::get({ 3 }, builder.getI32Type()), memref, indexes);