Mesa (master): mesa: Detect and provide macros for function attributes pure and const.

Eric Anholt anholt at kemper.freedesktop.org
Fri Jul 17 19:38:01 UTC 2015


Module: Mesa
Branch: master
Commit: be1f49bda90425b7fd009ac177b307e61da0f994
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=be1f49bda90425b7fd009ac177b307e61da0f994

Author: Eric Anholt <eric at anholt.net>
Date:   Fri Jul 10 16:25:26 2015 -0700

mesa: Detect and provide macros for function attributes pure and const.

These are really useful hints to the compiler in the absence of link-time
optimization, and I'm going to use them in VC4.

I've made the const attribute be ATTRIBUTE_CONST unlike other function
attributes, because we have other things in the tree #defining CONST for
their own unrelated purposes.

v2: Alphabetize.

Reviewed-by: Kenneth Graunke <kenneth at whitecape.org> (v1)

---

 configure.ac      |    2 ++
 src/util/macros.h |   20 ++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/configure.ac b/configure.ac
index bdfd134..bb6d4f6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -205,10 +205,12 @@ AX_GCC_BUILTIN([__builtin_popcount])
 AX_GCC_BUILTIN([__builtin_popcountll])
 AX_GCC_BUILTIN([__builtin_unreachable])
 
+AX_GCC_FUNC_ATTRIBUTE([const])
 AX_GCC_FUNC_ATTRIBUTE([flatten])
 AX_GCC_FUNC_ATTRIBUTE([format])
 AX_GCC_FUNC_ATTRIBUTE([malloc])
 AX_GCC_FUNC_ATTRIBUTE([packed])
+AX_GCC_FUNC_ATTRIBUTE([pure])
 AX_GCC_FUNC_ATTRIBUTE([unused])
 AX_GCC_FUNC_ATTRIBUTE([warn_unused_result])
 
diff --git a/src/util/macros.h b/src/util/macros.h
index 66698e7..5c5c92e 100644
--- a/src/util/macros.h
+++ b/src/util/macros.h
@@ -103,6 +103,17 @@ do {                       \
 #define assume(expr) assert(expr)
 #endif
 
+/* Attribute const is used for functions that have no effects other than their
+ * return value, and only rely on the argument values to compute the return
+ * value.  As a result, calls to it can be CSEed.  Note that using memory
+ * pointed to by the arguments is not allowed for const functions.
+ */
+#ifdef HAVE_FUNC_ATTRIBUTE_CONST
+#define ATTRIBUTE_CONST __attribute__((__const__))
+#else
+#define ATTRIBUTE_CONST
+#endif
+
 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
 #define FLATTEN __attribute__((__flatten__))
 #else
@@ -130,6 +141,15 @@ do {                       \
 #define PACKED
 #endif
 
+/* Attribute pure is used for functions that have no effects other than their
+ * return value.  As a result, calls to it can be dead code eliminated.
+ */
+#ifdef HAVE_FUNC_ATTRIBUTE_PURE
+#define PURE __attribute__((__pure__))
+#else
+#define PURE
+#endif
+
 #ifdef __cplusplus
 /**
  * Macro function that evaluates to true if T is a trivially




More information about the mesa-commit mailing list