KhronosGroup/webcl-validator — pull request #58 by ollisal (2013-12-12 20:11:15+00:00)
Title: Forward-declare all used built-in functions, fixing issue #54, and becoming 25% faster while doing that

This brings back the declarations for OpenCL builtin functions which were originally in kernel.cl, but only if they have been used in the source that is being validated.

The general approach is to look at the preprocessor identifier table after preprocessing stage is done, and seeing if any lexed identifiers match known OpenCL builtin function names. In this case, the appropriate macro invocations to declare the corresponding function are put into a temporary header that is included by the matcher and validation stages.

This originally increased the time it takes to run the test suite from around 60s to around 70s. For comparison, unconditionally including all builtins makes the test suite take 5-10min. (This is on Visual Studio Debug build, which is very slow to start with)

However, I discovered some interesting optimization possibilities while doing this. One of them is avoiding declaring lots of overloads for those builtin functions whose all overloads are anyway forbidden. This is the case for prefetch() and async_work_group(_strided)_copy(), for which one void * argument can replace all gentype * overloads. Nuking the gentype * overloads brought down time to run the test suite from 70s to ~45s.

It would be possible to optimize further to <=35s by declaring the as_XXX(YYY) functions only if they are used, with the same infrastructure. This would require refactoring the corresponding macros in kernel.cl quite extensively. Smaller optimizations could be achieved by only declaring the atomic_*() functions if used, etc.

Even more optimization suggestions are included as TODO comments.