11#include "mlir/Dialect/Func/IR/FuncOps.h"
12#include "mlir/IR/Builders.h"
13#include "mlir/IR/Diagnostics.h"
14#include "slang/ast/Compilation.h"
15#include "slang/ast/SemanticFacts.h"
16#include "slang/ast/Statement.h"
17#include "slang/ast/SystemSubroutine.h"
18#include "llvm/ADT/ScopeExit.h"
22using namespace ImportVerilog;
34 bool isTerminated()
const {
return !builder.getInsertionBlock(); }
35 void setTerminated() { builder.clearInsertionPoint(); }
37 Block &createBlock() {
38 assert(builder.getInsertionBlock());
39 auto block = std::make_unique<Block>();
40 block->insertAfter(builder.getInsertionBlock());
41 return *block.release();
44 LogicalResult recursiveForeach(
const slang::ast::ForeachLoopStatement &stmt,
47 const auto &loopDim = stmt.loopDims[level];
48 if (!loopDim.range.has_value())
49 return mlir::emitError(loc) <<
"dynamic loop variable is unsupported";
50 auto &exitBlock = createBlock();
51 auto &stepBlock = createBlock();
52 auto &bodyBlock = createBlock();
53 auto &checkBlock = createBlock();
56 context.loopStack.push_back({&stepBlock, &exitBlock});
57 llvm::scope_exit done([&] {
context.loopStack.pop_back(); });
59 const auto &iter = loopDim.loopVar;
60 auto type =
context.convertType(*iter->getDeclaredType());
64 Value initial = moore::ConstantOp::create(
65 builder, loc, cast<moore::IntType>(type), loopDim.range->lower());
68 Value varOp = moore::VariableOp::create(
69 builder, loc, moore::RefType::get(cast<moore::UnpackedType>(type)),
70 builder.getStringAttr(iter->name), initial);
71 context.valueSymbols.insertIntoScope(
context.valueSymbols.getCurScope(),
74 cf::BranchOp::create(builder, loc, &checkBlock);
75 builder.setInsertionPointToEnd(&checkBlock);
78 auto upperBound = moore::ConstantOp::create(
79 builder, loc, cast<moore::IntType>(type), loopDim.range->upper());
81 auto var = moore::ReadOp::create(builder, loc, varOp);
82 Value cond = moore::SleOp::create(builder, loc, var, upperBound);
85 cond = builder.createOrFold<moore::BoolCastOp>(loc, cond);
86 if (
auto ty = dyn_cast<moore::IntType>(cond.getType());
87 ty && ty.getDomain() == Domain::FourValued) {
88 cond = moore::LogicToIntOp::create(builder, loc, cond);
90 cond = moore::ToBuiltinIntOp::create(builder, loc, cond);
91 cf::CondBranchOp::create(builder, loc, cond, &bodyBlock, &exitBlock);
93 builder.setInsertionPointToEnd(&bodyBlock);
98 for (uint32_t nextLevel = level + 1; nextLevel < stmt.loopDims.size();
100 if (stmt.loopDims[nextLevel].loopVar) {
101 if (failed(recursiveForeach(stmt, nextLevel)))
109 if (failed(
context.convertStatement(stmt.body)))
113 cf::BranchOp::create(builder, loc, &stepBlock);
115 builder.setInsertionPointToEnd(&stepBlock);
118 var = moore::ReadOp::create(builder, loc, varOp);
120 moore::ConstantOp::create(builder, loc, cast<moore::IntType>(type), 1);
121 auto postValue = moore::AddOp::create(builder, loc, var, one).getResult();
122 moore::BlockingAssignOp::create(builder, loc, varOp, postValue);
123 cf::BranchOp::create(builder, loc, &checkBlock);
125 if (exitBlock.hasNoPredecessors()) {
129 builder.setInsertionPointToEnd(&exitBlock);
135 LogicalResult visit(
const slang::ast::EmptyStatement &) {
return success(); }
144 LogicalResult visit(
const slang::ast::StatementList &stmts) {
145 for (
auto *stmt : stmts.list) {
146 if (isTerminated()) {
147 auto loc =
context.convertLocation(stmt->sourceRange);
148 mlir::emitWarning(loc,
"unreachable code");
151 if (failed(
context.convertStatement(*stmt)))
161 LogicalResult visit(
const slang::ast::BlockStatement &stmt) {
162 moore::JoinKind kind;
163 switch (stmt.blockKind) {
164 case slang::ast::StatementBlockKind::Sequential:
166 return context.convertStatement(stmt.body);
167 case slang::ast::StatementBlockKind::JoinAll:
168 kind = moore::JoinKind::Join;
170 case slang::ast::StatementBlockKind::JoinAny:
171 kind = moore::JoinKind::JoinAny;
173 case slang::ast::StatementBlockKind::JoinNone:
174 kind = moore::JoinKind::JoinNone;
181 auto *threadList = stmt.body.as_if<slang::ast::StatementList>();
182 unsigned int threadCount = threadList ? threadList->list.size() : 1;
184 auto forkOp = moore::ForkJoinOp::create(builder, loc, kind, threadCount);
185 OpBuilder::InsertionGuard guard(builder);
190 auto &tBlock = forkOp->getRegion(0).emplaceBlock();
191 builder.setInsertionPointToStart(&tBlock);
192 if (failed(
context.convertStatement(stmt.body)))
194 moore::CompleteOp::create(builder, loc);
199 for (
auto *thread : threadList->list) {
200 auto &tBlock = forkOp->getRegion(i).emplaceBlock();
201 builder.setInsertionPointToStart(&tBlock);
204 if (failed(
context.convertStatement(*thread)))
206 moore::CompleteOp::create(builder, loc);
213 LogicalResult visit(
const slang::ast::ExpressionStatement &stmt) {
215 if (
const auto *call = stmt.expr.as_if<slang::ast::CallExpression>()) {
216 if (
const auto *info =
217 std::get_if<slang::ast::CallExpression::SystemCallInfo>(
218 &call->subroutine)) {
219 auto handled = visitSystemCall(stmt, *call, *info);
233 if (!call->getSubroutineName().compare(
"$sformat") ||
234 !call->getSubroutineName().compare(
"$swrite")) {
237 auto *lhsExpr = call->arguments().front();
240 context.convertFormatString(call->arguments().subspan(1), loc,
241 moore::IntFormat::Decimal,
false);
242 if (failed(fmtValue))
245 auto strValue = moore::FormatStringToStringOp::create(builder, loc,
251 if (
auto assignExpr =
252 lhsExpr->as_if<slang::ast::AssignmentExpression>()) {
253 auto lhs =
context.convertLvalueExpression(assignExpr->left());
257 auto convertedValue =
context.materializeConversion(
258 cast<moore::RefType>(lhs.getType()).getNestedType(), strValue,
260 moore::BlockingAssignOp::create(builder, loc, lhs, convertedValue);
268 auto value =
context.convertRvalueExpression(stmt.expr);
274 if (
auto *defOp = value.getDefiningOp())
275 if (isOpTriviallyDead(defOp))
282 LogicalResult visit(
const slang::ast::VariableDeclStatement &stmt) {
283 const auto &var = stmt.symbol;
284 auto type =
context.convertType(*var.getDeclaredType());
289 if (
const auto *init = var.getInitializer()) {
290 initial =
context.convertRvalueExpression(*init, type);
296 auto varOp = moore::VariableOp::create(
297 builder, loc, moore::RefType::get(cast<moore::UnpackedType>(type)),
298 builder.getStringAttr(var.name), initial);
299 context.valueSymbols.insertIntoScope(
context.valueSymbols.getCurScope(),
301 const auto &canonTy = var.getType().getCanonicalType();
302 if (
const auto *vi = canonTy.as_if<slang::ast::VirtualInterfaceType>())
303 if (failed(
context.registerVirtualInterfaceMembers(var, *vi, loc)))
309 LogicalResult visit(
const slang::ast::ConditionalStatement &stmt) {
313 for (
const auto &condition : stmt.conditions) {
314 if (condition.pattern)
315 return mlir::emitError(loc,
316 "match patterns in if conditions not supported");
317 auto cond =
context.convertRvalueExpression(*condition.expr);
320 cond = builder.createOrFold<moore::BoolCastOp>(loc, cond);
322 allConds = moore::AndOp::create(builder, loc, allConds, cond);
326 assert(allConds &&
"slang guarantees at least one condition");
327 if (
auto ty = dyn_cast<moore::IntType>(allConds.getType());
328 ty && ty.getDomain() == Domain::FourValued) {
329 allConds = moore::LogicToIntOp::create(builder, loc, allConds);
331 allConds = moore::ToBuiltinIntOp::create(builder, loc, allConds);
334 Block &exitBlock = createBlock();
335 Block *falseBlock = stmt.ifFalse ? &createBlock() : nullptr;
336 Block &trueBlock = createBlock();
337 cf::CondBranchOp::create(builder, loc, allConds, &trueBlock,
338 falseBlock ? falseBlock : &exitBlock);
341 builder.setInsertionPointToEnd(&trueBlock);
342 if (failed(
context.convertStatement(stmt.ifTrue)))
345 cf::BranchOp::create(builder, loc, &exitBlock);
349 builder.setInsertionPointToEnd(falseBlock);
350 if (failed(
context.convertStatement(*stmt.ifFalse)))
353 cf::BranchOp::create(builder, loc, &exitBlock);
358 if (exitBlock.hasNoPredecessors()) {
362 builder.setInsertionPointToEnd(&exitBlock);
368 LogicalResult visit(
const slang::ast::CaseStatement &caseStmt) {
369 using slang::ast::AttributeSymbol;
370 using slang::ast::CaseStatementCondition;
371 auto caseExpr =
context.convertRvalueExpression(caseStmt.expr);
378 auto &exitBlock = createBlock();
379 Block *lastMatchBlock =
nullptr;
380 SmallVector<moore::FVIntegerAttr> itemConsts;
382 for (
const auto &item : caseStmt.items) {
385 auto &matchBlock = createBlock();
386 lastMatchBlock = &matchBlock;
391 for (
const auto *expr : item.expressions) {
392 auto value =
context.convertRvalueExpression(*expr);
395 auto itemLoc = value.getLoc();
398 auto maybeConst = value;
399 while (isa_and_nonnull<moore::ConversionOp, moore::IntToLogicOp,
400 moore::LogicToIntOp>(maybeConst.getDefiningOp()))
401 maybeConst = maybeConst.getDefiningOp()->getOperand(0);
402 if (
auto defOp = maybeConst.getDefiningOp<moore::ConstantOp>())
403 itemConsts.push_back(defOp.getValueAttr());
407 switch (caseStmt.condition) {
408 case CaseStatementCondition::Normal:
409 cond = moore::CaseEqOp::create(builder, itemLoc, caseExpr, value);
411 case CaseStatementCondition::WildcardXOrZ:
412 cond = moore::CaseXZEqOp::create(builder, itemLoc, caseExpr, value);
414 case CaseStatementCondition::WildcardJustZ:
415 cond = moore::CaseZEqOp::create(builder, itemLoc, caseExpr, value);
417 case CaseStatementCondition::Inside:
418 mlir::emitError(loc,
"unsupported set membership case statement");
421 if (
auto ty = dyn_cast<moore::IntType>(cond.getType());
422 ty && ty.getDomain() == Domain::FourValued) {
423 cond = moore::LogicToIntOp::create(builder, loc, cond);
425 cond = moore::ToBuiltinIntOp::create(builder, loc, cond);
429 auto &nextBlock = createBlock();
430 mlir::cf::CondBranchOp::create(builder, itemLoc, cond, &matchBlock,
432 builder.setInsertionPointToEnd(&nextBlock);
438 matchBlock.moveBefore(builder.getInsertionBlock());
441 OpBuilder::InsertionGuard guard(builder);
442 builder.setInsertionPointToEnd(&matchBlock);
443 if (failed(
context.convertStatement(*item.stmt)))
445 if (!isTerminated()) {
446 auto loc =
context.convertLocation(item.stmt->sourceRange);
447 mlir::cf::BranchOp::create(builder, loc, &exitBlock);
451 const auto caseStmtAttrs =
context.compilation.getAttributes(caseStmt);
452 const bool hasFullCaseAttr =
453 llvm::find_if(caseStmtAttrs, [](
const AttributeSymbol *attr) {
454 return attr->name ==
"full_case";
455 }) != caseStmtAttrs.end();
466 auto twoStateExhaustive =
false;
467 if (
auto intType = dyn_cast<moore::IntType>(caseExpr.getType());
468 intType && intType.getWidth() < 32 &&
469 itemConsts.size() == (1 << intType.getWidth())) {
471 llvm::sort(itemConsts, [](
auto a,
auto b) {
472 return a.getValue().getRawValue().ult(
b.getValue().getRawValue());
480 for (
auto value : itemConsts) {
481 if (value.getValue() != nextValue)
485 twoStateExhaustive = nextValue.isZero();
496 if ((twoStateExhaustive || (hasFullCaseAttr && !caseStmt.defaultCase)) &&
498 caseStmt.condition == CaseStatementCondition::Normal) {
499 mlir::cf::BranchOp::create(builder, loc, lastMatchBlock);
502 if (caseStmt.defaultCase)
503 if (failed(
context.convertStatement(*caseStmt.defaultCase)))
506 mlir::cf::BranchOp::create(builder, loc, &exitBlock);
511 if (exitBlock.hasNoPredecessors()) {
515 builder.setInsertionPointToEnd(&exitBlock);
521 LogicalResult visit(
const slang::ast::ForLoopStatement &stmt) {
523 for (
auto *initExpr : stmt.initializers)
524 if (!
context.convertRvalueExpression(*initExpr))
528 auto &exitBlock = createBlock();
529 auto &stepBlock = createBlock();
530 auto &bodyBlock = createBlock();
531 auto &checkBlock = createBlock();
532 cf::BranchOp::create(builder, loc, &checkBlock);
535 context.loopStack.push_back({&stepBlock, &exitBlock});
536 llvm::scope_exit done([&] {
context.loopStack.pop_back(); });
539 builder.setInsertionPointToEnd(&checkBlock);
540 auto cond =
context.convertRvalueExpression(*stmt.stopExpr);
543 cond = builder.createOrFold<moore::BoolCastOp>(loc, cond);
544 if (
auto ty = dyn_cast<moore::IntType>(cond.getType());
545 ty && ty.getDomain() == Domain::FourValued) {
546 cond = moore::LogicToIntOp::create(builder, loc, cond);
548 cond = moore::ToBuiltinIntOp::create(builder, loc, cond);
549 cf::CondBranchOp::create(builder, loc, cond, &bodyBlock, &exitBlock);
552 builder.setInsertionPointToEnd(&bodyBlock);
553 if (failed(
context.convertStatement(stmt.body)))
556 cf::BranchOp::create(builder, loc, &stepBlock);
559 builder.setInsertionPointToEnd(&stepBlock);
560 for (
auto *stepExpr : stmt.steps)
561 if (!
context.convertRvalueExpression(*stepExpr))
564 cf::BranchOp::create(builder, loc, &checkBlock);
568 if (exitBlock.hasNoPredecessors()) {
572 builder.setInsertionPointToEnd(&exitBlock);
577 LogicalResult visit(
const slang::ast::ForeachLoopStatement &stmt) {
578 for (uint32_t level = 0; level < stmt.loopDims.size(); level++) {
579 if (stmt.loopDims[level].loopVar)
580 return recursiveForeach(stmt, level);
586 LogicalResult visit(
const slang::ast::RepeatLoopStatement &stmt) {
587 auto intType = moore::IntType::getInt(
context.getContext(), 32);
588 auto count =
context.convertRvalueExpression(stmt.count, intType);
593 auto &exitBlock = createBlock();
594 auto &stepBlock = createBlock();
595 auto &bodyBlock = createBlock();
596 auto &checkBlock = createBlock();
597 auto currentCount = checkBlock.addArgument(count.getType(), count.getLoc());
598 cf::BranchOp::create(builder, loc, &checkBlock, count);
601 context.loopStack.push_back({&stepBlock, &exitBlock});
602 llvm::scope_exit done([&] {
context.loopStack.pop_back(); });
605 builder.setInsertionPointToEnd(&checkBlock);
606 auto cond = builder.createOrFold<moore::BoolCastOp>(loc, currentCount);
607 if (
auto ty = dyn_cast<moore::IntType>(cond.getType());
608 ty && ty.getDomain() == Domain::FourValued) {
609 cond = moore::LogicToIntOp::create(builder, loc, cond);
611 cond = moore::ToBuiltinIntOp::create(builder, loc, cond);
612 cf::CondBranchOp::create(builder, loc, cond, &bodyBlock, &exitBlock);
615 builder.setInsertionPointToEnd(&bodyBlock);
616 if (failed(
context.convertStatement(stmt.body)))
619 cf::BranchOp::create(builder, loc, &stepBlock);
622 builder.setInsertionPointToEnd(&stepBlock);
623 auto one = moore::ConstantOp::create(
624 builder, count.getLoc(), cast<moore::IntType>(count.getType()), 1);
626 moore::SubOp::create(builder, count.getLoc(), currentCount, one);
627 cf::BranchOp::create(builder, loc, &checkBlock, nextCount);
631 if (exitBlock.hasNoPredecessors()) {
635 builder.setInsertionPointToEnd(&exitBlock);
641 LogicalResult createWhileLoop(
const slang::ast::Expression &condExpr,
642 const slang::ast::Statement &bodyStmt,
645 auto &exitBlock = createBlock();
646 auto &bodyBlock = createBlock();
647 auto &checkBlock = createBlock();
648 cf::BranchOp::create(builder, loc, atLeastOnce ? &bodyBlock : &checkBlock);
650 bodyBlock.moveBefore(&checkBlock);
653 context.loopStack.push_back({&checkBlock, &exitBlock});
654 llvm::scope_exit done([&] {
context.loopStack.pop_back(); });
657 builder.setInsertionPointToEnd(&checkBlock);
658 auto cond =
context.convertRvalueExpression(condExpr);
661 cond = builder.createOrFold<moore::BoolCastOp>(loc, cond);
662 if (
auto ty = dyn_cast<moore::IntType>(cond.getType());
663 ty && ty.getDomain() == Domain::FourValued) {
664 cond = moore::LogicToIntOp::create(builder, loc, cond);
666 cond = moore::ToBuiltinIntOp::create(builder, loc, cond);
667 cf::CondBranchOp::create(builder, loc, cond, &bodyBlock, &exitBlock);
670 builder.setInsertionPointToEnd(&bodyBlock);
671 if (failed(
context.convertStatement(bodyStmt)))
674 cf::BranchOp::create(builder, loc, &checkBlock);
678 if (exitBlock.hasNoPredecessors()) {
682 builder.setInsertionPointToEnd(&exitBlock);
687 LogicalResult visit(
const slang::ast::WhileLoopStatement &stmt) {
688 return createWhileLoop(stmt.cond, stmt.body,
false);
691 LogicalResult visit(
const slang::ast::DoWhileLoopStatement &stmt) {
692 return createWhileLoop(stmt.cond, stmt.body,
true);
696 LogicalResult visit(
const slang::ast::ForeverLoopStatement &stmt) {
698 auto &exitBlock = createBlock();
699 auto &bodyBlock = createBlock();
700 cf::BranchOp::create(builder, loc, &bodyBlock);
703 context.loopStack.push_back({&bodyBlock, &exitBlock});
704 llvm::scope_exit done([&] {
context.loopStack.pop_back(); });
707 builder.setInsertionPointToEnd(&bodyBlock);
708 if (failed(
context.convertStatement(stmt.body)))
711 cf::BranchOp::create(builder, loc, &bodyBlock);
715 if (exitBlock.hasNoPredecessors()) {
719 builder.setInsertionPointToEnd(&exitBlock);
725 LogicalResult visit(
const slang::ast::TimedStatement &stmt) {
726 return context.convertTimingControl(stmt.timing, stmt.stmt);
730 LogicalResult visit(
const slang::ast::ReturnStatement &stmt) {
732 auto expr =
context.convertRvalueExpression(*stmt.expr);
735 mlir::func::ReturnOp::create(builder, loc, expr);
737 mlir::func::ReturnOp::create(builder, loc);
744 LogicalResult visit(
const slang::ast::ContinueStatement &stmt) {
746 return mlir::emitError(loc,
747 "cannot `continue` without a surrounding loop");
748 cf::BranchOp::create(builder, loc,
context.loopStack.back().continueBlock);
754 LogicalResult visit(
const slang::ast::BreakStatement &stmt) {
756 return mlir::emitError(loc,
"cannot `break` without a surrounding loop");
757 cf::BranchOp::create(builder, loc,
context.loopStack.back().breakBlock);
763 LogicalResult visit(
const slang::ast::ImmediateAssertionStatement &stmt) {
764 auto cond =
context.convertRvalueExpression(stmt.cond);
765 cond =
context.convertToBool(cond);
770 if (stmt.ifTrue && stmt.ifTrue->as_if<slang::ast::EmptyStatement>()) {
771 auto defer = moore::DeferAssert::Immediate;
773 defer = moore::DeferAssert::Final;
774 else if (stmt.isDeferred)
775 defer = moore::DeferAssert::Observed;
777 switch (stmt.assertionKind) {
778 case slang::ast::AssertionKind::Assert:
779 moore::AssertOp::create(builder, loc, defer, cond, StringAttr{});
781 case slang::ast::AssertionKind::Assume:
782 moore::AssumeOp::create(builder, loc, defer, cond, StringAttr{});
784 case slang::ast::AssertionKind::CoverProperty:
785 moore::CoverOp::create(builder, loc, defer, cond, StringAttr{});
790 mlir::emitError(loc) <<
"unsupported immediate assertion kind: "
791 << slang::ast::toString(stmt.assertionKind);
796 if (
auto ty = dyn_cast<moore::IntType>(cond.getType());
797 ty && ty.getDomain() == Domain::FourValued) {
798 cond = moore::LogicToIntOp::create(builder, loc, cond);
800 cond = moore::ToBuiltinIntOp::create(builder, loc, cond);
803 Block &exitBlock = createBlock();
804 Block *falseBlock = stmt.ifFalse ? &createBlock() : nullptr;
805 Block &trueBlock = createBlock();
806 cf::CondBranchOp::create(builder, loc, cond, &trueBlock,
807 falseBlock ? falseBlock : &exitBlock);
810 builder.setInsertionPointToEnd(&trueBlock);
811 if (stmt.ifTrue && failed(
context.convertStatement(*stmt.ifTrue)))
814 cf::BranchOp::create(builder, loc, &exitBlock);
818 builder.setInsertionPointToEnd(falseBlock);
819 if (failed(
context.convertStatement(*stmt.ifFalse)))
822 cf::BranchOp::create(builder, loc, &exitBlock);
827 if (exitBlock.hasNoPredecessors()) {
831 builder.setInsertionPointToEnd(&exitBlock);
837 LogicalResult visit(
const slang::ast::ConcurrentAssertionStatement &stmt) {
838 auto loc =
context.convertLocation(stmt.sourceRange);
849 const slang::ast::AssertionExpr *propertySpec;
850 const slang::ast::ClockingAssertionExpr *clocking =
851 stmt.propertySpec.as_if<slang::ast::ClockingAssertionExpr>();
853 propertySpec = &(clocking->expr);
855 propertySpec = &(stmt.propertySpec);
857 if (
auto *disableIff =
858 propertySpec->as_if<slang::ast::DisableIffAssertionExpr>()) {
861 auto disableCond =
context.convertRvalueExpression(disableIff->condition);
862 auto enableCond = moore::NotOp::create(builder, loc, disableCond);
864 enable =
context.convertToI1(enableCond);
868 auto clockingExpr = slang::ast::ClockingAssertionExpr(
869 clocking->clocking, disableIff->expr);
870 property =
context.convertAssertionExpression(clockingExpr, loc);
872 property =
context.convertAssertionExpression(disableIff->expr, loc);
875 property =
context.convertAssertionExpression(stmt.propertySpec, loc);
882 if (stmt.ifTrue && stmt.ifTrue->as_if<slang::ast::EmptyStatement>()) {
883 switch (stmt.assertionKind) {
884 case slang::ast::AssertionKind::Assert:
885 verif::AssertOp::create(builder, loc, property, enable, StringAttr{});
887 case slang::ast::AssertionKind::Assume:
888 verif::AssumeOp::create(builder, loc, property, enable, StringAttr{});
893 mlir::emitError(loc) <<
"unsupported concurrent assertion kind: "
894 << slang::ast::toString(stmt.assertionKind);
899 <<
"concurrent assertion statements with action blocks "
900 "are not supported yet";
914 getDisplayMessage(std::span<const slang::ast::Expression *const> args) {
915 if (args.size() == 0)
924 if (args[0]->as_if<slang::ast::StringLiteral>()) {
925 return context.convertFormatString(args, loc);
928 if (args.size() == 1) {
929 return context.convertRvalueExpression(
930 *args[0], builder.getType<moore::FormatStringType>());
933 return emitError(loc) <<
"Failed to convert Display Message!";
940 visitSystemCall(
const slang::ast::ExpressionStatement &stmt,
941 const slang::ast::CallExpression &expr,
942 const slang::ast::CallExpression::SystemCallInfo &info) {
943 using ksn = slang::parsing::KnownSystemName;
944 const auto &subroutine = *
info.subroutine;
945 auto nameId = subroutine.knownNameId;
946 auto args = expr.arguments();
950 if (nameId == ksn::Stop) {
951 createFinishMessage(args.size() >= 1 ? args[0] : nullptr);
952 moore::StopBIOp::create(builder, loc);
956 if (nameId == ksn::Finish) {
957 createFinishMessage(args.size() >= 1 ? args[0] : nullptr);
958 moore::FinishBIOp::create(builder, loc, 0);
959 moore::UnreachableOp::create(builder, loc);
964 if (nameId == ksn::Exit) {
973 using moore::IntFormat;
974 bool isDisplay =
false;
975 bool appendNewline =
false;
976 IntFormat defaultFormat = IntFormat::Decimal;
980 appendNewline =
true;
984 appendNewline =
true;
985 defaultFormat = IntFormat::Binary;
989 appendNewline =
true;
990 defaultFormat = IntFormat::Octal;
994 appendNewline =
true;
995 defaultFormat = IntFormat::HexLower;
1002 defaultFormat = IntFormat::Binary;
1006 defaultFormat = IntFormat::Octal;
1010 defaultFormat = IntFormat::HexLower;
1018 context.convertFormatString(args, loc, defaultFormat, appendNewline);
1019 if (failed(message))
1021 if (*message == Value{})
1023 moore::DisplayBIOp::create(builder, loc, *message);
1028 using moore::Severity;
1029 std::optional<Severity> severity;
1030 if (nameId == ksn::Info)
1031 severity = Severity::Info;
1032 else if (nameId == ksn::Warning)
1033 severity = Severity::Warning;
1034 else if (nameId == ksn::Error)
1035 severity = Severity::Error;
1036 else if (nameId == ksn::Fatal)
1037 severity = Severity::Fatal;
1041 const slang::ast::Expression *verbosityExpr =
nullptr;
1042 if (severity == Severity::Fatal && args.size() >= 1) {
1043 verbosityExpr = args[0];
1044 args = args.subspan(1);
1047 FailureOr<Value> maybeMessage = getDisplayMessage(args);
1048 if (failed(maybeMessage))
1050 auto message = maybeMessage.value();
1052 if (message == Value{})
1053 message = moore::FormatLiteralOp::create(builder, loc,
"");
1054 moore::SeverityBIOp::create(builder, loc, *severity, message);
1057 if (severity == Severity::Fatal) {
1058 createFinishMessage(verbosityExpr);
1059 moore::FinishBIOp::create(builder, loc, 1);
1060 moore::UnreachableOp::create(builder, loc);
1068 if (args.size() >= 1 && args[0]->type->isQueue()) {
1069 auto queue =
context.convertLvalueExpression(*args[0]);
1073 if (nameId == ksn::Delete) {
1074 if (args.size() == 1) {
1075 moore::QueueClearOp::create(builder, loc, queue);
1078 if (args.size() == 2) {
1079 auto index =
context.convertRvalueExpression(*args[1]);
1080 moore::QueueDeleteOp::create(builder, loc, queue, index);
1083 }
else if (nameId == ksn::Insert && args.size() == 3) {
1084 auto index =
context.convertRvalueExpression(*args[1]);
1085 auto item =
context.convertRvalueExpression(*args[2]);
1087 moore::QueueInsertOp::create(builder, loc, queue, index, item);
1089 }
else if (nameId == ksn::PushBack && args.size() == 2) {
1090 auto item =
context.convertRvalueExpression(*args[1]);
1091 moore::QueuePushBackOp::create(builder, loc, queue, item);
1093 }
else if (nameId == ksn::PushFront && args.size() == 2) {
1094 auto item =
context.convertRvalueExpression(*args[1]);
1095 moore::QueuePushFrontOp::create(builder, loc, queue, item);
1103 if (args.size() >= 1 && args[0]->type->isAssociativeArray()) {
1104 auto assocArray =
context.convertLvalueExpression(*args[0]);
1109 if (nameId == ksn::Delete) {
1110 if (args.size() == 1) {
1111 moore::AssocArrayClearOp::create(builder, loc, assocArray);
1114 if (args.size() == 2) {
1115 auto index =
context.convertRvalueExpression(*args[1]);
1116 moore::AssocArrayDeleteOp::create(builder, loc, assocArray, index);
1123 if (nameId == ksn::MonitorOn || nameId == ksn::MonitorOff) {
1124 context.ensureMonitorGlobals();
1125 bool enable = (nameId == ksn::MonitorOn);
1126 auto enabledRef = moore::GetGlobalVariableOp::create(
1128 auto value = moore::ConstantOp::create(
context.builder, loc,
1130 moore::BlockingAssignOp::create(
context.builder, loc, enabledRef, value);
1135 if (nameId == ksn::Monitor || nameId == ksn::MonitorB ||
1136 nameId == ksn::MonitorO || nameId == ksn::MonitorH) {
1137 context.ensureMonitorGlobals();
1140 unsigned myId =
context.nextMonitorId++;
1143 auto i32Type = moore::IntType::getInt(
context.getContext(), 32);
1145 moore::ConstantOp::create(
context.builder, loc, i32Type, myId);
1146 auto activeRef = moore::GetGlobalVariableOp::create(
1148 moore::BlockingAssignOp::create(
context.builder, loc, activeRef, idConst);
1151 context.pendingMonitors.push_back({myId, loc, &expr});
1162 void createFinishMessage(
const slang::ast::Expression *verbosityExpr) {
1163 unsigned verbosity = 1;
1164 if (verbosityExpr) {
1166 context.evaluateConstant(*verbosityExpr).integer().as<
unsigned>();
1167 assert(value &&
"Slang guarantees constant verbosity parameter");
1172 moore::FinishMessageBIOp::create(builder, loc, verbosity > 1);
1176 LogicalResult visit(
const slang::ast::EventTriggerStatement &stmt) {
1178 mlir::emitError(loc) <<
"unsupported delayed event trigger";
1184 auto target =
context.convertLvalueExpression(stmt.target);
1190 Value inverted = moore::ReadOp::create(builder, loc, target);
1191 inverted = moore::NotOp::create(builder, loc, inverted);
1193 if (stmt.isNonBlocking)
1194 moore::NonBlockingAssignOp::create(builder, loc, target, inverted);
1196 moore::BlockingAssignOp::create(builder, loc, target, inverted);
1201 LogicalResult visit(
const slang::ast::WaitStatement &stmt) {
1202 auto waitOp = moore::WaitLevelOp::create(builder, loc);
1204 OpBuilder::InsertionGuard guard(builder);
1205 builder.setInsertionPointToStart(&waitOp.getBody().emplaceBlock());
1206 auto cond =
context.convertRvalueExpression(stmt.cond);
1209 cond = builder.createOrFold<moore::BoolCastOp>(loc, cond);
1210 moore::DetectLevelOp::create(builder, loc, cond);
1213 if (failed(
context.convertStatement(stmt.stmt)))
1219 LogicalResult visit(
const slang::ast::WaitForkStatement &stmt) {
1220 moore::WaitForkOp::create(builder, loc);
1225 template <
typename T>
1226 LogicalResult visit(T &&stmt) {
1227 mlir::emitError(loc,
"unsupported statement: ")
1228 << slang::ast::toString(stmt.kind);
1229 return mlir::failure();
1232 LogicalResult visitInvalid(
const slang::ast::Statement &stmt) {
1233 mlir::emitError(loc,
"invalid statement: ")
1234 << slang::ast::toString(stmt.kind);
1235 return mlir::failure();
1240LogicalResult Context::convertStatement(
const slang::ast::Statement &stmt) {
1257 OpBuilder::InsertionGuard guard(
builder);
1261 auto i32Type = moore::IntType::getInt(
getContext(), 32);
1262 auto i1Type = moore::IntType::getInt(
getContext(), 1);
1267 builder, loc,
"__monitor_active_id", i32Type);
1269 OpBuilder::InsertionGuard initGuard(
builder);
1270 builder.setInsertionPointToStart(
1272 auto zero = moore::ConstantOp::create(
builder, loc, i32Type, 0);
1273 moore::YieldOp::create(
builder, loc, zero);
1279 builder, loc,
"__monitor_enabled", i1Type);
1281 OpBuilder::InsertionGuard initGuard(
builder);
1282 builder.setInsertionPointToStart(
1286 moore::YieldOp::create(
builder, loc, trueVal);
1292 using ksn = slang::parsing::KnownSystemName;
1294 auto &call = *pending.call;
1295 auto loc = pending.loc;
1299 std::get<slang::ast::CallExpression::SystemCallInfo>(call.subroutine);
1300 auto nameId = info.subroutine->knownNameId;
1303 auto defaultFormat = moore::IntFormat::Decimal;
1306 defaultFormat = moore::IntFormat::Binary;
1309 defaultFormat = moore::IntFormat::Octal;
1312 defaultFormat = moore::IntFormat::HexLower;
1321 auto alwaysProc = moore::ProcedureOp::create(
1322 builder, loc, moore::ProcedureKind::AlwaysComb);
1323 OpBuilder::InsertionGuard guard(
builder);
1324 builder.setInsertionPointToStart(&alwaysProc.getBody().emplaceBlock());
1329 if (failed(message))
1333 auto i32Type = moore::IntType::getInt(
getContext(), 32);
1334 auto myId = moore::ConstantOp::create(
builder, loc, i32Type, pending.id);
1337 isActive = moore::ReadOp::create(
builder, loc, isActive);
1338 isActive = moore::EqOp::create(
builder, loc, isActive, myId);
1342 enabled = moore::ReadOp::create(
builder, loc, enabled);
1343 enabled = moore::AndOp::create(
builder, loc, isActive, enabled);
1344 enabled = moore::ToBuiltinIntOp::create(
builder, loc, enabled);
1348 auto &printBlock = alwaysProc.getBody().emplaceBlock();
1349 auto &skipBlock = alwaysProc.getBody().emplaceBlock();
1350 cf::CondBranchOp::create(
builder, loc, enabled, &printBlock, &skipBlock);
1354 builder.setInsertionPointToStart(&printBlock);
1356 moore::DisplayBIOp::create(
builder, loc, *message);
1357 moore::ReturnOp::create(
builder, loc);
1360 builder.setInsertionPointToStart(&skipBlock);
1361 moore::ReturnOp::create(
builder, loc);
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static FVInt getZero(unsigned numBits)
Construct an FVInt with all bits set to 0.
This helps visit TypeOp nodes.
@ TwoValued
Two-valued types such as bit or int.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
SmallVector< PendingMonitor > pendingMonitors
Pending $monitor calls that need to be converted at module level.
LogicalResult flushPendingMonitors()
Process any pending $monitor calls and generate the monitoring procedures at module level.
OpBuilder builder
The builder used to create IR operations.
void ensureMonitorGlobals()
Ensure that the global variables for $monitor state exist.
FailureOr< Value > convertFormatString(std::span< const slang::ast::Expression *const > arguments, Location loc, moore::IntFormat defaultFormat=moore::IntFormat::Decimal, bool appendNewline=false)
Convert a list of string literal arguments with formatting specifiers and arguments to be interpolate...
moore::GlobalVariableOp monitorActiveIdGlobal
Global variable ops for $monitor state management.
moore::GlobalVariableOp monitorEnabledGlobal
mlir::ModuleOp intoModuleOp
SymbolTable symbolTable
A symbol table of the MLIR module we are emitting into.
MLIRContext * getContext()
Return the MLIR context.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.