For Years We Preached restrict. Now We Have a Number 4.9x.
emmtrix Tech Posts
Category: Code Optimization
While compiling TinyLlama 1.1B to C with emx-onnx-cgen, one qualifier on each operator parameter took a decode step from 1,150 ms to 236 ms.
We did not go looking for this result.
When restrict came up in discussions with customers, the example was usually memcpy, whose source and destination must not overlap. A rather abstract case. We have emitted restrict in our code generators for years as a precaution, but never had a number we could point to. Now we do.
The restrict keyword in C is a promise to the compiler: the memory behind this pointer is reached through no other pointer. The compiler may then optimize as if nothing overlaps.
Our new wiki article looks at what is behind that number, and what is not.
๐ช๐ต๐ ๐ถ๐ ๐บ๐ฎ๐๐๐ฒ๐ฟ๐ ๐๐ผ ๐บ๐๐ฐ๐ต ๐ต๐ฒ๐ฟ๐ฒ
In a quantized language model, the compiler cannot work out on its own that the results and the weights sit in different memory. So it plays safe and leaves many of the fast vector instructions unused. restrict gives it the missing information.
๐ช๐ต๐ฎ๐ ๐๐ต๐ฒ ๐ฎ๐ฟ๐๐ถ๐ฐ๐น๐ฒ ๐ฐ๐ผ๐๐ฒ๐ฟ๐
- Why restrict often changes little in simple loops, and a lot in nested ones
- Which compilers actually use it: two of the six we tried accept it and ignore it
- How Fortran and Rust solve the same problem without the keyword
- How to use it safely, and why that is easier for a code generator than for hand-written code
One caveat: the figures are single runs on a PC, not on an embedded target. The details are in the article.
restrict is a promise that nobody checks. It is worth a factor of five when it is true, and a long afternoon when it is not.
