Anyone know if ?: counts as a logic branch?

Jeff Adams
  • Jeff Adams
    Jeff Adams

    The "Best Practices" guide says the following in section 6:

    High Priority: Avoid different execution paths within the same warp.
    Any flow control instruction (if, switch, do, for, while) can significantly affect the instruction throughput by causing threads of the same warp to diverge; that is, to follow different execution paths. If this happens, the different execution paths must be serialized, increasing the total number of instructions executed for this warp. When all the different execution paths have completed, the threads converge back to the same execution path.

    Low Priority: Make it easy for the compiler to use branch predication in lieu of loops or control statements.

    When using branch predication, none of the instructions whose execution depends on the controlling condition is skipped. Instead, each such instruction is associated with a per-thread condition code or predicate that is set to true or false according to the controlling condition. Although each of these instructions is scheduled for execution, only the instructions with a true predicate are actually executed. Instructions with a false predicate do not write results, and they also do not evaluate addresses or read operands.
    The compiler replaces a branch instruction with predicated instructions only if the number of instructions controlled by the branch condition is less than or equal to a certain threshold: If the compiler determines that the condition is likely to produce many divergent warps, this threshold is 7; otherwise it is 4.

    So my guess is that the ?: operator is probably trivial for the compiler to use branch predication for (because there is a single assignment statement on both legs of the branch), but I haven't got any proof either way.  I was curious if anyone knows for sure that the ?: does not cause your warp to diverge.

  • nythrix

    It looks like both "if" and "?:" are treated the same if they contain the same sort of instructions. ?: is indeed predominantly used for short expressions but that doesn't make it necessarily faster. On the other hand it surely is no slower (the compiler has a lot to say here).

    Interesting material. Where does it come from?

  • mux85

    the best practice guide from nvidia. i have read it too