Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ImportVerilog] Add foreach statement support. #8017

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions lib/Conversion/ImportVerilog/Statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,93 @@ struct StmtVisitor {
return *block.release();
}

LogicalResult recursiveForeach(const slang::ast::ForeachLoopStatement &stmt,
uint32_t level) {
// find current dimension we are operate.
const auto &loopDim = stmt.loopDims[level];
if (!loopDim.range.has_value()) {
emitError(loc) << "dynamic loop variable is unsupported";
}
auto &exitBlock = createBlock();
auto &stepBlock = createBlock();
auto &bodyBlock = createBlock();
auto &checkBlock = createBlock();

// Push the blocks onto the loop stack such that we can continue and break.
context.loopStack.push_back({&stepBlock, &exitBlock});
auto done = llvm::make_scope_exit([&] { context.loopStack.pop_back(); });

const auto &iter = loopDim.loopVar;
auto type = context.convertType(*iter->getDeclaredType());
if (!type)
return failure();

Value initial = builder.create<moore::ConstantOp>(
loc, cast<moore::IntType>(type), loopDim.range->lower());

// Create loop varirable in this dimension
Value varOp = builder.create<moore::VariableOp>(
loc, moore::RefType::get(cast<moore::UnpackedType>(type)),
builder.getStringAttr(iter->name), initial);
context.valueSymbols.insertIntoScope(context.valueSymbols.getCurScope(),
iter, varOp);

builder.create<cf::BranchOp>(loc, &checkBlock);
builder.setInsertionPointToEnd(&checkBlock);

// When the loop variable is greater than the upper bound, goto exit
auto upperBound = builder.create<moore::ConstantOp>(
loc, cast<moore::IntType>(type), loopDim.range->upper());

auto var = builder.create<moore::ReadOp>(loc, varOp);
Value cond = builder.create<moore::SleOp>(loc, var, upperBound);
if (!cond)
return failure();
cond = builder.createOrFold<moore::BoolCastOp>(loc, cond);
cond = builder.create<moore::ConversionOp>(loc, builder.getI1Type(), cond);
builder.create<cf::CondBranchOp>(loc, cond, &bodyBlock, &exitBlock);

builder.setInsertionPointToEnd(&bodyBlock);

// find next dimension in this foreach statement, it finded then recuersive
// resolve, else perform body statement
bool hasNext = false;
for (uint32_t nextLevel = level + 1; nextLevel < stmt.loopDims.size();
nextLevel++) {
if (stmt.loopDims[nextLevel].loopVar) {
if (failed(recursiveForeach(stmt, nextLevel)))
return failure();
hasNext = true;
break;
}
}

if (!hasNext) {
if (failed(context.convertStatement(stmt.body)))
return failure();
}
if (!isTerminated())
builder.create<cf::BranchOp>(loc, &stepBlock);

builder.setInsertionPointToEnd(&stepBlock);

// add one to loop variable
var = builder.create<moore::ReadOp>(loc, varOp);
auto one =
builder.create<moore::ConstantOp>(loc, cast<moore::IntType>(type), 1);
auto postValue = builder.create<moore::AddOp>(loc, var, one).getResult();
builder.create<moore::BlockingAssignOp>(loc, varOp, postValue);
builder.create<cf::BranchOp>(loc, &checkBlock);

if (exitBlock.hasNoPredecessors()) {
exitBlock.erase();
setTerminated();
} else {
builder.setInsertionPointToEnd(&exitBlock);
}
return success();
}

// Skip empty statements (stray semicolons).
LogicalResult visit(const slang::ast::EmptyStatement &) { return success(); }

Expand Down Expand Up @@ -309,6 +396,14 @@ struct StmtVisitor {
return success();
}

LogicalResult visit(const slang::ast::ForeachLoopStatement &stmt) {
for (uint32_t level = 0; level < stmt.loopDims.size(); level++) {
if (stmt.loopDims[level].loopVar)
return recursiveForeach(stmt, level);
}
return failure();
}

// Handle `repeat` loops.
LogicalResult visit(const slang::ast::RepeatLoopStatement &stmt) {
auto count = context.convertRvalueExpression(stmt.count);
Expand Down
60 changes: 60 additions & 0 deletions test/Conversion/ImportVerilog/basic.sv
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,66 @@ function void ForeverLoopStatements(bit x, bit y);
forever dummyA();
endfunction

// CHECK: func.func private @ForeachStatements(%[[ARG0:.*]]: !moore.i32, %[[ARG1:.*]]: !moore.i1) {
function void ForeachStatements(int x, bit y);
// CHECK: %[[ARRAY:.*]] = moore.variable : <uarray<3 x uarray<3 x uarray<3 x uarray<8 x l8>>>>>
logic [7:0] array [3:1][4:2][5:3][6:-1];
// CHECK: %[[C2:.*]] = moore.constant 2 : i32
// CHECK: %[[I:.*]] = moore.variable %[[C2]] : <i32>
// CHECK: cf.br ^[[BB1:.*]]
// CHECK: ^[[BB1]]:
// CHECK: %[[C4:.*]] = moore.constant 4 : i32
// CHECK: %[[I_VAL:.*]] = moore.read %[[I]] : <i32>
// CHECK: %[[CMP1:.*]] = moore.sle %[[I_VAL]], %[[C4]] : i32 -> i1
// CHECK: %[[CONV1:.*]] = moore.conversion %[[CMP1]] : !moore.i1 -> i1
// CHECK: cf.cond_br %[[CONV1]], ^[[BB2:.*]], ^[[BB10:.*]]
// CHECK: ^[[BB2]]:
// CHECK: %[[CM1:.*]] = moore.constant -1 : i32
// CHECK: %[[J:.*]] = moore.variable %[[CM1]] : <i32>
// CHECK: cf.br ^[[BB3:.*]]
// CHECK: ^[[BB3]]:
// CHECK: %[[C6:.*]] = moore.constant 6 : i32
// CHECK: %[[J_VAL:.*]] = moore.read %[[J]] : <i32>
// CHECK: %[[CMP2:.*]] = moore.sle %[[J_VAL]], %[[C6]] : i32 -> i1
// CHECK: %[[CONV2:.*]] = moore.conversion %[[CMP2]] : !moore.i1 -> i1
// CHECK: cf.cond_br %[[CONV2]], ^[[BB4:.*]], ^[[BB8:.*]]
foreach (array[, i, ,j]) begin
// CHECK: ^[[BB4]]:
// CHECK: %[[CONV3:.*]] = moore.conversion %[[ARG1]] : !moore.i1 -> i1
// CHECK: cf.cond_br %[[CONV3]], ^[[BB5:.*]], ^[[BB6:.*]]
if (y) begin
// CHECK: ^[[BB5]]:
// CHECK: call @dummyA() : () -> ()
// CHECK: cf.br ^[[BB8]]
dummyA();
break;
end else begin
// CHECK: ^[[BB6]]:
// CHECK: call @dummyB() : () -> ()
// CHECK: cf.br ^[[BB7:.*]]
dummyB();
continue;
end
// CHECK: ^[[BB7]]:
// CHECK: %[[J_VAL2:.*]] = moore.read %[[J]] : <i32>
// CHECK: %[[C1_1:.*]] = moore.constant 1 : i32
// CHECK: %[[ADD1:.*]] = moore.add %[[J_VAL2]], %[[C1_1]] : i32
// CHECK: moore.blocking_assign %[[J]], %[[ADD1]] : i32
// CHECK: cf.br ^[[BB3]]
// CHECK: ^[[BB8]]:
// CHECK: cf.br ^[[BB9:.*]]
// CHECK: ^[[BB9]]:
// CHECK: %[[I_VAL2:.*]] = moore.read %[[I]] : <i32>
// CHECK: %[[C1_2:.*]] = moore.constant 1 : i32
// CHECK: %[[ADD2:.*]] = moore.add %[[I_VAL2]], %[[C1_2]] : i32
// CHECK: moore.blocking_assign %[[I]], %[[ADD2]] : i32
// CHECK: cf.br ^[[BB1]]
hailongSun2000 marked this conversation as resolved.
Show resolved Hide resolved
// CHECK: ^[[BB10]]:
// CHECK: return
end
endfunction


// CHECK-LABEL: func.func private @WhileLoopStatements(
// CHECK-SAME: %arg0: !moore.i1
// CHECK-SAME: %arg1: !moore.i1
Expand Down
Loading