Skip to content

Commit

Permalink
Failing assertions is unlikely (pytorch#20876)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#20876

Tell the compiler that assertions are likely to succeed.
This allows the compiler to generate betterr code and optimize for the success case.

Differential Revision: D15480066

fbshipit-source-id: 4485154d66b2ee0ef8a401718712dbd61d811aee
  • Loading branch information
smessmer authored and facebook-github-bot committed May 29, 2019
1 parent 9daf485 commit 99b057d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions c10/util/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ inline std::string if_empty_then(std::string x, std::string y) {
//
#ifdef C10_MOBILE
#define TORCH_INTERNAL_ASSERT(cond, ...) \
if (!(cond)) { \
if (C10_UNLIKELY(!(cond))) { \
C10_THROW_ERROR(Error, \
#cond " INTERNAL ASSERT FAILED at" \
__FILE__ \
); \
}
#else
#define TORCH_INTERNAL_ASSERT(cond, ...) \
if (!(cond)) { \
if (C10_UNLIKELY(!(cond))) { \
C10_THROW_ERROR(Error, ::c10::str( \
#cond " INTERNAL ASSERT FAILED at ", \
__FILE__, \
Expand Down Expand Up @@ -213,23 +213,23 @@ inline std::string if_empty_then(std::string x, std::string y) {
//
#ifdef C10_MOBILE
#define TORCH_CHECK(cond, ...) \
if (!(cond)) { \
if (C10_UNLIKELY(!(cond))) { \
C10_THROW_ERROR(Error, \
#cond " CHECK FAILED at " \
__FILE__ \
); \
}
#else
#define TORCH_CHECK(cond, ...) \
if (!(cond)) { \
if (C10_UNLIKELY(!(cond))) { \
C10_THROW_ERROR(Error, \
::c10::detail::if_empty_then( \
::c10::str(__VA_ARGS__), \
"Expected " #cond " to be true, but got false. " \
"(Could this error message be improved? If so, " \
"please report an enhancement request to PyTorch.)" \
) \
); \
) \
); \
}
#endif
// TODO: We're going to get a lot of similar looking string literals
Expand All @@ -238,15 +238,15 @@ inline std::string if_empty_then(std::string x, std::string y) {
// Like TORCH_CHECK, but raises IndexErrors instead of Errors.
#ifdef C10_MOBILE
#define TORCH_CHECK_INDEX(cond, ...) \
if (!(cond)) { \
if (C10_UNLIKELY(!(cond))) { \
C10_THROW_ERROR(Error, \
#cond " INDEX CHECK FAILED at " \
__FILE__ \
); \
}
#else
#define TORCH_CHECK_INDEX(cond, ...) \
if (!(cond)) { \
if (C10_UNLIKELY(!(cond))) { \
C10_THROW_ERROR(IndexError, \
::c10::detail::if_empty_then( \
::c10::str(__VA_ARGS__), \
Expand Down
8 changes: 4 additions & 4 deletions c10/util/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ using EnforceNotMet = ::c10::Error;

#define CAFFE_ENFORCE(condition, ...) \
do { \
if (!(condition)) { \
if (C10_UNLIKELY(!(condition))) { \
::c10::ThrowEnforceNotMet( \
__FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \
} \
} while (false)

#define CAFFE_ENFORCE_WITH_CALLER(condition, ...) \
do { \
if (!(condition)) { \
if (C10_UNLIKELY(!(condition))) { \
::c10::ThrowEnforceNotMet( \
__FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__), this); \
} \
Expand Down Expand Up @@ -198,7 +198,7 @@ BINARY_COMP_HELPER(LessEquals, <=)
do { \
using namespace ::c10::enforce_detail; \
const EnforceFailMessage& CAFFE_ENFORCE_THAT_IMPL_r_ = (condition); \
if (CAFFE_ENFORCE_THAT_IMPL_r_.bad()) { \
if (C10_UNLIKELY(CAFFE_ENFORCE_THAT_IMPL_r_.bad())) { \
::c10::ThrowEnforceNotMet( \
__FILE__, \
__LINE__, \
Expand All @@ -213,7 +213,7 @@ BINARY_COMP_HELPER(LessEquals, <=)
using namespace ::c10::enforce_detail; \
const EnforceFailMessage& CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_ = \
(condition); \
if (CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_.bad()) { \
if (C10_UNLIKELY(CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_.bad())) { \
::c10::ThrowEnforceNotMet( \
__FILE__, \
__LINE__, \
Expand Down

0 comments on commit 99b057d

Please sign in to comment.