[PATCH] D59467: [clang] Adding the Likely Attribute from C++2a to AST

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 18 06:17:44 PDT 2019


aaron.ballman added a comment.

In D59467#1432675 <https://reviews.llvm.org/D59467#1432675>, @Tyker wrote:

> if likely/unlikely can be applied to any statement or label what should be generated when it is applied to a statement that don't have any conditional branch ? should it be ignored  ?


That's the semantics we need to figure out. The standard talks about paths of execution including the attribute (http://eel.is/c++draft/dcl.attr.likelihood#2.sentence-1), so I imagine this means we should be supporting code like:

  void func(bool some_condition) {
    if (some_condition) {
      [[likely]];
    } else {
      [[unlikely]];
    }
  }

While that may seem a bit silly (because you can add the attribute to the compound-statement), there are situations where you cannot do that, but the feature still has use. Consider:

  void func() {
    try {
      some_operation();
    } catch (some_exception &e) {
      [[likely]];
      recover_without_killing_everyone_on_the_elevator();
    }
  }

In some safety-critical situations, you may not care how fast the normal operation works but still need the failure mode to be highly optimized to avoid loss of life.

We have other fun questions to figure out as well, such as whether to diagnose this code and what the semantics should be:

  void func(bool foo) {
    if (foo) {
      [[likely]];
      [[unlikely]];
    }
  }
  
  void func2(bool foo) {
    if (foo) {
      [[likely]];
    } else {
      [[likely]];
    }
  }

We don't have to solve it all up front, but having an idea as to what the backend is going to do with this information may help us design the appropriate frontend behavior where the standard gives us this much latitude. Do you know how this is expected to hook in to LLVM?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D59467/new/

https://reviews.llvm.org/D59467





More information about the cfe-commits mailing list