13#include "mlir/Analysis/TopologicalSortUtils.h"
14#include "mlir/IR/BuiltinAttributes.h"
15#include "mlir/IR/Matchers.h"
16#include "mlir/IR/OpDefinition.h"
17#include "mlir/IR/PatternMatch.h"
18#include "llvm/ADT/APInt.h"
19#include "llvm/Support/Casting.h"
20#include "llvm/Support/LogicalResult.h"
24using namespace circt::synth::mig;
25using namespace circt::synth::aig;
28#include "circt/Dialect/Synth/Synth.cpp.inc"
30LogicalResult MajorityInverterOp::verify() {
31 if (getNumOperands() % 2 != 1)
32 return emitOpError(
"requires an odd number of operands");
37llvm::APInt MajorityInverterOp::evaluate(ArrayRef<APInt> inputs) {
38 assert(inputs.size() == getNumOperands() &&
39 "Number of inputs must match number of operands");
41 if (inputs.size() == 3) {
42 auto a = (isInverted(0) ? ~inputs[0] : inputs[0]);
43 auto b = (isInverted(1) ? ~inputs[1] : inputs[1]);
44 auto c = (isInverted(2) ? ~inputs[2] : inputs[2]);
45 return (a & b) | (a & c) | (b & c);
49 auto width = inputs[0].getBitWidth();
50 APInt result(width, 0);
52 for (
size_t bit = 0; bit < width; ++bit) {
54 for (
size_t i = 0; i < inputs.size(); ++i) {
56 if (isInverted(i) ^ inputs[i][bit])
60 if (count > inputs.size() / 2)
67OpFoldResult MajorityInverterOp::fold(FoldAdaptor adaptor) {
70 SmallVector<APInt, 3> inputValues;
71 for (
auto input : adaptor.getInputs()) {
72 auto attr = llvm::dyn_cast_or_null<IntegerAttr>(input);
75 inputValues.push_back(attr.getValue());
78 auto result = evaluate(inputValues);
79 return IntegerAttr::get(getType(), result);
82LogicalResult MajorityInverterOp::canonicalize(MajorityInverterOp op,
83 PatternRewriter &rewriter) {
84 if (op.getNumOperands() == 1) {
85 if (op.getInverted()[0])
87 rewriter.replaceOp(op, op.getOperand(0));
92 if (op.getNumOperands() != 3)
97 auto getConstant = [&](
unsigned index) -> std::optional<llvm::APInt> {
99 if (mlir::matchPattern(op.getInputs()[index], mlir::m_ConstantInt(&value)))
100 return op.isInverted(index) ? ~value : value;
105 auto replaceWithIndex = [&](
int index) {
106 bool inverted = op.isInverted(index);
108 rewriter.replaceOpWithNewOp<MajorityInverterOp>(
109 op, op.getType(), op.getOperand(index),
true);
111 rewriter.replaceOp(op, op.getOperand(index));
118 for (
int i = 0; i < 2; ++i) {
119 for (
int j = i + 1; j < 3; ++j) {
123 if (op.getOperand(i) == op.getOperand(j)) {
125 if (op.isInverted(i) != op.isInverted(j))
126 return replaceWithIndex(k);
127 return replaceWithIndex(i);
136 op, op.getType(), mlir::IntegerAttr::get(op.getType(), *c1));
141 return replaceWithIndex(k);
153OpFoldResult AndInverterOp::fold(FoldAdaptor adaptor) {
154 if (getNumOperands() == 1 && !isInverted(0))
155 return getOperand(0);
157 auto inputs = adaptor.getInputs();
158 if (inputs.size() == 2 && inputs[1]) {
159 auto value = cast<IntegerAttr>(inputs[1]).getValue();
163 return IntegerAttr::get(
164 IntegerType::get(getContext(), value.getBitWidth()), value);
165 if (value.isAllOnes()) {
169 return getOperand(0);
175LogicalResult AndInverterOp::canonicalize(AndInverterOp op,
176 PatternRewriter &rewriter) {
178 SmallVector<Value> uniqueValues;
179 SmallVector<bool> uniqueInverts;
182 APInt::getAllOnes(op.getResult().getType().getIntOrFloatBitWidth());
184 bool invertedConstFound =
false;
185 bool flippedFound =
false;
187 for (
auto [value, inverted] :
llvm::zip(op.getInputs(), op.getInverted())) {
188 bool newInverted = inverted;
191 constValue &= ~constOp.getValue();
192 invertedConstFound =
true;
194 constValue &= constOp.getValue();
199 if (
auto andInverterOp = value.getDefiningOp<synth::aig::AndInverterOp>()) {
200 if (andInverterOp.getInputs().size() == 1 &&
201 andInverterOp.isInverted(0)) {
202 value = andInverterOp.getOperand(0);
203 newInverted = andInverterOp.isInverted(0) ^ inverted;
208 auto it = seen.find(value);
209 if (it == seen.end()) {
210 seen.insert({value, newInverted});
211 uniqueValues.push_back(value);
212 uniqueInverts.push_back(newInverted);
213 }
else if (it->second != newInverted) {
216 op, APInt::getZero(value.getType().getIntOrFloatBitWidth()));
222 if (constValue.isZero()) {
228 if ((uniqueValues.size() == op.getInputs().size() && !flippedFound) ||
229 (!constValue.isAllOnes() && !invertedConstFound &&
230 uniqueValues.size() + 1 == op.getInputs().size()))
233 if (!constValue.isAllOnes()) {
235 uniqueInverts.push_back(
false);
236 uniqueValues.push_back(constOp);
240 if (uniqueValues.empty()) {
246 replaceOpWithNewOpAndCopyNamehint<synth::aig::AndInverterOp>(
247 rewriter, op, uniqueValues, uniqueInverts);
251APInt AndInverterOp::evaluate(ArrayRef<APInt> inputs) {
252 assert(inputs.size() == getNumOperands() &&
253 "Expected as many inputs as operands");
254 assert(!inputs.empty() &&
"Expected non-empty input list");
255 APInt result = APInt::getAllOnes(inputs.front().getBitWidth());
256 for (
auto [idx, input] :
llvm::enumerate(inputs)) {
266 ArrayRef<bool> inverts,
267 PatternRewriter &rewriter) {
268 switch (operands.size()) {
270 assert(0 &&
"cannot be called with empty operand range");
274 return AndInverterOp::create(rewriter, op.getLoc(), operands[0],
true);
278 return AndInverterOp::create(rewriter, op.getLoc(), operands[0],
279 operands[1], inverts[0], inverts[1]);
281 auto firstHalf = operands.size() / 2;
284 inverts.take_front(firstHalf), rewriter);
287 inverts.drop_front(firstHalf), rewriter);
288 return AndInverterOp::create(rewriter, op.getLoc(), lhs, rhs);
294 AndInverterOp op, PatternRewriter &rewriter)
const {
295 if (op.getInputs().size() <= 2)
301 op, op.getOperands(), op.getInverted(), rewriter));
307 llvm::function_ref<
bool(mlir::Value, mlir::Operation *)> isOperandReady) {
309 auto walkResult = op->walk([&](Region *region) {
311 dyn_cast<mlir::RegionKindInterface>(region->getParentOp());
313 regionKindOp.hasSSADominance(region->getRegionNumber()))
314 return WalkResult::advance();
317 for (
auto &block : *region) {
318 if (!mlir::sortTopologically(&block, isOperandReady))
319 return WalkResult::interrupt();
321 return WalkResult::advance();
324 return success(!walkResult.wasInterrupted());
assert(baseType &&"element must be base type")
static std::optional< APSInt > getConstant(Attribute operand)
Determine the value of a constant operand for the sake of constant folding.
static Value lowerVariadicAndInverterOp(AndInverterOp op, OperandRange operands, ArrayRef< bool > inverts, PatternRewriter &rewriter)
LogicalResult topologicallySortGraphRegionBlocks(mlir::Operation *op, llvm::function_ref< bool(mlir::Value, mlir::Operation *)> isOperandReady)
This function performs a topological sort on the operations within each block of graph regions in the...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
mlir::LogicalResult matchAndRewrite(aig::AndInverterOp op, mlir::PatternRewriter &rewriter) const override