237 bool emitWireAtBlockBegin =
false) {
238 Block *block = op.getBlock();
239 auto builder = ImplicitLocOpBuilder::atBlockBegin(op.getLoc(), block);
242 auto createWireForResult = [&](Value result, StringAttr name) {
244 Type wireElementType = result.getType();
245 bool isResultInOut =
false;
248 if (
auto inoutType = hw::type_dyn_cast<hw::InOutType>(result.getType())) {
249 wireElementType = inoutType.getElementType();
250 isResultInOut =
true;
251 }
else if (
auto handleElementType =
252 sv::getHandleElementType(result.getType())) {
253 wireElementType = handleElementType;
254 isResultInOut =
true;
258 if (isProceduralRegion)
265 while (!result.use_empty()) {
266 OpOperand &use = *result.getUses().begin();
271 use.set(newWireRead);
272 newWireRead->moveBefore(use.getOwner());
283 if (isProceduralRegion)
284 connect = BPAssignOp::create(
285 builder, newWire, isResultInOut ? resultRead.getResult() : result);
288 builder, newWire, isResultInOut ? resultRead.getResult() : result);
290 connect->moveAfter(&op);
292 resultRead->moveBefore(connect);
295 if (!emitWireAtBlockBegin) {
301 newWire.getDefiningOp()->moveAfter(&op);
307 if (op.getNumResults() == 1) {
309 op.removeAttr(
"sv.namehint");
310 createWireForResult(op.getResult(0), namehint);
315 for (
auto result : op.getResults())
316 createWireForResult(result, StringAttr());
872 ArrayRef<WireLowering> wireLowerings) {
873 bool isProceduralRegion = block.getParentOp()->hasTrait<
ProceduralRegion>();
875 for (
auto [hwWireOp, declarePoint, assignPoint] : wireLowerings) {
877 ImplicitLocOpBuilder builder(hwWireOp.getLoc(), &block, declarePoint);
879 if (isProceduralRegion) {
882 hwWireOp.getInnerSymAttr());
885 hwWireOp.getNameAttr(),
886 hwWireOp.getInnerSymAttr());
890 auto defaultAttrNames = hwWireOp.getAttributeNames();
891 for (
auto namedAttr : hwWireOp->getAttrs())
892 if (!llvm::is_contained(defaultAttrNames, namedAttr.getName()))
893 decl.getDefiningOp()->setAttr(namedAttr.getName(),
894 namedAttr.getValue());
899 if (assignPoint != declarePoint)
900 builder.setInsertionPoint(&block, assignPoint);
901 if (isProceduralRegion)
902 BPAssignOp::create(builder, decl, hwWireOp.getInput());
910 if (assignPoint != declarePoint)
911 builder.setInsertionPointAfterValue(decl);
915 hwWireOp.replaceAllUsesWith(readOp.getResult());
918 for (
auto [hwWireOp, declarePoint, assignPoint] : wireLowerings)
929 for (
auto &op : block) {
931 for (
auto ®ion : op.getRegions()) {
942 SmallVector<WireLowering> wireLowerings;
950 bool isProceduralRegion = block.getParentOp()->hasTrait<
ProceduralRegion>();
954 DenseSet<Operation *> visitedAlwaysInlineOperations;
958 SmallVector<Operation *> debugOpsToMoveToEnd;
960 for (Block::iterator opIterator = block.begin(), e = block.end();
962 auto &op = *opIterator++;
964 if (!isa<CombDialect, SVDialect, HWDialect, ltl::LTLDialect,
965 verif::VerifDialect, debug::DebugDialect>(op.getDialect())) {
966 auto d = op.emitError() <<
"dialect \"" << op.getDialect()->getNamespace()
967 <<
"\" not supported for direct Verilog emission";
968 d.attachNote() <<
"ExportVerilog cannot emit this operation; it needs "
969 "to be lowered before running ExportVerilog";
974 if (isa<ltl::LTLDialect>(op.getDialect()))
978 if (isa<debug::DebugDialect>(op.getDialect())) {
979 debugOpsToMoveToEnd.push_back(&op);
988 if (
auto inst = dyn_cast<HWInstanceLike>(op)) {
997 if (
auto call = dyn_cast<mlir::CallOpInterface>(op))
1002 if (isProceduralRegion && isa<LogicOp, RegOp, sv::VarOp>(op)) {
1007 op.moveBefore(parentOp);
1014 isProceduralRegion) {
1020 if (!mlir::isMemoryEffectFree(&op)) {
1040 if (op.use_empty()) {
1046 if (visitedAlwaysInlineOperations.insert(&op).second)
1052 if (
auto aggregateConstantOp = dyn_cast<hw::AggregateConstantOp>(op);
1054 if (hw::StructType structType =
1057 SmallVector<Value> operands;
1058 ImplicitLocOpBuilder builder(op.getLoc(), op.getContext());
1059 builder.setInsertionPointAfter(&op);
1060 for (
auto [value, field] :
1061 llvm::zip(aggregateConstantOp.getFieldsAttr(),
1062 structType.getElements())) {
1063 if (
auto arrayAttr = dyn_cast<mlir::ArrayAttr>(value))
1064 operands.push_back(hw::AggregateConstantOp::create(
1065 builder, field.type, arrayAttr));
1068 builder, field.type, cast<mlir::IntegerAttr>(value)));
1073 aggregateConstantOp.getResult().replaceAllUsesWith(structCreate);
1075 opIterator = std::next(op.getIterator());
1082 if (
auto structCreateOp = dyn_cast<hw::StructCreateOp>(op);
1087 ImplicitLocOpBuilder builder(op.getLoc(), &op);
1088 hw::StructType structType =
1089 cast<hw::StructType>(structCreateOp.getResult().getType());
1096 for (
auto [input, field] :
1097 llvm::zip(structCreateOp.getInput(), structType.getElements())) {
1099 sv::StructFieldInOutOp::create(builder, wireOp, field.name);
1101 BPAssignOp::create(builder, target, input);
1107 llvm::make_early_inc_range(structCreateOp.getResult().getUses()))
1110 structCreateOp.erase();
1116 if (
auto arrayInjectOp = dyn_cast<hw::ArrayInjectOp>(op)) {
1118 ImplicitLocOpBuilder builder(op.getLoc(), &op);
1124 for (
auto &use : llvm::make_early_inc_range(arrayInjectOp->getUses()))
1130 auto alwaysOp = sv::AlwaysCombOp::create(builder);
1131 builder.setInsertionPointToStart(alwaysOp.getBodyBlock());
1135 sv::BPAssignOp::create(builder, decl, arrayInjectOp.getInput());
1138 auto target = sv::ArrayIndexInOutOp::create(builder, decl,
1139 arrayInjectOp.getIndex());
1140 sv::BPAssignOp::create(builder, target, arrayInjectOp.getElement());
1142 arrayInjectOp.erase();
1150 IndexedPartSelectInOutOp, IndexedPartSelectOp>(&op)) {
1155 if (
auto maybeReadOp =
1157 if (isa_and_nonnull<sv::WireOp, LogicOp, sv::VarOp>(
1158 maybeReadOp.getInput().getDefiningOp())) {
1159 wireOp = maybeReadOp.getInput();
1160 readOp = maybeReadOp;
1165 ImplicitLocOpBuilder builder(op.getLoc(), &op);
1167 auto type = op.getOperand(1).getType();
1168 const auto *name =
"_GEN_ARRAY_IDX";
1172 BPAssignOp::create(builder, wireOp, op.getOperand(1));
1179 op.setOperand(1, readOp);
1182 sv::addSVAttributes(wireOp.getDefiningOp(),
1183 SVAttributeAttr::get(wireOp.getContext(),
"keep",
1197 if (!isProceduralRegion ||
1208 if (isProceduralRegion)
1226 if (op.getNumOperands() != 2 && op.getNumResults() == 1 &&
1227 op.hasTrait<mlir::OpTrait::IsCommutative>() &&
1228 mlir::isMemoryEffectFree(&op) && op.getNumRegions() == 0 &&
1229 op.getNumSuccessors() == 0 &&
1230 llvm::all_of(op.getAttrs(), [](NamedAttribute attr) {
1231 return attr.getNameDialect() != nullptr ||
1232 attr.getName() ==
"twoState";
1235 SmallVector<Operation *> newOps;
1237 op.getResult(0).replaceAllUsesWith(result);
1241 if (!newOps.empty())
1242 opIterator = Block::iterator(newOps.front());
1247 if (
auto addOp = dyn_cast<comb::AddOp>(op)) {
1248 if (
auto cst = addOp.getOperand(1).getDefiningOp<
hw::ConstantOp>()) {
1249 assert(addOp.getNumOperands() == 2 &&
"commutative lowering is done");
1250 if (cst.getValue().isNegative()) {
1252 opIterator = Block::iterator(firstOp);
1260 if (
auto structExplodeOp = dyn_cast<hw::StructExplodeOp>(op)) {
1262 opIterator = Block::iterator(firstOp);
1274 auto debugBuilder = OpBuilder::atBlockEnd(&block);
1275 if (!block.empty() && block.back().mightHaveTrait<OpTrait::IsTerminator>())
1276 debugBuilder.setInsertionPoint(&block.back());
1277 for (
auto *op : debugOpsToMoveToEnd) {
1279 debugBuilder.insert(op);
1282 if (isProceduralRegion) {
1291 std::pair<Block *, Block::iterator> logicOpInsertionPoint =
1293 for (
auto &op : llvm::make_early_inc_range(block)) {
1294 if (isa<LogicOp, VarOp>(op)) {
1297 if (logicOpInsertionPoint.second == op.getIterator()) {
1298 ++logicOpInsertionPoint.second;
1302 op.moveBefore(logicOpInsertionPoint.first,
1303 logicOpInsertionPoint.second);
1313 SmallPtrSet<Operation *, 32> seenOperations;
1315 for (
auto &op : llvm::make_early_inc_range(block)) {
1318 if (isa<ltl::LTLDialect, debug::DebugDialect>(op.getDialect()))
1324 bool haveAnyOutOfOrderUses =
false;
1325 for (
auto *userOp : op.getUsers()) {
1328 while (&block != &userOp->getParentRegion()->front())
1329 userOp = userOp->getParentOp();
1331 if (seenOperations.count(userOp)) {
1332 haveAnyOutOfOrderUses =
true;
1338 seenOperations.insert(&op);
1341 if (!haveAnyOutOfOrderUses)
1347 op.moveBefore(&block.front());
1353 op.moveBefore(&block.front());
1359 if (
auto readInOut = dyn_cast<ReadInOutOp>(op)) {
1360 auto *def = readInOut.getInput().getDefiningOp();
1362 op.moveBefore(&block.front());
1363 def->moveBefore(&block.front());