<div dir="ltr">Hi,<div><br></div><div>In x86 there are 2 vector extension generations, SSE and AVX, and the flags controlling these are UseSSE and UseAVX, respectively. By setting UseAVX to 0, you are emulating an SSE4 machine, which has 16-byte vector registers. 64-bit VM requires SSE2, so you cannot set a value lower than 2 for UseSSE, and for all such values there are always 16-byte vector registers available. As a result, the minimum preferred vector size on x86_64 is 16 bytes.</div><div><br></div><div>Regards,</div><div>Quan Anh</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 24 May 2023 at 19:39, Chris Hegarty <<a href="mailto:chegar999@gmail.com">chegar999@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
Over in Lucene-land we're experimenting with the Vector API, and ran <br>
into an issue when testing against different environments. We're <br>
effectively simulating different environments with Hotspot's command <br>
line flags, and see an issue with `-XX:UseAVX=0`. With this option we <br>
expect the shape of the preferred species to fallback to 64 bits, but it <br>
is actually 128.<br>
<br>
Trivial reproducer that just prints the preferred int species:<br>
<br>
$ java -version<br>
openjdk version "21-ea" 2023-09-19<br>
OpenJDK Runtime Environment (build 21-ea+23-1988)<br>
OpenJDK 64-Bit Server VM (build 21-ea+23-1988, mixed mode, sharing)<br>
<br>
$ cat PrintPreferredVectorSize.java<br>
import jdk.incubator.vector.*;<br>
<br>
public class PrintPreferredVectorSize {<br>
<br>
     public static void main(String... args) {<br>
         System.out.println(IntVector.SPECIES_PREFERRED);<br>
     }<br>
}<br>
<br>
// Start with the default - no flags, then downsize with MaxVectorSize.<br>
// All looks good.<br>
$ java --add-modules jdk.incubator.vector PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 16, S_512_BIT]<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:MaxVectorSize=64<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 16, S_512_BIT]<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:MaxVectorSize=32<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 8, S_256_BIT]<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:MaxVectorSize=16<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 4, S_128_BIT]<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:MaxVectorSize=8<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 2, S_64_BIT]<br>
<br>
^^^ this all is as expected ^^^<br>
<br>
Now do similar(ish) with UseAVX<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:UseAVX=3<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 16, S_512_BIT]<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:UseAVX=2<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 8, S_256_BIT]<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:UseAVX=1<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 4, S_128_BIT]<br>
<br>
$ java --add-modules jdk.incubator.vector -XX:UseAVX=0<br>
PrintPreferredVectorSize<br>
WARNING: Using incubator modules: jdk.incubator.vector<br>
Species[int, 4, S_128_BIT]<br>
<br>
It is this last one that is surprising to us. We expect similar <br>
-XX:MaxVectorSize=8, which is Species[int, 2, S_64_BIT]<br>
<br>
Firstly, is this a bug? Is our expectation correct? If not, then I'm <br>
failing to understand something.<br>
<br>
If I'm not mistaken, then I can see that the <br>
Matcher::vector_width_in_bytes assumes a minimum of 16 regardless of <br>
whether UseAVX is < 1. Trivially, and as a hack, I just retrofitted <br>
vector_width_in_bytes to return 0 if UseAVX < 1. And we get <br>
Species[int, 2, S_64_BIT].<br>
<br>
The layer of these flags is not straightforward, so I won't pretend that <br>
my hack is the right way to fix this, but I just wanted to ensure that I <br>
was looking in the correct general area.<br>
<br>
Thanks,<br>
-Chris.<br>
<br>
<br>
</blockquote></div>