CIRCT 22.0.0git
Loading...
Searching...
No Matches
MaximumAndCover.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// This pass performs maximum AND-cover optimization by collapsing single-fanout
10// AND nodes into their consumers.
11//
12//===----------------------------------------------------------------------===//
13
17#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
18
19#define DEBUG_TYPE "synth-maximum-and-cover"
20
21namespace circt {
22namespace synth {
23#define GEN_PASS_DEF_MAXIMUMANDCOVER
24#include "circt/Dialect/Synth/Transforms/SynthPasses.h.inc"
25} // namespace synth
26} // namespace circt
27
28using namespace circt;
29using namespace synth;
30
31namespace {
32struct MaximumAndCoverPattern : OpRewritePattern<aig::AndInverterOp> {
33 using OpRewritePattern<aig::AndInverterOp>::OpRewritePattern;
34
35 LogicalResult matchAndRewrite(aig::AndInverterOp op,
36 PatternRewriter &rewriter) const override {
37 // Check if any operand can be collapsed (single-fanout non-inverted AND)
38 llvm::SmallVector<Value> newFanins;
39 llvm::SmallVector<bool> newInverts;
40 bool changed = false;
41
42 for (auto [input, inverted] : llvm::zip(op.getInputs(), op.getInverted())) {
43 auto andOp = input.getDefiningOp<aig::AndInverterOp>();
44
45 // Can only collapse if:
46 // 1. Input is an AND operation
47 // 2. Input is not inverted in current op
48 // 3. AND operation has only one use (single fanout)
49 if (!inverted && andOp && andOp->hasOneUse()) {
50 // Collect fanin node's inputs into current node
51 for (auto [fanin, faninInverted] :
52 llvm::zip(andOp.getInputs(), andOp.getInverted())) {
53 newFanins.push_back(fanin);
54 newInverts.push_back(faninInverted);
55 }
56 changed = true;
57 } else {
58 // Keep the original input
59 newFanins.push_back(input);
60 newInverts.push_back(inverted);
61 }
62 }
63
64 if (!changed)
65 return failure();
66
67 // Create new AND operation with collapsed inputs
68 rewriter.replaceOpWithNewOp<aig::AndInverterOp>(op, newFanins, newInverts);
69 return success();
70 }
71};
72
73struct MaximumAndCoverPass
74 : public impl::MaximumAndCoverBase<MaximumAndCoverPass> {
75 void runOnOperation() override;
76};
77} // namespace
78
79void MaximumAndCoverPass::runOnOperation() {
80 RewritePatternSet patterns(&getContext());
81 patterns.add<MaximumAndCoverPattern>(&getContext());
82
83 mlir::FrozenRewritePatternSet frozenPatterns(std::move(patterns));
84 if (failed(mlir::applyPatternsGreedily(getOperation(), frozenPatterns)))
85 return signalPassFailure();
86}
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition synth.py:1