5 ms·
Exactly. Consider this example: a = f(z); b = g(z); v = x > y ? a : b; Assuming computing the two function calls f() and g() is relativelly expensive, i
by mpreda 2y ago
Exactly. Consider this example:
a = f(z);
b = g(z);
v = x > y ? a : b;
Assuming computing the two function calls f() and g() is relativelly expensive, it becomes a trade-off whether to emit conditional code or to compute both followed by a select. So it's not a simple choice, and the decision is made by the compiler.
- dragontamer 2y agoThis is a GPU focused article. The GPU will almost always execute f and g due to GPU differences vs CPU. You can avoid the f vs g if you can ensure a scalar Boolean / if statement that is consistent across the warp. So it's not 'always' but requires incredibly specific coding patterns to 'force' the optimizer + GPU compiler into making the branch.
- justsid 2y agoIt depends. If the code flow is uniform for the warp, only side of the branch needs to be evaluated. But you could still end up with pessimistic register allocation because the compiler can’t know it is uniform. It’s sometimes weirdly hard to reason about how exactly code will end up executing on the GPU.
- danybittel 2y agof or g may have side effects too. Like writing to memory. Now a conditional has a different meaning. You could also have some fun stuff, where f and g return a boolean, because thanks to short circuit evaluation && || are actually also conditionals in disguise.
- account42 2y agoSide effects will be masked, the GPU is still executing exactly the same code for the entire workgroup.