Skip to content

Commit

Permalink
Optimization of nowrshmsk
Browse files Browse the repository at this point in the history
The stride implification of the generated AST_CASE is generalized to
handle variable slices on the form dst[i*stride +: width] = src

To avoid optimization issues, the simplification of the non_opt_range
AST is reverted, and the removal of multiplication of index by stride
is temporarily disabled.
  • Loading branch information
daglem committed Aug 8, 2023
1 parent a23b70a commit a2e6956
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
27 changes: 19 additions & 8 deletions frontends/ast/simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2863,18 +2863,29 @@ bool AstNode::simplify(bool const_fold, bool in_lvalue, int stage, int width_hin
int stride = 1;
int div_stride = 1;

// Optimization: Remove multiplication of index by stride for two-dimensional packed arrays and
// variable slices on the form dst[i*w +: w] = src.
if (!source_offset && shift_expr->type == AST_MUL &&
((shift_expr->children[0]->type == AST_CONSTANT && (int)shift_expr->children[0]->integer == result_width) ||
(shift_expr->children[1]->type == AST_CONSTANT && (int)shift_expr->children[1]->integer == result_width)))
// Extract (index)*(width) from non_opt_range pattern (@selfsz@((index)*(width)))+(0)).
AstNode *lsb_expr =
shift_expr->type == AST_ADD && shift_expr->children[0]->type == AST_SELFSZ &&
shift_expr->children[1]->type == AST_CONSTANT && shift_expr->children[1]->integer == 0 ?
shift_expr->children[0]->children[0] :
shift_expr;

// Optimization: Extract stride from indexing of two-dimensional packed arrays and
// variable slices on the form dst[i*stride +: width] = src.
if (!source_offset && lsb_expr->type == AST_MUL &&
(lsb_expr->children[0]->type == AST_CONSTANT || lsb_expr->children[1]->type == AST_CONSTANT))
{
int var_i = shift_expr->children[0]->type == AST_CONSTANT;
AstNode *tmp = shift_expr->children[var_i]->clone();
stride = result_width;
int const_i = lsb_expr->children[1]->type == AST_CONSTANT;
stride = (int)lsb_expr->children[const_i]->integer;

// Remove multiplication of index by stride.
// FIXME: Counterintuitively, this can yield higher resource usage. Disable for now.
#if 0
div_stride = stride;
AstNode *tmp = lsb_expr->children[1 - const_i]->clone();
delete shift_expr;
shift_expr = tmp;
#endif
}
else if (member_node) // Member in packed struct/union
{
Expand Down
12 changes: 6 additions & 6 deletions frontends/verilog/verilog_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -884,15 +884,15 @@ non_opt_range:
} |
'[' expr TOK_POS_INDEXED expr ']' {
$$ = new AstNode(AST_RANGE);
AstNode *expr = new AstNode(AST_SELFSZ, $2->clone());
$$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr, $4), AstNode::mkconst_int(1, true)));
$$->children.push_back($2);
AstNode *expr = new AstNode(AST_SELFSZ, $2);
$$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr->clone(), $4), AstNode::mkconst_int(1, true)));
$$->children.push_back(new AstNode(AST_ADD, expr, AstNode::mkconst_int(0, true)));
} |
'[' expr TOK_NEG_INDEXED expr ']' {
$$ = new AstNode(AST_RANGE);
AstNode *expr = new AstNode(AST_SELFSZ, $2->clone());
$$->children.push_back($2);
$$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr, AstNode::mkconst_int(1, true)), $4));
AstNode *expr = new AstNode(AST_SELFSZ, $2);
$$->children.push_back(new AstNode(AST_ADD, expr, AstNode::mkconst_int(0, true)));
$$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr->clone(), AstNode::mkconst_int(1, true)), $4));
} |
'[' expr ']' {
$$ = new AstNode(AST_RANGE);
Expand Down
2 changes: 1 addition & 1 deletion tests/svtypes/struct_dynamic_range.ys
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
read_verilog -sv struct_dynamic_range.sv
select -assert-count 3 t:$mul
select -assert-count 4 t:$mul
select -assert-count 2 t:$shift
select -assert-count 2 t:$shiftx
prep -top top
Expand Down

0 comments on commit a2e6956

Please sign in to comment.