Skip to content

[mlir][linalg] Migrate Detensorize pass to new dialect conversion driver #152912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: users/matthias-springer/prototype_one_shot
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
34 changes: 32 additions & 2 deletions mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,22 @@ struct LinalgDetensorize
}
};

/// A listener that forwards notifyBlockErased and notifyOperationErased to
/// the given callbacks.
struct CallbackListener : public RewriterBase::Listener {
CallbackListener(std::function<void(Operation *op)> onOperationErased,
std::function<void(Block *block)> onBlockErased)
: onOperationErased(onOperationErased), onBlockErased(onBlockErased) {}

void notifyBlockErased(Block *block) override { onBlockErased(block); }
void notifyOperationErased(Operation *op) override {
onOperationErased(op);
}

std::function<void(Operation *op)> onOperationErased;
std::function<void(Block *block)> onBlockErased;
};

void runOnOperation() override {
MLIRContext *context = &getContext();
DetensorizeTypeConverter typeConverter;
Expand Down Expand Up @@ -551,8 +567,22 @@ struct LinalgDetensorize
populateBranchOpInterfaceTypeConversionPattern(patterns, typeConverter,
shouldConvertBranchOperand);

if (failed(
applyFullConversion(getOperation(), target, std::move(patterns))))
ConversionConfig config;
auto onOperationErased = [&](Operation *op) {
opsToDetensor.erase(op);
detensorableBranchOps.erase(op);
};
auto onBlockErased = [&](Block *block) {
for (BlockArgument arg : block->getArguments()) {
blockArgsToDetensor.erase(arg);
}
};
CallbackListener listener(onOperationErased, onBlockErased);

config.listener = &listener;
config.allowPatternRollback = false;
if (failed(applyFullConversion(getOperation(), target, std::move(patterns),
Copy link
Member

Choose a reason for hiding this comment

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

Since the callback doesn't return anything, it won't change the success/failure result, so this should be fine.

config)))
signalPassFailure();

RewritePatternSet canonPatterns(context);
Expand Down
7 changes: 4 additions & 3 deletions mlir/test/Dialect/Linalg/detensorize_0d.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ func.func @detensor_op_sequence(%arg1: tensor<f32>, %arg2: tensor<f32>) -> tenso
}
// CHECK-LABEL: func @detensor_op_sequence
// CHECK-SAME: (%[[arg1:.*]]: tensor<f32>, %[[arg2:.*]]: tensor<f32>)
// CHECK-DAG: %[[arg1_val:.*]] = tensor.extract %[[arg1]]
// CHECK-DAG: %[[arg1_val_1:.*]] = tensor.extract %[[arg1]]
// CHECK-DAG: %[[arg2_val:.*]] = tensor.extract %[[arg2]]
// CHECK: %[[detensored_res:.*]] = arith.addf %[[arg1_val]], %[[arg2_val]]
// CHECK: %[[detensored_res2:.*]] = arith.mulf %[[arg1_val]], %[[detensored_res]]
// CHECK-DAG: %[[arg1_val_2:.*]] = tensor.extract %[[arg1]]
// CHECK: %[[detensored_res:.*]] = arith.addf %[[arg1_val_2]], %[[arg2_val]]
Copy link
Member

Choose a reason for hiding this comment

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

CHECK-DAG can come in any order, but the op here specifies them in a particular order, and the two arg1 vals have the same pattern. This may randomly fail.

// CHECK: %[[detensored_res2:.*]] = arith.mulf %[[arg1_val_1]], %[[detensored_res]]
// CHECK: %[[detensored_res3:.*]] = arith.divf %[[detensored_res]], %[[detensored_res2]]
// CHECK: %[[new_tensor_res:.*]] = tensor.from_elements %[[detensored_res3]]
// CHECK: return %[[new_tensor_res]]
Expand Down