From dd9776be63b6f67f051a7aa0e82d10ac1c9f2f79 Mon Sep 17 00:00:00 2001 From: Filip Niksic Date: Mon, 3 Mar 2025 13:25:57 -0800 Subject: [PATCH] Replace C++20 branch predictor hints with C++17-compliant Abseil versions. PiperOrigin-RevId: 733038863 --- centipede/runner_interceptors.cc | 9 +++++---- common/BUILD | 1 + common/CMakeLists.txt | 1 + common/blob_file.cc | 11 +++++------ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/centipede/runner_interceptors.cc b/centipede/runner_interceptors.cc index 5bb3622d..dfb5f3f6 100644 --- a/centipede/runner_interceptors.cc +++ b/centipede/runner_interceptors.cc @@ -21,6 +21,7 @@ #include #include "absl/base/nullability.h" +#include "absl/base/optimization.h" #include "./centipede/runner.h" using centipede::tls; @@ -143,7 +144,7 @@ static NO_SANITIZE int memcmp_fallback(const void *s1, const void *s2, extern "C" NO_SANITIZE int memcmp(const void *s1, const void *s2, size_t n) { const int result = memcmp_orig ? memcmp_orig(s1, s2, n) : memcmp_fallback(s1, s2, n); - if (!tls.started) [[unlikely]] { + if (ABSL_PREDICT_FALSE(!tls.started)) { return result; } tls.TraceMemCmp(reinterpret_cast(__builtin_return_address(0)), @@ -167,7 +168,7 @@ extern "C" NO_SANITIZE int strcmp(const char *s1, const char *s2) { // Need to include one more byte than the shorter string length // when falling back to memcmp e.g. "foo" < "foobar". strcmp_orig ? strcmp_orig(s1, s2) : memcmp_fallback(s1, s2, len + 1); - if (!tls.started) [[unlikely]] { + if (ABSL_PREDICT_FALSE(!tls.started)) { return result; } // Pass `len` here to avoid storing the trailing '\0' in the dictionary. @@ -190,7 +191,7 @@ extern "C" NO_SANITIZE int strncmp(const char *s1, const char *s2, size_t n) { if (n > len + 1) n = len + 1; const int result = strncmp_orig ? strncmp_orig(s1, s2, n) : memcmp_fallback(s1, s2, n); - if (!tls.started) [[unlikely]] { + if (ABSL_PREDICT_FALSE(!tls.started)) { return result; } // Pass `len` here to avoid storing the trailing '\0' in the dictionary. @@ -206,7 +207,7 @@ extern "C" int pthread_create(absl::Nonnull thread, absl::Nullable attr, void *(*start_routine)(void *), absl::Nullable arg) { - if (!tls.started) [[unlikely]] { + if (ABSL_PREDICT_FALSE(!tls.started)) { return REAL(pthread_create)(thread, attr, start_routine, arg); } // Wrap the arguments. Will be deleted in MyThreadStart. diff --git a/common/BUILD b/common/BUILD index e2d55d3f..56f57c49 100644 --- a/common/BUILD +++ b/common/BUILD @@ -62,6 +62,7 @@ cc_library( ":logging", ":remote_file", ":status_macros", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index c7eed491..07b70297 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -51,6 +51,7 @@ fuzztest_cc_library( fuzztest::remote_file fuzztest::status_macros absl::check + absl::core_headers absl::log absl::nullability absl::status diff --git a/common/blob_file.cc b/common/blob_file.cc index 50110518..bdc8f2f3 100644 --- a/common/blob_file.cc +++ b/common/blob_file.cc @@ -26,6 +26,7 @@ #include #include "absl/base/nullability.h" +#include "absl/base/optimization.h" #include "absl/log/check.h" #include "absl/log/log.h" #include "absl/status/status.h" @@ -179,11 +180,11 @@ class DefaultBlobFileReader : public BlobFileReader { ASSIGN_OR_RETURN_IF_NOT_OK(std::unique_ptr new_reader, CreateRiegeliFileReader(path)); riegeli_reader_.Reset(std::move(new_reader)); - if (riegeli_reader_.CheckFileFormat()) [[likely]] { + if (ABSL_PREDICT_TRUE(riegeli_reader_.CheckFileFormat())) { // File could be opened and is in the Riegeli format. return absl::OkStatus(); } - if (!riegeli_reader_.src()->ok()) [[unlikely]] { + if (ABSL_PREDICT_FALSE(!riegeli_reader_.src()->ok())) { // File could not be opened. return riegeli_reader_.src()->status(); } @@ -210,8 +211,7 @@ class DefaultBlobFileReader : public BlobFileReader { else return absl::FailedPreconditionError("no reader open"); #else - if (legacy_reader_) [[unlikely]] - return legacy_reader_->Read(blob); + if (ABSL_PREDICT_FALSE(legacy_reader_)) return legacy_reader_->Read(blob); absl::string_view record; if (!riegeli_reader_.ReadRecord(record)) { @@ -230,8 +230,7 @@ class DefaultBlobFileReader : public BlobFileReader { legacy_reader_ = nullptr; return absl::OkStatus(); #else - // NOLINTNEXTLINE(readability/braces). Similar to b/278586863. - if (legacy_reader_) [[unlikely]] { + if (ABSL_PREDICT_FALSE(legacy_reader_)) { legacy_reader_ = nullptr; return absl::OkStatus(); }