Skip to content

Commit

Permalink
Move testing properties to provider class (no classloading deadlock p…
Browse files Browse the repository at this point in the history
…ossible) and fallback to default provider in non-test mode
  • Loading branch information
uschindler committed Oct 15, 2023
1 parent 650a47e commit c8852a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.apache.lucene.util.SuppressForbidden;
import org.apache.lucene.util.VectorUtil;

Expand All @@ -40,6 +43,35 @@
*/
public abstract class VectorizationProvider {

static final OptionalInt TESTS_VECTOR_SIZE;
static final boolean TESTS_FORCE_INTEGER_VECTORS;

static {
var vs = OptionalInt.empty();
try {
vs =
Stream.ofNullable(System.getProperty("tests.vectorsize"))
.filter(Predicate.not(String::isEmpty))
.mapToInt(Integer::parseInt)
.findAny();
} catch (
@SuppressWarnings("unused")
SecurityException se) {
// ignored
}
TESTS_VECTOR_SIZE = vs;

boolean enforce = false;
try {
enforce = Boolean.getBoolean("tests.forceintegervectors");
} catch (
@SuppressWarnings("unused")
SecurityException se) {
// ignored
}
TESTS_FORCE_INTEGER_VECTORS = enforce;
}

/**
* Returns the default instance of the provider matching vectorization possibilities of actual
* runtime.
Expand Down Expand Up @@ -90,10 +122,17 @@ static VectorizationProvider lookup(boolean testMode) {
return new DefaultVectorizationProvider();
}
vectorMod.ifPresent(VectorizationProvider.class.getModule()::addReads);
// check if client VM
if (!testMode && isClientVM()) {
LOG.warning("C2 compiler is disabled; Java vector incubator API can't be enabled");
return new DefaultVectorizationProvider();
// check for testMode and otherwise fallback to default if slowness could happen
if (!testMode) {
if (TESTS_VECTOR_SIZE.isPresent() || TESTS_FORCE_INTEGER_VECTORS) {
LOG.warning(
"Vector bitsize and/or integer vectors enforcement; using default vectorization provider outside of testMode");
return new DefaultVectorizationProvider();
}
if (isClientVM()) {
LOG.warning("C2 compiler is disabled; Java vector incubator API can't be enabled");
return new DefaultVectorizationProvider();
}
}
try {
// we use method handles with lookup, so we do not need to deal with setAccessible as we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {
// default to platform supported bitsize
int vectorBitSize = VectorShape.preferredShape().vectorBitSize();
// but allow easy overriding for testing
try {
vectorBitSize = Integer.getInteger("tests.vectorsize", vectorBitSize);
} catch (SecurityException ignored) {
}
vectorBitSize = VectorizationProvider.TESTS_VECTOR_SIZE.orElse(vectorBitSize);
INT_SPECIES = VectorSpecies.of(int.class, VectorShape.forBitSize(vectorBitSize));
VECTOR_BITSIZE = INT_SPECIES.vectorBitSize();
FLOAT_SPECIES = INT_SPECIES.withLanes(float.class);
Expand All @@ -76,15 +73,8 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {
// hotspot misses some SSE intrinsics, workaround it
// to be fair, they do document this thing only works well with AVX2/AVX3 and Neon
boolean isAMD64withoutAVX2 = Constants.OS_ARCH.equals("amd64") && VECTOR_BITSIZE < 256;
boolean hasFastIntegerVectors = isAMD64withoutAVX2 == false;
try {
hasFastIntegerVectors =
Boolean.parseBoolean(
System.getProperty(
"tests.forceintegervectors", Boolean.toString(hasFastIntegerVectors)));
} catch (SecurityException ignored) {
}
HAS_FAST_INTEGER_VECTORS = hasFastIntegerVectors;
HAS_FAST_INTEGER_VECTORS =
VectorizationProvider.TESTS_FORCE_INTEGER_VECTORS || (isAMD64withoutAVX2 == false);
}

@Override
Expand Down

0 comments on commit c8852a0

Please sign in to comment.