Commit 9d8f9764 authored by Hans Wennborg's avatar Hans Wennborg Committed by Commit Bot

Revert "Unify CPU features detection code"

This reverts commit 6f7e5e79.

Reason for revert: This broke the build, e.g.
https://ci.chromium.org/p/chromium/builders/ci/ios-device/144512

../../third_party/zlib/cpu_features.c:75:13: error: unused function '_cpu_check_features' [-Werror,-Wunused-function]
static void _cpu_check_features(void)
            ^

Original change's description:
> Unify CPU features detection code
> 
> This will allow to remove some duplicated code (i.e. thread synchronization)
> while at same time removing unnecessary use of inline ASM for Intel features
> detection.
> 
> A few other advantages:
>  - remove some extra logic (e.g. no need to test the platform to include the
>    correct CPU detection header).
>  - simplifies the buildsystem (i.e. we always include cpu_features.c)
>  - get rid of the simd_stub file.
> 
> Bug: 1032721
> Change-Id: Ic93472d3337bc2cbe092d4cf8fbe4b31b1ceca6d
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1976820
> Reviewed-by: Chris Blume <cblume@chromium.org>
> Reviewed-by: Adenilson Cavalcanti <cavalcantii@chromium.org>
> Commit-Queue: Adenilson Cavalcanti <cavalcantii@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#727038}

TBR=cavalcantii@chromium.org,cblume@chromium.org,mtklein@chromium.org,adenilson.cavalcanti@arm.com

Change-Id: I20c5dedb98ba8b5d304ff1339042bcf243505e88
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 1032721
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1979790Reviewed-by: default avatarHans Wennborg <hans@chromium.org>
Commit-Queue: Hans Wennborg <hans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#727039}
parent 6f7e5e79
......@@ -26,20 +26,9 @@ if (current_cpu == "arm" || current_cpu == "arm64") {
use_x86_x64_optimizations =
(current_cpu == "x86" || current_cpu == "x64") && !is_ios
if (!use_x86_x64_optimizations && !use_arm_neon_optimizations) {
# Apparently android_cronet bot builds with NEON disabled and
# we also should disable optimizations for iOS@x86 (a.k.a. simulator).
defines = [ "CPU_NO_SIMD" ]
}
config("zlib_adler32_simd_config") {
if (use_x86_x64_optimizations) {
defines = [ "ADLER32_SIMD_SSSE3" ]
if (is_win) {
defines += [ "X86_WINDOWS" ]
} else {
defines += [ "X86_NOT_WINDOWS" ]
}
}
if (use_arm_neon_optimizations) {
......@@ -105,13 +94,24 @@ if (use_arm_neon_optimizations) {
if (!is_ios) {
include_dirs = [ "." ]
if (!is_win && !is_clang) {
if (is_android) {
import("//build/config/android/config.gni")
if (defined(android_ndk_root) && android_ndk_root != "") {
deps = [
"//third_party/android_ndk:cpu_features",
]
} else {
assert(false, "CPU detection requires the Android NDK")
}
} else if (!is_win && !is_clang) {
assert(!use_thin_lto,
"ThinLTO fails mixing different module-level targets")
cflags_c = [ "-march=armv8-a+crc" ]
}
sources = [
"arm_features.c",
"arm_features.h",
"crc32_simd.c",
"crc32_simd.h",
]
......@@ -218,6 +218,10 @@ source_set("zlib_x86_simd") {
"-mpclmul",
]
}
} else {
sources = [
"simd_stub.c",
]
}
configs -= [ "//build/config/compiler:chromium_code" ]
......@@ -244,8 +248,6 @@ component("zlib") {
"chromeconf.h",
"compress.c",
"contrib/optimizations/insert_string.h",
"cpu_features.c",
"cpu_features.h",
"crc32.c",
"crc32.h",
"deflate.c",
......@@ -265,6 +267,7 @@ component("zlib") {
"trees.c",
"trees.h",
"uncompr.c",
"x86.h",
"zconf.h",
"zlib.h",
"zutil.c",
......@@ -281,6 +284,7 @@ component("zlib") {
]
if (use_x86_x64_optimizations) {
sources += [ "x86.c" ]
deps += [ ":zlib_crc32_simd" ]
} else if (use_arm_neon_optimizations) {
sources += [ "contrib/optimizations/slide_hash_neon.h" ]
......@@ -290,15 +294,6 @@ component("zlib") {
sources += [ "inflate.c" ]
}
if (is_android) {
import("//build/config/android/config.gni")
if (defined(android_ndk_root) && android_ndk_root != "") {
deps += [ "//third_party/android_ndk:cpu_features" ]
} else {
assert(false, "CPU detection requires the Android NDK")
}
}
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [
":zlib_internal_config",
......@@ -324,7 +319,6 @@ config("minizip_warnings") {
}
static_library("minizip") {
defines = []
sources = [
"contrib/minizip/ioapi.c",
"contrib/minizip/ioapi.h",
......@@ -346,7 +340,7 @@ static_library("minizip") {
if (is_mac || is_ios || is_android || is_nacl) {
# Mac, Android and the BSDs don't have fopen64, ftello64, or fseeko64. We
# use fopen, ftell, and fseek instead on these systems.
defines += [ "USE_FILE32API" ]
defines = [ "USE_FILE32API" ]
}
deps = [
......
......@@ -59,8 +59,10 @@ local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
# define MOD63(a) a %= BASE
#endif
#include "cpu_features.h"
#if defined(ADLER32_SIMD_SSSE3) || defined(ADLER32_SIMD_NEON)
#if defined(ADLER32_SIMD_SSSE3)
#include "adler32_simd.h"
#include "x86.h"
#elif defined(ADLER32_SIMD_NEON)
#include "adler32_simd.h"
#endif
......@@ -106,7 +108,7 @@ uLong ZEXPORT adler32_z(adler, buf, len)
*/
if (buf == Z_NULL) {
if (!len) /* Assume user is calling adler32(0, NULL, 0); */
cpu_check_features();
x86_check_features();
return 1L;
}
#else
......
/* cpu_features.c -- Processor features detection.
/* arm_features.c -- ARM processor features detection.
*
* Copyright 2018 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the Chromium source repository LICENSE file.
*/
#include "cpu_features.h"
#include "arm_features.h"
#include "zutil.h"
#include <stdint.h>
#if defined(_MSC_VER)
#include <intrin.h>
#elif defined(ADLER32_SIMD_SSSE3)
#include <cpuid.h>
#endif
/* TODO(cavalcantii): remove checks for x86_flags on deflate.
*/
int ZLIB_INTERNAL arm_cpu_enable_crc32 = 0;
int ZLIB_INTERNAL arm_cpu_enable_pmull = 0;
int ZLIB_INTERNAL x86_cpu_enable_ssse3 = 0;
int ZLIB_INTERNAL x86_cpu_enable_simd = 0;
#if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || defined(ARMV8_OS_FUCHSIA)
#include <pthread.h>
......@@ -35,44 +25,39 @@ int ZLIB_INTERNAL x86_cpu_enable_simd = 0;
#include <zircon/features.h>
#include <zircon/syscalls.h>
#include <zircon/types.h>
#elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS)
#elif defined(ARMV8_OS_WINDOWS)
#include <windows.h>
#elif !defined(_MSC_VER)
#include <pthread.h>
#else
#error cpu_features.c CPU feature detection in not defined for your platform
#error arm_features.c ARM feature detection in not defined for your platform
#endif
#ifndef CPU_NO_SIMD
static void _cpu_check_features(void);
#endif
static void _arm_check_features(void);
#if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || defined(ARMV8_OS_FUCHSIA) || defined(X86_NOT_WINDOWS)
#if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || defined(ARMV8_OS_FUCHSIA)
static pthread_once_t cpu_check_inited_once = PTHREAD_ONCE_INIT;
void ZLIB_INTERNAL cpu_check_features(void)
void ZLIB_INTERNAL arm_check_features(void)
{
pthread_once(&cpu_check_inited_once, _cpu_check_features);
pthread_once(&cpu_check_inited_once, _arm_check_features);
}
#elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS)
#elif defined(ARMV8_OS_WINDOWS)
static INIT_ONCE cpu_check_inited_once = INIT_ONCE_STATIC_INIT;
static BOOL CALLBACK _cpu_check_features_forwarder(PINIT_ONCE once, PVOID param, PVOID* context)
static BOOL CALLBACK _arm_check_features_forwarder(PINIT_ONCE once, PVOID param, PVOID* context)
{
_cpu_check_features();
_arm_check_features();
return TRUE;
}
void ZLIB_INTERNAL cpu_check_features(void)
void ZLIB_INTERNAL arm_check_features(void)
{
InitOnceExecuteOnce(&cpu_check_inited_once, _cpu_check_features_forwarder,
InitOnceExecuteOnce(&cpu_check_inited_once, _arm_check_features_forwarder,
NULL, NULL);
}
#endif
#if (defined(__ARM_NEON__) || defined(__ARM_NEON))
/*
* See http://bit.ly/2CcoEsr for run-time detection of ARM features and also
* crbug.com/931275 for android_getCpuFeatures() use in the Android sandbox.
*/
static void _cpu_check_features(void)
static void _arm_check_features(void)
{
#if defined(ARMV8_OS_ANDROID) && defined(__aarch64__)
uint64_t features = android_getCpuFeatures();
......@@ -103,32 +88,3 @@ static void _cpu_check_features(void)
arm_cpu_enable_pmull = IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE);
#endif
}
#elif defined(X86_NOT_WINDOWS) || defined(X86_WINDOWS)
/* On x86 we simply use a instruction to check the CPU features.
* (i.e. CPUID).
*/
static void _cpu_check_features(void)
{
int x86_cpu_has_sse2;
int x86_cpu_has_ssse3;
int x86_cpu_has_sse42;
int x86_cpu_has_pclmulqdq;
int abcd[4];
#ifdef _MSC_VER
__cpuid(abcd, 1);
#else
__cpuid(1, abcd[0], abcd[1], abcd[2], abcd[3]);
#endif
x86_cpu_has_sse2 = abcd[3] & 0x4000000;
x86_cpu_has_ssse3 = abcd[2] & 0x000200;
x86_cpu_has_sse42 = abcd[2] & 0x100000;
x86_cpu_has_pclmulqdq = abcd[2] & 0x2;
x86_cpu_enable_ssse3 = x86_cpu_has_ssse3;
x86_cpu_enable_simd = x86_cpu_has_sse2 &&
x86_cpu_has_sse42 &&
x86_cpu_has_pclmulqdq;
}
#endif
/* cpu_features.h -- Processor features detection.
/* arm_features.h -- ARM processor features detection.
*
* Copyright 2018 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
......@@ -7,11 +7,7 @@
#include "zlib.h"
/* TODO(cavalcantii): remove checks for x86_flags on deflate.
*/
extern int arm_cpu_enable_crc32;
extern int arm_cpu_enable_pmull;
extern int x86_cpu_enable_ssse3;
extern int x86_cpu_enable_simd;
void cpu_check_features(void);
void arm_check_features(void);
......@@ -10,7 +10,6 @@
#define INLINE inline
#endif
#include "cpu_features.h"
/* Optimized insert_string block */
#if defined(CRC32_SIMD_SSE42_PCLMUL) || defined(CRC32_ARMV8_CRC32)
#define TARGET_CPU_WITH_CRC
......@@ -26,6 +25,7 @@
#define _cpu_crc32_u32 _mm_crc32_u32
#elif defined(CRC32_ARMV8_CRC32)
#include "arm_features.h"
#if defined(__clang__)
#undef TARGET_CPU_WITH_CRC
#define __crc32cw __builtin_arm_crc32cw
......
......@@ -29,10 +29,13 @@
#endif /* MAKECRCH */
#include "deflate.h"
#include "cpu_features.h"
#include "x86.h"
#include "zutil.h" /* for STDC and FAR definitions */
#if defined(CRC32_SIMD_SSE42_PCLMUL) || defined(CRC32_ARMV8_CRC32)
#if defined(CRC32_SIMD_SSE42_PCLMUL)
#include "crc32_simd.h"
#elif defined(CRC32_ARMV8_CRC32)
#include "arm_features.h"
#include "crc32_simd.h"
#endif
......@@ -223,7 +226,7 @@ unsigned long ZEXPORT crc32_z(crc, buf, len)
*/
if (buf == Z_NULL) {
if (!len) /* Assume user is calling crc32(0, NULL, 0); */
cpu_check_features();
x86_check_features();
return 0UL;
}
......@@ -286,7 +289,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
*/
if (buf == Z_NULL) {
if (!len) /* Assume user is calling crc32(0, NULL, 0); */
cpu_check_features();
arm_check_features();
return 0UL;
}
......@@ -497,31 +500,25 @@ uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
ZLIB_INTERNAL void crc_reset(deflate_state *const s)
{
#ifdef ADLER32_SIMD_SSSE3
if (x86_cpu_enable_simd) {
crc_fold_init(s);
return;
}
#endif
s->strm->adler = crc32(0L, Z_NULL, 0);
}
ZLIB_INTERNAL void crc_finalize(deflate_state *const s)
{
#ifdef ADLER32_SIMD_SSSE3
if (x86_cpu_enable_simd)
s->strm->adler = crc_fold_512to32(s);
#endif
}
ZLIB_INTERNAL void copy_with_crc(z_streamp strm, Bytef *dst, long size)
{
#ifdef ADLER32_SIMD_SSSE3
if (x86_cpu_enable_simd) {
crc_fold_copy(strm->state, dst, strm->next_in, size);
return;
}
#endif
zmemcpy(dst, strm->next_in, size);
strm->adler = crc32(strm->adler, dst, size);
}
......@@ -50,7 +50,7 @@
/* @(#) $Id$ */
#include <assert.h>
#include "deflate.h"
#include "cpu_features.h"
#include "x86.h"
#include "contrib/optimizations/insert_string.h"
#if (defined(__ARM_NEON__) || defined(__ARM_NEON))
......@@ -244,8 +244,10 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
// for all wrapper formats (e.g. RAW, ZLIB, GZIP).
// Feature detection is not triggered while using RAW mode (i.e. we never
// call crc32() with a NULL buffer).
#if defined(CRC32_ARMV8_CRC32) || defined(CRC32_SIMD_SSE42_PCLMUL)
cpu_check_features();
#if defined(CRC32_ARMV8_CRC32)
arm_check_features();
#elif defined(CRC32_SIMD_SSE42_PCLMUL)
x86_check_features();
#endif
if (version == Z_NULL || version[0] != my_version[0] ||
......@@ -1517,12 +1519,11 @@ local void fill_window_c(deflate_state *s);
local void fill_window(deflate_state *s)
{
#ifdef ADLER32_SIMD_SSSE3
if (x86_cpu_enable_simd) {
fill_window_sse(s);
return;
}
#endif
fill_window_c(s);
}
......
/* simd_stub.c -- stub implementations
* Copyright (C) 2014 Intel Corporation
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include <assert.h>
#include "deflate.h"
#include "x86.h"
int ZLIB_INTERNAL x86_cpu_enable_simd = 0;
void ZLIB_INTERNAL crc_fold_init(deflate_state *const s) {
assert(0);
}
void ZLIB_INTERNAL crc_fold_copy(deflate_state *const s,
unsigned char *dst,
const unsigned char *src,
long len) {
assert(0);
}
unsigned ZLIB_INTERNAL crc_fold_512to32(deflate_state *const s) {
assert(0);
return 0;
}
void ZLIB_INTERNAL fill_window_sse(deflate_state *s)
{
assert(0);
}
void x86_check_features(void)
{
}
/*
* x86 feature check
*
* Copyright (C) 2013 Intel Corporation. All rights reserved.
* Author:
* Jim Kukunas
*
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "x86.h"
#include "zutil.h"
int ZLIB_INTERNAL x86_cpu_enable_ssse3 = 0;
int ZLIB_INTERNAL x86_cpu_enable_simd = 0;
#ifndef _MSC_VER
#include <pthread.h>
pthread_once_t cpu_check_inited_once = PTHREAD_ONCE_INIT;
static void _x86_check_features(void);
void x86_check_features(void)
{
pthread_once(&cpu_check_inited_once, _x86_check_features);
}
static void _x86_check_features(void)
{
int x86_cpu_has_sse2;
int x86_cpu_has_ssse3;
int x86_cpu_has_sse42;
int x86_cpu_has_pclmulqdq;
unsigned eax, ebx, ecx, edx;
eax = 1;
#ifdef __i386__
__asm__ __volatile__ (
"xchg %%ebx, %1\n\t"
"cpuid\n\t"
"xchg %1, %%ebx\n\t"
: "+a" (eax), "=S" (ebx), "=c" (ecx), "=d" (edx)
);
#else
__asm__ __volatile__ (
"cpuid\n\t"
: "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
);
#endif /* (__i386__) */
x86_cpu_has_sse2 = edx & 0x4000000;
x86_cpu_has_ssse3 = ecx & 0x000200;
x86_cpu_has_sse42 = ecx & 0x100000;
x86_cpu_has_pclmulqdq = ecx & 0x2;
x86_cpu_enable_ssse3 = x86_cpu_has_ssse3;
x86_cpu_enable_simd = x86_cpu_has_sse2 &&
x86_cpu_has_sse42 &&
x86_cpu_has_pclmulqdq;
}
#else
#include <intrin.h>
#include <windows.h>
static BOOL CALLBACK _x86_check_features(PINIT_ONCE once,
PVOID param,
PVOID *context);
static INIT_ONCE cpu_check_inited_once = INIT_ONCE_STATIC_INIT;
void x86_check_features(void)
{
InitOnceExecuteOnce(&cpu_check_inited_once, _x86_check_features,
NULL, NULL);
}
static BOOL CALLBACK _x86_check_features(PINIT_ONCE once,
PVOID param,
PVOID *context)
{
int x86_cpu_has_sse2;
int x86_cpu_has_ssse3;
int x86_cpu_has_sse42;
int x86_cpu_has_pclmulqdq;
int regs[4];
__cpuid(regs, 1);
x86_cpu_has_sse2 = regs[3] & 0x4000000;
x86_cpu_has_ssse3 = regs[2] & 0x000200;
x86_cpu_has_sse42 = regs[2] & 0x100000;
x86_cpu_has_pclmulqdq = regs[2] & 0x2;
x86_cpu_enable_ssse3 = x86_cpu_has_ssse3;
x86_cpu_enable_simd = x86_cpu_has_sse2 &&
x86_cpu_has_sse42 &&
x86_cpu_has_pclmulqdq;
return TRUE;
}
#endif /* _MSC_VER */
/* x86.h -- check for x86 CPU features
* Copyright (C) 2013 Intel Corporation Jim Kukunas
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef X86_H
#define X86_H
#include "zlib.h"
extern int x86_cpu_enable_ssse3;
extern int x86_cpu_enable_simd;
void x86_check_features(void);
#endif /* X86_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment