|
| 1 | +/* Copyright 2026 The Shardy Authors. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 19 | +#include "mlir/IR/Block.h" |
| 20 | +#include "mlir/IR/BuiltinOps.h" |
| 21 | +#include "shardy/dialect/sdy/ir/utils.h" |
| 22 | +#include "shardy/dialect/sdy/transforms/import/passes.h" // IWYU pragma: keep |
| 23 | + |
| 24 | +namespace mlir { |
| 25 | +namespace sdy { |
| 26 | + |
| 27 | +#define GEN_PASS_DEF_PREORDERFUNCSPASS |
| 28 | +#include "shardy/dialect/sdy/transforms/import/passes.h.inc" |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +struct PreOrderFuncsPass |
| 33 | + : public impl::PreOrderFuncsPassBase<PreOrderFuncsPass> { |
| 34 | + using PreOrderFuncsPassBase::PreOrderFuncsPassBase; |
| 35 | + |
| 36 | + void runOnOperation() override { |
| 37 | + ModuleOp moduleOp = getOperation(); |
| 38 | + std::vector<func::FuncOp> funcsInPreOrder; |
| 39 | + iterateFuncs( |
| 40 | + moduleOp, |
| 41 | + [&](func::FuncOp funcOp) { funcsInPreOrder.push_back(funcOp); }, |
| 42 | + /*preOrder=*/true); |
| 43 | + mlir::Block& body = moduleOp.getBodyRegion().front(); |
| 44 | + for (func::FuncOp funcOp : funcsInPreOrder) { |
| 45 | + funcOp->moveBefore(&body, body.end()); |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +} // namespace |
| 51 | + |
| 52 | +} // namespace sdy |
| 53 | +} // namespace mlir |
0 commit comments