Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mlir/include/mlir/Analysis/SliceAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ struct BackwardSliceOptions : public SliceOptions {
/// backward slice computation traverses block arguments and asserts that the
/// parent op has a single region with a single block.
bool omitBlockArguments = false;

/// When omitUsesFromAbove is true, the backward slice computation omits
/// traversing values that are captured from above.
/// TODO: this should default to `false` after users have been updated.
bool omitUsesFromAbove = true;
};

using ForwardSliceOptions = SliceOptions;
Expand Down
9 changes: 8 additions & 1 deletion mlir/lib/Analysis/SliceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "mlir/IR/Operation.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Transforms/RegionUtils.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"

Expand Down Expand Up @@ -91,7 +92,13 @@ static void getBackwardSliceImpl(Operation *op,
if (options.filter && !options.filter(op))
return;

for (const auto &en : llvm::enumerate(op->getOperands())) {
auto operands = op->getOperands();
SetVector<Value> valuesToFollow(operands.begin(), operands.end());
if (!options.omitUsesFromAbove) {
getUsedValuesDefinedAbove(op->getRegions(), valuesToFollow);
}

for (const auto &en : llvm::enumerate(valuesToFollow)) {
Copy link
Collaborator

@joker-eph joker-eph Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way you can make it "free" when there are no regions is to define this code as a lambda that operates on an ValueRange.

auto processValues = [&] (ValueRange valuesToFollow) {
  ...
};

And then dispatch like this:

  processValues(op->getOperands());

  if (!options.omitUsesFromAbove) {
    SetVector<Value> valuesToFollow;
    getUsedValuesDefinedAbove(op->getRegions(), valuesToFollow);
    processValues(valuesToFollow);
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, that's a good solution.

I tweaked it a bit to take a Value so it can get passed to visitValuesDefinedAbove which takes a lambda with an OpOperand* arg. This prevents storing the values in a set (backwardSlice->count ensures they are still only visited once)

auto operand = en.value();
if (auto *definingOp = operand.getDefiningOp()) {
if (backwardSlice->count(definingOp) == 0)
Expand Down
28 changes: 27 additions & 1 deletion mlir/test/IR/slice.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: mlir-opt -slice-analysis-test %s | FileCheck %s
// RUN: mlir-opt -slice-analysis-test -split-input-file %s | FileCheck %s

func.func @slicing_linalg_op(%arg0 : index, %arg1 : index, %arg2 : index) {
%a = memref.alloc(%arg0, %arg2) : memref<?x?xf32>
Expand Down Expand Up @@ -33,3 +33,29 @@ func.func @slicing_linalg_op(%arg0 : index, %arg1 : index, %arg2 : index) {
// CHECK-DAG: %[[B:.+]] = memref.alloc(%[[ARG2]], %[[ARG1]]) : memref<?x?xf32>
// CHECK-DAG: %[[C:.+]] = memref.alloc(%[[ARG0]], %[[ARG1]]) : memref<?x?xf32>
// CHECK: return

// -----

#map = affine_map<(d0, d1) -> (d0, d1)>
func.func @slice_use_from_above(%arg0: tensor<5x5xf32>, %arg1: tensor<5x5xf32>) {
%0 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%arg0 : tensor<5x5xf32>) outs(%arg1 : tensor<5x5xf32>) {
^bb0(%in: f32, %out: f32):
%2 = arith.addf %in, %in : f32
linalg.yield %2 : f32
} -> tensor<5x5xf32>
%collapsed = tensor.collapse_shape %0 [[0, 1]] : tensor<5x5xf32> into tensor<25xf32>
%1 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%0 : tensor<5x5xf32>) outs(%arg1 : tensor<5x5xf32>) {
^bb0(%in: f32, %out: f32):
%c2 = arith.constant 2 : index
%extracted = tensor.extract %collapsed[%c2] : tensor<25xf32>
%2 = arith.addf %extracted, %extracted : f32
linalg.yield %2 : f32
} -> tensor<5x5xf32>
return
}

// CHECK-LABEL: func @slice_use_from_above__backward_slice__0
// CHECK-SAME: %[[ARG0:[a-zA-Z0-9_]+]]: tensor
// CHECK: %[[A:.+]] = linalg.generic {{.*}} ins(%[[ARG0]]
// CHECK: %[[B:.+]] = tensor.collapse_shape %[[A]]
// CHECK: return
2 changes: 2 additions & 0 deletions mlir/test/lib/IR/TestSlicing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ static LogicalResult createBackwardSliceFunction(Operation *op,
SetVector<Operation *> slice;
BackwardSliceOptions options;
options.omitBlockArguments = omitBlockArguments;
// TODO: Make this default.
options.omitUsesFromAbove = false;
getBackwardSlice(op, &slice, options);
for (Operation *slicedOp : slice)
builder.clone(*slicedOp, mapper);
Expand Down
Loading