Skip to content
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

Open
wants to merge 1 commit into
base: main
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
1 change: 1 addition & 0 deletions include/ttmlir/Conversion/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#ifdef TTMLIR_ENABLE_STABLEHLO
#include "ttmlir/Conversion/ArithToStableHLO/ArithToStableHLO.h"
#include "ttmlir/Conversion/RedundantBroadcastElimination/RedundantBroadcastElimination.h"
Copy link
Contributor

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.

#include "ttmlir/Conversion/StableHLOToTTIR/StableHLOToTTIR.h"
#endif
#include "ttmlir/Conversion/TTIRToTTIRDecomposition/TTIRToTTIRDecomposition.h"
Expand Down
5 changes: 5 additions & 0 deletions include/ttmlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ let summary = "Convert Arith Dialect to StableHLO dialect.";
let constructor = "createConvertArithToStableHLOPass()";
let dependentDialects = ["mlir::stablehlo::StablehloDialect", "mlir::arith::ArithDialect"];
}
def RedundantBroadcastElimination : Pass<"redundant-broadcast-elimination", "::mlir::ModuleOp"> {
let summary = "Eliminate any redundant broadcast ops by folding them.";
let constructor = "createRedundantBroadcastEliminationPass()";
let dependentDialects = ["mlir::stablehlo::StablehloDialect"];
}
#endif

def ConvertTosaToTTIR : Pass<"convert-tosa-to-ttir", "::mlir::ModuleOp"> {
Expand Down
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
6 changes: 6 additions & 0 deletions include/ttmlir/Dialect/TTIR/Pipelines/TTIRPipelines.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ struct StableHLOToTTIRPipelineOptions
// This pass will convert stablehlo.composite ops into func.call ops so
// that the TTIR inliner pass may inline the ops.
llvm::cl::init(true)};
Option<bool> eliminateRedundantBroadcast{
*this, "eliminate-redundant-broadcast",
llvm::cl::desc("Eliminate redundant broadcast ops."),
// Stablehlo can generate redundant broadcast ops where the input and
// output shapes are same. This pass folds those broadcasts.
llvm::cl::init(true)};
};

void createStableHLOToTTIRPipeline(
Expand Down
1 change: 1 addition & 0 deletions lib/Conversion/StableHLOToTTIR/ArithToStableHLOPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "ttmlir/Conversion/ArithToStableHLO/ArithToStableHLO.h"
#include "ttmlir/Conversion/RedundantBroadcastElimination/RedundantBroadcastElimination.h"

#include <mlir/Dialect/Arith/IR/Arith.h>
#include <mlir/Dialect/Func/IR/FuncOps.h>
Expand Down
1 change: 1 addition & 0 deletions lib/Conversion/StableHLOToTTIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_mlir_library(TTMLIRStableHLOToTTIR
StableHLOToTTIRPatterns.cpp
StableHLOToTTIRPass.cpp
ArithToStableHLOPass.cpp
RedundantBroadcastElimination.cpp

ADDITIONAL_HEADER_DIRS
${PROJECT_SOURCE_DIR}/include/ttmlir/Conversion/StableHLOToTTIR
Expand Down
70 changes: 70 additions & 0 deletions lib/Conversion/StableHLOToTTIR/RedundantBroadcastElimination.cpp
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can pattern match directly w/ walk by writing:

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()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, this was not what I expected the name RedundantBroadcastEliminationPass to mean. We need a simple test demonstrating this case.

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
3 changes: 3 additions & 0 deletions lib/Dialect/TTIR/Pipelines/TTIRPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void createStableHLOToTTIRPipeline(
pm.addPass(stablehlo::createStablehloLegalizeCompositeToCallPass());
}
pm.addPass(createConvertStableHLOToTTIRPass());
if (options.eliminateRedundantBroadcast) {
pm.addPass(createRedundantBroadcastEliminationPass());
}
if (options.removeDeadValuesEnabled) {
pm.addPass(mlir::createRemoveDeadValuesPass());
}
Expand Down
Loading