|
| 1 | +#include <string> |
| 2 | + |
| 3 | +#include "circt/Dialect/Moore/MooreOps.h" |
| 4 | +#include "circt/Dialect/Moore/MoorePasses.h" |
| 5 | +#include "circt/Support/LLVM.h" |
| 6 | +#include "llvm/Support/Debug.h" |
| 7 | + |
| 8 | +#define DEBUG_TYPE "merge-procedures" |
| 9 | + |
| 10 | +namespace circt { |
| 11 | +namespace moore { |
| 12 | +#define GEN_PASS_DEF_MERGEPROCEDURES |
| 13 | +#include "circt/Dialect/Moore/MoorePasses.h.inc" |
| 14 | +} // namespace moore |
| 15 | +} // namespace circt |
| 16 | + |
| 17 | +using namespace circt; |
| 18 | +using namespace moore; |
| 19 | +using mlir::IRRewriter; |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +struct PrintMatchFailure : mlir::RewriterBase::Listener { |
| 24 | + void notifyMatchFailure( |
| 25 | + Location loc, function_ref<void(Diagnostic&)> reasonCallback) override { |
| 26 | + Diagnostic diag(loc, mlir::DiagnosticSeverity::Remark); |
| 27 | + reasonCallback(diag); |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +struct MergeProceduresPass |
| 32 | + : public circt::moore::impl::MergeProceduresBase<MergeProceduresPass> { |
| 33 | + void runOnOperation() override; |
| 34 | + LogicalResult TryMerge(IRRewriter& rewriter, |
| 35 | + ArrayRef<ProcedureOp> procedures); |
| 36 | +}; |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +std::unique_ptr<mlir::Pass> circt::moore::createMergeProceduresPass() { |
| 41 | + return std::make_unique<MergeProceduresPass>(); |
| 42 | +} |
| 43 | + |
| 44 | +LogicalResult MergeProceduresPass::TryMerge(IRRewriter& rewriter, |
| 45 | + ArrayRef<ProcedureOp> procedures) { |
| 46 | + if (procedures.size() <= 1) return success(); |
| 47 | + |
| 48 | + // Only merge always/always_ff procedures if their wait_events are identical. |
| 49 | + SmallVector<WaitEventOp> wait_events; |
| 50 | + wait_events.reserve(procedures.size()); |
| 51 | + for (ProcedureOp proc : procedures) { |
| 52 | + auto proc_wait_events = proc.getOps<WaitEventOp>(); |
| 53 | + if (!hasSingleElement(proc_wait_events)) { |
| 54 | + return rewriter.notifyMatchFailure(proc, "expected a single wait event"); |
| 55 | + } |
| 56 | + wait_events.push_back(*proc_wait_events.begin()); |
| 57 | + } |
| 58 | + |
| 59 | + // We only merge procedures with a single block only. Otherwise it needs more |
| 60 | + // work to maintain correct control flow. |
| 61 | + for (ProcedureOp proc : procedures) { |
| 62 | + if (proc.getBody().getBlocks().size() != 1) { |
| 63 | + return rewriter.notifyMatchFailure( |
| 64 | + proc, "can only merge procedures with a single block"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Compare wait event ops by string serialization. |
| 69 | + WaitEventOp first_wait_event = wait_events[0]; |
| 70 | + auto is_equivalent = [&](Value lhs, Value rhs) -> LogicalResult { |
| 71 | + // lhs refers to 'first_wait_event'. |
| 72 | + auto producer = lhs.getDefiningOp(); |
| 73 | + // If it's a block argument, check for value equivalence. |
| 74 | + if (!producer) return success(lhs == rhs); |
| 75 | + // If the value was produced within the wait event, it's equivalent. |
| 76 | + if (first_wait_event->isProperAncestor(producer)) return success(); |
| 77 | + // If the value was produced outside of the wait event, check for value |
| 78 | + // equivalence. |
| 79 | + return success(lhs == rhs); |
| 80 | + }; |
| 81 | + |
| 82 | + // Check if wait_events are equivalent. |
| 83 | + for (WaitEventOp wait_event : wait_events) { |
| 84 | + if (!mlir::OperationEquivalence::isEquivalentTo( |
| 85 | + first_wait_event, wait_event, |
| 86 | + /*checkEquivalent=*/is_equivalent, |
| 87 | + /*markEquivalent=*/nullptr, |
| 88 | + mlir::OperationEquivalence::Flags::IgnoreLocations)) { |
| 89 | + return rewriter.notifyMatchFailure(wait_event, "wait event mismatch"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Remove all but the first wait_event. |
| 94 | + for (WaitEventOp wait_event : llvm::ArrayRef(wait_events).drop_front()) { |
| 95 | + wait_event.erase(); |
| 96 | + } |
| 97 | + |
| 98 | + // Merge all procedures into the first one. |
| 99 | + ProcedureOp first_proc = procedures[0]; |
| 100 | + Block* dst_proc_block = &first_proc.getBody().getBlocks().front(); |
| 101 | + for (ProcedureOp proc : llvm::ArrayRef(procedures).drop_front()) { |
| 102 | + Block* src_proc_block = &proc.getBody().getBlocks().front(); |
| 103 | + src_proc_block->getTerminator()->erase(); |
| 104 | + rewriter.inlineBlockBefore(src_proc_block, dst_proc_block, |
| 105 | + // insert before block terminator. |
| 106 | + std::prev(dst_proc_block->end())); |
| 107 | + // Delete all but the first procedure. |
| 108 | + proc.erase(); |
| 109 | + } |
| 110 | + |
| 111 | + return success(); |
| 112 | +} |
| 113 | + |
| 114 | +void MergeProceduresPass::runOnOperation() { |
| 115 | + /* |
| 116 | + Sample rewrite |
| 117 | +
|
| 118 | + moore.module @bug(...) { |
| 119 | + moore.procedure always_ff { |
| 120 | + moore.wait_event { |
| 121 | + %3 = moore.read %wr_clk_0 : <l1> |
| 122 | + moore.detect_event posedge %3 : l1 |
| 123 | + } |
| 124 | + ... // A |
| 125 | + } |
| 126 | + moore.procedure always_ff { |
| 127 | + moore.wait_event { |
| 128 | + %3 = moore.read %wr_clk_0 : <l1> |
| 129 | + moore.detect_event posedge %3 : l1 |
| 130 | + } |
| 131 | + ... // B |
| 132 | + } |
| 133 | +
|
| 134 | + ~> |
| 135 | +
|
| 136 | + moore.module @bug(...) { |
| 137 | + moore.procedure always_ff { |
| 138 | + moore.wait_event { |
| 139 | + %3 = moore.read %wr_clk_0 : <l1> |
| 140 | + moore.detect_event posedge %3 : l1 |
| 141 | + } |
| 142 | + ... // A |
| 143 | + ... // B |
| 144 | + } |
| 145 | + ... |
| 146 | + */ |
| 147 | + |
| 148 | + // Collect all moore.procedures and group by their kind. We try to merge |
| 149 | + // procedures of each kind individually, so we merge multiple always_ff |
| 150 | + // procedures, but we don't merge always_ff and always together. |
| 151 | + llvm::DenseMap<moore::ProcedureKind, SmallVector<ProcedureOp>> procedures; |
| 152 | + getOperation().walk( |
| 153 | + [&](ProcedureOp proc) { procedures[proc.getKind()].push_back(proc); }); |
| 154 | + |
| 155 | + IRRewriter rewriter(&getContext()); |
| 156 | + PrintMatchFailure listener; |
| 157 | + LLVM_DEBUG(rewriter.setListener(&listener);); |
| 158 | + |
| 159 | + // Try to merge procedures of each kind. Failing to merge is not an error - |
| 160 | + // instead the error may later surface when lowering arc to llvm. |
| 161 | + for (moore::ProcedureKind kind : |
| 162 | + {moore::ProcedureKind::AlwaysFF, moore::ProcedureKind::Always}) { |
| 163 | + if (failed(TryMerge(rewriter, procedures[kind]))) { |
| 164 | + getOperation().emitWarning("could not merge procedures of kind ") |
| 165 | + << static_cast<uint32_t>(kind); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments