The Difference In Optimizations Between NIR & GLSL

Written by Michael Larabel in Mesa on 24 April 2015 at 04:51 PM EDT. 28 Comments
One of the biggest additions to Mesa so far this year has been the introduction of NIR, the new intermediate representation designed to replace GLSL IR and designed by a bright student fresh out of high school.

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)
return (ir == NULL) ? false : ir->is_value(4.0, 4);
if (is_vec_four(op_const[1]))
ir_variable *x = new(ir) ir_variable(ir->operands[1]->type, "x",
base_ir->insert_before(assign(x, ir->operands[0]));

ir_variable *squared = new(ir) ir_variable(ir->operands[1]->type,
base_ir->insert_before(assign(squared, mul(x, x)));
return mul(squared, squared);
Meanwhile, the same transformation in NIR is just one line:
(('fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),
Related News
About The Author
Michael Larabel

Michael Larabel is the principal author of and founded the site in 2004 with a focus on enriching the Linux hardware experience. Michael has written more than 20,000 articles covering the state of Linux hardware support, Linux performance, graphics drivers, and other topics. Michael is also the lead developer of the Phoronix Test Suite, Phoromatic, and automated benchmarking software. He can be followed via Twitter, LinkedIn, or contacted via

Popular News This Week