October 24, 2014
Hi, I am the guy who implemented the (currently incomplete) vrp for sdc.

I see vrp as a tool to avoid _unnecessary_ casts. _Not_ as means to avoid _all_ casts.

> void main(in string[] args) {
>     immutable size_t len = args.length % 10;
>     ubyte x = len;
>     ubyte[] a;
>     foreach (immutable i; 0u .. len)
>         a ~= i;
> }

The problem with vrp for non-static immutable values is, that vrp becomes a runtime-thing and I would like to avoid that!
Tracking the range of a Variable at runtime could cause significant overhead!

Doing static analysis and tracking Variable-Ranges and assignments at compile-time, is possible but difficult and there are a number of reasons why not to do it.

1. Slows compilation down
2. implementation is not straight-forward and bugs could be hard to find.
3. Code that relays on this may not be easy to read, and it may be hard for the programmer to see why a particular assignment does not need a cast!

For simple code like the one posted above it is easy to see why it works.
anything mod 10 can only be in the range of [-9 .. 9]

But if we have much logic and control-flow between the assignment and the definition of the assigning variable then not haveing an explicit cast could cause a bit of puzzlement.
October 24, 2014
Stefan Koch:

> The problem with vrp for non-static immutable values is, that vrp becomes a runtime-thing and I would like to avoid that!
> Tracking the range of a Variable at runtime could cause significant overhead!

Nope, the value range tracking is purely compile-time.


> 2. implementation is not straight-forward and bugs could be hard to find.

It's much better to remove bugs from the compiler than from an unbound amount of user code that uses casts incorrectly.


> 3. Code that relays on this may not be easy to read,

I don't believe this. Please show one or more examples.


> and it may be hard for the programmer to see why a particular assignment does not need a cast!

And this is bad because?


> But if we have much logic and control-flow between the assignment and the definition of the assigning variable then not haveing an explicit cast could cause a bit of puzzlement.

I think most D programmers are used to C/C++ coding, that doesn't require most of those casts. I don't think that "puzzlement" is real, or problematic.

Bye,
bearophile
October 24, 2014
you are right. I misread the code-snippt above. Of course this is staticly checkable!
October 24, 2014
On 10/22/2014 11:31 AM, Shammah Chancellor wrote:
> A couple of us working on SDC are trying to get ValueRange propigation
> implemented.   I was wonder if someone could offer some insight as to
> how VRP works in DMD.   If for example, trying to get the value range of
> a global, what is the expected behavior?
>
> It seems as though VRP is a language feature, and not a compiler feature
> -- since this allows some code to compile and not others.   Is there a
> specification for how it should work somewhere?  If not, it's hard to
> implement other compilers that will not generate errors in the same
> circumstances as DMD.
>

AFAIK:

- arithmetic operators: new range is given by the minimum and maximum values that the whole expression can attain given that operands are arbitrary values from their respective ranges. (DMD currently does something worse for bitwise operators, and probably modulo.) If overflow may occur, the full data type range is assumed. Likewise if divide by zero may occur. (This despite the fact that divide by zero is supposedly undefined behaviour. Go figure.)

- mutable variables: full data type range is assumed

- immutable variables: range of initializer is assumed. If it is a foreach range index variable, combine value ranges of lower and upper bound appropriately.
October 24, 2014
On 10/22/2014 10:32 PM, Walter Bright wrote:
> The specification is straightforward - a narrowing conversion can be
> implicitly performed if it can be proved that it would not lose
> information.

This is only straightforward to state because it is so ill-defined.
The main aspect in need of specification is the procedure to perform those automated proofs with.
October 28, 2014
On 10/22/14 1:32 PM, Walter Bright wrote:
> On 10/22/2014 2:31 AM, Shammah Chancellor wrote:
>> A couple of us working on SDC are trying to get ValueRange propigation
>> implemented.   I was wonder if someone could offer some insight as to
>> how VRP
>> works in DMD.   If for example, trying to get the value range of a
>> global, what
>> is the expected behavior?
>>
>> It seems as though VRP is a language feature, and not a compiler
>> feature --
>> since this allows some code to compile and not others.   Is there a
>> specification for how it should work somewhere?  If not, it's hard to
>> implement
>> other compilers that will not generate errors in the same
>> circumstances as DMD.
>>
>
> VRP is definitely a language feature, not a compiler feature. The
> specification is straightforward - a narrowing conversion can be
> implicitly performed if it can be proved that it would not lose
> information.
>
> How it works, though, is kinda tricky, and the only guide to it is the
> compiler source code.

I think specification is equally tricky because it needs to specify the exact algorithms. -- Andrei

1 2
Next ›   Last »