Experiments with SHFL

- 1 min read

I wanted to double-check my understanding of Kepler’s shfl operation when using negative indices or negative index offsets. The PTX documentation on this instruction is accurate but a little terse so a micro-test was in order.

The summary results are:

  • shfl.idx handles negative indices without any problem which means “shuffle rotations” are feasible.
  • shfl.up has no chance to mask the lane - bval value so it sets the in-range predicate to false when negative indices are produced and the lane’s current value is assigned.
  • shfl.down has similar behavior.
    • negative shfl.[up|down] offsets are treated as unsigned 5-bit values. e.g. -5 = 27.

It’s also important to note that invoking shfl.idx with a signed offset subtracted from the laneId results in no surprises and is a two or three-instruction SASS sequence:

S2R R4, SR_LaneId;
IADD R4, R4, -<register or constant>;
SHFL.IDX pt, R4, R0, R4, 0x1f;

I like to think of this operation as a shfl.rot.

Source code can be found here.

Here’s a screenshot of the output. An ‘x’ indicates a false predicate.