Skip to content

Commit 90adc62

Browse files
authored
Merge pull request #509 from Xilinx/bump_to_9190e1c0
[AutoBump] Merge with fixes of 9190e1c (Jan 09) (39) [Only tested MLIR]
2 parents b93bd73 + ae81078 commit 90adc62

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -650,30 +650,33 @@ static Value createLinalgBodyCalculationForElementwiseOp(
650650
static Value expandRank(PatternRewriter &rewriter, Location loc, Value tensor,
651651
int64_t rank) {
652652
// No need to expand if we are already at the desired rank
653-
auto shapedType = dyn_cast<ShapedType>(tensor.getType());
654-
assert(shapedType && shapedType.hasRank() && "expected a ranked shaped type");
655-
int64_t numExtraDims = rank - shapedType.getRank();
653+
auto tensorType = dyn_cast<RankedTensorType>(tensor.getType());
654+
assert(tensorType && "expected a ranked tensor type");
655+
int64_t tensorRank = tensorType.getRank();
656+
int64_t numExtraDims = rank - tensorRank;
656657
assert(numExtraDims >= 0 && "cannot expand tensor to a lower rank");
657658
if (!numExtraDims)
658659
return tensor;
659660

660661
// Compute reassociation indices
661-
SmallVector<SmallVector<int64_t, 2>> reassociationIndices(
662-
shapedType.getRank());
662+
SmallVector<ReassociationIndices> reassociationIndices(tensorRank);
663663
int64_t index = 0;
664-
for (index = 0; index <= numExtraDims; index++)
665-
reassociationIndices[0].push_back(index);
666-
for (size_t position = 1; position < reassociationIndices.size(); position++)
667-
reassociationIndices[position].push_back(index++);
664+
if (tensorRank != 0) {
665+
for (index = 0; index <= numExtraDims; index++)
666+
reassociationIndices[0].push_back(index);
667+
for (size_t position = 1; position < reassociationIndices.size();
668+
position++)
669+
reassociationIndices[position].push_back(index++);
670+
}
668671

669672
// Compute result type
670673
SmallVector<int64_t> resultShape;
671674
for (index = 0; index < numExtraDims; index++)
672675
resultShape.push_back(1);
673-
for (auto size : shapedType.getShape())
676+
for (auto size : tensorType.getShape())
674677
resultShape.push_back(size);
675678
auto resultType =
676-
RankedTensorType::get(resultShape, shapedType.getElementType());
679+
RankedTensorType::get(resultShape, tensorType.getElementType());
677680

678681
// Emit 'tensor.expand_shape' op
679682
return rewriter.create<tensor::ExpandShapeOp>(loc, resultType, tensor,

mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ func.func @test_add_uint8(%arg0: tensor<ui8>, %arg1: tensor<ui8>) -> tensor<ui8>
120120

121121
// -----
122122

123+
// CHECK: #[[$MAP0:.+]] = affine_map<(d0, d1) -> (d0, d1)>
124+
// CHECK: #[[$MAP1:.+]] = affine_map<(d0, d1) -> (0, d1)>
125+
126+
// CHECK-LABEL: func.func @test_add_0d_broadcast(
127+
// CHECK-SAME: %[[ARG0:.*]]: tensor<2x1xf32>,
128+
// CHECK-SAME: %[[ARG1:.*]]: tensor<f32>) -> tensor<2x1xf32> {
129+
// CHECK: %[[EXPANDED:.*]] = tensor.expand_shape %[[ARG1]] [] output_shape [1, 1] : tensor<f32> into tensor<1x1xf32>
130+
// CHECK: %[[EMPTY_TENSOR:.*]] = tensor.empty() : tensor<2x1xf32>
131+
// CHECK: %[[RESULT:.*]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]], #[[$MAP0]]], iterator_types = ["parallel", "parallel"]} ins(%[[ARG0]], %[[EXPANDED]] : tensor<2x1xf32>, tensor<1x1xf32>) outs(%[[EMPTY_TENSOR]] : tensor<2x1xf32>) {
132+
// CHECK: ^bb0(%[[IN0:.*]]: f32, %[[IN1:.*]]: f32, %[[OUT:.*]]: f32):
133+
// CHECK: %[[ADD:.*]] = arith.addf %[[IN0]], %[[IN1]] : f32
134+
// CHECK: linalg.yield %[[ADD]] : f32
135+
// CHECK: } -> tensor<2x1xf32>
136+
// CHECK: return %[[RESULT]] : tensor<2x1xf32>
137+
// CHECK: }
138+
func.func @test_add_0d_broadcast(%arg0: tensor<2x1xf32>, %arg1: tensor<f32>) -> tensor<2x1xf32> {
139+
%0 = tosa.add %arg0, %arg1 : (tensor<2x1xf32>, tensor<f32>) -> tensor<2x1xf32>
140+
return %0 : tensor<2x1xf32>
141+
}
142+
143+
// -----
144+
123145
// CHECK: #[[$MAP0:.+]] = affine_map<(d0) -> (0)>
124146
// CHECK: #[[$MAP1:.+]] = affine_map<(d0) -> (d0)>
125147
// CHECK-LABEL: @test_add_1d_all_dynamic

0 commit comments

Comments
 (0)