Skip to content

Commit

Permalink
Revert: Random: Use target attribute instead of -march
Browse files Browse the repository at this point in the history
If the user overrides -march e.g. via BAZEL_CXXOPTS it may conflict with
the -march override for Randen. So instead use a target attribute which
works on clang and gcc.

PiperOrigin-RevId: 728408888
Change-Id: Ie870600a80fb25f95b53f5e87bb9cc7ade53d69d
  • Loading branch information
Abseil Team authored and copybara-github committed Feb 19, 2025
1 parent 3ae6324 commit 33d9ce7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
29 changes: 8 additions & 21 deletions absl/random/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,16 @@ licenses(["notice"])
selects.config_setting_group(
name = "gcc_compatible",
match_any = [
# For some reason compiler:clang does not match when targeting
# Android/CrOS but since we only support building with Clang on these we
# assume the compiler is Clang if the target OS is one of these.
"@platforms//os:android",
"@platforms//os:chromiumos",
"@rules_cc//cc/compiler:clang",
"@rules_cc//cc/compiler:gcc",
],
)

selects.config_setting_group(
name = "aarch32",
match_any = [
"@platforms//cpu:aarch32",
"@platforms//cpu:armv7",
],
)

selects.config_setting_group(
name = "gcc_compatible-aarch32",
match_all = [
":aarch32",
":gcc_compatible",
"@platforms//cpu:aarch32",
],
)

Expand Down Expand Up @@ -106,15 +93,15 @@ selects.config_setting_group(
# Some libraries are compiled with options to generate AES-NI
# instructions, and runtime dispatch is used to determine if the host
# microarchitecture supports AES-NI or if a portable fallback library
# should be called. Most architectures use target attributes to enable
# the AES intrinsics so no compile flags are needed, but 32-bit ARM and
# PowerPC have their intrinsics guarded by #ifdef so we need to pass flags.
# should be called.
ABSL_RANDOM_RANDEN_COPTS = select({
":gcc_compatible-aarch32": [
"-mcpu=cortex-a53",
"-mfpu=crypto-neon-fp-armv8",
],
":gcc_compatible-aarch32": ["-mfpu=neon"],
":gcc_compatible-aarch64": ["-march=armv8-a+crypto"],
":gcc_compatible-ppc_crypto": ["-mcrypto"],
":gcc_compatible-x86_64": [
"-maes",
"-msse4.1",
],
"//conditions:default": [],
})

Expand Down
19 changes: 7 additions & 12 deletions absl/random/internal/randen_hwaes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@
// ABSL_RANDEN_HWAES_IMPL indicates whether this file will contain
// a hardware accelerated implementation of randen, or whether it
// will contain stubs that exit the process.
#if ABSL_HAVE_ACCELERATED_AES
// The following platforms have implemented RandenHwAes.
#if defined(ABSL_ARCH_X86_64) || defined(ABSL_ARCH_X86_32) || \
defined(ABSL_ARCH_PPC) || defined(ABSL_ARCH_ARM) || \
defined(ABSL_ARCH_AARCH64)
#define ABSL_RANDEN_HWAES_IMPL 1
#endif
#endif

#if !defined(ABSL_RANDEN_HWAES_IMPL)
// No accelerated implementation is supported.
// The RandenHwAes functions are stubs that print an error and exit.

#if ABSL_HAVE_ACCELERATED_AES
#error "Need to update the definition of ABSL_RANDEN_HWAES_IMPL"
#endif

#include <cstdio>
#include <cstdlib>

Expand Down Expand Up @@ -96,15 +95,12 @@ using absl::random_internal::RandenTraits;

// TARGET_CRYPTO defines a crypto attribute for each architecture.
//
// We generally prefer to define these unless the architecture forces us to pass
// a compile flag to get the intrinsics. This is because architecture compile
// flags can affect the whole program (see HERMETIC NOTE at the top of
// randen_hwaes.h).
// NOTE: Evaluate whether we should eliminate ABSL_TARGET_CRYPTO.
#if (defined(__clang__) || defined(__GNUC__))
#if defined(ABSL_ARCH_X86_64) || defined(ABSL_ARCH_X86_32)
#define ABSL_TARGET_CRYPTO __attribute__((target("aes")))
#elif defined(ABSL_ARCH_AARCH64)
#define ABSL_TARGET_CRYPTO __attribute__((target("+aes")))
#elif defined(ABSL_ARCH_PPC)
#define ABSL_TARGET_CRYPTO __attribute__((target("crypto")))
#else
#define ABSL_TARGET_CRYPTO
#endif
Expand Down Expand Up @@ -215,8 +211,7 @@ inline ABSL_TARGET_CRYPTO void SwapEndian(void*) {}

#elif defined(ABSL_ARCH_X86_64) || defined(ABSL_ARCH_X86_32)
// On x86 we rely on the aesni instructions
#include <emmintrin.h>
#include <wmmintrin.h>
#include <immintrin.h>

namespace {

Expand Down

0 comments on commit 33d9ce7

Please sign in to comment.