Skip to main content

QtWS15- Writing better code with help from Qt and the compiler, Thiago Macieira, Intel

Writing better code with help from Qt and the compiler: Traditionally, getting the most out of a processor required writing assembly code that used specialised instructions to accomplish some particular tasks. And though that's still widely used, processors are very complex and maintaining assembly code by hand is a hard and tedious task. Add to that the fact that processors evolve and getting the timings right of each generation is better left to the compiler. A little known feature of the compilers is that it is possible to get access to certain instructions from high-level C and C++ code, by way of intrinsic functions, thus allowing developers of native code to get very close to bare metal performance. Yet modern compilers can offer more functionality to help the bold developer write better code. For example, GCC can offer an automatic way of dispatching to different functions based on the architecture the code is being run on. Both GCC and the Intel compiler also allow code for multiple targets to be written in one single C or C++ source file, thereby allowing for better optimisation by way of inlines and dead code elimination. All is well and good if you know exactly which compiler you're using. When one has to deal with multiple compilers, like Qt routinely has to, things may get tricky. If the objective is to write optimal code for important, hot code paths, we don't want to settle for "lowest common denominator" of compiler support. To make sure we don't fall into that trap, Qt has a series of macros and both compile- and run-time processor feature detection support to generate the best code given the environment in use. This presentation will present those techniques and show which ones work on GCC, Clang, the Intel compiler and Microsoft Visual Studio. It will also show how to get more information on which intrinsics exist and what they do. It will draw heavily upon the presenter's experience implementing this very type of optimisations for Qt 5.3 and 5.3.