Skip to content

Commit

Permalink
Overhauled compiler-time CPU-extension detection. (open-quantum-safe#713
Browse files Browse the repository at this point in the history
)

Refactored .CMake/alg_support.cmake, and kem/ and sig/ CMakeLists.txt files.

Refactored copy_from_pqclean/ templating.

Added custom x64 CPU extension detection code and removed cpu_features.

Removed duplicate Kyber source directories.
  • Loading branch information
xvzcf authored Apr 14, 2020
1 parent a00781c commit 722ca64
Show file tree
Hide file tree
Showing 295 changed files with 1,105 additions and 7,643 deletions.
246 changes: 20 additions & 226 deletions .CMake/alg_support.cmake

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions .CMake/compiler_flags.cmake → .CMake/compiler_opts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall)
add_compile_options(-Wextra)
add_compile_options(-Wpedantic)
add_compile_options(-Wno-unused-command-line-argument)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-g3)
add_compile_options(-fno-omit-frame-pointer)
Expand Down Expand Up @@ -33,8 +34,8 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
endif()
elseif(CMAKE_BUILD_TYPE STREQUAL "Optimized")
add_compile_options(-O3)
add_compile_options(-march=native)
add_compile_options(-fomit-frame-pointer)
include(${CMAKE_CURRENT_LIST_DIR}/gcc_clang_intrinsics.cmake)
else() #Build type = Generic/Dependency
add_compile_options(-O3)
add_compile_options(-fomit-frame-pointer)
Expand All @@ -59,7 +60,6 @@ elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-ggdb3)
elseif(CMAKE_BUILD_TYPE STREQUAL "Optimized")
add_compile_options(-O3)
add_compile_options(-march=native)
add_compile_options(-fomit-frame-pointer)
add_compile_options(-fdata-sections)
add_compile_options(-ffunction-sections)
Expand All @@ -68,6 +68,7 @@ elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
else ()
add_compile_options(-Wl,--gc-sections)
endif ()
include(${CMAKE_CURRENT_LIST_DIR}/gcc_clang_intrinsics.cmake)
else() #Build type = Generic/Dependency
add_compile_options(-O3)
add_compile_options(-fomit-frame-pointer)
Expand Down
18 changes: 0 additions & 18 deletions .CMake/cpu_extensions.cmake

This file was deleted.

44 changes: 44 additions & 0 deletions .CMake/detect_gcc_clang_intrinsics.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdio.h>

int main(void) {
#if defined(__AES__)
printf("AES;");
#endif
#if defined(__AVX__)
printf("AVX;");
#endif
#if defined(__AVX2__)
printf("AVX2;");
#endif
#if defined(__AVX512BW__)
printf("AVX512BW;");
#endif
#if defined(__AVX512DQ__)
printf("AVX512DQ;");
#endif
#if defined(__AVX512F__)
printf("AVX512F;");
#endif
#if defined(__BMI__)
printf("BMI;");
#endif
#if defined(__BMI2__)
printf("BMI2;");
#endif
#if defined(__FMA__)
printf("FMA;");
#endif
#if defined(__POPCNT__)
printf("POPCNT;");
#endif
#if defined(__SSE__)
printf("SSE;");
#endif
#if defined(__SSE2__)
printf("SSE2;");
#endif
#if defined(__SSE3__)
printf("SSE3;");
#endif
return 0;
}
15 changes: 15 additions & 0 deletions .CMake/gcc_clang_intrinsics.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
try_run(RUN_RESULT COMPILE_RESULT
"${CMAKE_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/.CMake/detect_gcc_clang_intrinsics.c"
COMPILE_DEFINITIONS -march=native
RUN_OUTPUT_VARIABLE RUN_OUTPUT)
if(NOT RUN_RESULT EQUAL 0)
message(FATAL_ERROR ".CMake/detect_gcc_clang_intrinsics.c returned exit code: " ${RUN_RESULT})
endif()
foreach(CPU_EXTENSION ${RUN_OUTPUT})
set(OQS_USE_${CPU_EXTENSION}_INSTRUCTIONS ON)
endforeach()
if(OQS_USE_AVX512BW_INSTRUCTIONS AND
OQS_USE_AVX512DQ_INSTRUCTIONS AND
OQS_USE_AVX512F_INSTRUCTIONS)
set(OQS_USE_AVX512_INSTRUCTIONS ON)
endif()
79 changes: 0 additions & 79 deletions .CMake/list_cpu_extensions.c

This file was deleted.

2 changes: 1 addition & 1 deletion .astylerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# find src tests -name '*.[ch]' | grep -v '/external/' | grep -v 'kem.*/pqclean_' | grep -v 'sig.*/pqclean_' | xargs astyle --options=.astylerc
# find src tests .CMake -name '*.[ch]' | grep -v '/external/' | grep -v 'kem.*/pqclean_' | grep -v 'sig.*/pqclean_' | xargs astyle --options=.astylerc
--style=google
--indent=tab
#--indent-preproc-define
Expand Down
14 changes: 7 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ localCheckout: &localCheckout
name: win/default
steps:
- checkout
- run:
- run:
name: Install ninja
command: $ProgressPreference="SilentlyContinue" ; Invoke-RestMethod -Uri https://github.com/ninja-build/ninja/releases/download/v1.10.0/ninja-win.zip -Method Get -OutFile ninja.zip; Expand-Archive ninja.zip
command: $ProgressPreference="SilentlyContinue" ; Invoke-RestMethod -Uri https://github.com/ninja-build/ninja/releases/download/v1.10.0/ninja-win.zip -Method Get -OutFile ninja.zip; Expand-Archive ninja.zip
shell: powershell.exe
- run:
name: Install dependencies
Expand Down Expand Up @@ -199,7 +199,7 @@ jobs:
win-static:
<<: *winjob
environment:
CONFIGURE_ARGS: -DOQS_USE_OPENSSL=OFF
CONFIGURE_ARGS: -DOQS_USE_OPENSSL=OFF
SKIP_TESTS: style
win-shared:
<<: *winjob
Expand All @@ -212,14 +212,14 @@ workflows:
build:
jobs:
- win-static:
filters:
filters:
branches:
only:
only:
- /mb-.*/
- win-shared:
filters:
filters:
branches:
only:
only:
- /mb-.*/
- centos-7-amd64
- centos-8-amd64
Expand Down
21 changes: 14 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
cmake_minimum_required (VERSION 3.5)
cmake_policy(SET CMP0063 NEW)
# option() honors normal variables.
# see: https://cmake.org/cmake/help/git-stage/policy/CMP0077.html
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()
# Honor symbol visibility properties for all target types.
# see: https://cmake.org/cmake/help/git-stage/policy/CMP0063.html
if(POLICY CMP0063)
cmake_policy(SET CMP0063 NEW)
endif()

project(liboqs C ASM)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand All @@ -11,8 +21,6 @@ set(OQS_COMPILE_BUILD_TARGET "${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_HOST_SYSTEM}")

if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
set(ARCH "x86_64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86|i386|i686")
set(ARCH "x86")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
set(ARCH "arm64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
Expand All @@ -30,11 +38,10 @@ endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()
include(.CMake/compiler_flags.cmake)
if(CMAKE_BUILD_TYPE STREQUAL "Optimized")
include(third_party/cpu_features/add_lib.cmake)
include(.CMake/cpu_extensions.cmake)
set(OQS_OPTIMIZED_BUILD ON)
endif()
include(.CMake/compiler_opts.cmake)

include(.CMake/alg_support.cmake)

Expand Down Expand Up @@ -136,7 +143,7 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Dependency")
if(NOT WIN32)
add_custom_target(
prettyprint
COMMAND find src tests -name '*.[ch]' | grep -v '/external/' | grep -v 'kem.*/pqclean_' | grep -v 'sig.*/pqclean_' | xargs astyle --options=.astylerc
COMMAND find src tests .CMake -name '*.[ch]' | grep -v '/external/' | grep -v 'kem.*/pqclean_' | grep -v 'sig.*/pqclean_' | xargs astyle --options=.astylerc
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
USES_TERMINAL)
endif()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@

{% for family in instructions['kems'] %}
option(OQS_ENABLE_KEM_{{ family['name']|upper }} "" ON)
{%- for scheme in family['schemes'] %}
{%- for scheme in family['schemes'] %}
cmake_dependent_option(OQS_ENABLE_KEM_{{ family['name'] }}_{{ scheme['scheme'] }} "" ON "OQS_ENABLE_KEM_{{ family['name']|upper }}" OFF)
{% for impl in scheme['metadata']['implementations'] -%}
{% if impl['supported_platforms'] -%}
{% for platform in impl['supported_platforms'] -%}
{% for os in platform['operating_systems'] -%}
if ({{ os }} STREQUAL CMAKE_SYSTEM_NAME)
if ({{ impl['cmake_options'] }})
set(OQS_ENABLE_KEM_{{ family['name'] }}_{{ scheme['scheme'] }}_{{ impl['name'] }} ON)
endif()
{%- for impl in scheme['metadata']['implementations'] if impl['name'] != family['default_implementation'] and impl['supported_platforms'] -%}
{%- for platform in impl['supported_platforms'] if platform['architecture'] == 'x86_64' %}
if(ARCH STREQUAL "x86_64" AND CMAKE_SYSTEM_NAME MATCHES "{{ platform['operating_systems']|join('|') }}" {%- if platform['required_flags'] %} AND {% for flag in platform['required_flags'] -%} OQS_USE_{{ flag|upper }}_INSTRUCTIONS {%- if not loop.last %} AND {% endif -%}{%- endfor -%}{%- endif -%})
cmake_dependent_option(OQS_ENABLE_KEM_{{ family['name'] }}_{{ scheme['scheme'] }}_{{ impl['name'] }} "" ON "OQS_ENABLE_KEM_{{ family['name'] }}_{{ scheme['scheme'] }}" OFF)
endif()
{%- endfor -%}
{%- endfor -%}
{%- endfor %}
{% endfor -%}
{% endfor -%}
{% else -%}
set(OQS_ENABLE_KEM_{{ family['name'] }}_{{ scheme['scheme'] }}_{{ impl['name'] }} ON)
{% endif -%}
{% endfor -%}
{% endfor %}
{% endfor -%}

{% for family in instructions['sigs'] %}
option(OQS_ENABLE_SIG_{{ family['name']|upper }} "" ON)
{%- for scheme in family['schemes'] %}
{%- for scheme in family['schemes'] %}
cmake_dependent_option(OQS_ENABLE_SIG_{{ family['name'] }}_{{ scheme['scheme'] }} "" ON "OQS_ENABLE_SIG_{{ family['name']|upper }}" OFF)
{% for impl in scheme['metadata']['implementations'] -%}
{% if impl['supported_platforms'] -%}
{% for platform in impl['supported_platforms'] -%}
{% for os in platform['operating_systems'] -%}
if ({{ os }} STREQUAL CMAKE_SYSTEM_NAME)
if ({{ impl['cmake_options'] }})
set(OQS_ENABLE_SIG_{{ family['name'] }}_{{ scheme['scheme'] }}_{{ impl['name'] }} ON)
endif()
{%- for impl in scheme['metadata']['implementations'] if impl['name'] != family['default_implementation'] and impl['supported_platforms'] -%}
{%- for platform in impl['supported_platforms'] if platform['architecture'] == 'x86_64' %}
if(ARCH STREQUAL "x86_64" AND CMAKE_SYSTEM_NAME MATCHES "{{ platform['operating_systems']|join('|') }}" {%- if platform['required_flags'] %} AND {% for flag in platform['required_flags'] -%} OQS_USE_{{ flag|upper }}_INSTRUCTIONS {%- if not loop.last %} AND {% endif -%}{%- endfor -%}{%- endif -%})
cmake_dependent_option(OQS_ENABLE_SIG_{{ family['name'] }}_{{ scheme['scheme'] }}_{{ impl['name'] }} "" ON "OQS_ENABLE_SIG_{{ family['name'] }}_{{ scheme['scheme'] }}" OFF)
endif()
{%- endfor -%}
{%- endfor -%}
{%- endfor %}
{% endfor -%}
{% endfor -%}
{% else -%}
set(OQS_ENABLE_SIG_{{ family['name'] }}_{{ scheme['scheme'] }}_{{ impl['name'] }} ON)
{% endif -%}
{% endfor -%}
{% endfor %}
{% endfor %}

Loading

0 comments on commit 722ca64

Please sign in to comment.