<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72839>72839</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang fails to vectorize masked addition loop with avx512 target
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jhorstmann
      </td>
    </tr>
</table>

<pre>
    The following c code compiled with clang and target `skylake-avx512` results in only scalar instructions in the assembly, testing the individual bits and branching on the value. Instead I would have expected this to use `avx512f` `vpaddd` instruction using a `k` register for the mask.

```c
void add_masked(int* __restrict a, int m, int* __restrict b, int* __restrict c) {
    for (int i=0, bit=1; i<8; i++, bit <<= 1) {
        int x = (m & bit) != 0 ? b[i] : 0;
        c[i] = a[i] + x;
 }
}
```

[Compiler Explorer](https://rust.godbolt.org/z/5Pz1e35eq)

GCC and also rustc with equivalent rust code manage to vectorize the code (although with an additional roundtrip of the bit mask through vector registers, see #72803).

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVM2S4ygMfhrlopoUwXESH3zoJJutue1h710yKDbTGDKA3el--i1wtnt6dlNUDPr5JMQnUYymd8wt1Eeozyua0uBD-2PwIaaRnFt1Xr-1fw-MV2-tfzWuR4XKa0blx5uxrPHVpAGVJdcjOY2JQs8JYSfiy5ulF_5G873eSNgJDBwnmyIah97ZN4yKLAU0LqYwqWS8K7o0MFKMPHb2DeQJE8eUI2e5cdrMRk9ksTMplpBdIKeGbOEX55nsxGv87mJi0vgdX_1kNQ40M_L9xiqxxjSYiMnjFDlnu2R5zWnCTsw30lrnwy_J4RRzEMoGL8t9ehMTB7z6UAKPFF_WIM4gnh7_O7EstZxnbzSS1s_ZkjXIg3EJ5BM-PweOKRiVkPKdjUs4Pja_6bv_FyuQDcL-uARCxJLVEgENVGeR_TqToDpvoDpm2emwbOSxrKJGqE5lnXHzH8j8y3h3zHqQhxFB7gpqNpWbLBYI1QU7qI8G6jNC9YQCqt9Q1Kf6jPRxkEe8f9rC_vyo48fm34J-qXJ9PC10DPjH_WZ94AD1GeRhSOkWoXoCeQF5CVNM697rztu09qEHeXkHean_et9wVfNPkM2vsH-eToVfZKPH7KsWsvPPycxk2aUiXfphJEc9Z0LNrJIP5p0LJYoS5IFsGvzUDwsEucwCk1lFFoOfnE7B3NBfi1N-hswQTEMoTgvmB-FifqvIGbjay4OoQDYP3q10W-mmamjF7WYvxHYvNvvtamhFJbdKKyIpO70VotN61-iaWKl6d1XNyrRSyGqzkUIctk21XZPcNyS34to0TVNrAVvBIxm7tnYec_1WJsaJ2708VM3KUsc2llEiZZkHIGWeKqHN9t-6qY-wFdbEFD8RkkmW21MZH1cyNn6t4NImH7VC6_3tUcHSr49xs5qCbb--dW_SMHVr5UeQlxzu8fl2C_4HqwTyUrKPIC_lAv8EAAD__zmfkoE">