IntelLabs / RiverTrail

An API for data parallelism in JavaScript
Other
748 stars 71 forks source link

a possible bug related to '++' and 'cast' #27

Closed hujiajie closed 11 years ago

hujiajie commented 11 years ago

Steps to reproduce:

var pa = new ParallelArray(1, 2, 3, 4);
pa.map(function (x) {
  var i = 0;
  i++;
  for (var j = 0; j < 1; j++) { i++; }
  return 0;
});

Expected result:

[0, 0, 0, 0]

Actual result:

The OpenCL compiler failed. Log was `:2:508: error: assignment to cast is illegal, lvalue casts are not supported

Remarks: Here is the generated kernel string:

__kernel void RT_nameless(__global int *_FAILRET,  __global double* opThisVect , int opThisVect__offset, __global double* retVal, int retVal__offset)
{
  int _sel_idx_tmp;
  int _FAIL = 0;
  int _id_0 = (int)get_global_id(0);
  int _writeoffset = 0+1 * _id_0;
  int _readoffset = 0 + _writeoffset;
  _writeoffset += retVal__offset;
  __global double*  tempThis;
  tempThis = &( opThisVect[opThisVect__offset]);
  double  tempResult;
  const double RTl_x = tempThis[_readoffset];
  {
    double RTl_i;
    double RTl_j;
    RTl_i = ((double) 0);
    ((int)RTl_i)++;; /* ERROR! */
    for ( RTl_j = ((double) 0); (RTl_j<((double) 1));(RTl_j += ((double) 1))) {
      (RTl_i += ((double) 1));;
    };
    tempResult = ((double) 0);
    retVal[_writeoffset] =  tempResult;
    if (_FAIL) {*_FAILRET = _FAIL;}
    return; ;
  }
}
  1. By debugging the AST, I find that RTl_i and RTl_j are integers at first. Then, during the range analysis, the range information is invalidated here. I failed to understand this part though and don't know whether the result is right.
  2. The i++ statement in the for loop is translated correctly. Why do they differ?

Thanks!

sherhut commented 11 years ago

The range information is invalidated as our loop analysis cannot handle unconstrained increments like the i++ in your example. We do not currently compute trip counts.

You found a bug in the code generation of ++ though. Although it was computed on doubles, the integer ++ was used in OpenCL. The wrong cast was just a symptom of that wrong behavior.

Thanks for reporting this. Stephan