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

if-statement/ternary dataflow bug #4643

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sarutob1
Copy link
Contributor

@sarutob1 sarutob1 commented Oct 30, 2023

I've encountered this bug with if-statements/contitional-expressions that are moving data larger than 32 bits.
e.g.:

        if (wordQ.size() > 0) begin
            myint = 100'(wordQ.pop_front());
        end else begin
            myint = 100'(99);
        end

The emitted C++ code can come out as this:

    VL_EXTENDS_WI(100,32, __Vtemp_h8963a0fe__1, vlSelf->t__DOT__wordQ.pop_front());
    t__DOT__myint[0U] = (VL_LTS_III(32, 0U, vlSelf->t__DOT__wordQ.size())
                          ? __Vtemp_h8963a0fe__1[0U]
                          : 0x63U);
    t__DOT__myint[1U] = (VL_LTS_III(32, 0U, vlSelf->t__DOT__wordQ.size())
                          ? __Vtemp_h8963a0fe__1[1U]
                          : 0U);
    t__DOT__myint[2U] = (VL_LTS_III(32, 0U, vlSelf->t__DOT__wordQ.size())
                          ? __Vtemp_h8963a0fe__1[2U]
                          : 0U);
    t__DOT__myint[3U] = (VL_LTS_III(32, 0U, vlSelf->t__DOT__wordQ.size())
                          ? (0xfU & __Vtemp_h8963a0fe__1[3U])
                          : 0U);

OR

VL_COND_WIWW(100, __Vtemp_11, VL_LTS_III(32, 0U, vlSelf->wordQ.size()), vlSelf->wordQ.pop_front(), __Vtemp_9);

Both of which use the value of pop_front without accounting for side effects.

Source code/test is included in the pull request, but I'm not familiar with with the C++ emission part of verilator. Which files/modules should I look at to try and fix this?

@sarutob1 sarutob1 marked this pull request as draft October 30, 2023 14:14
@wsnyder
Copy link
Member

wsnyder commented Oct 31, 2023

There's two bugs here. First, the "if", is because V3Premit inserts a temporary above the if, instead of on the if's branch. It should be straight forward to fix that, can you consider taking a look and making a pull to fix it (then likely comment out the conditional ?: in your test so we can merge the fix for the 'if' only).

Separately the conditional likewise requires a temporary, but unlike the "if" that can't be just put before the statement. Instead we need to see if there's side effects, and if so, put the new temporary into a AstExprStmt that returns the value of the temporary. We want to do that only when side effects as the AstExprStmt will disable other later optimizations.

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.

None yet

2 participants