Aussie AI
People Helping Parsers
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
People Helping Parsers
The humble C++ compiler needs your attention. Hat in hand, the compiler is sitting there saying “I am but a poor, helpless lexer, without even a single neural network. Please help me.” Hence, please consider donating your time to help a poor struggling compiler in your neighborhood.
There is a long history of the C++ compiler needing “hints” about optimization from the programmer.
The early C++ language in the 1990s had a “register
” specifier that hinted to the compiler
that a variable was going to be highly used, and the compiler should optimize it by putting the variable in a CPU register.
The “register
” keyword has since been deprecated in C++17, which indicates that compiler register allocation algorithms
no longer benefit from human help.
Some of the other longstanding C++ keywords that can be used for efficiency-related purposes include:
inline
const
static
And with the evolving C++ standards, there's a whole new set of directives that are hints to the compiler about how to optimize:
constexpr
constinit
consteval
reinterpret_cast
- restricted pointers (“
restrict
”) [[likely]]
and[[unlikely]]
path attributes
The constexpr
and related directives help the compiler do “constant folding” and “constant propagation”
to compute as much as possible at compile-time, thereby avoiding any runtime cost for lots of code.
In fact, the idea is extended to its logical asymptote, whereby you can declare an entire function as “constexpr
”
and then expect the poor compiler to interpret the whole mess at compile-time.
Pity the overworked compiler designers.
The “restrict
” pointer declarations help the compiler with advanced optimizations like loop unrolling
and vectorization by telling the compiler to ignore potential “aliasing” of pointers, allowing much more powerful
code transformations on loops.
The restricted pointer optimizations are actually of more interest than constexpr
for AI development.
These have been formalized in C++23, but non-standard versions have long existed.
The possible benefit for C++ AI engines is that restricted pointer specifications
might help the compiler do auto-vectorization of loops into parallel hardware-assisted code.
How much do these help?
It's rather unclear,
and the compiler is free to simply ignore these hints.
Compilers already did a lot of constant propagation optimizations before the “constexpr
” directives came along,
so presumably compiler designers have upped their game even further now.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |