-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shared library fails to build with LTO enabled #5906
Comments
Thanks for reporting this! It's a known bug and we thought we'd fixed it: with an API change in 3.0 and a crude local workaround in 2.28 LTS. So it turns out LTO defeats the local workaround Note that it's a spurious warning: the sha512 functions can either output a 48-byte SHA-384 or a 64-byte SHA-512. In this case the caller requests a 48-byte SHA-384 into a 48-byte buffer so all is fine. But GCC sees I don't know how we'll fix that one. Incidentally, why are you enabling LTO? In my limited experience, LTO doesn't improve performance. I only did very casual benchmarks though, and it probably depends on factors such as cache sizes. On the other hand LTO does help a lot when optimizing for size. |
Thanks for your fast reply :D
Maybe this is a dumb question, but why not substitute
It can help with some kinds of optimizations, but mainly because some environments build software with LTO enabled by default (like Ubuntu, Arch Linux, etc.), and build failures are always annoying. And I could ask you instead: why not enabling it? :) |
As a data point, here are timings from running
For a LTO does make a significant difference to code size. It seems to reduce the size of our sample programs to about 70% to 50%, with a few outliers (code that doesn't use PSA maybe?) being reduced to about 10%. That's in the We do aim to support LTO, and I'm preparing a pull request to add LTO runs to the CI and fix the warnings. But I would recommend using LTO only if you're optimizing for size.
Higher risk of bugs (either due to library bugs such as subtle undefined behavior or improperly declared assembly blocks, or due to compiler bugs). Higher compilation times. No benefits if you don't care about code size. |
Il giorno mer 31 ago 2022 alle 08:31:24 -07:00:00, Gilles Peskine
***@***.***> ha scritto:
For a -Ofast build, the difference in performance with -flto is
within the variance. The inversion with gcc -flto -Ofast might have
been bad luck: I did a few more runs and they don't seem to be
consistently ranked. Obviously this can depend on the target
architecture, on the exact machine (cache size etc.) and on the
specific application's workload. But in general I expect this
conclusion to be true: link-time optimization has small or negligible
impact on the performance of Mbed TLS.
I wouldn't ever use `-Ofast` personally (in Debian we use `-O2`, but
`-O3` should be fine as long as you don't rely on undefined behaviour),
as that disregards standards compliance, but yeah, I wouldn't have
expected `-flto` to significantly impact performance either.
LTO does make a significant difference to code size. It seems to
reduce the size of our sample programs to about 70% to 50%, with a
few outliers (code that doesn't use PSA maybe?) being reduced to
about 10%. That's in the full config, I'd expect less of a difference
on a build with just the few cryptographic algorithms that a
specification application actually needs.
That's nice indeed.
We do aim to support LTO, and I'm preparing a pull request to add LTO
runs to the CI and fix the warnings. But I would recommend using LTO
only if you're optimizing for size.
Thanks! We do not want to enable LTO for MbedTLS specifically, we're
just turning the switch for the whole archive; having to manually add a
package to the blacklist is always annoying.
> And I could ask you instead: why not enabling it? :)
>
Higher risk of bugs (either due to library bugs such as subtle
undefined behavior or improperly declared assembly blocks, or due to
compiler bugs). Higher compilation times. No benefits if you don't
care about code size.
Every kind of optimization can can lead to bugs when relying on UB, and
LTO is no exception. I think that turning LTO on can help spotting the
subtle errors you mention and fix them before a new compiler version
starts generating buggy code because of them.
Anyway, thanks for taking some time to improve the situation!
…--
OpenPGP key: 66DE F152 8299 0C21 99EF A801 A8A1 28A8 AB1C EE49
|
Summary
When enabling LTO with
CMAKE_INTERPROCEDURAL_OPTIMIZATION
andUSE_SHARED_MBEDTLS_LIBRARY
, builds fail with the following Werror:System information
Mbed TLS version (number or commit id): 2.28.0 and 7e163d7
Operating system and version: Debian Testing (12)
Configuration (if not default, please attach
mbedtls_config.h
): defaultCompiler and options (if you used a pre-built binary, please indicate how you obtained it): GCC 11, no custom options (apart from LTO)
Additional environment information: None
Expected behavior
MbedTLS should compile fine with LTO enabled
Actual behavior
This error prevents some users from using MbedTLS with LTO enabled. Notably, Ubuntu added the
mbedtls
package to their LTO blacklist.Steps to reproduce
Note that not all the options above are required to reproduce the problem, the needed ones are
-DUSE_SHARED_MBEDTLS_LIBRARY=true -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=true -DCMAKE_POLICY_DEFAULT_CMP0069=NEW
. The others are there to speed up compilation by skipping the build of unrelated parts.Additional information
Discovered when building the Debian package.
The development branch seems to not be affected.
The text was updated successfully, but these errors were encountered: