Skip to content

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Apr 5, 2022

On master (9ce1c50) the ENABLE_ARM_SHANI, ENABLE_AVX2, ENABLE_SSE41, and ENABLE_X86_SHANI macros are defined twice in the build system. First time, using the AC_DEFINE macro in the configure script. And second time, in Makefile as a part of CPPFLAGS.

This PR removes such duplication.

This change is required for #24773.

@Empact
Copy link
Contributor

Empact commented Apr 6, 2022

Just to be clear on my understanding: this removes the definitions in these files in the case where they do not pass the test in configure.ac. The reason the current behavior is not broken where these functions are defined despite missing required behavior, is that while these functions are always included, their callers also check for the definitions, and that prevents their being called in those cases. Is that right?

If so, would it be worthwhile and feasible to remove these archives entirely, in the cases where the functionality is not present? E.g. crypto_libbitcoin_crypto_sse41_a includes only crypto/sha256_sse41.cpp. which only defines functionality if ENABLE_SSE41 is defined.

@hebasto
Copy link
Member Author

hebasto commented Apr 6, 2022

Just to be clear on my understanding: this removes the definitions in these files in the case where they do not pass the test in configure.ac.

The build system includes the crypto parts only when the related checks passed

bitcoin/src/Makefile.am

Lines 40 to 56 in 41720a1

LIBBITCOIN_CRYPTO= $(LIBBITCOIN_CRYPTO_BASE)
if ENABLE_SSE41
LIBBITCOIN_CRYPTO_SSE41 = crypto/libbitcoin_crypto_sse41.a
LIBBITCOIN_CRYPTO += $(LIBBITCOIN_CRYPTO_SSE41)
endif
if ENABLE_AVX2
LIBBITCOIN_CRYPTO_AVX2 = crypto/libbitcoin_crypto_avx2.a
LIBBITCOIN_CRYPTO += $(LIBBITCOIN_CRYPTO_AVX2)
endif
if ENABLE_X86_SHANI
LIBBITCOIN_CRYPTO_X86_SHANI = crypto/libbitcoin_crypto_x86_shani.a
LIBBITCOIN_CRYPTO += $(LIBBITCOIN_CRYPTO_X86_SHANI)
endif
if ENABLE_ARM_SHANI
LIBBITCOIN_CRYPTO_ARM_SHANI = crypto/libbitcoin_crypto_arm_shani.a
LIBBITCOIN_CRYPTO += $(LIBBITCOIN_CRYPTO_ARM_SHANI)
endif

@laanwj
Copy link
Member

laanwj commented Apr 7, 2022

Build system review and concept ACK 8251431

Including the config header instead of relying on definitions on the command line is a no-brainer.

I would guess this doesn't need parallel changes to the MSVC build system because it doesn't support special instruction sets yet.

@laanwj
Copy link
Member

laanwj commented Apr 7, 2022

Thinking of it, why do we compile these files at all when the respective option is disabled? #ifdefing out the entire source file is a bit curious (but maybe I'm missing something).

@fanquake
Copy link
Member

fanquake commented Apr 7, 2022

Some of this might have been done (at some point) to support weird / nonstandard / non-autoconf builds in some way, i.e similar to

// While not technically a supported configuration, defaulting to defining these
// DECLs when we were compiled without autotools makes it easier for other build
// systems to build things like libbitcoinconsensus for strange targets.

@laanwj
Copy link
Member

laanwj commented Apr 7, 2022

I can see a slight point for a library like libbitcoin_consensus; e.g. if you want to build it as part of a.rust binding, you don't particularly want to call into automake/autoconf. Still. I'm not sure having files compiled only when e.g. ENABLE_AVX2 is set, then having the whole file guarded with #ifdef ENABLE_AVX2…#endif helps there.

@hebasto
Copy link
Member Author

hebasto commented Apr 7, 2022

@laanwj

Thinking of it, why do we compile these files at all when the respective option is disabled?

What does make you think so?

For example, when the configure script fails to check AC_MSG_CHECKING([for x86 SHA-NI intrinsics]) ..., the sha256_x86_shani.cpp source file is not compiled. Also #24774 (comment).

@hebasto
Copy link
Member Author

hebasto commented Apr 7, 2022

e.g. if you want to build it as part of a.rust binding, you don't particularly want to call into automake/autoconf.

Also we do not use automake/autoconf when building with MSVC.

@DrahtBot
Copy link
Contributor

DrahtBot commented Apr 15, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25302 (build: Check usages of #if defined(...) by brokenprogrammer)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@fanquake
Copy link
Member

This PR removes such duplication.

I bought this up very briefly with Cory, and he said that this was done on purpose. So this change shouldn't be made under the guise of "duplication", and/or move ahead until he weighs in here. If this code does have other use cases then we need to evaluate whether removing it just to support things in the MSVC build is actually more worthwhile.

@hebasto
Copy link
Member Author

hebasto commented Apr 17, 2022

I bought this up very briefly with Cory, and he said that this was done on purpose.

What is the purpose?

@theuni
Copy link
Member

theuni commented Apr 21, 2022

@hebasto whoops, sorry for the delayed response.

It's rather convenient imo to have the crypto folder work outside of Core. I use it often as a quick way to add a known/trustworthy sha2 impl to another project, like here: https://github.com/theuni/libutreexo

I believe @TheBlueMatt has done/does something similar for some of his projects.

So for what's in crypto/ at least, unless it's somehow problematic, I'd much prefer to keep the need for bitcoin-config.h to a minimum.

hebasto added 4 commits May 28, 2022 14:50
This change deduplicates the `ENABLE_ARM_SHANI` macro in the build
system.
This change deduplicates the `ENABLE_AVX2` macro in the build system.
This change deduplicates the `ENABLE_SSE41` macro in the build system.
This change deduplicates the `ENABLE_X86_SHANI` macro in the build
system.
@hebasto
Copy link
Member Author

hebasto commented May 28, 2022

Rebased 8251431 -> 6241d16 (pr24774.02 -> pr24774.03) due to conflict with #24322.

@hebasto
Copy link
Member Author

hebasto commented May 28, 2022

@theuni

I'd much prefer to keep the need for bitcoin-config.h to a minimum.

This change does not make the bitcoin-config.h required. It remains optional, i.e., only when the HAVE_CONFIG_H is defined.

It's rather convenient imo to have the crypto folder work outside of Core. I use it often as a quick way to add a known/trustworthy sha2 impl to another project, like here: https://github.com/theuni/libutreexo

I believe @TheBlueMatt has done/does something similar for some of his projects.

I believe all of the cases mentioned above are still working with this PR without any adjustments.

In other words, this PR makes crypto recognize bitcoin-config.h as an additional build option. All other build options, which do not use bitcoin-config.h, are not affected.

@achow101
Copy link
Member

Are you still working on this?

@hebasto hebasto closed this Oct 12, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants