mattgodbolt / pt-three-ways

Path tracing, done three ways
MIT License
191 stars 20 forks source link

Unpredictable::any doesn't produce efficient code with clang #8

Open njames93 opened 2 years ago

njames93 commented 2 years ago

When building with clang, the Unpredictable::any workaround will still cause multiple branches, playing havoc with the branch predictor. Clang has a way to define a branch as being unpredictable and using that yields much better code and on the suzanne scene, my render time dropped from 81s -> 66s The hacked way i did this was

#if __has_builtin(__builtin_unpredictable)
#define UNPREDICTABLE(x) __builtin_unpredictable(x)
#else
#define UNPREDICTABLE(x) x
#endif
    if (UNPREDICTABLE(u < 0.0) || UNPREDICTABLE(u > 1.0)
        || UNPREDICTABLE(v < 0.0) || UNPREDICTABLE((u + v) > 1))
      continue;

For the case when that isn't available, I tried using this(which works for gcc too), but clang still emitted branches here

#elif __has_builtin(__builtin_expect_with_probability)
#define UNPREDICTABLE(x) __builtin_expect_with_probability(x, 1, 0.5)