imageworks/OpenShadingLanguage — pull request #87 by lgritz (2011-10-24 22:21:58+00:00)
Title: Review: new runtime optimizations

Two new optimizations:

1. Allow aliasing of non-constants within blocks, if they are not subsequently written. That is,

a = b
... [no changing of a or b here]
c = a

can have the last statement changed to "c=b", which could even lead to the elimination of a entirely. We did this before if b was a constant, but now we will do it for non-constants (such as params or locals) as long as their value is not subsequently changed.

2. A more general understanding of "stale values" -- assignments that have not yet been used, and therefore if the variable is reassigned again, the first one can be retroactively eliminated. This comes up in circumstances like this:

a = foo // any op that completely overwrites a and has no other side effects
... no more uses of 'a' here, either reads or writes...
a = bar // any op that completely overwrites a again

In this case, the a=foo can be eliminated, since it won't be used before its value is changed.

Previously, we recognized that simple case of "a=b;a=c" => "a=c", but only for "assign" and only if they were immediately adjacent instructions within the same basic block. This fully generalizes -- they can have other instructions in between, and it can be ops other than assignment. For example, "a=b+c; ..all sorts of stuf... ; a=d*e" is still safe to eliminate that first add.

I think the comments and code make it clear how exactly this works and how we ensure that it's safe.

This results in about 10% fewer instructions for the Tweedle test scene, by the time our optimizer gets done with it. I'm sorry to say that the runtime is not changed significantly, but maybe it'll be better for other scenes, and in any case I'm still adding other optimizations.