1//===- TestReshardingPartition.cpp - Test resharding partition ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir/Dialect/Func/IR/FuncOps.h"
10#include "mlir/Dialect/Shard/IR/ShardOps.h"
11#include "mlir/Dialect/Shard/Transforms/Partition.h"
12#include "mlir/IR/BuiltinDialect.h"
13#include "mlir/IR/BuiltinOps.h"
14#include "mlir/IR/BuiltinTypeInterfaces.h"
15#include "mlir/IR/Diagnostics.h"
16#include "mlir/IR/ImplicitLocOpBuilder.h"
17#include "mlir/IR/PatternMatch.h"
18#include "mlir/IR/SymbolTable.h"
19#include "mlir/IR/Value.h"
20#include "mlir/Pass/Pass.h"
21#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
22
23using namespace mlir;
24using namespace mlir::shard;
25
26namespace {
27
28struct TestReshardingRewritePattern : OpRewritePattern<ShardOp> {
29 using OpRewritePattern<ShardOp>::OpRewritePattern;
30
31 LogicalResult matchAndRewrite(ShardOp op,
32 PatternRewriter &rewriter) const override {
33 if (op.getAnnotateForUsers()) {
34 return failure();
35 }
36
37 SymbolTableCollection symbolTable;
38 shard::GridOp grid = symbolTable.lookupNearestSymbolFrom<shard::GridOp>(
39 from: op, symbol: cast<ShardingOp>(Val: op.getSharding().getDefiningOp()).getGridAttr());
40
41 bool foundUser = false;
42 for (auto *user : op->getUsers()) {
43 if (auto targetShardOp = llvm::dyn_cast<ShardOp>(Val: user)) {
44 if (targetShardOp.getAnnotateForUsers() &&
45 grid == symbolTable.lookupNearestSymbolFrom<shard::GridOp>(
46 from: targetShardOp,
47 symbol: cast<ShardingOp>(
48 Val: targetShardOp.getSharding().getDefiningOp())
49 .getGridAttr())) {
50 foundUser = true;
51 break;
52 }
53 }
54 }
55
56 if (!foundUser) {
57 return failure();
58 }
59
60 for (auto *user : op->getUsers()) {
61 auto targetShardOp = llvm::dyn_cast<ShardOp>(Val: user);
62 if (!targetShardOp || !targetShardOp.getAnnotateForUsers() ||
63 symbolTable.lookupNearestSymbolFrom<shard::GridOp>(
64 from: targetShardOp,
65 symbol: cast<ShardingOp>(Val: targetShardOp.getSharding().getDefiningOp())
66 .getGridAttr()) != grid) {
67 continue;
68 }
69
70 ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
71 ShapedType sourceShardShape =
72 shardShapedType(shape: op.getResult().getType(), grid, sharding: op.getSharding());
73 TypedValue<ShapedType> sourceShard = cast<TypedValue<ShapedType>>(
74 Val: UnrealizedConversionCastOp::create(builder, resultTypes: sourceShardShape,
75 operands: op.getSrc())
76 ->getResult(idx: 0));
77 TypedValue<ShapedType> targetShard =
78 reshard(builder, grid, source: op, target: targetShardOp, sourceShardValue: sourceShard);
79 Value newTargetUnsharded =
80 UnrealizedConversionCastOp::create(
81 builder, resultTypes: targetShardOp.getResult().getType(), operands: targetShard)
82 ->getResult(idx: 0);
83 rewriter.replaceAllUsesWith(from: targetShardOp.getResult(),
84 to: newTargetUnsharded);
85 }
86
87 return success();
88 }
89};
90
91struct TestReshardingPass
92 : public PassWrapper<TestReshardingPass, OperationPass<ModuleOp>> {
93 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestReshardingPass)
94
95 void runOnOperation() override {
96 RewritePatternSet patterns(&getContext());
97 patterns.insert<TestReshardingRewritePattern>(arg: &getContext());
98 if (failed(Result: applyPatternsGreedily(op: getOperation().getOperation(),
99 patterns: std::move(patterns)))) {
100 return signalPassFailure();
101 }
102 }
103 void getDependentDialects(DialectRegistry &registry) const override {
104 reshardingRegisterDependentDialects(registry);
105 registry.insert<BuiltinDialect>();
106 }
107 StringRef getArgument() const final {
108 return "test-grid-resharding-partition";
109 }
110 StringRef getDescription() const final {
111 return "Test Shard dialect resharding partition.";
112 }
113};
114} // namespace
115
116namespace mlir {
117namespace test {
118void registerTestReshardingPartitionPass() {
119 PassRegistration<TestReshardingPass>();
120}
121} // namespace test
122} // namespace mlir
123