Skip to content

intel/compiler: Move Gen4/5 rounding to visitor

Matt Turner requested to merge mattst88/mesa:gen4-rounding-fix into master

Gen4/5's rounding instructions operate differently than later Gens'. They all return the floor of the input and the "Round-increment" conditional modifier answers whether the result should be incremented by 1.0 to get the appropriate result for the operation (and thus its behavior is determined by the round opcode; e.g., RNDZ vs RNDE).

Since this requires a second instruciton (a predicated ADD) that consumes the result of the round instruction, the round instruction cannot write its result directly to the (write-only) message registers. By emitting the ADD in the generator, the backend thinks it's safe to store the round's result directly to the message register file.

To avoid this, we move the emission of the ADD instruction to the NIR translator so that the backend has the information it needs.

I suspect this also fixes code generated for RNDZ.SAT but since Gen4/5 don't support GLSL 1.30 which adds the trunc() function, I couldn't write a piglit test to confirm. My thinking is that if x=-0.5:

  sat(trunc(-0.5)) = 0.0

But on Gen4/5 where sat(trunc(x)) is implemented as

  rndz.r.f0  result, x             // result = floor(x)
                                   // set f0 if increment needed
  (+f0) add  result, result, 1.0   // fixup so result = trunc(x)

then putting saturate on both instructions will give the wrong result.

  floor(-0.5) = -1.0
  sat(floor(-0.5)) = 0.0
  // +1 increment would be needed since floor(-0.5) != trunc(-0.5)
  sat(sat(floor(-0.5)) + 1.0) = 1.0

Fixes: 6f394343 ("nir/algebraic: i2f(f2i()) -> trunc()") Closes: #2355 (closed)

Merge request reports