BlackToppStudios / Mezz_StaticFoundation

All of the parts of the Mezzanine that need to be handled prior to the real build, like compiler options and platform detection.
GNU General Public License v3.0
2 stars 0 forks source link

Make it easy to mark functions as Pure #66

Open Sqeaky opened 5 years ago

Sqeaky commented 5 years ago

In GCC attribute((const)) and attribute__((pure)) are used to mark functions as depending only their input. Having this information allows compilers to make more aggressive optimizations correctly and more easily.

According to the GCC manual - https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html :

pure Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure. For example,

          int square (int) __attribute__ ((pure));

says that the hypothetical function square is safe to call fewer times than the program says.

Some of common examples of pure functions are strlen or memcmp. Interesting non-pure functions are functions with infinite loops or those depending on volatile memory or other system resource, that may change between two consecutive calls (such as feof in a multithreading environment).

The attribute pure is not implemented in GCC versions earlier than 2.96.

Const is just like pure except the function also isn't allowed to dereference pointers it is passed. Here is the manual entry for the const attribute:

const Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory.

Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void.

The attribute const is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function has no side effects, which works in the current version and in some older versions, is as follows:

          typedef int intfn ();

          extern const intfn square;

This approach does not work in GNU C++ from 2.6.0 on, since the language specifies that the `const' must be attached to the return value.

Most other compilers seem to support GCC attributes including Clang and intel: https://stackoverflow.com/questions/2798188/pure-const-function-attributes-in-different-compilers

I propose that we add two macros for tagging functions PURE for const and PURE_WITH_GLOBAL for pure. Which apply these attributes to functions in compilers that support them and resolve to empty on compilers that don't. We might want to come up with better names before.

Sqeaky commented 5 years ago

This should probably also take [[nodiscard]] into account as both involve going over every function and [[nodiscard]] relates to function purity.