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

Conditional impure function always being executed #963

Closed
veripoolbot opened this issue Sep 8, 2015 · 7 comments
Closed

Conditional impure function always being executed #963

veripoolbot opened this issue Sep 8, 2015 · 7 comments
Assignees
Labels
area: wrong runtime result Issue involves an incorrect runtine result from Verilated model resolution: fixed Closed; fixed

Comments

@veripoolbot
Copy link
Contributor


Author Name: Wei Song
Original Redmine Issue: 963 from https://www.veripool.org
Original Date: 2015-09-08
Original Assignee: Wilson Snyder (@wsnyder)


I have a code similar like this:

always_comb
  if(valid)
     ready = dpi_function(a,b,c);
  else
     ready = 1'b0;

Looking at the compiled code, this always block behave like this

rv = dpi_function(a,b,c)
ready = valid && rv

While this is OK if the function is a pure function.
However, the function here is a DPI function which has side-effect on the C++ side.
I did define any "pure" key word when declaring DPI-C.
Is there a way to disable this scheduling?

I found if I add something, like a $display() in the if block, the function is scheduled OK.

@veripoolbot
Copy link
Contributor Author


Original Redmine Comment
Author Name: Wei Song
Original Date: 2015-09-08T11:38:08Z


Typo:

I did NOT define any "pure" key word when declaring DPI-C.

@veripoolbot
Copy link
Contributor Author


Original Redmine Comment
Author Name: Jie Xu (@jiexu)
Original Date: 2015-09-09T07:09:29Z


This is not a scheduling problem. Instead the issue is introduced during optimization. Verilator will try to simplify

IF(valid) ready = dpi_function; else ready = 1'b0

into

ready = valid ? dpi_function : 1'b0

And then in your code since @Valid == 1'b0@ Verilator further simplify the code into

ready = valid & dpi_function

The second optimization is the issue here. In C++, the @&@ operator is actually eager operator [[ https://en.wikipedia.org/wiki/Short-circuit_evaluation]] which means the @dpi_function@ will be always called.

@veripoolbot
Copy link
Contributor Author


Original Redmine Comment
Author Name: Wei Song
Original Date: 2015-09-09T08:02:35Z


Thanks for the rewording. Surely I am not fully aware of the process of synthesis.
I do understand the reason to do this optimization when possible.
However, it is difficult to see whether a DPI function is pure or not.

I would suggest Verilator treats all DPI functions as impure unless a "pure" key word is defined. The SystemVerilog does define this key word to indicate the DPI function is pure, isn't it?

Hopefully it is not a big trouble.

@veripoolbot
Copy link
Contributor Author


Original Redmine Comment
Author Name: Wei Song
Original Date: 2015-09-09T08:07:06Z


Actually, if

ready = valid & dpi_function

really follows the short cut rule, when valid is 0, dpi_function() does not need to be evaluated?

@veripoolbot
Copy link
Contributor Author


Original Redmine Comment
Author Name: Jie Xu (@jiexu)
Original Date: 2015-09-09T08:14:10Z


@&@ is an eager operator instead of short-circuit operator in C++. @&&@ is a short-circuit operator.

@veripoolbot
Copy link
Contributor Author


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2015-09-18T23:08:34Z


Fixed in git towards 3.877.

Note that the short-circuit operator handing e.g. &&, still does not generally work, so do not rely on this. However if to conditional was intended to be safe.

@veripoolbot
Copy link
Contributor Author


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2015-11-01T13:23:11Z


In 3.878.

@veripoolbot veripoolbot added area: wrong runtime result Issue involves an incorrect runtine result from Verilated model resolution: fixed Closed; fixed labels Dec 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: wrong runtime result Issue involves an incorrect runtine result from Verilated model resolution: fixed Closed; fixed
Projects
None yet
Development

No branches or pull requests

2 participants