Skip to content

Commit

Permalink
#sdy Add canonicalization rewrite pattern for ReshardOp.
Browse files Browse the repository at this point in the history
Add a rewrite pattern to replace a chain of ReshardOp with one ReshardOp.

PiperOrigin-RevId: 653379478
  • Loading branch information
bixia1 authored and copybara-github committed Jul 18, 2024
1 parent 84547f2 commit 4d9ebe6
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
16 changes: 16 additions & 0 deletions shardy/dialect/sdy/ir/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ td_library(
name = "sdy_td_files",
srcs = [
"attrs.td",
"canonicalization.td",
"dialect.td",
"enums.td",
"ops.td",
Expand Down Expand Up @@ -94,6 +95,19 @@ gentbl_cc_library(
deps = [":sdy_td_files"],
)

gentbl_cc_library(
name = "canonicalization_inc",
tbl_outs = [
(
["-gen-rewriters"],
"canonicalization.cc.inc",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "canonicalization.td",
deps = [":sdy_td_files"],
)

gentbl_filegroup(
name = "dialect_doc_gen",
tbl_outs = [
Expand All @@ -113,6 +127,7 @@ gentbl_filegroup(
cc_library(
name = "dialect",
srcs = [
"canonicalization.cc",
"data_flow_utils.cc",
"dialect.cc",
"parsers.cc",
Expand All @@ -130,6 +145,7 @@ cc_library(
],
deps = [
":attrs_inc",
":canonicalization_inc",
":dialect_inc",
":enums_inc",
":ops_inc",
Expand Down
36 changes: 36 additions & 0 deletions shardy/dialect/sdy/ir/canonicalization.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2024 The Shardy Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OperationSupport.h"
#include "mlir/IR/PatternMatch.h"
#include "shardy/dialect/sdy/ir/dialect.h"

namespace mlir {
namespace sdy {

namespace {

#include "shardy/dialect/sdy/ir/canonicalization.cc.inc"

} // namespace

void ReshardOp::getCanonicalizationPatterns(RewritePatternSet& results,
MLIRContext* context) {
results.add<ReshardOfReshardPattern>(context);
}

} // namespace sdy
} // namespace mlir
28 changes: 28 additions & 0 deletions shardy/dialect/sdy/ir/canonicalization.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2024 The Shardy Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef SDY_CANONICALIZATION
#define SDY_CANONICALIZATION

include "shardy/dialect/sdy/ir/ops.td"
include "mlir/IR/PatternBase.td"

def HasOneUse: Constraint<CPred<"$_self.hasOneUse()">, "has one use">;

def ReshardOfReshardPattern :
Pat<(Sdy_ReshardOp (Sdy_ReshardOp:$inner_reshard $tensor, $inner_attr), $outer_attr),
(Sdy_ReshardOp $tensor, $outer_attr), [(HasOneUse:$inner_reshard)]>;

#endif // SDY_CANONICALIZATION
1 change: 1 addition & 0 deletions shardy/dialect/sdy/ir/ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def Sdy_ReshardOp : Sdy_Op<"reshard",

let assemblyFormat = "$input $sharding attr-dict `:` type($result)";
let hasVerifier = 1;
let hasCanonicalizer = 1;
}

def Sdy_ReturnOp : Sdy_Op<"return", [Pure, Terminator]> {
Expand Down
33 changes: 33 additions & 0 deletions shardy/dialect/sdy/ir/test/reshard_canonicalization.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: sdy_opt %s -canonicalize | FileCheck %s

sdy.mesh @mesh = <"a"=2, "b"=2>

// CHECK-LABEL: func @reshard_of_reshard_no_other_uses
// CHECK-NEXT: %0 = sdy.reshard %arg0 <@mesh, [{"a", ?}, {?}]>
// CHECK-NEXT: return %0
func.func @reshard_of_reshard_no_other_uses(%arg0: tensor<8x8xf32>) -> tensor<8x8xf32> {
%0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]> : tensor<8x8xf32>
%1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]> : tensor<8x8xf32>
return %1 : tensor<8x8xf32>
}

// CHECK-LABEL: func @reshard_of_reshard_with_other_uses
// CHECK-NEXT: %0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]>
// CHECK-NEXT: %1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]>
// CHECK-NEXT: return %0, %1
func.func @reshard_of_reshard_with_other_uses(%arg0: tensor<8x8xf32>) -> (tensor<8x8xf32>, tensor<8x8xf32>) {
%0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]> : tensor<8x8xf32>
%1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]> : tensor<8x8xf32>
return %0, %1 : tensor<8x8xf32>, tensor<8x8xf32>
}

// CHECK-LABEL: func @reshard_chain_of_three_no_other_uses
// CHECK-NEXT: %0 = sdy.reshard %arg0 <@mesh, [{?}, {"a", ?}]>
// CHECK-NEXT: return %0
func.func @reshard_chain_of_three_no_other_uses(%arg0: tensor<8x8xf32>) -> tensor<8x8xf32> {
%0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]> : tensor<8x8xf32>
%1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]> : tensor<8x8xf32>
%2 = sdy.reshard %1 <@mesh, [{?}, {"a", ?}]> : tensor<8x8xf32>
return %2 : tensor<8x8xf32>
}

0 comments on commit 4d9ebe6

Please sign in to comment.