Skip to content

Commit 208560e

Browse files
lhutton1Jaddyen
authored andcommitted
[mlir][tosa] Allow creation of reshape with unranked output (llvm#140617)
This commit allows reshape to be created with an unranked output, allowing it to be inferred by the shape inference pass.
1 parent 16721a2 commit 208560e

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ def Tosa_ReshapeOp : Tosa_InferTensorTypeOp<"reshape"> {
19591959
);
19601960

19611961
let results = (outs
1962-
Tosa_RankedTensor:$output
1962+
Tosa_Tensor:$output
19631963
);
19641964

19651965
list<Availability> availability = [

mlir/lib/Dialect/Tosa/IR/TosaOps.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,14 +2064,21 @@ llvm::LogicalResult tosa::ReshapeOp::verify() {
20642064
return failure();
20652065
}
20662066
TensorType inputType = getInput1().getType();
2067-
RankedTensorType outputType = getType();
20682067

20692068
SmallVector<int64_t> shapeValues;
20702069
if (!tosa::getConstShapeValues(getShape().getDefiningOp(), shapeValues)) {
20712070
// skip following checks if shape is not constant
20722071
return mlir::success();
20732072
}
20742073

2074+
int missingDims = llvm::count(shapeValues, -1);
2075+
if (missingDims > 1)
2076+
return emitOpError() << "expected at most one target dimension to be -1";
2077+
2078+
const auto outputType = dyn_cast<RankedTensorType>(getType());
2079+
if (!outputType)
2080+
return success();
2081+
20752082
if ((int64_t)shapeValues.size() != outputType.getRank())
20762083
return emitOpError() << "new shape does not match result rank";
20772084

@@ -2108,10 +2115,6 @@ llvm::LogicalResult tosa::ReshapeOp::verify() {
21082115
}
21092116
}
21102117

2111-
int missingDims = llvm::count(shapeValues, -1);
2112-
if (missingDims > 1)
2113-
return emitOpError() << "expected at most one target dimension to be -1";
2114-
21152118
return mlir::success();
21162119
}
21172120

mlir/test/Dialect/Tosa/ops.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@ func.func @test_reshape(%arg0: tensor<13x21x3xf32>) -> tensor<1x819xf32> {
643643
return %0 : tensor<1x819xf32>
644644
}
645645

646+
// -----
647+
// CHECK-LABEL: reshape_unranked_output
648+
func.func @test_reshape_unranked_output(%arg0: tensor<13x21x3xf32>) -> tensor<*xf32> {
649+
%1 = tosa.const_shape {values = dense<[21, 13, 3]> : tensor<3xindex>} : () -> !tosa.shape<3>
650+
%0 = tosa.reshape %arg0, %1 : (tensor<13x21x3xf32>, !tosa.shape<3>) -> tensor<*xf32>
651+
return %0 : tensor<*xf32>
652+
}
653+
646654
// -----
647655
// CHECK-LABEL: reverse
648656
func.func @test_reverse(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {

0 commit comments

Comments
 (0)