Skip to content
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

[zigcc] -mavx2 without -mpopcnt fails if using popcnt #17572

Open
jkbonfield opened this issue Oct 17, 2023 · 4 comments
Open

[zigcc] -mavx2 without -mpopcnt fails if using popcnt #17572

jkbonfield opened this issue Oct 17, 2023 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@jkbonfield
Copy link

Zig Version

0.10.0-dev.3523+4d7f5a191

Steps to Reproduce and Observed Behavior

Create a file using _mm_popcnt_u32 and build with -mavx2 or -mavx512f.

Example:

@ [samtools.../htscodecs]; cat _.c
#include <x86intrin.h>
#include <stdio.h>

int test1(int val) {
    return _mm_popcnt_u32(val);
}

@ [samtools.../htscodecs]; zig cc -c  _.c

@ [samtools.../htscodecs]; zig cc -mavx2 -c  _.c
_.c:5:12: error: always_inline function '_mm_popcnt_u32' requires target feature 'popcnt', but would be inlined into function 'test1' that is compiled without support for 'popcnt'
    return _mm_popcnt_u32(val);
           ^
1 error generated.

Expected Behavior

I have a Makefile test for whether or not -mpopcnt is required, but this claims it is not as in the absence of any other command line option it's automatically enabled. Unfortunately as soon as I then do -mavx2 it disables popcnt support, which feels a bit weird because it's almost certainly there once we get to avx2. (Technically it may not be, but I'm not aware of a single chip that omits popcnt while adding AVX.)

If we want to be explicit and require -mpopcnt when doing -mavx2, then it shouldn't accept popcnt without other options either.

@jkbonfield jkbonfield added the bug Observed behavior contradicts documented or intended behavior label Oct 17, 2023
@jkbonfield jkbonfield changed the title -mavx2 without -mpopcnt fails if using popcnt [zigcc] -mavx2 without -mpopcnt fails if using popcnt Oct 17, 2023
@Vexu
Copy link
Member

Vexu commented Oct 17, 2023

See #12227

@jkbonfield
Copy link
Author

jkbonfield commented Oct 17, 2023

A related issue indeed, but not quite the same. Here we don't have conflicting features, just inconsistent behaviour.

It's illogical that popcnt is considered to be a feature that is enabled by default unless AVX2/AVX512 is enabled, at which point it disables it. Consistency is what I'd like, as it makes autoconf feature detection challenging and I don't want to have to test all combinations.

@Vexu
Copy link
Member

Vexu commented Oct 17, 2023

It doesn't seem to be mentioned in the issue but I think it should also include features depending on other features.

@jkbonfield
Copy link
Author

I understand more a bit of the problem here.

I have code using AVX2 intrinsics and also a couple POPCNTs. My Makefile was @MAVX2@ automake expansion to -maxv2 or otherwise as required. (I probably should have just hardcoded it instead of attempting to use autoconf auto-detection methods.)

If I change that to @MAXV2@ @POPCNT@ it doesn't work, as the build system detected that -mpopcnt wasn't needed when in isolation, but -mavx2 is. So attempting to work around this issue doesn't fix the problem.

It turns out it's because zig is targetting the CPU options native to the host I'm building on. THat host has popcnt, but not avx2. So -mpopcnt isn't necessary, but -mavx2 is. Yet -mavx2 requires -mpopcnt as it appears explicitly adding it turns off the native support for other features. This does feel like a split-brain thing. I'd naively think -mavx2 is adding a feature, not resetting the CPU to some base CPU (like gcc's defaults) and then adding the one new option.

If zig cc is going to be doing native CPU options by default, then it probably ought to retain those auto-detected features when adding more with -m, unless some specific architecture is selected that turns them off eg --mwestmere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants