Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easy to mark functions as Pure #66

Open
Sqeaky opened this issue Sep 9, 2019 · 1 comment
Open

Make it easy to mark functions as Pure #66

Sqeaky opened this issue Sep 9, 2019 · 1 comment

Comments

@Sqeaky
Copy link
Member

Sqeaky commented Sep 9, 2019

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 Sqeaky added this to the PreJagatiSprint milestone Sep 9, 2019
@Sqeaky
Copy link
Member Author

Sqeaky commented Sep 9, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant