<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
Hi Chris,
<div><br>
</div>
<div>getMaxLaneCount will return -1 *if* HotSpot is compiled with C2 disabled, a special case. It is not currently affected when C2 is disabled at runtime, we could check that too but need to think through the implications.</div>
<div><br>
</div>
<div>The Vector API degrades with functional equivalence but does not currently generate the same code as if it were scalar code. Unfortunately the fallback implementation in pure Java is currently slow since it operates on instances of Vector etc as ordinary
classes. There is no C1 support to optimize this case.</div>
<div> </div>
<div>You should not rely on the UseAVX/UseSSE flags to restrict (they are really there to emulate). For testing purposes I currently recommend setting the MaxVectorSize to limit and check the preferred species or the value of VectorShape.S_Max_BIT.vectorBitSize(),
or otherwise explicit detect C2 is disabled (not clear how easy that is).</div>
<div><br>
</div>
<div>Paul.<br>
<div><br>
<blockquote type="cite">
<div>On May 24, 2023, at 5:20 AM, Chris Hegarty <chegar999@gmail.com> wrote:</div>
<br class="Apple-interchange-newline">
<div><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">Hi,</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">Thanks
for your reply. Lemme try to clarify the actual source of the issue we see, UseAVX=0 might be related but also could be a red herring.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">Some
Lucene test runs with C2 effectively "disabled", with -XX:TieredStopAtLevel=1, and we observe horribly slow performance - much worse than the scalar equivalent. And we see that the preferred species is still wider than expected.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">$
java --add-modules jdk.incubator.vector -XX:TieredStopAtLevel=1 PrintPreferredVectorSize</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">WARNING:
Using incubator modules: jdk.incubator.vector</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">Species[int,
16, S_512_BIT]</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">But
(and again this could be a red herring), VectorShape::getMaxVectorBitSize indicates that this should not be the case, e.g.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">//
VectorSupport.getMaxLaneCount may return -1 if C2 is not enabled,</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">//
or a value smaller than the S_64_BIT.vectorBitSize / elementSizeInBits if MaxVectorSize < 16</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">//
If so default to S_64_BIT</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">I
had assume that this is because the code will not benefit from the C2 intrinsics.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">Basically,
how so we determine whether the environment should use the Vector API or not (fallback to a scalar implementation)? I would assume this is a question that we should not have to answer - the Vectorized implementation should degrade gracefully.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">-Chris.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;">On
24/05/2023 12:47, Quân Anh Mai wrote:</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
Hi,<br>
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.<br>
Regards,<br>
Quan Anh<br>
On Wed, 24 May 2023 at 19:39, Chris Hegarty <<a href="mailto:chegar999@gmail.com">chegar999@gmail.com</a><span class="Apple-converted-space"> </span><<a href="mailto:chegar999@gmail.com">mailto:chegar999@gmail.com</a>>> wrote:<br>
Hi,<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,<br>
but it<br>
is actually 128.<br>
Trivial reproducer that just prints the preferred int species:<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>
$ cat PrintPreferredVectorSize.java<br>
import jdk.incubator.vector.*;<br>
public class PrintPreferredVectorSize {<br>
public static void main(String... args) {<br>
System.out.println(IntVector.SPECIES_PREFERRED);<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>
$ 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>
$ 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>
$ 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>
$ 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>
^^^ this all is as expected ^^^<br>
Now do similar(ish) with UseAVX<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>
$ 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>
$ 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>
$ 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>
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>
Firstly, is this a bug? Is our expectation correct? If not, then I'm<br>
failing to understand something.<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>
The layer of these flags is not straightforward, so I won't pretend<br>
that<br>
my hack is the right way to fix this, but I just wanted to ensure<br>
that I<br>
was looking in the correct general area.<br>
Thanks,<br>
-Chris.</blockquote>
</div>
</blockquote>
</div>
<br>
</div>
</body>
</html>