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"
25using namespace circt::synth::mig;
26using namespace circt::synth::aig;
29#include "circt/Dialect/Synth/Synth.cpp.inc"
31LogicalResult ChoiceOp::verify() {
32 if (getNumOperands() < 1)
33 return emitOpError(
"requires at least one operand");
37LogicalResult MajorityInverterOp::verify() {
38 if (getNumOperands() % 2 != 1)
39 return emitOpError(
"requires an odd number of operands");
44llvm::APInt MajorityInverterOp::evaluate(ArrayRef<APInt> inputs) {
45 assert(inputs.size() == getNumOperands() &&
46 "Number of inputs must match number of operands");
48 if (inputs.size() == 3) {
49 auto a = (isInverted(0) ? ~inputs[0] : inputs[0]);
50 auto b = (isInverted(1) ? ~inputs[1] : inputs[1]);
51 auto c = (isInverted(2) ? ~inputs[2] : inputs[2]);
52 return (a & b) | (
a & c) | (b & c);
56 auto width = inputs[0].getBitWidth();
57 APInt result(width, 0);
59 for (
size_t bit = 0; bit < width; ++bit) {
61 for (
size_t i = 0; i < inputs.size(); ++i) {
63 if (isInverted(i) ^ inputs[i][bit])
67 if (count > inputs.size() / 2)
74OpFoldResult MajorityInverterOp::fold(FoldAdaptor adaptor) {
77 SmallVector<APInt, 3> inputValues;
78 for (
auto input : adaptor.getInputs()) {
79 auto attr = llvm::dyn_cast_or_null<IntegerAttr>(input);
82 inputValues.push_back(attr.getValue());
85 auto result = evaluate(inputValues);
86 return IntegerAttr::get(getType(), result);
89LogicalResult MajorityInverterOp::canonicalize(MajorityInverterOp op,
90 PatternRewriter &rewriter) {
91 if (op.getNumOperands() == 1) {
92 if (op.getInverted()[0])
94 rewriter.replaceOp(op, op.getOperand(0));
99 if (op.getNumOperands() != 3)
104 auto getConstant = [&](
unsigned index) -> std::optional<llvm::APInt> {
106 if (mlir::matchPattern(op.getInputs()[index], mlir::m_ConstantInt(&value)))
107 return op.isInverted(index) ? ~value : value;
112 auto replaceWithIndex = [&](
int index) {
113 bool inverted = op.isInverted(index);
115 rewriter.replaceOpWithNewOp<MajorityInverterOp>(
116 op, op.getType(), op.getOperand(index),
true);
118 rewriter.replaceOp(op, op.getOperand(index));
125 for (
int i = 0; i < 2; ++i) {
126 for (
int j = i + 1; j < 3; ++j) {
130 if (op.getOperand(i) == op.getOperand(j)) {
132 if (op.isInverted(i) != op.isInverted(j))
133 return replaceWithIndex(k);
134 return replaceWithIndex(i);
143 op, op.getType(), mlir::IntegerAttr::get(op.getType(), *c1));
148 return replaceWithIndex(k);
160OpFoldResult AndInverterOp::fold(FoldAdaptor adaptor) {
161 if (getNumOperands() == 1 && !isInverted(0))
162 return getOperand(0);
164 auto inputs = adaptor.getInputs();
165 if (inputs.size() == 2)
166 if (
auto intAttr = dyn_cast_or_null<IntegerAttr>(inputs[1])) {
167 auto value = intAttr.getValue();
171 return IntegerAttr::get(
172 IntegerType::get(getContext(), value.getBitWidth()), value);
173 if (value.isAllOnes()) {
177 return getOperand(0);
183LogicalResult AndInverterOp::canonicalize(AndInverterOp op,
184 PatternRewriter &rewriter) {
186 SmallVector<Value> uniqueValues;
187 SmallVector<bool> uniqueInverts;
190 APInt::getAllOnes(op.getResult().getType().getIntOrFloatBitWidth());
192 bool invertedConstFound =
false;
193 bool flippedFound =
false;
195 for (
auto [value, inverted] :
llvm::zip(op.getInputs(), op.getInverted())) {
196 bool newInverted = inverted;
199 constValue &= ~constOp.getValue();
200 invertedConstFound =
true;
202 constValue &= constOp.getValue();
207 if (
auto andInverterOp = value.getDefiningOp<synth::aig::AndInverterOp>()) {
208 if (andInverterOp.getInputs().size() == 1 &&
209 andInverterOp.isInverted(0)) {
210 value = andInverterOp.getOperand(0);
211 newInverted = andInverterOp.isInverted(0) ^ inverted;
216 auto it = seen.find(value);
217 if (it == seen.end()) {
218 seen.insert({value, newInverted});
219 uniqueValues.push_back(value);
220 uniqueInverts.push_back(newInverted);
221 }
else if (it->second != newInverted) {
224 op, APInt::getZero(value.getType().getIntOrFloatBitWidth()));
230 if (constValue.isZero()) {
236 if ((uniqueValues.size() == op.getInputs().size() && !flippedFound) ||
237 (!constValue.isAllOnes() && !invertedConstFound &&
238 uniqueValues.size() + 1 == op.getInputs().size()))
241 if (!constValue.isAllOnes()) {
243 uniqueInverts.push_back(
false);
244 uniqueValues.push_back(constOp);
248 if (uniqueValues.empty()) {
254 replaceOpWithNewOpAndCopyNamehint<synth::aig::AndInverterOp>(
255 rewriter, op, uniqueValues, uniqueInverts);
259APInt AndInverterOp::evaluate(ArrayRef<APInt> inputs) {
260 assert(inputs.size() == getNumOperands() &&
261 "Expected as many inputs as operands");
262 assert(!inputs.empty() &&
"Expected non-empty input list");
263 APInt result = APInt::getAllOnes(inputs.front().getBitWidth());
264 for (
auto [idx, input] :
llvm::enumerate(inputs)) {
274 ArrayRef<bool> inverts,
275 PatternRewriter &rewriter) {
276 switch (operands.size()) {
278 assert(0 &&
"cannot be called with empty operand range");
282 return AndInverterOp::create(rewriter, op.getLoc(), operands[0],
true);
286 return AndInverterOp::create(rewriter, op.getLoc(), operands[0],
287 operands[1], inverts[0], inverts[1]);
289 auto firstHalf = operands.size() / 2;
292 inverts.take_front(firstHalf), rewriter);
295 inverts.drop_front(firstHalf), rewriter);
296 return AndInverterOp::create(rewriter, op.getLoc(), lhs, rhs);
302 AndInverterOp op, PatternRewriter &rewriter)
const {
303 if (op.getInputs().size() <= 2)
309 op, op.getOperands(), op.getInverted(), rewriter));
315 llvm::function_ref<
bool(mlir::Value, mlir::Operation *)> isOperandReady) {
317 auto walkResult = op->walk([&](Region *region) {
319 dyn_cast<mlir::RegionKindInterface>(region->getParentOp());
321 regionKindOp.hasSSADominance(region->getRegionNumber()))
322 return WalkResult::advance();
325 for (
auto &block : *region) {
326 if (!mlir::sortTopologically(&block, isOperandReady))
327 return WalkResult::interrupt();
329 return WalkResult::advance();
332 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