Skip to content
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
9 changes: 5 additions & 4 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: %[[arg2_val:.*]] = tensor.extract %[[arg2]]
// CHECK: %[[detensored_res:.*]] = arith.addf %[[arg1_val]], %[[arg2_val]]
// CHECK: %[[detensored_res2:.*]] = arith.mulf %[[arg1_val]], %[[detensored_res]]
// CHECK: %[[arg1_val_1:.*]] = tensor.extract %[[arg1]]
// CHECK: %[[arg2_val:.*]] = tensor.extract %[[arg2]]
// CHECK: %[[arg1_val_2:.*]] = tensor.extract %[[arg1]]
// CHECK: %[[detensored_res:.*]] = arith.addf %[[arg1_val_2]], %[[arg2_val]]
// 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