Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Support "__builtin_expect_with_probability" (C/C++) #38442

Open Quuxplusone opened 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR39469
Status NEW
Importance P enhancement
Reported by Haneef Mubarak (haneef503@gmail.com)
Reported on 2018-10-28 02:58:59 -0700
Last modified on 2018-10-29 07:31:09 -0700
Version trunk
Hardware All All
CC dgregor@apple.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk, spatel+llvm@rotateright.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also

The next GCC includes support for a new builtin ` [1] that is similar to__builtin_expect()`, except with support for real probabilities:

(long exp, long c, long probability)

The built-in has same semantics as __builtin_expect_with_probability, but user can provide expected probability (in percent) for value of exp. Last argument probability is of float type and valid values are in inclusive range 0.0f and 1.0f.

This builtin is useful in that it allows better specification of branch probabilities, which in theory should be able to be used to make more informed decisions regarding code block generation and organization.

Quuxplusone commented 6 years ago

That documentation is incoherent. Is the last parameter of type float or long? Is it a percentage or a value from 0 to 1?

Quuxplusone commented 6 years ago
(In reply to Richard Smith from comment #1)
> That documentation is incoherent. Is the last parameter of type float or
> long? Is it a percentage or a value from 0 to 1?

Admittedly they did a poor job with the documentation there. The last parameter
is of type float (single) and is a value in the inclusive range 0.0f to 1.0,
where 0.0f represents a 0% (never) probability of the branch being taken and
1.0f represents as 100% (always) probability of the branch being taken.
Quuxplusone commented 6 years ago

Is there a link to the discussion that led to that addition in GCC? I'm curious what optimizer/code-generation expectation customers will have for this.

The chance of us doing anything useful with that percentage parameter seem slim to me. Ie, we have very little hope of statically modeling any reasonably complex dynamic branch predictor. A 50/50 branch may be entirely predictable dynamically.