Add support for musl libc which returns aligned memory#6255
Add support for musl libc which returns aligned memory#6255rhuitl wants to merge 1 commit intoPointCloudLibrary:masterfrom
Conversation
|
Hi, first of all, thanks for the pull request. I have a few questions:
To my knowledge, PCL 1.14.1 is already available on Alpine Linux. Am I mistaken? Or is it PCL 1.15.0 that can only be successfully built with your proposed changes? Please describe any build error in more detail, so that I can understand the reasons behind this pull request better. Why is it necessary to test Why did you set |
|
Hi Markus, you are absolutely right, we don't need the first hunk. But we need one for the block that defines Not only for 1.15.0, but also for 1.13.x. Once I got MALLOC_ALIGNED defined, I get this warning, which is silenced by setting Here are all the built-in preprocessor macros: https://pastebin.com/38Y0iKMH Note that I'm building for Alpine, but with Conan, so I'm not using any prebuilt packages. I took a look at the package you mentioned: https://gitlab.alpinelinux.org/alpine/aports/-/blob/master/testing/pcl/APKBUILD. They apply two patches, and there is a discussion with the same error message I was facing here: https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/36503 I'll take a closer look what they did, but it doesn't look like something that can be applied to PCL, in general. |
|
Okay, so it turns out that no code changes are needed to get the desired behavior. Passing However, I still get the second warning, but I think it's a false alarm. |
…that use musl libc (e.g. Alpine) This addresses the warning: src/common/include/pcl/memory.h:71:2: warning: #warning "Potential runtime error due to aligned malloc mismatch! PCL was likely compiled without AVX support but you enabled AVX for your code (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)" [-Wcpp]
|
Summary: PCL can be compiled against musl libc by configuring PCL with The remaining warning is coming from Eigen. I made sure the Eigen package exports def package_info(self):
# ...
if self.settings.get_safe("os.distro") == "alpine":
self.cpp_info.components["eigen3"].defines = ["EIGEN_MALLOC_ALREADY_ALIGNED"] |
Alternatively, you could also add
Seems like a good solution, at least as far as my knowledge of musl and Conan goes. Be aware though that |
I missed that. It's because Conan automatically adds a compiler flag: https://github.com/conan-io/conan/blob/develop2/conan/tools/build/flags.py#L44 and
So is my assumption correct that I'd have to go fully custom with the flags in order to configure an SSE+AVX-enabled PCL build that will run on my target architecture? So a combination of
Yes... correct, and it would only return 32 byte aligned memory. Not good. I changed my approach slightly, adding a patch for Eigen's Eigen patch
--- a/Eigen/src/Core/util/Memory.h 2021-08-18 22:41:58.000000000 +0200
+++ b/Eigen/src/Core/util/Memory.h 2025-03-20 20:20:06.748421180 +0100
@@ -38,6 +38,13 @@
#define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
#endif
+#if defined(__MUSL__) \
+ && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ ) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
+ #define EIGEN_MUSL_MALLOC_ALREADY_ALIGNED 1
+#else
+ #define EIGEN_MUSL_MALLOC_ALREADY_ALIGNED 0
+#endif
+
// FreeBSD 6 seems to have 16-byte aligned malloc
// See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
// FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
@@ -50,7 +57,8 @@
#if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
|| (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
- || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
+ || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
+ || EIGEN_MUSL_MALLOC_ALREADY_ALIGNED \
|| EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
#define EIGEN_MALLOC_ALREADY_ALIGNED 1
#elseAnd if self.settings.get_safe("os.distro") == "alpine":
self.cpp_info.components["eigen3"].defines = ["__MUSL__"] |
I agree,
I found this on the topic: https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html#using-compile-checks So if I understand correctly, if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
option(PCL_ENABLE_SSE "Enable or Disable SSE optimizations." OFF)
else()
option(PCL_ENABLE_SSE "Enable or Disable SSE optimizations." ON)
endif()
mark_as_advanced(PCL_ENABLE_SSE)Same for AVX.
The compiler flags and preprocessor defines are handled correctly by PCL's CMake scripts, no? Note that gcc and clang automatically define
If |
|
I think I'm going to change this PR to run the alignment detection routines outside the SSE detection. That would fully resolve the issue I initially had. I'm now setting So the question is what do we need
What I don't get is why we do compiler checks (or even runtime checks as you noticed), either with There are a few other things the SSE detection file does:
In summary, I feel like the SSE and AVX cmake files could be removed more or less completely. People should set |
Great! Feel free to do that.
Sounds like a good solution.
I think a big advantage of the sse/avx checks is that they enable an optimized/fast PCL installation by default, without requiring the user to do or specify anything. This is especially convenient for new users or users who are not so familiar with sse/avx/-march flags. Also, I am not sure if there is a (easy) way to detect the target architecture and pass it to PCL via |
|
I opened another PR for the extraction of the memory alignment detection code.
Absolutely! But you can achieve this by setting Anyhow, I don't have a strong opinion here, also because it seems that it won't affect too many people. Ubuntu, for example, compiles PCL with the SSE detection disabled (https://git.launchpad.net/ubuntu/+source/pcl/tree/debian/rules#n35), so we get SSE2 on x86_64. |
This patch adds a parameter
WITH_MUSLthat, when enabled, makes PCL go thestd::mallocroute inpcl_macros.h. This is valid because musl libc returns aligned memory frommalloc()(https://www.openwall.com/lists/musl/2019/07/07/1).Unfortunately, musl intentionally does not want to be detected, so it looks like we're forced to detect it outside the build (e.g. check for
/etc/alpine-releaseas a proxy for musl libc) and pass the info into the build, like this patch is doing.See also https://catfox.life/2022/04/16/the-musl-preprocessor-debate/ and https://stackoverflow.com/questions/58177815/how-to-actually-detect-musl-libc for some background on why there is no
__MUSL__or similar preprocessor macro.With this patch, PCL can be built on Alpine Linux, which uses musl libc.