Skip to content

Fix Index Out-of-Bounds Compilation Error in adder_tree.sv #4

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

Closed
wants to merge 1 commit into from

Conversation

JacyCui
Copy link
Contributor

@JacyCui JacyCui commented Jul 18, 2025

Description

The current implementation of adder_tree.sv contains a potential ​​compile-time index out-of-bounds error​​ due to unsafe always_comb logic inside the generate block. When INPUTS_NUM is not a power of two (e.g., 125), the generate loop may attempt to access idata[adder] with adder >= INPUTS_NUM , even if the else branch is logically unreachable.
This violates SystemVerilog's requirement that ​​constant indices must be compile-time valid​​, regardless of runtime conditions.

Current Problematic Code

always_comb begin
    if (adder < INPUTS_NUM) begin
        data[stage][adder][ST_WIDTH-1:0] <= idata[adder][ST_WIDTH-1:0]; // May OOB if adder >= INPUTS_NUM
        data[stage][adder][ODATA_WIDTH-1:ST_WIDTH] <= '0;
    end else begin
        data[stage][adder][ODATA_WIDTH-1:0] <= '0;
    end
end

Proposed Fix

if (adder < INPUTS_NUM) begin
    always_comb begin
        data[stage][adder][ST_WIDTH-1:0] <= idata[adder][ST_WIDTH-1:0];
        // Safe: only generated if adder < INPUTS_NUM
        data[stage][adder][ODATA_WIDTH-1:ST_WIDTH] <= '0;
    end
end else begin
    always_comb begin
        data[stage][adder][ODATA_WIDTH-1:0] <= '0;
    end
end

Impact

  • ​​No functional change​​: The logic remains identical.
  • Compilation safety​​: Eliminates "constant index out of bounds" errors.

@pConst
Copy link
Owner

pConst commented Aug 2, 2025

Hi! There is no point to fix "potential" and "theoretical" errors.
I suspect your PRs are GPT-generated. If so, please don`t bother me with this shit anymore

@pConst pConst closed this Aug 2, 2025
@JacyCui
Copy link
Contributor Author

JacyCui commented Aug 3, 2025

Hi, thanks for your reply—I truly appreciate your contributions to this project.

I understand your perspective, and I agree that some issues may seem purely theoretical at first glance. That said, Verilog/SystemVerilog is a notoriously complex language, and real-world toolchains often diverge subtly from the spec. In our experience, writing more defensively around constant index expressions improves portability and avoids surprises across different simulators and synthesis tools.

In this particular case, I actually encountered a real compile-time error during development. Our internal toolchain performs stricter static checks and flagged the original code for potential constant index out-of-bounds—even though the branch is unreachable at runtime. The fix in this PR resolved the issue in our setup, which is why I proposed it. Unfortunately, I’m unable to share a reproducible artifact due to internal restrictions.

Lastly, I sincerely apologize if the PR came off as intrusive or annoying—that was never my intention. I simply wanted to share a fix that helped in my own use case.

Thanks again for your time and for maintaining the project.

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.

2 participants