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);
128 rewriter.replaceOp(op, op.getOperand(i));
138 op, op.getType(), mlir::IntegerAttr::get(op.getType(), *c1));
143 return replaceWithIndex(k);
155OpFoldResult AndInverterOp::fold(FoldAdaptor adaptor) {
156 if (getNumOperands() == 1 && !isInverted(0))
157 return getOperand(0);
159 auto inputs = adaptor.getInputs();
160 if (inputs.size() == 2 && inputs[1]) {
161 auto value = cast<IntegerAttr>(inputs[1]).getValue();
165 return IntegerAttr::get(
166 IntegerType::get(getContext(), value.getBitWidth()), value);
167 if (value.isAllOnes()) {
171 return getOperand(0);
177LogicalResult AndInverterOp::canonicalize(AndInverterOp op,
178 PatternRewriter &rewriter) {
180 SmallVector<Value> uniqueValues;
181 SmallVector<bool> uniqueInverts;
184 APInt::getAllOnes(op.getResult().getType().getIntOrFloatBitWidth());
186 bool invertedConstFound =
false;
187 bool flippedFound =
false;
189 for (
auto [value, inverted] :
llvm::zip(op.getInputs(), op.getInverted())) {
190 bool newInverted = inverted;
193 constValue &= ~constOp.getValue();
194 invertedConstFound =
true;
196 constValue &= constOp.getValue();
201 if (
auto andInverterOp = value.getDefiningOp<synth::aig::AndInverterOp>()) {
202 if (andInverterOp.getInputs().size() == 1 &&
203 andInverterOp.isInverted(0)) {
204 value = andInverterOp.getOperand(0);
205 newInverted = andInverterOp.isInverted(0) ^ inverted;
210 auto it = seen.find(value);
211 if (it == seen.end()) {
212 seen.insert({value, newInverted});
213 uniqueValues.push_back(value);
214 uniqueInverts.push_back(newInverted);
215 }
else if (it->second != newInverted) {
218 op, APInt::getZero(value.getType().getIntOrFloatBitWidth()));
224 if (constValue.isZero()) {
230 if ((uniqueValues.size() == op.getInputs().size() && !flippedFound) ||
231 (!constValue.isAllOnes() && !invertedConstFound &&
232 uniqueValues.size() + 1 == op.getInputs().size()))
235 if (!constValue.isAllOnes()) {
237 uniqueInverts.push_back(
false);
238 uniqueValues.push_back(constOp);
242 if (uniqueValues.empty()) {
248 replaceOpWithNewOpAndCopyNamehint<synth::aig::AndInverterOp>(
249 rewriter, op, uniqueValues, uniqueInverts);
253APInt AndInverterOp::evaluate(ArrayRef<APInt> inputs) {
254 assert(inputs.size() == getNumOperands() &&
255 "Expected as many inputs as operands");
256 assert(!inputs.empty() &&
"Expected non-empty input list");
257 APInt result = APInt::getAllOnes(inputs.front().getBitWidth());
258 for (
auto [idx, input] :
llvm::enumerate(inputs)) {
268 ArrayRef<bool> inverts,
269 PatternRewriter &rewriter) {
270 switch (operands.size()) {
272 assert(0 &&
"cannot be called with empty operand range");
276 return AndInverterOp::create(rewriter, op.getLoc(), operands[0],
true);
280 return AndInverterOp::create(rewriter, op.getLoc(), operands[0],
281 operands[1], inverts[0], inverts[1]);
283 auto firstHalf = operands.size() / 2;
286 inverts.take_front(firstHalf), rewriter);
289 inverts.drop_front(firstHalf), rewriter);
290 return AndInverterOp::create(rewriter, op.getLoc(), lhs, rhs);
296 AndInverterOp op, PatternRewriter &rewriter)
const {
297 if (op.getInputs().size() <= 2)
303 op, op.getOperands(), op.getInverted(), rewriter));
309 llvm::function_ref<
bool(mlir::Value, mlir::Operation *)> isOperandReady) {
311 auto walkResult = op->walk([&](Region *region) {
313 dyn_cast<mlir::RegionKindInterface>(region->getParentOp());
315 regionKindOp.hasSSADominance(region->getRegionNumber()))
316 return WalkResult::advance();
319 for (
auto &block : *region) {
320 if (!mlir::sortTopologically(&block, isOperandReady))
321 return WalkResult::interrupt();
323 return WalkResult::advance();
326 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