Skip to content

Fix Verilog codegen for narrow array update indices - #4718

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_959354191
Aug 10, 2026
Merged

Fix Verilog codegen for narrow array update indices#4718
copybara-service[bot] merged 1 commit into
mainfrom
test_959354191

Conversation

@copybara-service

Copy link
Copy Markdown

Fix Verilog codegen for narrow array update indices

When codegen for Op::kArrayUpdate, target indexes were compared directly
against the 32-bit loop counter (index_expr == induction_var).

When index_expr was narrower than 32 bits, Verilog's context-determined
width rules expanded index_expr to 32 bits before applying the operation
creating the index. For example, if the index_expr is ~1. The 1 is expanded
to 32 bits first and then do ~ evaluated as 32'hFFFFFFFF (4294967295)
instead of 1'b1 zero-extended to 32'd1, causing the index comparison to
fail for all elements and reducing the array update to a no-op in generated
Verilog.

This change explicitly zero-extends narrow index expressions to 32 bits via
concatenation ({ (32 - width)'b0, index_expr }), creating a self-determined
context so unary operations evaluate at their native width before
comparison.

When codegen for Op::kArrayUpdate, target indexes were compared directly
against the 32-bit loop counter (index_expr == induction_var).

When index_expr was narrower than 32 bits, Verilog's context-determined
width rules expanded index_expr to 32 bits before applying the operation
creating the index. For example, if the index_expr is ~1. The 1 is expanded
to 32 bits first and then do ~ evaluated as 32'hFFFFFFFF (4294967295)
instead of 1'b1 zero-extended to 32'd1, causing the index comparison to
fail for all elements and reducing the array update to a no-op in generated
Verilog.

This change explicitly zero-extends narrow index expressions to 32 bits via
concatenation ({ (32 - width)'b0, index_expr }), creating a self-determined
context so unary operations evaluate at their native width before
comparison.

PiperOrigin-RevId: 962249938
@copybara-service
copybara-service Bot merged commit 9375c2b into main Aug 10, 2026
@copybara-service
copybara-service Bot deleted the test_959354191 branch August 10, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant