|
| 1 | +// Copyright 2024 RISC Zero, Inc. |
| 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 | +#include "zirgen/Dialect/ZHLT/IR/ZHLT.h" |
| 16 | +#include "zirgen/Dialect/ZHLT/Transforms/PassDetail.h" |
| 17 | +#include "llvm/Support/Debug.h" |
| 18 | +#include "llvm/ADT/Statistic.h" |
| 19 | + |
| 20 | +#define DEBUG_TYPE "mux-hoisting" |
| 21 | + |
| 22 | +using namespace mlir; |
| 23 | +using namespace zirgen::ZStruct; |
| 24 | + |
| 25 | +namespace zirgen::Zhlt { |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +bool isHoistable(Operation* op) { |
| 30 | + // Check if all of the operation's operands are defined outside of the mux. If |
| 31 | + // they are, then we can probably hoist -- but make sure not to reorder side |
| 32 | + // effects, and never hoist any ops with regions, AliasLayoutOps or block |
| 33 | + // terminators. |
| 34 | + return op->getNumRegions() == 0 && |
| 35 | + !isa<LoadOp>(op) && // could be more precise by checking for writes within the block |
| 36 | + !isa<AliasLayoutOp>(op) && |
| 37 | + !op->hasTrait<OpTrait::IsTerminator>() && |
| 38 | + llvm::all_of(op->getOperands(), [=](Value value) { |
| 39 | + return value.getParentRegion() != op->getParentRegion(); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +bool compare(Operation* op1, Operation* op2) { |
| 44 | + if (op1->getName() != op2->getName() || |
| 45 | + op1->getNumOperands() != op2->getNumOperands() || |
| 46 | + op1->getAttrs().size() != op2->getAttrs().size()) |
| 47 | + return false; |
| 48 | + for (auto [opn1, opn2] : llvm::zip(op1->getOperands(), op2->getOperands())) { |
| 49 | + if (opn1 != opn2) |
| 50 | + return false; |
| 51 | + } |
| 52 | + for (auto [attr1, attr2] : llvm::zip(op1->getAttrs(), op2->getAttrs())) { |
| 53 | + if (attr1 != attr2) |
| 54 | + return false; |
| 55 | + } |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | +} // namespace |
| 60 | + |
| 61 | +// For structure-like components, if two members are equal in the PackOp at the |
| 62 | +// end of the constructor, those members will ultimately be equal in all other |
| 63 | +// situations, such as when reconstructing an instance from a back and when |
| 64 | +// zero-initializing (trivially, since both members are zeroed). |
| 65 | +struct HoistCommonMuxCodePass : public HoistCommonMuxCodeBase<HoistCommonMuxCodePass> { |
| 66 | + HoistCommonMuxCodePass() = default; |
| 67 | + HoistCommonMuxCodePass(const HoistCommonMuxCodePass& pass) {} |
| 68 | + |
| 69 | + void runOnOperation() override { |
| 70 | + getOperation().walk<WalkOrder::PostOrder>([&](SwitchOp mux) { |
| 71 | + // If code in the mux is shared by multiple but not all mux arms, then |
| 72 | + // hoisting it reduces code size but increases execution cost. Since we |
| 73 | + // want code shared by all arms, search the first mux arm for hoistable |
| 74 | + // operations, and then search the other mux arms for matching operations. |
| 75 | + auto it = mux.getRegion(0).op_begin(); |
| 76 | + while (it != mux->getRegion(0).op_end()) { |
| 77 | + Operation& op = *(it++); |
| 78 | + if (isHoistable(&op)) { |
| 79 | + SmallVector<Operation*> toHoist; |
| 80 | + toHoist.reserve(mux.getArms().size()); |
| 81 | + toHoist.push_back(&op); |
| 82 | + |
| 83 | + bool hoistable = llvm::all_of(mux.getRegions(), [&](Region* region) { |
| 84 | + if (region->getRegionNumber() == 0) |
| 85 | + return true; |
| 86 | + |
| 87 | + return llvm::any_of(region->getOps(), [&](Operation& op2) { |
| 88 | + if (compare(&op, &op2)) { |
| 89 | + toHoist.push_back(&op2); |
| 90 | + return true; |
| 91 | + } |
| 92 | + return false; |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + if (hoistable) { |
| 97 | + LLVM_DEBUG(llvm::dbgs() << "hoist: " << *toHoist[0] << "\n"); |
| 98 | + toHoist[0]->moveBefore(mux); |
| 99 | + for (size_t i = 1; i < toHoist.size(); ++i) { |
| 100 | + toHoist[i]->replaceAllUsesWith(toHoist[0]->getResults()); |
| 101 | + toHoist[i]->erase(); |
| 102 | + ++opsDeleted; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + Statistic opsDeleted{this, "opsDeleted", "number of operations saved by mux hoisting"}; |
| 111 | +}; |
| 112 | + |
| 113 | +std::unique_ptr<OperationPass<ModuleOp>> createHoistCommonMuxCodePass() { |
| 114 | + return std::make_unique<HoistCommonMuxCodePass>(); |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace zirgen::Zhlt |
0 commit comments