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.

Illustrative C function signature with the restrict keyword highlighted on three pointer parameters. A blue arrow and โ€œWHY?โ€ draw attention to the keyword. Text reads: โ€œOne keyword. 4.9ร— faster.โ€ emmtrix Technologies logo at the bottom right.

The restrict Keyword: One Keyword. 4.9x Faster.

Cookie Consent with Real Cookie Banner