Skip to content

Commit

Permalink
Use Bazel Platforms to support AES-NI compile options for Randen
Browse files Browse the repository at this point in the history
Bazel has deprecated selecting directly on the --cpu. This change
uses platforms to select the appropriate compile options.

Fixes #1573
Fixes #1797

PiperOrigin-RevId: 712638567
Change-Id: Id16e478fe4ff1d27992b263d51c822cce0f7a98c
  • Loading branch information
derekmauro authored and copybara-github committed Jan 6, 2025
1 parent 506f107 commit 4e09561
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 58 deletions.
43 changes: 0 additions & 43 deletions absl/copts/configure_copts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ load(
"ABSL_MSVC_FLAGS",
"ABSL_MSVC_LINKOPTS",
"ABSL_MSVC_TEST_FLAGS",
"ABSL_RANDOM_HWAES_ARM32_FLAGS",
"ABSL_RANDOM_HWAES_ARM64_FLAGS",
"ABSL_RANDOM_HWAES_MSVC_X64_FLAGS",
"ABSL_RANDOM_HWAES_X64_FLAGS",
)

ABSL_DEFAULT_COPTS = select({
Expand All @@ -41,42 +37,3 @@ ABSL_DEFAULT_LINKOPTS = select({
"//absl:msvc_compiler": ABSL_MSVC_LINKOPTS,
"//conditions:default": [],
})

# ABSL_RANDOM_RANDEN_COPTS blaze copts flags which are required by each
# environment to build an accelerated RandenHwAes library.
ABSL_RANDOM_RANDEN_COPTS = select({
# APPLE
":cpu_darwin_x86_64": ABSL_RANDOM_HWAES_X64_FLAGS,
":cpu_darwin": ABSL_RANDOM_HWAES_X64_FLAGS,
":cpu_x64_windows_msvc": ABSL_RANDOM_HWAES_MSVC_X64_FLAGS,
":cpu_x64_windows": ABSL_RANDOM_HWAES_MSVC_X64_FLAGS,
":cpu_k8": ABSL_RANDOM_HWAES_X64_FLAGS,
":cpu_ppc": ["-mcrypto"],
":cpu_aarch64": ABSL_RANDOM_HWAES_ARM64_FLAGS,

# Supported by default or unsupported.
"//conditions:default": [],
})

# absl_random_randen_copts_init:
# Initialize the config targets based on cpu, os, etc. used to select
# the required values for ABSL_RANDOM_RANDEN_COPTS
def absl_random_randen_copts_init():
"""Initialize the config_settings used by ABSL_RANDOM_RANDEN_COPTS."""

# CPU configs.
# These configs have consistent flags to enable HWAES intsructions.
cpu_configs = [
"ppc",
"k8",
"darwin_x86_64",
"darwin",
"x64_windows_msvc",
"x64_windows",
"aarch64",
]
for cpu in cpu_configs:
native.config_setting(
name = "cpu_%s" % cpu,
values = {"cpu": cpu},
)
11 changes: 0 additions & 11 deletions absl/copts/copts.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,4 @@ def GccStyleFilterAndCombine(default_flags, test_flags):
# Object file doesn't export any previously undefined symbols
"-ignore:4221",
],
# "HWAES" is an abbreviation for "hardware AES" (AES - Advanced Encryption
# Standard). These flags are used for detecting whether or not the target
# architecture has hardware support for AES instructions which can be used
# to improve performance of some random bit generators.
"ABSL_RANDOM_HWAES_ARM64_FLAGS": ["-march=armv8-a+crypto"],
"ABSL_RANDOM_HWAES_ARM32_FLAGS": ["-mfpu=neon"],
"ABSL_RANDOM_HWAES_X64_FLAGS": [
"-maes",
"-msse4.1",
],
"ABSL_RANDOM_HWAES_MSVC_X64_FLAGS": [],
}
72 changes: 68 additions & 4 deletions absl/random/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
# limitations under the License.
#

load("@bazel_skylib//lib:selects.bzl", "selects")

# Internal-only implementation classes for Abseil Random
load(
"//absl:copts/configure_copts.bzl",
"ABSL_DEFAULT_COPTS",
"ABSL_DEFAULT_LINKOPTS",
"ABSL_RANDOM_RANDEN_COPTS",
"ABSL_TEST_COPTS",
"absl_random_randen_copts_init",
)

default_package_visibility = [
Expand All @@ -39,6 +39,72 @@ package(

licenses(["notice"])

# Used to select on compilers that support GCC-compatible options
# (e.g. "-maes").
selects.config_setting_group(
name = "gcc_compatible",
match_any = [
"@rules_cc//cc/compiler:clang",
"@rules_cc//cc/compiler:gcc",
],
)

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

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

selects.config_setting_group(
name = "ppc_crypto",
match_any = [
"@platforms//cpu:ppc",
"@platforms//cpu:ppc32",
"@platforms//cpu:ppc64le",
],
)

selects.config_setting_group(
name = "gcc_compatible-ppc_crypto",
match_all = [
":gcc_compatible",
":ppc_crypto",
],
)

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

# 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.
ABSL_RANDOM_RANDEN_COPTS = select({
":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": [],
})

cc_library(
name = "traits",
hdrs = ["traits.h"],
Expand Down Expand Up @@ -321,8 +387,6 @@ cc_library(
],
)

absl_random_randen_copts_init()

cc_library(
name = "randen_hwaes",
srcs = [
Expand Down

0 comments on commit 4e09561

Please sign in to comment.