[mlir][standard] Fix parsing of scalar subview and canonicalize

Parsing of a scalar subview did not create the required static_offsets attribute.
This also adds support for folding scalar subviews away.

Differential Revision: https://reviews.llvm.org/D89467
This commit is contained in:
Stephan Herhut 2020-10-15 16:27:31 +02:00
parent c66e091023
commit 307124535f
4 changed files with 23 additions and 1 deletions

View File

@ -3097,6 +3097,7 @@ def SubViewOp : BaseOpWithOffsetSizesAndStrides<
}];
let hasCanonicalizer = 1;
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//

View File

@ -2531,8 +2531,10 @@ parseListOfOperandsOrIntegers(OpAsmParser &parser, OperationState &result,
if (failed(parser.parseLSquare()))
return failure();
// 0-D.
if (succeeded(parser.parseOptionalRSquare()))
if (succeeded(parser.parseOptionalRSquare())) {
result.addAttribute(attrName, parser.getBuilder().getArrayAttr({}));
return success();
}
SmallVector<int64_t, 4> attrVals;
while (true) {
@ -3333,6 +3335,13 @@ void SubViewOp::getCanonicalizationPatterns(OwningRewritePatternList &results,
SubViewOpMemRefCastFolder>(context);
}
OpFoldResult SubViewOp::fold(ArrayRef<Attribute> operands) {
if (getResultRank() == 0 && getSourceRank() == 0)
return getViewSource();
return {};
}
//===----------------------------------------------------------------------===//
// SubTensorOp
//===----------------------------------------------------------------------===//

View File

@ -827,6 +827,9 @@ func @memref_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
%21 = subview %20[0, 0, 0][1, 16, 4][1, 1, 1] : memref<8x16x4xf32> to memref<16x4xf32>
%22 = subview %20[3, 4, 2][1, 6, 3][1, 1, 1] : memref<8x16x4xf32> to memref<6x3xf32, offset: 210, strides: [4, 1]>
%23 = alloc() : memref<f32>
%78 = subview %23[] [] [] : memref<f32> to memref<f32>
return
}

View File

@ -744,3 +744,12 @@ func @splat_fold() -> (vector<4xf32>, tensor<4xf32>) {
// CHECK-NEXT: [[T:%.*]] = constant dense<1.000000e+00> : tensor<4xf32>
// CHECK-NEXT: return [[V]], [[T]] : vector<4xf32>, tensor<4xf32>
}
// -----
// CHECK-LABEL: func @subview_scalar_fold
func @subview_scalar_fold(%arg0: memref<f32>) -> memref<f32> {
// CHECK-NOT: subview
%c = subview %arg0[] [] [] : memref<f32> to memref<f32>
return %c : memref<f32>
}