-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add redundant broadcast elimination pass after stablehlo conversion. #1252
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef TTMLIR_CONVERSION_REDUNDANT_BROADCAST_ELIMINATION_H | ||
#define TTMLIR_CONVERSION_REDUNDANT_BROADCAST_ELIMINATION_H | ||
|
||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace mlir::tt { | ||
|
||
#ifdef TTMLIR_ENABLE_STABLEHLO | ||
std::unique_ptr<OperationPass<ModuleOp>> | ||
createRedundantBroadcastEliminationPass(); | ||
#endif | ||
|
||
} // namespace mlir::tt | ||
|
||
#endif // TTMLIR_CONVERSION_REDUNDANT_BROADCAST_ELIMINATION_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <mlir/Dialect/Func/IR/FuncOps.h> | ||
#include <mlir/Dialect/Func/Transforms/FuncConversions.h> | ||
#include <mlir/Dialect/Tensor/IR/Tensor.h> | ||
#include <mlir/IR/BuiltinOps.h> | ||
#include <mlir/IR/Dialect.h> | ||
#include <mlir/IR/PatternMatch.h> | ||
#include <mlir/Pass/Pass.h> | ||
|
||
#include <stablehlo/dialect/StablehloOps.h> | ||
|
||
#include "ttmlir/Conversion/RedundantBroadcastElimination/RedundantBroadcastElimination.h" | ||
#include "ttmlir/Dialect/TT/IR/TT.h" | ||
#include "ttmlir/Dialect/TT/IR/TTOpsTypes.h" | ||
#include "ttmlir/Dialect/TTIR/IR/TTIR.h" | ||
#include "ttmlir/Dialect/TTIR/IR/TTIROps.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::tt; | ||
|
||
namespace mlir::tt::ttir { | ||
|
||
#define GEN_PASS_DEF_REDUNDANTBROADCASTELIMINATION | ||
#include "ttmlir/Conversion/Passes.h.inc" | ||
|
||
} // namespace mlir::tt::ttir | ||
|
||
namespace { | ||
|
||
class RedundantBroadcastEliminationPass | ||
: public ttir::impl::RedundantBroadcastEliminationBase< | ||
RedundantBroadcastEliminationPass> { | ||
public: | ||
using ttir::impl::RedundantBroadcastEliminationBase< | ||
RedundantBroadcastEliminationPass>::RedundantBroadcastEliminationBase; | ||
|
||
void runOnOperation() final { | ||
ModuleOp module = getOperation(); | ||
IRRewriter rewriter(&getContext()); | ||
|
||
module->walk([&](Operation *op) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can pattern match directly w/ module->walk([&](mlir::tt::ttir::BroadcastOp op) { |
||
if (mlir::isa<mlir::tt::ttir::BroadcastOp>(op)) { | ||
if (op->use_empty()) { | ||
return; | ||
} | ||
|
||
if (op->getResult(0).getType() == op->getOperand(0).getType()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this was not what I expected the name To me this implies that the broadcast is operating on a tensor that's already broadcasted in that dimension, but should this be illegal by construction? |
||
// This broadcast is redundant | ||
rewriter.replaceAllUsesWith((Value)op->getResult(0), | ||
(Value)op->getOperand(0)); | ||
rewriter.eraseOp(op); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace mlir::tt { | ||
|
||
std::unique_ptr<OperationPass<ModuleOp>> | ||
createRedundantBroadcastEliminationPass() { | ||
return std::make_unique<RedundantBroadcastEliminationPass>(); | ||
} | ||
|
||
} // namespace mlir::tt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we should make it a generic pass instead of StableHLO dependent pass. Other dialects may also require this optimization.