OSVVM 2015.03 problem pure function GenRandSeed calls impure procedure Alert
Why OSVVM™? › Forums › OSVVM › OSVVM 2015.03 problem pure function GenRandSeed calls impure procedure Alert
Tagged: ghdl
- This topic has 9 replies, 3 voices, and was last updated 9 years, 4 months ago by Olof Kraigher.
-
AuthorPosts
-
May 11, 2015 at 01:28 #969Olof KraigherMemberMay 11, 2015 at 03:11 #972Olof KraigherMember
Changing the function signature of GenRandSeed to “impure function” makes it not violate the LRM and I believe it has no limitations on the usage of the function since its a member of a protected type.
NOTE: The GHDL error is actually a run-time error occurring when the call is made and not a statical error on analysis. Thus I cannot promise that there are no other instances of this problem within the OSVVM code since I do not have any running example which executes the entire code base. However every subprogram that is a pure function within a protected type is a potential problem.
May 11, 2015 at 07:08 #975Jim LewisMemberHi Olof,
I will change it in the next release (either May or June) if it does not break any of the other supported simulators. Currently I test with ModelSim/QuestaSim and ActiveHDL/RivieraPro.
I will also talk to the GHDL folks about their implementation and pure subprograms. I will have to look at the LRM as the intent of a pure function is that since it accesses an internal, it may return a different value on different executions. Maybe that applies here since the alerts cause fileIO and cause variables internal to the Alert structure to change.
Whether GHDL is right or not about the usage issue, as long as the change does not impact other tools, I will make it – OSVVM is about things working.
Jim
May 11, 2015 at 07:21 #976Olof KraigherMemberI am sure it applies since the state changes between calls and thus the program result is affected by the number of calls to the function. Is not the idea behind pure functions to provide referential transparency (https://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29)?
Anyway I am convinced that changing the signature to impure cannot have any effects since it is a subprogram within a protected type which cannot be passed as an argument to a pure function anyway and cannot be used anywhere where a pure function would be required.
I have made this modification myself locally to OSVVM 2015.03 and I am able to use it with the latest GHDL using the LLVM backend and VHDL 2008 by the way. I was quite impressed by development of GHDL lately.
May 12, 2015 at 03:17 #981Lars AsplundMemberIsn’t the problem that the intent
” Certain functions, designated pure functions,
return the same value each time they are called with the same values as actual
parameters”isn’t the same as saying that you can’t access shared variables, files and so on? As long as you just write to them the intent is fulfilled.
May 12, 2015 at 03:33 #982Olof KraigherMemberMay 12, 2015 at 04:27 #985Lars AsplundMemberYou’re right Olof. I guess there’s not much point in having pure and impure if it’s just about repetitive behaviour and can’t be used by the compiler to make optimizations.
May 12, 2015 at 04:55 #986Olof KraigherMemberUnfortunately VHDL already breaks referential transparency of pure functions by allowing reports in them. Still compilers might be able to optimize pure functions which do not contain any report statements or calls to other pure function containing report statements.
Consider the following code:
entity test is
end entity;
architecture a of test is
function expensive_function_returning_false return boolean is
begin
report “This cost much”;
return false;
end function;
begin
main : process
begin
assert (not (expensive_function_returning_false or expensive_function_returning_false));
wait;
end process;
end architecture;
This cannot and is not optimized by at least Modelsim and GHDL.
Modelsim outputs this “This cost much” twice as does GHDL. NVC which is another open source VHDL simulator will print nothing. It is likely it performs a static constant folding removing the assert entirely falsely believing it can optimize pure functions in this way.
It is not easy to determine if GHDL or Modelsim would have performed constant folding and optimized away the assert if there were no report statement without deep knowledge of their binary formats and a binary viewer.May 12, 2015 at 05:46 #988Lars AsplundMemberOlof, you just answered my follow-up question. Do you know if there a difference in how the simulators treat report (write to stdout) and a write to a file?
May 12, 2015 at 06:14 #989Olof KraigherMemberYes report is allowed in pure functions, presumably to debug code executed during elaboration, while writing to file is not allowed by not allowing file variables or passing files as parameters.
-
AuthorPosts
- You must be logged in to reply to this topic.