[LLVMbugs] [Bug 19230] New: Honor __builtin_expect through inlining
bugzilla-daemon at llvm.org
bugzilla-daemon at llvm.org
Sun Mar 23 07:21:27 PDT 2014
http://llvm.org/bugs/show_bug.cgi?id=19230
Bug ID: 19230
Summary: Honor __builtin_expect through inlining
Product: libraries
Version: trunk
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P
Component: Transformation Utilities
Assignee: unassignedbugs at nondot.org
Reporter: wielkiegie at gmail.com
CC: llvmbugs at cs.uiuc.edu
Classification: Unclassified
Created attachment 12269
--> http://llvm.org/bugs/attachment.cgi?id=12269&action=edit
Test case
A common usage model of __builtin_expect is to create helper macros called
'likely' and 'unlikely':
#if __has_builtin(__builtin_expect)
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
#else
# define likely(x) (x)
# define unlikely(x) (x)
#endif
However using macros (especially in C++) has a major drawback: short names
pollute namespace and long ones are harder to use.
I wanted to fix this by using inline functions (which can reside in a
namespace) instead of macros:
#if __has_builtin(__builtin_expect)
inline bool likely (const bool x) { return __builtin_expect(x, 1); }
inline bool unlikely(const bool x) { return __builtin_expect(x, 0); }
#else
inline bool likely (const bool x) { return x; }
inline bool unlikely(const bool x) { return x; }
#endif
These inline functions above work as expected in GCC but unfortunately not in
LLVM/Clang (tested both with -O3). Apparently, the branch weight metadata from
llvm.expect intrinsic are dropped before the inlining occurs. Doing the
inlining explicitly before any other optimizations fixes this problem.
$ clang++ llvm-expect.cpp -o - -emit-llvm -c -O3 -DUSE_MACROS | llvm-dis | grep
branch_weights
!1 = metadata !{metadata !"branch_weights", i32 64, i32 4}
$ clang++ llvm-expect.cpp -o - -emit-llvm -c -O3 | llvm-dis | grep
branch_weights
$ clang++ llvm-expect.cpp -o - -emit-llvm -c | opt -inline | opt -O3 | llvm-dis
| grep branch_weights
!1 = metadata !{metadata !"branch_weights", i32 64, i32 4}
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140323/ea774a82/attachment.html>
More information about the llvm-bugs
mailing list