Skip to content

Commit 71604f4

Browse files
author
Mahesh Ravishankar
committed
[mlir][Linalg] Fix out of bounds access while handling multiple results.
Previous change (a7bfdc2) added support for fusion of `linalg.generic` op with `tensor.expand_shape` op when the former had multiple results. Fix a bug related to this that resulted in a segfault. Reviewed By: hanchung Differential Revision: https://reviews.llvm.org/D132631
1 parent 41a3b57 commit 71604f4

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ fuseWithReshapeByExpansion(GenericOp genericOp, Operation *reshapeOp,
768768
outputs.push_back(rewriter.create<tensor::ExpandShapeOp>(
769769
genericOp.getLoc(), expandedOutputType, opOperand->get(),
770770
reassociation));
771+
} else {
772+
outputs.push_back(opOperand->get());
771773
}
772774
}
773775

‎mlir/test/Dialect/Linalg/reshape_fusion.mlir‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,37 @@ func.func @reshape_as_consumer_permutation_with_multiple_results
539539
// CHECK-SAME: ins(%[[RESHAPE0]], %[[RESHAPE1]] :
540540
// CHECK-SAME: outs(%[[RESHAPE2]], %[[RESHAPE3]] :
541541
// CHECK: return %[[GENERIC]]#0, %[[GENERIC]]#1
542+
543+
// -----
544+
545+
#map0 = affine_map<(d0, d1) -> (d1)>
546+
#map1 = affine_map<(d0, d1) -> (d0, d1)>
547+
module {
548+
func.func @multi_result_op_expansion(%arg0: tensor<512xf32>, %arg1: tensor<512xf32>,
549+
%arg2: tensor<512xf32>, %arg3: tensor<200x512xf32>) -> tensor<25x8x1x512xf32> {
550+
%0:2 = linalg.generic {
551+
indexing_maps = [#map0, #map0, #map0, #map1],
552+
iterator_types = ["parallel", "parallel"]}
553+
ins(%arg0, %arg1 : tensor<512xf32>, tensor<512xf32>)
554+
outs(%arg2, %arg3 : tensor<512xf32>, tensor<200x512xf32>) {
555+
^bb0(%arg4: f32, %arg5: f32, %arg6: f32, %arg7: f32):
556+
%2 = arith.addf %arg4, %arg5 : f32
557+
linalg.yield %2, %2 : f32, f32
558+
} -> (tensor<512xf32>, tensor<200x512xf32>)
559+
%1 = tensor.expand_shape %0#1 [[0, 1, 2], [3]] : tensor<200x512xf32> into tensor<25x8x1x512xf32>
560+
return %1 : tensor<25x8x1x512xf32>
561+
}
562+
}
563+
// CHECK-DAG: #[[MAP0:.+]] = affine_map<(d0, d1, d2, d3) -> (d3)>
564+
// CHECK-DAG: #[[MAP1:.+]] = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
565+
// CHECK: func.func @multi_result_op_expansion(
566+
// CHECK-SAME: %[[ARG0:[a-zA-Z0-9]+]]: tensor<512xf32>
567+
// CHECK-SAME: %[[ARG1:[a-zA-Z0-9]+]]: tensor<512xf32>
568+
// CHECK-SAME: %[[ARG2:[a-zA-Z0-9]+]]: tensor<512xf32>
569+
// CHECK-SAME: %[[ARG3:[a-zA-Z0-9]+]]: tensor<200x512xf32>
570+
// CHECK: %[[OUTS:.+]] = tensor.expand_shape %[[ARG3]] {{\[}}[0, 1, 2], [3]{{\]}}
571+
// CHECK: %[[GENERIC:.+]]:2 = linalg.generic
572+
// CHECK-SAME: indexing_maps = [#[[MAP0]], #[[MAP0]], #[[MAP0]], #[[MAP1]]]
573+
// CHECK-SAME: ins(%[[ARG0]], %[[ARG1]] :
574+
// CHECK-SAME: outs(%[[ARG2]], %[[OUTS]] :
575+
// CHECK: return %[[GENERIC]]#1

0 commit comments

Comments
 (0)