32 #include "mlir/Pass/Pass.h"
33 #include "llvm/ADT/MapVector.h"
34 #include "llvm/ADT/TypeSwitch.h"
35 #include "llvm/Support/raw_ostream.h"
38 #define GEN_PASS_DEF_CONVERTHWTOBTOR2
39 #include "circt/Conversion/Passes.h.inc"
42 using namespace circt;
48 struct ConvertHWToBTOR2Pass
49 :
public circt::impl::ConvertHWToBTOR2Base<ConvertHWToBTOR2Pass>,
50 public comb::CombinationalVisitor<ConvertHWToBTOR2Pass>,
51 public sv::Visitor<ConvertHWToBTOR2Pass>,
52 public hw::TypeOpVisitor<ConvertHWToBTOR2Pass>,
53 public verif::Visitor<ConvertHWToBTOR2Pass> {
55 ConvertHWToBTOR2Pass(raw_ostream &os) : os(os) {}
57 void runOnOperation()
override;
73 DenseMap<size_t, size_t> sortToLIDMap;
78 DenseMap<APInt, size_t> constToLIDMap;
83 DenseMap<Operation *, size_t> opLIDMap;
87 DenseMap<size_t, size_t> inputLIDs;
93 SmallVector<Operation *> regOps;
97 llvm::SmallMapVector<Operation *, OperandRange::iterator, 16> worklist;
100 DenseSet<Operation *> handledOps;
103 static constexpr
size_t noLID = -1UL;
104 [[maybe_unused]]
static constexpr int64_t noWidth = -1L;
111 size_t getOpLID(Operation *op) {
114 Operation *defOp = op;
115 auto &f = opLIDMap[defOp];
125 size_t setOpLID(Operation *op) {
126 size_t oplid = lid++;
127 opLIDMap[op] = oplid;
134 size_t getOpLID(Value value) {
135 Operation *defOp = value.getDefiningOp();
137 if (
auto it = opLIDMap.find(defOp); it != opLIDMap.end())
142 if (BlockArgument barg = dyn_cast<BlockArgument>(value)) {
144 size_t argIdx = barg.getArgNumber();
147 if (
auto it = inputLIDs.find(argIdx); it != inputLIDs.end())
159 size_t getSortLID(
size_t w) {
160 if (
auto it = sortToLIDMap.find(w); it != sortToLIDMap.end())
168 size_t setSortLID(
size_t w) {
169 size_t sortlid = lid;
171 sortToLIDMap[w] = lid++;
178 size_t getConstLID(int64_t val,
size_t w) {
179 if (
auto it = constToLIDMap.find(APInt(w, val)); it != constToLIDMap.end())
187 size_t setConstLID(int64_t val,
size_t w) {
188 size_t constlid = lid;
190 constToLIDMap[APInt(w, val)] = lid++;
198 void genSort(StringRef type,
size_t width) {
200 if (getSortLID(
width) != noLID) {
204 size_t sortlid = setSortLID(
width);
209 <<
" " << type <<
" " <<
width <<
"\n";
213 void genInput(
size_t inlid,
size_t width, StringRef name) {
215 size_t sid = sortToLIDMap.at(
width);
220 <<
" " << sid <<
" " << name <<
"\n";
224 void genConst(int64_t value,
size_t width, Operation *op) {
227 size_t opLID = getOpLID(op);
230 size_t sid = sortToLIDMap.at(
width);
234 <<
" " << sid <<
" " << value <<
"\n";
238 size_t genZero(
size_t width) {
240 size_t zlid = getConstLID(0,
width);
245 size_t sid = sortToLIDMap.at(
width);
248 size_t constlid = setConstLID(0,
width);
251 os << constlid <<
" "
253 <<
" " << sid <<
"\n";
259 void genInit(Operation *
reg, Value initVal, int64_t
width) {
261 size_t regLID = getOpLID(
reg);
262 size_t sid = sortToLIDMap.at(
width);
263 size_t initValLID = getOpLID(initVal);
269 <<
" " << sid <<
" " << regLID <<
" " << initValLID <<
"\n";
274 void genBinOp(StringRef inst, Operation *binop, Value op1, Value op2,
277 size_t opLID = getOpLID(binop);
280 size_t sid = sortToLIDMap.at(
width);
284 size_t op1LID = getOpLID(op1);
285 size_t op2LID = getOpLID(op2);
288 os << opLID <<
" " << inst <<
" " << sid <<
" " << op1LID <<
" " << op2LID
293 void genSlice(Operation *srcop, Value op0,
size_t lowbit, int64_t
width) {
295 size_t opLID = getOpLID(srcop);
298 size_t sid = sortToLIDMap.at(
width);
302 size_t op0LID = getOpLID(op0);
307 <<
" " << sid <<
" " << op0LID <<
" " << (
width - 1) <<
" " << lowbit
312 void genUnaryOp(Operation *srcop, Operation *op0, StringRef inst,
315 size_t opLID = getOpLID(srcop);
318 size_t sid = sortToLIDMap.at(
width);
322 size_t op0LID = getOpLID(op0);
324 os << opLID <<
" " << inst <<
" " << sid <<
" " << op0LID <<
"\n";
329 void genUnaryOp(Operation *srcop, Value op0, StringRef inst,
size_t width) {
330 genUnaryOp(srcop, op0.getDefiningOp(), inst,
width);
334 size_t genUnaryOp(
size_t op0LID, StringRef inst,
size_t width) {
336 size_t curLid = lid++;
339 size_t sid = sortToLIDMap.at(
width);
341 os << curLid <<
" " << inst <<
" " << sid <<
" " << op0LID <<
"\n";
346 size_t genUnaryOp(Operation *op0, StringRef inst,
size_t width) {
347 return genUnaryOp(getOpLID(op0), inst,
width);
352 size_t genUnaryOp(Value op0, StringRef inst,
size_t width) {
353 return genUnaryOp(getOpLID(op0), inst,
width);
359 void genBad(Operation *assertop) {
361 size_t assertLID = getOpLID(assertop);
368 void genBad(
size_t assertLID) {
373 <<
" " << assertLID <<
"\n";
378 void genConstraint(Value expr) {
380 size_t exprLID = getOpLID(expr);
382 genConstraint(exprLID);
387 void genConstraint(
size_t exprLID) {
392 <<
" " << exprLID <<
"\n";
397 void genIte(Operation *srcop, Value cond, Value t, Value f, int64_t
width) {
399 size_t condLID = getOpLID(cond);
400 size_t tLID = getOpLID(t);
401 size_t fLID = getOpLID(f);
403 genIte(srcop, condLID, tLID, fLID,
width);
408 void genIte(Operation *srcop,
size_t condLID,
size_t tLID,
size_t fLID,
411 size_t opLID = getOpLID(srcop);
414 size_t sid = sortToLIDMap.at(
width);
419 <<
" " << sid <<
" " << condLID <<
" " << tLID <<
" " << fLID <<
"\n";
423 size_t genImplies(Operation *srcop, Value lhs, Value rhs) {
425 size_t lhsLID = getOpLID(lhs);
426 size_t rhsLID = getOpLID(rhs);
428 return genImplies(srcop, lhsLID, rhsLID);
432 size_t genImplies(Operation *srcop,
size_t lhsLID,
size_t rhsLID) {
434 size_t opLID = getOpLID(srcop);
435 return genImplies(opLID, lhsLID, rhsLID);
438 size_t genImplies(
size_t opLID,
size_t lhsLID,
size_t rhsLID) {
440 size_t sid = sortToLIDMap.at(1);
444 <<
" " << sid <<
" " << lhsLID <<
" " << rhsLID <<
"\n";
449 void genState(Operation *srcop, int64_t
width, StringRef name) {
451 size_t opLID = getOpLID(srcop);
454 size_t sid = sortToLIDMap.at(
width);
459 <<
" " << sid <<
" " << name <<
"\n";
464 void genNext(Value next, Operation *
reg, int64_t
width) {
466 size_t sid = sortToLIDMap.at(
width);
469 size_t regLID = getOpLID(
reg);
470 size_t nextLID = getOpLID(next);
476 <<
" " << sid <<
" " << regLID <<
" " << nextLID <<
"\n";
481 int64_t requireSort(mlir::Type type) {
492 genSort(
"bitvec",
width);
498 void finalizeRegVisit(Operation *op) {
500 Value next, reset, resetVal;
503 if (
auto reg = dyn_cast<seq::CompRegOp>(op)) {
505 next =
reg.getInput();
506 reset =
reg.getReset();
507 resetVal =
reg.getResetValue();
508 }
else if (
auto reg = dyn_cast<seq::FirRegOp>(op)) {
510 next =
reg.getNext();
511 reset =
reg.getReset();
512 resetVal =
reg.getResetValue();
514 op->emitError(
"Invalid register operation !");
518 genSort(
"bitvec",
width);
523 size_t nextLID = noLID;
527 if (BlockArgument barg = dyn_cast<BlockArgument>(next)) {
529 size_t argIdx = barg.getArgNumber();
532 nextLID = inputLIDs[argIdx];
535 nextLID = getOpLID(next);
540 size_t resetValLID = noLID;
544 size_t resetLID = noLID;
545 if (BlockArgument barg = dyn_cast<BlockArgument>(reset)) {
548 size_t argIdx = barg.getArgNumber();
551 resetLID = inputLIDs[argIdx];
554 resetLID = getOpLID(reset);
559 resetValLID = getOpLID(resetVal.getDefiningOp());
561 resetValLID = genZero(
width);
564 setOpLID(next.getDefiningOp());
573 genIte(next.getDefiningOp(), resetLID, resetValLID, nextLID,
width);
576 if (nextLID == noLID) {
577 next.getDefiningOp()->emitError(
578 "Register input does not point to a valid op!");
584 genNext(next, op,
width);
594 void visit(hw::PortInfo &port) {
598 if (port.isInput() && !isa<seq::ClockType>(port.type)) {
600 StringRef iName = port.getName();
604 int64_t w = requireSort(port.type);
611 inputLIDs[port.argNum] = lid;
616 genInput(inlid, w, iName);
625 int64_t w = requireSort(op.getType());
629 int64_t value = op.getValue().getSExtValue();
630 genConst(value, w, op);
634 void visit(hw::WireOp op) {
635 op->emitError(
"Wires are not supported in btor!");
636 return signalPassFailure();
639 void visitTypeOp(Operation *op) { visitInvalidTypeOp(op); }
642 void visitInvalidTypeOp(Operation *op) {
644 dispatchCombinationalVisitor(op);
649 template <
typename Op>
650 void visitBinOp(Op op, StringRef inst) {
652 int64_t w = requireSort(op.getType());
655 Value op1 = op.getOperand(0);
656 Value op2 = op.getOperand(1);
659 genBinOp(inst, op, op1, op2, w);
663 void visitComb(
comb::AddOp op) { visitBinOp(op,
"add"); }
664 void visitComb(
comb::SubOp op) { visitBinOp(op,
"sub"); }
665 void visitComb(
comb::MulOp op) { visitBinOp(op,
"mul"); }
666 void visitComb(
comb::DivSOp op) { visitBinOp(op,
"sdiv"); }
667 void visitComb(
comb::DivUOp op) { visitBinOp(op,
"udiv"); }
668 void visitComb(
comb::ModSOp op) { visitBinOp(op,
"smod"); }
669 void visitComb(
comb::ShlOp op) { visitBinOp(op,
"sll"); }
670 void visitComb(
comb::ShrUOp op) { visitBinOp(op,
"srl"); }
671 void visitComb(
comb::ShrSOp op) { visitBinOp(op,
"sra"); }
672 void visitComb(
comb::AndOp op) { visitBinOp(op,
"and"); }
673 void visitComb(
comb::OrOp op) { visitBinOp(op,
"or"); }
674 void visitComb(
comb::XorOp op) { visitBinOp(op,
"xor"); }
680 int64_t w = requireSort(op.getType());
684 Value op0 = op.getOperand();
685 size_t lb = op.getLowBit();
688 genSlice(op, op0, lb, w);
694 void visitComb(comb::ICmpOp op) {
695 Value lhs = op.getOperand(0);
696 Value rhs = op.getOperand(1);
700 StringRef pred = stringifyICmpPredicate(op.getPredicate());
707 genSort(
"bitvec", 1);
711 genBinOp(pred, op, lhs, rhs, 1);
717 Value pred = op.getCond();
718 Value tval = op.getTrueValue();
719 Value fval = op.getFalseValue();
723 int64_t w = requireSort(op.getType());
726 genIte(op, pred, tval, fval, w);
729 void visitComb(Operation *op) { visitInvalidComb(op); }
732 void visitInvalidComb(Operation *op) { dispatchSVVisitor(op); }
735 void visitSV(sv::AssertOp op) {
737 Value expr = op.getExpression();
740 genSort(
"bitvec", 1);
746 if (
auto ifop = dyn_cast<sv::IfOp>(((Operation *)op)->getParentOp())) {
747 Value
en = ifop.getOperand();
750 genImplies(ifop,
en, expr);
753 genUnaryOp(op, ifop,
"not", 1);
756 genUnaryOp(op, expr,
"not", 1);
763 void visitSV(sv::AssumeOp op) {
765 Value expr = op.getExpression();
769 void visitSV(Operation *op) { visitInvalidSV(op); }
772 void visitInvalidSV(Operation *op) { dispatchVerifVisitor(op); }
774 template <
typename Op>
775 void visitAssertLike(Op op) {
777 Value prop = op.getProperty();
778 Value
en = op.getEnable();
781 genSort(
"bitvec", 1);
783 size_t assertLID = noLID;
787 genImplies(op,
en, prop);
790 assertLID = genUnaryOp(op,
"not", 1);
793 assertLID = genUnaryOp(prop.getDefiningOp(),
"not", 1);
800 template <
typename Op>
801 void visitAssumeLike(Op op) {
803 Value prop = op.getProperty();
804 Value
en = op.getEnable();
806 size_t assumeLID = getOpLID(prop);
810 genSort(
"bitvec", 1);
812 assumeLID = genImplies(op,
en, prop);
816 genConstraint(assumeLID);
821 void visitVerif(verif::AssertOp op) { visitAssertLike(op); }
822 void visitVerif(verif::ClockedAssertOp op) { visitAssertLike(op); }
826 void visitVerif(verif::AssumeOp op) { visitAssumeLike(op); }
827 void visitVerif(verif::ClockedAssumeOp op) { visitAssumeLike(op); }
830 void visitVerif(verif::CoverOp op) {
831 op->emitError(
"Cover is not supprted in btor2!");
832 return signalPassFailure();
835 void visitVerif(verif::ClockedCoverOp op) {
836 op->emitError(
"Cover is not supprted in btor2!");
837 return signalPassFailure();
840 void visitInvalidVerif(Operation *op) { visit(op); }
844 void visit(Operation *op) {
847 TypeSwitch<Operation *, void>(op)
848 .Case<seq::FirRegOp,
seq::CompRegOp>([&](
auto expr) { visit(expr); })
849 .Default([&](
auto expr) { visitUnsupportedOp(op); });
855 void visit(seq::FirRegOp
reg) {
857 StringRef regName =
reg.getName();
858 int64_t w = requireSort(
reg.getType());
861 genState(
reg, w, regName);
866 regOps.push_back(
reg);
872 StringRef regName =
reg.getName().value();
873 int64_t w = requireSort(
reg.getType());
877 auto init =
reg.getInitialValue();
880 genState(
reg, w, regName);
886 if (!initialConstant)
887 reg->emitError(
"PowerOn Value must be constant!!");
890 dispatchTypeOpVisitor(initialConstant);
893 handledOps.insert(initialConstant);
896 genInit(
reg, initialConstant, w);
902 regOps.push_back(
reg);
907 void ignore(Operation *op) {}
911 void visitUnsupportedOp(Operation *op) {
914 TypeSwitch<Operation *, void>(op)
916 .Case<sv::MacroDefOp, sv::MacroDeclOp, sv::VerbatimOp,
917 sv::VerbatimExprOp, sv::VerbatimExprSEOp, sv::IfOp,
sv::IfDefOp,
918 sv::IfDefProceduralOp, sv::AlwaysOp, sv::AlwaysCombOp,
919 seq::InitialOp, sv::AlwaysFFOp, seq::FromClockOp, seq::InitialOp,
921 [&](
auto expr) { ignore(op); })
924 .Case<seq::FromClockOp>([&](
auto expr) {
925 if (++nclocks > 1UL) {
926 op->emitOpError(
"Mutli-clock designs are not supported!");
927 return signalPassFailure();
933 .Default([&](
auto expr) {
934 op->emitOpError(
"is an unsupported operation");
935 return signalPassFailure();
941 void ConvertHWToBTOR2Pass::runOnOperation() {
947 for (
auto &port : module.getPortList()) {
952 module.walk([&](Operation *op) {
953 TypeSwitch<Operation *, void>(op)
954 .Case<seq::FirRegOp, seq::CompRegOp>([&](auto reg) {
956 handledOps.insert(op);
958 .Default([&](
auto expr) {});
962 module.walk([&](Operation *op) {
964 if (isa<hw::InstanceOp>(op)) {
965 op->emitOpError(
"not supported in BTOR2 conversion");
970 if (handledOps.contains(op))
974 worklist.insert({op, op->operand_begin()});
977 while (!worklist.empty()) {
978 auto &[op, operandIt] = worklist.back();
979 if (operandIt == op->operand_end()) {
981 dispatchTypeOpVisitor(op);
984 handledOps.insert(op);
991 Value operand = *(operandIt++);
992 auto *defOp = operand.getDefiningOp();
995 if (!defOp || handledOps.contains(defOp))
1000 if (!worklist.insert({defOp, defOp->operand_begin()}).second) {
1001 defOp->emitError(
"dependency cycle");
1008 for (
size_t i = 0; i < regOps.size(); ++i) {
1009 finalizeRegVisit(regOps[i]);
1013 sortToLIDMap.clear();
1014 constToLIDMap.clear();
1023 std::unique_ptr<mlir::Pass>
1025 return std::make_unique<ConvertHWToBTOR2Pass>(os);
1030 return std::make_unique<ConvertHWToBTOR2Pass>(llvm::outs());
assert(baseType &&"element must be base type")
int64_t getBitWidth(mlir::Type type)
Return the hardware bit width of a type.
Value unwrapImmutableValue(mlir::TypedValue< seq::ImmutableType > immutableVal)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
std::unique_ptr< mlir::Pass > createConvertHWToBTOR2Pass(llvm::raw_ostream &os)
def reg(value, clock, reset=None, reset_value=None, name=None, sym_name=None)