<div dir="ltr">On Wed, Jul 3, 2013 at 8:36 AM, Aaron Ballman <span dir="ltr"><<a href="mailto:aaron@aaronballman.com" target="_blank">aaron@aaronballman.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">> Index: include/llvm/Support/Compiler.h<br>
> ===================================================================<br>
> --- include/llvm/Support/Compiler.h<br>
> +++ include/llvm/Support/Compiler.h<br>
> @@ -21,6 +21,25 @@<br>
>  # define __has_feature(x) 0<br>
>  #endif<br>
><br>
> +#ifndef __has_attribute<br>
> +# define __has_attribute(x) 0<br>
> +#endif<br>
> +<br>
> +#ifndef __has_builtin<br>
> +# define __has_builtin(x) 0<br>
> +#endif<br>
> +<br>
> +/// \macro __GNUC_PREREQ<br>
> +/// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.<br>
> +#ifndef __GNUC_PREREQ<br>
> +# if defined(__GNUC__) && defined(__GNUC_MINOR__)<br>
> +#  define __GNUC_PREREQ(maj, min) \<br>
> +    ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))<br>
> +# else<br>
> +#  define __GNUC_PREREQ(maj, min) 0<br>
> +# endif<br>
> +#endif<br>
> +<br>
>  /// \brief Does the compiler support r-value references?<br>
>  /// This implies that <utility> provides the one-argument std::move;  it<br>
>  /// does not imply the existence of any other C++ library features.<br>
> @@ -146,13 +165,13 @@<br>
>  /// into a shared library, then the class should be private to the library and<br>
>  /// not accessible from outside it.  Can also be used to mark variables and<br>
>  /// functions, making them private to any shared library they are linked into.<br>
> -#if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
> +#if __GNUC_PREREQ(4, 0) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
<br>
</div></div>__has_attribute(visibility)?</blockquote><div><br></div><div style>Sure.  I didn't change it because my goal is self-hosting clang on Windows, in which case I don't actually want this define, since library visibility is the default already.  I can probably disable this if LLVM_ON_WIN32.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
>  #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))<br>
>  #else<br>
>  #define LLVM_LIBRARY_VISIBILITY<br>
>  #endif<br>
><br>
> -#if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))<br>
> +#if __has_attribute(used) || __GNUC_PREREQ(3, 1)<br>
>  #define LLVM_ATTRIBUTE_USED __attribute__((__used__))<br>
>  #else<br>
>  #define LLVM_ATTRIBUTE_USED<br>
> @@ -166,31 +185,33 @@<br>
>  // more portable solution:<br>
>  //   (void)unused_var_name;<br>
>  // Prefer cast-to-void wherever it is sufficient.<br>
> -#if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))<br>
> +#if __has_attribute(unused) || __GNUC_PREREQ(3, 1)<br>
>  #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))<br>
>  #else<br>
>  #define LLVM_ATTRIBUTE_UNUSED<br>
>  #endif<br>
><br>
> -#if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
> +#if __GNUC_PREREQ(4, 0) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
<br>
</div>__has_attribute(weak)?</blockquote><div><br></div><div style>Again, I didn't want the behavior change, so I'll have to turn it off for win32 explicitly.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5">
>  #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))<br>
>  #else<br>
>  #define LLVM_ATTRIBUTE_WEAK<br>
>  #endif<br>
><br>
> -#ifdef __GNUC__ // aka 'CONST' but following LLVM Conventions.<br>
> +#if __has_attribute(const) || defined(__GNUC__)<br>
> +// aka 'CONST' but following LLVM Conventions.<br>
>  #define LLVM_READNONE __attribute__((__const__))<br>
>  #else<br>
>  #define LLVM_READNONE<br>
>  #endif<br>
><br>
> -#ifdef __GNUC__  // aka 'PURE' but following LLVM Conventions.<br>
> +#if __has_attribute(pure) || defined(__GNUC__)<br>
> +// aka 'PURE' but following LLVM Conventions.<br>
>  #define LLVM_READONLY __attribute__((__pure__))<br>
>  #else<br>
>  #define LLVM_READONLY<br>
>  #endif<br>
><br>
> -#if (__GNUC__ >= 4)<br>
> +#if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)<br>
>  #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)<br>
>  #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)<br>
>  #else<br>
> @@ -213,7 +234,7 @@<br>
><br>
>  /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,<br>
>  /// mark a method "not for inlining".<br>
> -#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))<br>
> +#if __GNUC_PREREQ(3, 4)<br>
<br>
</div></div>__has_attribute(noinline)?</blockquote><div><br></div><div style>Sure, no harm.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">

>  #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))<br>
>  #elif defined(_MSC_VER)<br>
>  #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)<br>
> @@ -225,7 +246,7 @@<br>
>  /// so, mark a method "always inline" because it is performance sensitive. GCC<br>
>  /// 3.4 supported this but is buggy in various cases and produces unimplemented<br>
>  /// errors, just use it in GCC 4.0 and later.<br>
> -#if __GNUC__ > 3<br>
> +#if __GNUC_PREREQ(3, 0)<br>
<br>
</div>__has_attribute(always_inline)</blockquote><div><br></div><div style>Done.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">

>  #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))<br>
>  #elif defined(_MSC_VER)<br>
>  #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline<br>
> @@ -267,27 +288,23 @@<br>
>  /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands<br>
>  /// to an expression which states that it is undefined behavior for the<br>
>  /// compiler to reach this point.  Otherwise is not defined.<br>
> -#if defined(__clang__) || (__GNUC__ > 4) \<br>
> - || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)<br>
> +#if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)<br>
>  # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()<br>
>  #elif defined(_MSC_VER)<br>
>  # define LLVM_BUILTIN_UNREACHABLE __assume(false)<br>
>  #endif<br>
><br>
>  /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression<br>
>  /// which causes the program to exit abnormally.<br>
> -#if defined(__clang__) || (__GNUC__ > 4) \<br>
> - || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)<br>
> +#if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)<br>
>  # define LLVM_BUILTIN_TRAP __builtin_trap()<br>
>  #else<br>
>  # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0<br>
>  #endif<br>
><br>
>  /// \macro LLVM_ASSUME_ALIGNED<br>
>  /// \brief Returns a pointer with an assumed alignment.<br>
> -#if !defined(__clang__) && ((__GNUC__ > 4) \<br>
> - || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))<br>
> -// FIXME: Enable on clang when it supports it.<br>
> +#if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)<br>
>  # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)<br>
>  #elif defined(LLVM_BUILTIN_UNREACHABLE)<br>
>  # define LLVM_ASSUME_ALIGNED(p, a) \<br>
<br>
</div></div>Looks good to me aside from what may or may not be missing<br>
__has_attribute calls.  Thanks!<br>
<br>
~Aaron<br>
<div><div class="h5"><br>
On Tue, Jul 2, 2013 at 5:26 PM, Reid Kleckner <<a href="mailto:rnk@google.com">rnk@google.com</a>> wrote:<br>
>     - Conditionally define __has_builtin.<br>
><br>
> <a href="http://llvm-reviews.chandlerc.com/D1080" target="_blank">http://llvm-reviews.chandlerc.com/D1080</a><br>
><br>
> CHANGE SINCE LAST DIFF<br>
>   <a href="http://llvm-reviews.chandlerc.com/D1080?vs=2654&id=2655#toc" target="_blank">http://llvm-reviews.chandlerc.com/D1080?vs=2654&id=2655#toc</a><br>
><br>
> Files:<br>
>   include/llvm/Support/Compiler.h<br>
><br>
> Index: include/llvm/Support/Compiler.h<br>
> ===================================================================<br>
> --- include/llvm/Support/Compiler.h<br>
> +++ include/llvm/Support/Compiler.h<br>
> @@ -21,6 +21,25 @@<br>
>  # define __has_feature(x) 0<br>
>  #endif<br>
><br>
> +#ifndef __has_attribute<br>
> +# define __has_attribute(x) 0<br>
> +#endif<br>
> +<br>
> +#ifndef __has_builtin<br>
> +# define __has_builtin(x) 0<br>
> +#endif<br>
> +<br>
> +/// \macro __GNUC_PREREQ<br>
> +/// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.<br>
> +#ifndef __GNUC_PREREQ<br>
> +# if defined(__GNUC__) && defined(__GNUC_MINOR__)<br>
> +#  define __GNUC_PREREQ(maj, min) \<br>
> +    ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))<br>
> +# else<br>
> +#  define __GNUC_PREREQ(maj, min) 0<br>
> +# endif<br>
> +#endif<br>
> +<br>
>  /// \brief Does the compiler support r-value references?<br>
>  /// This implies that <utility> provides the one-argument std::move;  it<br>
>  /// does not imply the existence of any other C++ library features.<br>
> @@ -146,13 +165,13 @@<br>
>  /// into a shared library, then the class should be private to the library and<br>
>  /// not accessible from outside it.  Can also be used to mark variables and<br>
>  /// functions, making them private to any shared library they are linked into.<br>
> -#if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
> +#if __GNUC_PREREQ(4, 0) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
>  #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))<br>
>  #else<br>
>  #define LLVM_LIBRARY_VISIBILITY<br>
>  #endif<br>
><br>
> -#if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))<br>
> +#if __has_attribute(used) || __GNUC_PREREQ(3, 1)<br>
>  #define LLVM_ATTRIBUTE_USED __attribute__((__used__))<br>
>  #else<br>
>  #define LLVM_ATTRIBUTE_USED<br>
> @@ -166,31 +185,33 @@<br>
>  // more portable solution:<br>
>  //   (void)unused_var_name;<br>
>  // Prefer cast-to-void wherever it is sufficient.<br>
> -#if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))<br>
> +#if __has_attribute(unused) || __GNUC_PREREQ(3, 1)<br>
>  #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))<br>
>  #else<br>
>  #define LLVM_ATTRIBUTE_UNUSED<br>
>  #endif<br>
><br>
> -#if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
> +#if __GNUC_PREREQ(4, 0) && !defined(__MINGW32__) && !defined(__CYGWIN__)<br>
>  #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))<br>
>  #else<br>
>  #define LLVM_ATTRIBUTE_WEAK<br>
>  #endif<br>
><br>
> -#ifdef __GNUC__ // aka 'CONST' but following LLVM Conventions.<br>
> +#if __has_attribute(const) || defined(__GNUC__)<br>
> +// aka 'CONST' but following LLVM Conventions.<br>
>  #define LLVM_READNONE __attribute__((__const__))<br>
>  #else<br>
>  #define LLVM_READNONE<br>
>  #endif<br>
><br>
> -#ifdef __GNUC__  // aka 'PURE' but following LLVM Conventions.<br>
> +#if __has_attribute(pure) || defined(__GNUC__)<br>
> +// aka 'PURE' but following LLVM Conventions.<br>
>  #define LLVM_READONLY __attribute__((__pure__))<br>
>  #else<br>
>  #define LLVM_READONLY<br>
>  #endif<br>
><br>
> -#if (__GNUC__ >= 4)<br>
> +#if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)<br>
>  #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)<br>
>  #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)<br>
>  #else<br>
> @@ -213,7 +234,7 @@<br>
><br>
>  /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,<br>
>  /// mark a method "not for inlining".<br>
> -#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))<br>
> +#if __GNUC_PREREQ(3, 4)<br>
>  #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))<br>
>  #elif defined(_MSC_VER)<br>
>  #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)<br>
> @@ -225,7 +246,7 @@<br>
>  /// so, mark a method "always inline" because it is performance sensitive. GCC<br>
>  /// 3.4 supported this but is buggy in various cases and produces unimplemented<br>
>  /// errors, just use it in GCC 4.0 and later.<br>
> -#if __GNUC__ > 3<br>
> +#if __GNUC_PREREQ(3, 0)<br>
>  #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))<br>
>  #elif defined(_MSC_VER)<br>
>  #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline<br>
> @@ -267,27 +288,23 @@<br>
>  /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands<br>
>  /// to an expression which states that it is undefined behavior for the<br>
>  /// compiler to reach this point.  Otherwise is not defined.<br>
> -#if defined(__clang__) || (__GNUC__ > 4) \<br>
> - || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)<br>
> +#if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)<br>
>  # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()<br>
>  #elif defined(_MSC_VER)<br>
>  # define LLVM_BUILTIN_UNREACHABLE __assume(false)<br>
>  #endif<br>
><br>
>  /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression<br>
>  /// which causes the program to exit abnormally.<br>
> -#if defined(__clang__) || (__GNUC__ > 4) \<br>
> - || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)<br>
> +#if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)<br>
>  # define LLVM_BUILTIN_TRAP __builtin_trap()<br>
>  #else<br>
>  # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0<br>
>  #endif<br>
><br>
>  /// \macro LLVM_ASSUME_ALIGNED<br>
>  /// \brief Returns a pointer with an assumed alignment.<br>
> -#if !defined(__clang__) && ((__GNUC__ > 4) \<br>
> - || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))<br>
> -// FIXME: Enable on clang when it supports it.<br>
> +#if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)<br>
>  # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)<br>
>  #elif defined(LLVM_BUILTIN_UNREACHABLE)<br>
>  # define LLVM_ASSUME_ALIGNED(p, a) \<br>
><br>
</div></div>> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
><br>
</blockquote></div><br></div></div>