Skip to content

[mlir][mesh] Make sharding propagation and spmdization work on FuncOpInterface #84415

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

Merged
merged 1 commit into from
Mar 8, 2024
Merged
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
4 changes: 2 additions & 2 deletions mlir/include/mlir/Dialect/Mesh/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include "mlir/Pass/PassBase.td"
// ShardingPropagation
//===----------------------------------------------------------------------===//

def ShardingPropagation : Pass<"sharding-propagation", "mlir::func::FuncOp"> {
def ShardingPropagation : InterfacePass<"sharding-propagation", "mlir::FunctionOpInterface"> {
let summary = "sharding propagation";
let description = [{
Propagates sharding information throughout the graph. After this pass, each
Expand All @@ -29,7 +29,7 @@ def ShardingPropagation : Pass<"sharding-propagation", "mlir::func::FuncOp"> {
];
}

def Spmdization : Pass<"mesh-spmdization", "mlir::func::FuncOp"> {
def Spmdization : InterfacePass<"mesh-spmdization", "mlir::FunctionOpInterface"> {
let summary = "Partition a function into SPMD form.";
let description = [{
This pass fits in right after a pass that annotates the function with
Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Dialect/Mesh/Transforms/ShardingPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mlir/Dialect/Mesh/IR/MeshDialect.h"
#include "mlir/Dialect/Mesh/IR/MeshOps.h"
#include "mlir/Dialect/Mesh/Interfaces/ShardingInterface.h"
#include "mlir/Interfaces/FunctionInterfaces.h"
#include "mlir/Pass/Pass.h"
#include "llvm/Support/Debug.h"
#include <vector>
Expand Down Expand Up @@ -172,9 +173,9 @@ static LogicalResult visitOp(Operation *op, OpBuilder &builder) {
struct ShardingPropagation
: public mesh::impl::ShardingPropagationBase<ShardingPropagation> {
void runOnOperation() override {
func::FuncOp funcOp = getOperation();
FunctionOpInterface funcOp = getOperation();
MLIRContext *ctx = funcOp.getContext();
Region &region = funcOp.getBody();
Region &region = funcOp.getFunctionBody();
OpBuilder builder(ctx);
if (!region.hasOneBlock()) {
funcOp.emitOpError() << "only one block is supported!";
Expand Down
18 changes: 10 additions & 8 deletions mlir/lib/Dialect/Mesh/Transforms/Spmdization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/IR/Value.h"
#include "mlir/Interfaces/ControlFlowInterfaces.h"
#include "mlir/Interfaces/FunctionInterfaces.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
Expand Down Expand Up @@ -694,7 +696,7 @@ static LogicalResult spmdizeBlock(Block &block, IRMapping &spmdizationMap,
}

static LogicalResult
spmdizeFuncOp(func::FuncOp op, IRMapping &spmdizationMap,
spmdizeFuncOp(FunctionOpInterface op, IRMapping &spmdizationMap,
SymbolTableCollection &symbolTableCollection) {
OpBuilder builder(op.getFunctionBody());

Expand All @@ -717,21 +719,21 @@ spmdizeFuncOp(func::FuncOp op, IRMapping &spmdizationMap,

// Find a return op and change the function results signature to its operands
// signature.
func::ReturnOp returnOp;
for (Block &block : op.getBody()) {
Operation *returnOp = nullptr;
for (Block &block : op.getFunctionBody()) {
if (block.empty()) {
continue;
}

returnOp = llvm::cast<func::ReturnOp>(block.back());
if (returnOp) {
if (block.back().hasTrait<OpTrait::ReturnLike>()) {
returnOp = &block.back();
break;
}
}
assert(returnOp);
op.setFunctionType(FunctionType::get(op->getContext(),
op.getBody().front().getArgumentTypes(),
returnOp->getOperandTypes()));
op.setType(FunctionType::get(op->getContext(),
op.getFunctionBody().front().getArgumentTypes(),
returnOp->getOperandTypes()));

return success();
}
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Dialect/Linalg/mesh-spmdization.mlir
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// RUN: mlir-opt \
// RUN: --mesh-spmdization \
// RUN: --test-constant-fold \
// RUN: --pass-pipeline="builtin.module(func.func(mesh-spmdization,test-constant-fold))" \
// RUN: --split-input-file \
// RUN: %s | FileCheck %s

Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/Mesh/sharding-propagation.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: mlir-opt -sharding-propagation %s | FileCheck %s
// RUN: mlir-opt --pass-pipeline="builtin.module(func.func(sharding-propagation))" %s | FileCheck %s

mesh.mesh @mesh_1d(shape = ?)
mesh.mesh @mesh_2d(shape = 2x4)
Expand Down
4 changes: 3 additions & 1 deletion mlir/test/Dialect/Mesh/spmdization.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: mlir-opt --mesh-spmdization --test-constant-fold %s | FileCheck %s
// RUN: mlir-opt \
// RUN: --pass-pipeline="builtin.module(func.func(mesh-spmdization,test-constant-fold))" \
// RUN: %s | FileCheck %s

mesh.mesh @mesh_1d(shape = 2)

Expand Down