This is an archive of the discontinued LLVM Phabricator instance.

Fix warning caused by __builtin_expect_with_probability was not handled in places such as constant folding
ClosedPublic

Authored by LukeZhuang on Jul 7 2020, 5:27 PM.

Details

Summary

Previously some places that should have handled __builtin_expect_with_probability is missing, so in some case it acts differently than __builtin_expect.
For example it was not handled in constant folding, thus in the following program, the "if" condition should be constantly true and folded, but previously it was not handled and cause warning "control may reach end of non-void function" (while __builtin_expect does not):

__attribute__((noreturn)) extern void bar();
int foo(int x, int y) {
  if (y) {
    if (__builtin_expect_with_probability(1, 1, 1))
      bar();
  }
   else
    return 0;
}

Now it's fixed.

Diff Detail

Event Timeline

LukeZhuang created this revision.Jul 7 2020, 5:27 PM
erichkeane added inline comments.Jul 8 2020, 6:10 AM
clang/test/Sema/builtin-expect-with-probability.cpp
12

Please also validate this by doing some sort of test in a constexpr function if possible. That way you can make sure the parameter is evaluated.

updated: 07/08/2020
(1) improve test case

LukeZhuang marked an inline comment as done.Jul 8 2020, 12:17 PM

@erichkeane Thank you very much for the comments. I have added test case to make sure it is evaluated during compile time. Previous code could not pass these test cases.

erichkeane added inline comments.Jul 8 2020, 12:21 PM
clang/test/Sema/builtin-expect-with-probability.cpp
17

I mean something more like:

template<int b>
void foo() { static_assert(!b,""); } // expected-error {{....}}

void bar() {
  foo<__builtin_expect_with_probability(1,1,1)>();
}

This example doesn't require that constf is executed at compile time. You'd need to either send the result of constf to a template, or assign it to a constexpr variable in order to make it work.

So something like:

void foo() {
   constexpr int f = constf();
   static_assert(f == 1, "");
}
LukeZhuang updated this revision to Diff 276557.Jul 8 2020, 2:24 PM

updated: 07/08/2020
(1) improve test case

LukeZhuang marked an inline comment as done.Jul 8 2020, 2:24 PM

Thank you for the new comment. I've modified my test cases

erichkeane accepted this revision.Jul 8 2020, 3:56 PM
This revision is now accepted and ready to land.Jul 8 2020, 3:56 PM

Hi, could you please help me commit this change? Thank you very much!

erichkeane closed this revision.Jul 9 2020, 8:03 AM

Gah, misspelled differential revision.

Thank you very much!