The Difference In Optimizations Between NIR & GLSL
The Intel driver has begun using NIR by default, the Freedreno driver has NIR support, and the Raspberry Pi VC4 Gallium3D driver has also been working on NIR support as being the initial "customers" of this new intermediate representation.
One of the advantages advertised from the get-go for this intermediate representation has been that more optimizations can be shared across drivers and in a better way than the current GLSL IR situation... So what's the difference? Matt Turner of Intel fortunately committed today the same optimization to both GLSL and NIR, which indirectly does a nice job for demonstrating the difference.
The optimization done by Matt is transforming a pow(x, 4) call just into (x*x)*(x*x). The GLSL optimization comes basically down to:
is_vec_four(ir_constant *ir)Meanwhile, the same transformation in NIR is just one line:
return (ir == NULL) ? false : ir->is_value(4.0, 4);
ir_variable *x = new(ir) ir_variable(ir->operands->type, "x",
ir_variable *squared = new(ir) ir_variable(ir->operands->type,
base_ir->insert_before(assign(squared, mul(x, x)));
return mul(squared, squared);
(('fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),