Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Fix CMakeLists.txt #25

Closed
wants to merge 4 commits into from
Closed

Fix CMakeLists.txt #25

wants to merge 4 commits into from

Conversation

neurolabusc
Copy link

This patch restores the functionality of CMake. In order to compile, the compiler requires the -mpclmul and you must source all the files. If you can add this, we can add this to other projects, for example this fork of pigz allows the user to select System, CloudFlare, zlib-ng or (if you accept this pull request) Intel. You can test with this with:

mkdir build
cd build
cmake ..
make

@jtkukunas
Copy link
Contributor

Thanks for this.

It looks like this change would compile all of zlib w/ -mpclmul. That would let the compiler generate those instructions outside of the cpuid checks, and potentially break older platforms that don't support pclmul.

If you look at the current Makefile, you'll see I compile only specific files with these cflags to avoid that issue.

@neurolabusc
Copy link
Author

Yes, the make file is fine. This patch is for cmake.

I did the minimal modification to allow the project to compile. Without my modifications the CMake file will always fail (e.g. with default x86.h settings: deflate.c requires slide_hash_sse, inflate.c and deflate.c require crc_fold_512to32, etc.). So this pull request is very simple, and improves a CMakefile from non-functional to functional. So it seems like a simple decision to apply it.

In future, one could extend this to ensure that the compiler and computer that support clmul (e.g. more recent than 2010):

  1. Update the CMakelist to ensure it supports clmul: check_c_compiler_flag(-mpclmul HAS_PCLMUL). Note that the x86.h already includes the nice x86_cpu_has_pclmul() function to determine if the cpu supports this instruction. So you only need to check compiler support and have this toggle the USE_PCLMUL_CRC definition currently in x86.h.

@jtkukunas
Copy link
Contributor

No. The runtime cpuid check I'm talking about above (x86_cpu_has_pclmul()) only works correctly if the compiler doesn't generate pclmul instructions outside those checked areas. With your patch applied, zlib built with cmake could crash on chips that do not support pclmul.

The fix to your patch is to follow the approach in the Makefile, i.e., only crc_folding.c should be compiled w/ -mpclmul

@neurolabusc
Copy link
Author

Hmm, I guess your fear is that including the -mpclmul might allow the compiler to optimize other code to use clmul instructions even though they are not explicitly used. This has not been my experience. However, I concede it is possible that some future compiler might be clever enough to do this. How about changing one line in my patch from:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mpclmul")
to read:
set_source_files_properties(crc_folding.c PROPERTIES COMPILE_FLAGS -mpclmul)

That would make the CMake work similar to your makefile.

Without adding a -mpclmul compile flag to the CMake, the CMake will fail:
crc_folding.c:431:14: error: '__builtin_ia32_pclmulqdq128' needs target feature pclmul

vkrasnov pushed a commit to cloudflare/zlib that referenced this pull request Feb 26, 2020
* Replace GPL CRC with BSD CRC (zlib-ng/zlib-ng#42), for validation see https://github.com/neurolabusc/simd_crc

* Westmere detection

* Update configure for Westmere (InsightSoftwareConsortium/ITK#416)

* use cpu_has_pclmul() to autodetect CPU hardware (InsightSoftwareConsortium/ITK#416)

* remove gpl code

* Improve support for compiling using Windows (https://github.com/ningfei/zlib)

* Import ucm.cmake from https://github.com/ningfei/zlib

* crc32_simd as separate file (#18)

* atomic and SKIP_CPUID_CHECK (#18)

* Remove unused code

* Only allow compiler to use clmul instructions to crc_simd unit (intel/zlib#25)

* Atomic does not compile on Ubuntu 14.04

* Allow "cmake -DSKIP_CPUID_CHECK=ON .." to not check SIMD CRC support on execution.

* Restore Windows compilation using "nmake -f win32\Makefile.msc"

* zconf.h required for Windows nmake

* support crc intrinsics for ARM CPUs

* Requested changes (#19)
neurolabusc added a commit to neurolabusc/pigz that referenced this pull request Feb 27, 2020
vkrasnov pushed a commit to cloudflare/zlib that referenced this pull request Dec 2, 2020
* Fix architecture macro on MSVC.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Update '.gitignore'.

Also remove 'zconf.h', it will be generated during building.

* Add 'build' subdir to .gitignore

* Fix architecture macro on MSVC.

* Update CMakeLists.txt

Clean up.
Add option to set runtime (useful for MSVC).
Add 'SSE4.2' and 'AVX' option.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Define '_LARGEFILE64_SOURCE' by default in CMakeLists.txt.

Also remove checking fseeko.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Set 'CMAKE_MACOSX_RPATH' to TRUE.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Update SSE4.2 and PCLMUL setting in CMakeLists.txt

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Add 'HAVE_HIDDEN' to CMakeLists.txt

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Fix building dll on MSVC.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Fix zlib.pc file generation.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Refine cmake settings.

* Change cmake function to lowercase.

* Fix zlib.pc

* Fix crc32_pclmul_le_16 type.

* Set POSITION_INDEPENDENT_CODE flag to ON by default.

* Replace GPL CRC with BSD CRC (zlib-ng/zlib-ng#42), for validation see https://github.com/neurolabusc/simd_crc

* Westmere detection

* Update configure for Westmere (InsightSoftwareConsortium/ITK#416)

* use cpu_has_pclmul() to autodetect CPU hardware (InsightSoftwareConsortium/ITK#416)

* remove gpl code

* Improve support for compiling using Windows (https://github.com/ningfei/zlib)

* Import ucm.cmake from https://github.com/ningfei/zlib

* crc32_simd as separate file (#18)

* atomic and SKIP_CPUID_CHECK (#18)

* Suppress some MSVC warnings.

* Remove unused code

* Fix ucm_set_runtime when only C is enabled for the project.

* Removed configured header file.

* Unify zconf.h template.

* Only allow compiler to use clmul instructions to crc_simd unit (intel/zlib#25)

* Atomic does not compile on Ubuntu 14.04

* Allow "cmake -DSKIP_CPUID_CHECK=ON .." to not check SIMD CRC support on execution.

* Restore Windows compilation using "nmake -f win32\Makefile.msc"

* Do not set SOVERSION on Cygwin.

* Fix file permission.

* Refine PCLMUL CMake option.

* zconf.h required for Windows nmake

* Do not set visibility flag on Cygwin.

* Fix compiling dll resource.

* Fix compiling using MinGW.

SSE4.2 and PCLMUL are also supported.

* Fix zconf.h for Windows.

* support crc intrinsics for ARM CPUs

* Add gzip -k option to minigzip (to aid benchmarks)

* Support Intel and ARMv8 optimization in CMake.

* Clean up.

* Fix compiling on Apple M1.

check_c_compiler_flag detects SSE2, SSE3, SSE42 and PCLMUL but compiling
fails. Workaround fix, need further investigation.

* Restore MSVC warnings.

Co-authored-by: neurolabusc <rorden@sc.edu>
Co-authored-by: Chris Rorden <rorden@mailbox.sc.edu>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants