Page MenuHomePhabricator

[llvm] add zstd to `llvm::compression` namespace
ClosedPublic

Authored by ckissane on Jun 23 2022, 2:27 PM.

Details

Summary
  • add zstd to llvm::compression namespace
  • add a CMake option LLVM_ENABLE_ZSTD with behavior mirroring that of LLVM_ENABLE_ZLIB
  • add tests for zstd to llvm/unittests/Support/CompressionTest.cpp
  • debian users should install libzstd when using LLVM_ENABLE_ZSTD=FORCE_ON from source due to this bug https://bugs.launchpad.net/ubuntu/+source/libzstd/+bug/1941956

Diff Detail

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes
ckissane edited the summary of this revision. (Show Details)Jul 12 2022, 12:25 PM

As I mentioned, the proper approach is to add zstd functionality along with the CMake change, instead of adding CMake to all llvm-project components without a way to test them.

@MaskRay, I have now done this and ran the ldd tests as requested:

With LLVM_ENABLE_ZSTD=ON
$ ninja check-lld
Testing Time: 8.98s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1
With LLVM_ENABLE_ZSTD=OFF
$ ninja check-lld
Testing Time: 8.95s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1

As I mentioned, the proper approach is to add zstd functionality along with the CMake change, instead of adding CMake to all llvm-project components without a way to test them.

@MaskRay, I have now done this and ran the ldd tests as requested:

With LLVM_ENABLE_ZSTD=ON
$ ninja check-lld
Testing Time: 8.98s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1
With LLVM_ENABLE_ZSTD=OFF
$ ninja check-lld
Testing Time: 8.95s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1

I request that you abandon this patch and incorporate the llvm cmake part into the llvm patch which you actually use zstd.
It is not appropriate to add zstd cmake to llvm-project components which don't use zstd.

ckissane updated this revision to Diff 444078.Jul 12 2022, 2:04 PM
  • add zstd tests to CompressionTest.cpp

As I mentioned, the proper approach is to add zstd functionality along with the CMake change, instead of adding CMake to all llvm-project components without a way to test them.

@MaskRay, I have now done this and ran the ldd tests as requested:

With LLVM_ENABLE_ZSTD=ON
$ ninja check-lld
Testing Time: 8.98s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1
With LLVM_ENABLE_ZSTD=OFF
$ ninja check-lld
Testing Time: 8.95s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1

I request that you abandon this patch and incorporate the llvm cmake part into the llvm patch which you actually use zstd.
It is not appropriate to add zstd cmake to llvm-project components which don't use zstd.

Let me see if I understand this correctly:
Are you saying that I should abandon this patch, and create a new patch that:

  • adds findZSTD.cmake
  • adds zstd to compression namespace
  • adds tests to CompressionTest.cpp
  • and does the minimal amount of cmake work to have the flags and test work

Is this correct?

I realize though that then https://reviews.llvm.org/D129406 or similar would be "the [first] llvm patch which actually use[s] zstd"

As I mentioned, the proper approach is to add zstd functionality along with the CMake change, instead of adding CMake to all llvm-project components without a way to test them.

@MaskRay, I have now done this and ran the ldd tests as requested:

With LLVM_ENABLE_ZSTD=ON
$ ninja check-lld
Testing Time: 8.98s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1
With LLVM_ENABLE_ZSTD=OFF
$ ninja check-lld
Testing Time: 8.95s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1

I request that you abandon this patch and incorporate the llvm cmake part into the llvm patch which you actually use zstd.
It is not appropriate to add zstd cmake to llvm-project components which don't use zstd.

Let me see if I understand this correctly:
Are you saying that I should abandon this patch, and create a new patch that:

  • adds findZSTD.cmake
  • adds zstd to compression namespace
  • adds tests to CompressionTest.cpp
  • and does the minimal amount of cmake work to have the flags and test work

Is this correct?

I realize though that then https://reviews.llvm.org/D129406 or similar would be "the [first] llvm patch which actually use[s] zstd"

You can find a component in llvm which uses zstd (e.g. lib/Support). Add the functionality with tests (e.g. a CompressionTest.cpp unittest) along with the CMake/(optional Bazel, gn) changes.
Keep clang, clang-tools-extra, lld, etc unchanged. These components don't yet use zstd and the CMake changes in this patch will be untestable.

As I mentioned, the proper approach is to add zstd functionality along with the CMake change, instead of adding CMake to all llvm-project components without a way to test them.

@MaskRay, I have now done this and ran the ldd tests as requested:

With LLVM_ENABLE_ZSTD=ON
$ ninja check-lld
Testing Time: 8.98s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1
With LLVM_ENABLE_ZSTD=OFF
$ ninja check-lld
Testing Time: 8.95s
  Unsupported      :   17
  Passed           : 2638
  Expectedly Failed:    1

I request that you abandon this patch and incorporate the llvm cmake part into the llvm patch which you actually use zstd.
It is not appropriate to add zstd cmake to llvm-project components which don't use zstd.

Let me see if I understand this correctly:
Are you saying that I should abandon this patch, and create a new patch that:

  • adds findZSTD.cmake
  • adds zstd to compression namespace
  • adds tests to CompressionTest.cpp
  • and does the minimal amount of cmake work to have the flags and test work

Is this correct?

I realize though that then https://reviews.llvm.org/D129406 or similar would be "the [first] llvm patch which actually use[s] zstd"

You can find a component in llvm which uses zstd (e.g. lib/Support). Add the functionality with tests (e.g. a CompressionTest.cpp unittest) along with the CMake/(optional Bazel, gn) changes.
Keep clang, clang-tools-extra, lld, etc unchanged. These components don't yet use zstd and the CMake changes in this patch will be untestable.

Got it!, this patch already has such tests (CompressionTest.cpp unittest) in lib/Support, so I should just remove the untestable cmake config.

ckissane updated this revision to Diff 444374.Wed, Jul 13, 12:02 PM
  • Trim CMAKE modifications to minimum needed
ckissane marked an inline comment as done.Wed, Jul 13, 12:02 PM

The patch looks mostly good but please fix the subject and the summary.

Note: if you fix the local commit message, you can use arc diff --head=HEAD 'HEAD^' --verbatim to update the subject and the summary.

llvm/lib/Support/Compression.cpp
83–86
130

const size_t

llvm/unittests/Support/CompressionTest.cpp
65

delete blank line

66

static void testZstd...

69

delete blank line

95
ckissane updated this revision to Diff 444400.Wed, Jul 13, 1:07 PM

tidy some code

ckissane retitled this revision from [llvm] cmake config groundwork to have ZSTD in LLVM and add ZSTD compression namespace to [llvm] add zstd to `llvm::compression` namespace.Wed, Jul 13, 1:09 PM
ckissane edited the summary of this revision. (Show Details)
ckissane marked 6 inline comments as done.Wed, Jul 13, 1:13 PM

Mark handled comments from @MaskRay as done

phosek added inline comments.Wed, Jul 13, 2:29 PM
llvm/lib/Support/Compression.cpp
62–63

I'd make changes to zlib separately.

83–86

Ditto here.

ckissane added inline comments.Wed, Jul 13, 2:58 PM
llvm/lib/Support/Compression.cpp
62–63
ckissane updated this revision to Diff 444432.Wed, Jul 13, 3:13 PM
  • Merge branch main
MaskRay accepted this revision.Wed, Jul 13, 5:21 PM
MaskRay added inline comments.
llvm/include/llvm/Support/Compression.h
55

I changed the zlib functions to use uint8_t * and ArrayRef<uint8_t> instead. This patch needs to switch to uint8_t too.

llvm/lib/Support/Compression.cpp
154

uint8_t

This revision is now accepted and ready to land.Wed, Jul 13, 5:21 PM
ckissane updated this revision to Diff 444489.Wed, Jul 13, 7:04 PM
  • Merge remote-tracking branch 'origin/main' into ckissane.add-zstd
  • [Support] update zstd interface to use uint8_t * and ArrayRef<uint8_t>
This revision was landed with ongoing or failed builds.Wed, Jul 13, 7:06 PM
This revision was automatically updated to reflect the committed changes.

Why was this reverted? Please make extensive tests especially for something dealing with the build system. When reverting a commit, briefly describe what happened.

This breaks builds when LLVM is included with CMake’s add_subdirectory.
I think the zstd target needs to be marked as imported.
The following patch fixes the problem, although I’m not familiar enough with CMake to know if this is the right way to go:

diff --git a/llvm/cmake/modules/FindZSTD.cmake b/llvm/cmake/modules/FindZSTD.cmake
index 43ccf7232138..8e6ec6e8a988 100644
--- a/llvm/cmake/modules/FindZSTD.cmake
+++ b/llvm/cmake/modules/FindZSTD.cmake
@@ -12,6 +12,10 @@ find_package_handle_standard_args(
     ZSTD_LIBRARY ZSTD_INCLUDE_DIR)

 if(ZSTD_FOUND)
+    if(NOT TARGET Zstd::zstd)
+      add_library(Zstd::zstd UNKNOWN IMPORTED)
+      set_target_properties(Zstd::zstd PROPERTIES IMPORTED_LOCATION "${ZSTD_LIBRARY}")
+    endif()
     set(ZSTD_LIBRARIES ${ZSTD_LIBRARY})
     set(ZSTD_INCLUDE_DIRS ${ZSTD_INCLUDE_DIR})
 endif()
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 52b95c5377d3..031adfa33ba8 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -26,7 +26,7 @@ if(LLVM_ENABLE_ZLIB)
 endif()

 if(LLVM_ENABLE_ZSTD)
-  list(APPEND imported_libs zstd)
+  list(APPEND imported_libs Zstd::zstd)
 endif()

 if( MSVC OR MINGW )

I just reverted this in 6e6be5f9504d because it seems to have broken macOS builds:

llvm/lib/Support/Compression.cpp:24:10: fatal error: 'zstd.h' file not found
#include <zstd.h>
         ^~~~~~~~

I just reverted this in 6e6be5f9504d because it seems to have broken macOS builds:

llvm/lib/Support/Compression.cpp:24:10: fatal error: 'zstd.h' file not found
#include <zstd.h>
         ^~~~~~~~

@aemerson Could you provide the output of your cmake command?
(I can't easily reproduce because I don't have a mac on hand)

@aemerson can you let me know if https://reviews.llvm.org/D129786 works on MacOS?
I am continuing work on that revision because this one has a lot of outdated unrelated history.

I just reverted this in 6e6be5f9504d because it seems to have broken macOS builds:

llvm/lib/Support/Compression.cpp:24:10: fatal error: 'zstd.h' file not found
#include <zstd.h>
         ^~~~~~~~

@aemerson Could you provide the output of your cmake command?
(I can't easily reproduce because I don't have a mac on hand)

FWIW I hit this last night on my mac desktop - it looked like I had zstd installed by homebrew (as a dependency on something I installed), and cmake was able to find it in /opt/homebrew (somehow) but when Compression.cpp was built, nothing had added -I/opt/homebrew/include so the header wasn't found. I hacked the FindZSTD.cmake to not find it, to finish what I was working on before bedtime. This might not be related to what @aemerson saw though.

I just reverted this in 6e6be5f9504d because it seems to have broken macOS builds:

llvm/lib/Support/Compression.cpp:24:10: fatal error: 'zstd.h' file not found
#include <zstd.h>
         ^~~~~~~~

@aemerson Could you provide the output of your cmake command?
(I can't easily reproduce because I don't have a mac on hand)

FWIW I hit this last night on my mac desktop - it looked like I had zstd installed by homebrew (as a dependency on something I installed), and cmake was able to find it in /opt/homebrew (somehow) but when Compression.cpp was built, nothing had added -I/opt/homebrew/include so the header wasn't found. I hacked the FindZSTD.cmake to not find it, to finish what I was working on before bedtime. This might not be related to what @aemerson saw though.

@jasonmolenda Thank you for the info, I believe it is similar, could you try: https://reviews.llvm.org/D129786 for me?

I just reverted this in 6e6be5f9504d because it seems to have broken macOS builds:

llvm/lib/Support/Compression.cpp:24:10: fatal error: 'zstd.h' file not found
#include <zstd.h>
         ^~~~~~~~

@aemerson Could you provide the output of your cmake command?
(I can't easily reproduce because I don't have a mac on hand)

FWIW I hit this last night on my mac desktop - it looked like I had zstd installed by homebrew (as a dependency on something I installed), and cmake was able to find it in /opt/homebrew (somehow) but when Compression.cpp was built, nothing had added -I/opt/homebrew/include so the header wasn't found. I hacked the FindZSTD.cmake to not find it, to finish what I was working on before bedtime. This might not be related to what @aemerson saw though.

@jasonmolenda Thank you for the info, I believe it is similar, could you try: https://reviews.llvm.org/D129786 for me?

I applied the patch from https://reviews.llvm.org/D129786 on a clean checkout, cmake ../llvm -G Ninja -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_ASSERTIONS=1 -DLLVM_ENABLE_PROJECTS='llvm;lldb;clang' '-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi' -DLLDB_ENABLE_PYTHON=1 (my normal cmake line) and got

-- Found ZSTD: /opt/homebrew/lib/libzstd.dylib  
-- Looking for ZSTD_compress
-- Looking for ZSTD_compress - found

[...]

FAILED: lib/Support/CMakeFiles/LLVMSupport.dir/Compression.cpp.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DGTEST_HAS_RTTI=0 -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/Users/jason/k/llvm/llvm-project/build/lib/Support -I/Users/jason/k/llvm/llvm-project/llvm/lib/Support -I/Users/jason/k/llvm/llvm-project/build/include -I/Users/jason/k/llvm/llvm-project/llvm/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -fdiagnostics-color  -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -std=c++14  -fno-exceptions -fno-rtti -MD -MT lib/Support/CMakeFiles/LLVMSupport.dir/Compression.cpp.o -MF lib/Support/CMakeFiles/LLVMSupport.dir/Compression.cpp.o.d -o lib/Support/CMakeFiles/LLVMSupport.dir/Compression.cpp.o -c /Users/jason/k/llvm/llvm-project/llvm/lib/Support/Compression.cpp
/Users/jason/k/llvm/llvm-project/llvm/lib/Support/Compression.cpp:24:10: fatal error: 'zstd.h' file not found
#include <zstd.h>
         ^~~~~~~~
1 error generated.

% file /opt/homebrew/lib/libzstd.dylib 
/opt/homebrew/lib/libzstd.dylib: Mach-O 64-bit dynamically linked shared library arm64
% head -2 /opt/homebrew/include/zstd.h 
/*
 * Copyright (c) Yann Collet, Facebook, Inc.
%

% grep -i zstd CMakeCache.txt 
//Use zstd for compression/decompression if available. Can be ON,
LLVM_ENABLE_ZSTD:STRING=ON
ZSTD_INCLUDE_DIR:PATH=/opt/homebrew/include
ZSTD_LIBRARY:FILEPATH=/opt/homebrew/lib/libzstd.dylib
//Details about finding ZSTD
FIND_PACKAGE_MESSAGE_DETAILS_ZSTD:INTERNAL=[/opt/homebrew/lib/libzstd.dylib][/opt/homebrew/include][v()]
//Have symbol ZSTD_compress
HAVE_ZSTD:INTERNAL=1
//ADVANCED property for variable: ZSTD_INCLUDE_DIR
ZSTD_INCLUDE_DIR-ADVANCED:INTERNAL=1
//ADVANCED property for variable: ZSTD_LIBRARY
ZSTD_LIBRARY-ADVANCED:INTERNAL=1
MaskRay reopened this revision.Sun, Jul 17, 10:42 AM
This revision is now accepted and ready to land.Sun, Jul 17, 10:42 AM
ckissane retitled this revision from [llvm] add zstd to `llvm::compression` namespace to [OLD] [llvm] add zstd to `llvm::compression` namespace.Mon, Jul 18, 12:27 PM
ckissane edited the summary of this revision. (Show Details)
ckissane edited the summary of this revision. (Show Details)

Instead of marking a differential [OLD] (which may confuse readers whether this is to be abandoned), you can mark a differential Request Reviews/Request Changes. Then it will not be in the green state.

I clicked "Reopen" to make it clear the patch hasn't relanded.

Instead of marking a differential [OLD] (which may confuse readers whether this is to be abandoned), you can mark a differential Request Reviews/Request Changes. Then it will not be in the green state.

I clicked "Reopen" to make it clear the patch hasn't relanded.

Oh, sorry about that!
I had actually created https://reviews.llvm.org/D129786 instead of reopening this one because at the time I tried, reopening didn't seem to work.

MaskRay added a comment.EditedMon, Jul 18, 12:40 PM

Instead of marking a differential [OLD] (which may confuse readers whether this is to be abandoned), you can mark a differential Request Reviews/Request Changes. Then it will not be in the green state.

I clicked "Reopen" to make it clear the patch hasn't relanded.

Oh, sorry about that!

That's fine :) We need to learn the web UI.

I had actually created https://reviews.llvm.org/D129786 instead of reopening this one because at the time I tried, reopening didn't seem to work.

A reverted patch by default is still in the "Closed" state. You can "reopen" it so that people know it hasn't relanded.
When repairing a differential, just reuse the existing patch. It keeps all discussions (including past suggestions and issues a previous commit did not address) in one thread.
If the new patch is very similar to this one, you probably need to abandon it in favor of this one.


If you revert B because you haven't done A, and A and B are somewhat not so related (e.g. B addresses an existing brittle build system issue which happens to affect some platform when A is added), then having A as a separate differential is fine.

ckissane retitled this revision from [OLD] [llvm] add zstd to `llvm::compression` namespace to [llvm] add zstd to `llvm::compression` namespace.Mon, Jul 18, 1:42 PM
ckissane edited the summary of this revision. (Show Details)
ckissane updated this revision to Diff 445615.Mon, Jul 18, 1:42 PM
  • move try_find_dependency into it's own file
  • add newline to end of TryFindDependencyMacro.cmake
  • clarify comment in TryFindDependencyMacro.cmake
ckissane updated this revision to Diff 445623.Mon, Jul 18, 2:14 PM
  • remove extra cmake info
ckissane edited the summary of this revision. (Show Details)Mon, Jul 18, 2:15 PM
ckissane requested review of this revision.Mon, Jul 18, 2:56 PM
MaskRay accepted this revision.Mon, Jul 18, 3:00 PM

Does this address the macOS build failure?

This revision is now accepted and ready to land.Mon, Jul 18, 3:00 PM
ckissane edited the summary of this revision. (Show Details)Mon, Jul 18, 3:24 PM
ckissane edited the summary of this revision. (Show Details)
ckissane added a comment.EditedMon, Jul 18, 3:26 PM

Does this address the macOS build failure?

I believe so! @leonardchan is double checking

leonardchan accepted this revision.Tue, Jul 19, 10:52 AM

LGTM tested on mac on my end

This revision was landed with ongoing or failed builds.Tue, Jul 19, 10:54 AM
This revision was automatically updated to reflect the committed changes.

@ckissane
I took fresh sources of LLVM and when I run cmake I get the following warning:

-- Looking for compress2
-- Looking for compress2 - found
CMake Warning at cmake/config-ix.cmake:149 (find_package):
  By not providing "Findzstd.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "zstd", but
  CMake did not find one.

  Could not find a package configuration file provided by "zstd" with any of
  the following names:

    zstdConfig.cmake
    zstd-config.cmake

  Add the installation prefix of "zstd" to CMAKE_PREFIX_PATH or set
  "zstd_DIR" to a directory containing one of the above files.  If "zstd"
  provides a separate development package or SDK, be sure it has been
  installed.
Call Stack (most recent call first):
  CMakeLists.txt:768 (include)

Ubuntu 22.04 LTS
cmake version 3.22.3

phosek added inline comments.Wed, Jul 20, 12:33 AM
llvm/cmake/config-ix.cmake
149

Since there's no Findzstd.cmake module shipped with CMake, and we don't provide in LLVM, we should use the config mode.

I am still seeing the

zstdConfig.cmake
zstd-config.cmake

warning. @ckissane @phosek will you fix it? :)

I am still seeing the

zstdConfig.cmake
zstd-config.cmake

warning. @ckissane @phosek will you fix it? :)

I've quieted the warnings in https://github.com/llvm/llvm-project/commit/a71a01bc10d509bd4e746e7d277c4613ef886875, though I'm just now seeing @phosek's suggestion to use CONFIG I'm not sure what that does

MaskRay added inline comments.Mon, Jul 25, 10:34 AM
llvm/include/llvm/Support/Compression.h
48

I missed the values here. Why is -5 picked for NoCompression? What does it mean?

zstd.h says ZSTD_maxCLevel() is currently 22. The CLI program mentions 19. Why is BestSizeCompression 12?

ZSTD_CLEVEL_DEFAULT/the CLI CLI uses 3 for the default level. Why is DefaultCompression 5?