Skip to content

Commit 831bce9

Browse files
committed
Refactor com.oracle.svm.hosted.dynamicaccessinference package classes.
1 parent e6f31c8 commit 831bce9

12 files changed

+611
-618
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ResourcesFeature.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ public void afterRegistration(AfterRegistrationAccess a) {
396396
ImageSingletons.add(RuntimeResourceSupport.class, resourcesRegistry);
397397
EmbeddedResourcesInfo embeddedResourcesInfo = new EmbeddedResourcesInfo();
398398
ImageSingletons.add(EmbeddedResourcesInfo.class, embeddedResourcesInfo);
399-
inferenceLog = ImageSingletons.contains(DynamicAccessInferenceLog.class) ? DynamicAccessInferenceLog.singleton() : null;
400399
}
401400

402401
private static ResourcesRegistryImpl resourceRegistryImpl() {
@@ -489,6 +488,8 @@ public void beforeAnalysis(BeforeAnalysisAccess a) {
489488
globWorkSet = Set.of();
490489

491490
resourceRegistryImpl().setAnalysisAccess(access);
491+
492+
inferenceLog = ImageSingletons.contains(DynamicAccessInferenceLog.class) ? DynamicAccessInferenceLog.singleton() : null;
492493
}
493494

494495
private static final class ResourceCollectorImpl extends ConditionalConfigurationRegistry implements ResourceCollector {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
import java.util.function.Function;
4848
import java.util.function.Predicate;
4949

50+
import com.oracle.svm.hosted.dynamicaccessinference.ConstantExpressionRegistry;
5051
import com.oracle.svm.hosted.dynamicaccessinference.StrictDynamicAccessInferenceFeature;
51-
import com.oracle.svm.hosted.dynamicaccessinference.StrictDynamicAccessInferenceSupport;
5252
import org.graalvm.nativeimage.AnnotationAccess;
5353
import org.graalvm.nativeimage.ImageSingletons;
5454
import org.graalvm.nativeimage.Platform;
@@ -221,7 +221,7 @@ public enum UsageKind {
221221
private final boolean buildingImageLayer = ImageLayerBuildingSupport.buildingImageLayer();
222222
private final LayeredStaticFieldSupport layeredStaticFieldSupport;
223223

224-
private final StrictDynamicAccessInferenceSupport strictDynamicAccessInferenceSupport;
224+
private final ConstantExpressionRegistry constantExpressionRegistry;
225225

226226
@SuppressWarnings("this-escape")
227227
public SVMHost(OptionValues options, ImageClassLoader loader, ClassInitializationSupport classInitializationSupport, AnnotationSubstitutionProcessor annotationSubstitutions,
@@ -260,7 +260,7 @@ public SVMHost(OptionValues options, ImageClassLoader loader, ClassInitializatio
260260
enableReachableInCurrentLayer = ImageLayerBuildingSupport.buildingExtensionLayer();
261261
layeredStaticFieldSupport = ImageLayerBuildingSupport.buildingImageLayer() ? LayeredStaticFieldSupport.singleton() : null;
262262

263-
strictDynamicAccessInferenceSupport = StrictDynamicAccessInferenceFeature.isDisabled() ? null : StrictDynamicAccessInferenceSupport.singleton();
263+
constantExpressionRegistry = StrictDynamicAccessInferenceFeature.isActive() ? ConstantExpressionRegistry.singleton() : null;
264264
}
265265

266266
/**
@@ -1277,7 +1277,7 @@ public SimulateClassInitializerSupport createSimulateClassInitializerSupport(Ana
12771277
return new SimulateClassInitializerSupport(aMetaAccess, this);
12781278
}
12791279

1280-
public StrictDynamicAccessInferenceSupport getStrictDynamicAccessInferenceSupport() {
1281-
return strictDynamicAccessInferenceSupport;
1280+
public ConstantExpressionRegistry getConstantExpressionRegistry() {
1281+
return constantExpressionRegistry;
12821282
}
12831283
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/dynamicaccessinference/ConstantExpressionAnalyzer.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@
8686
* into the abstract frame, while the inferred value represents the actual Java value which would be
8787
* observed during the run-time execution of the corresponding instruction.
8888
*/
89-
public final class ConstantExpressionAnalyzer extends AbstractInterpreter<ConstantExpressionAnalyzer.Value> {
89+
final class ConstantExpressionAnalyzer extends AbstractInterpreter<ConstantExpressionAnalyzer.Value> {
9090

9191
private static final NotACompileTimeConstant NOT_A_COMPILE_TIME_CONSTANT = new NotACompileTimeConstant();
9292

93-
private final ImageClassLoader classLoader;
93+
private final ClassLoader classLoader;
9494
private final Map<Method, Function<InvocationData, Value>> propagatingMethods;
9595

96-
public ConstantExpressionAnalyzer(CoreProviders providers, ImageClassLoader classLoader) {
96+
ConstantExpressionAnalyzer(CoreProviders providers, ClassLoader classLoader) {
9797
super(providers);
9898
this.classLoader = classLoader;
9999
this.propagatingMethods = buildPropagatingMethods();
@@ -462,7 +462,7 @@ private Value invokeForNameOne(Context context, List<Value> operands) {
462462
}
463463
ClassLoader loader = ClassForNameSupport.respectClassLoader()
464464
? OriginalClassProvider.getJavaClass(context.method().getDeclaringClass()).getClassLoader()
465-
: classLoader.getClassLoader();
465+
: classLoader;
466466
return findClass(context, className, loader);
467467
}
468468

@@ -480,7 +480,7 @@ private Value invokeForNameThree(Context context, List<Value> operands) {
480480
return defaultValue();
481481
}
482482
} else {
483-
loader = classLoader.getClassLoader();
483+
loader = classLoader;
484484
}
485485
return findClass(context, className, loader);
486486
}
@@ -508,9 +508,10 @@ private Value getLookup(Context context) {
508508

509509
/**
510510
* Looking up constants/types/fields/methods through a {@link WrappedConstantPool} can result in
511-
* UnsupportedFeatureException(s) being thrown. To avoid these exceptions, we essentially
512-
* simulate the behavior of the WrappedConstantPool when looking up the underlying JVMCI
513-
* constant/type/field/method, but skip the analysis (or hosted) universe lookup.
511+
* {@link com.oracle.graal.pointsto.constraints.UnsupportedFeatureException
512+
* UnsupportedFeatureException(s)} being thrown. To avoid this, we simulate the behavior of the
513+
* {@link WrappedConstantPool} when looking up the underlying JVMCI constant/type/field/method,
514+
* but skip the analysis (or hosted) universe lookup.
514515
*/
515516
private static ConstantPool unwrapIfWrapped(ConstantPool constantPool) {
516517
return constantPool instanceof WrappedConstantPool wrapper
@@ -553,23 +554,23 @@ protected JavaConstant lookupAppendix(ConstantPool constantPool, int cpi, int op
553554
* Marker interface for abstract values obtained during bytecode-level constant expression
554555
* inference.
555556
*/
556-
public interface Value {
557+
interface Value {
557558

558559
}
559560

560-
public static class NotACompileTimeConstant implements Value {
561+
static class NotACompileTimeConstant implements Value {
561562

562563
@Override
563564
public String toString() {
564565
return "Not a compile-time constant";
565566
}
566567
}
567568

568-
public abstract static class CompileTimeConstant implements Value {
569+
abstract static class CompileTimeConstant implements Value {
569570

570571
private final int sourceBci;
571572

572-
public CompileTimeConstant(int bci) {
573+
CompileTimeConstant(int bci) {
573574
this.sourceBci = bci;
574575
}
575576

@@ -602,11 +603,11 @@ public int hashCode() {
602603
}
603604
}
604605

605-
public static class CompileTimeValueConstant<T> extends CompileTimeConstant {
606+
static class CompileTimeValueConstant<T> extends CompileTimeConstant {
606607

607608
private final T value;
608609

609-
public CompileTimeValueConstant(int bci, T value) {
610+
CompileTimeValueConstant(int bci, T value) {
610611
super(bci);
611612
this.value = value;
612613
}
@@ -622,7 +623,7 @@ public String toString() {
622623
}
623624
}
624625

625-
public static class CompileTimeArrayConstant<T> extends CompileTimeConstant {
626+
static class CompileTimeArrayConstant<T> extends CompileTimeConstant {
626627

627628
/*
628629
* Sparse array representation to avoid possible large memory overheads when analyzing an
@@ -632,14 +633,14 @@ public static class CompileTimeArrayConstant<T> extends CompileTimeConstant {
632633
private final int size;
633634
private final Class<T> elementType;
634635

635-
public CompileTimeArrayConstant(int bci, int size, Class<T> elementType) {
636+
CompileTimeArrayConstant(int bci, int size, Class<T> elementType) {
636637
super(bci);
637638
this.value = new HashMap<>();
638639
this.size = size;
639640
this.elementType = elementType;
640641
}
641642

642-
public CompileTimeArrayConstant(int bci, CompileTimeArrayConstant<T> arrayConstant) {
643+
CompileTimeArrayConstant(int bci, CompileTimeArrayConstant<T> arrayConstant) {
643644
super(bci);
644645
this.value = new HashMap<>(arrayConstant.value);
645646
this.size = arrayConstant.size;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/dynamicaccessinference/ConstantExpressionRegistry.java

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,21 @@
2727
import java.util.Map;
2828
import java.util.concurrent.ConcurrentHashMap;
2929

30-
import org.graalvm.collections.Pair;
3130
import org.graalvm.nativeimage.ImageSingletons;
3231

3332
import com.oracle.svm.util.LogUtils;
3433
import com.oracle.svm.hosted.dataflow.AbstractFrame;
3534
import com.oracle.svm.hosted.dataflow.DataFlowAnalysisException;
3635

3736
import jdk.graal.compiler.bytecode.Bytecode;
38-
import jdk.graal.compiler.bytecode.BytecodeProvider;
39-
import jdk.graal.compiler.bytecode.ResolvedJavaMethodBytecodeProvider;
40-
import jdk.graal.compiler.nodes.graphbuilderconf.IntrinsicContext;
37+
import jdk.vm.ci.code.BytecodePosition;
4138
import jdk.vm.ci.meta.JavaKind;
4239
import jdk.vm.ci.meta.ResolvedJavaMethod;
4340

4441
/**
4542
* Holds information on constant expressions as inferred by {@link ConstantExpressionAnalyzer}.
4643
*/
47-
public class ConstantExpressionRegistry {
44+
public final class ConstantExpressionRegistry {
4845

4946
public static ConstantExpressionRegistry singleton() {
5047
return ImageSingletons.lookup(ConstantExpressionRegistry.class);
@@ -59,32 +56,29 @@ public static ConstantExpressionRegistry singleton() {
5956
* Maps method and BCI pairs into abstract frames which represent the execution frame right
6057
* before the corresponding bytecode instruction.
6158
*/
62-
private Map<Pair<ResolvedJavaMethod, Integer>, AbstractFrame<ConstantExpressionAnalyzer.Value>> registry;
63-
private boolean isSealed;
59+
private Map<BytecodePosition, AbstractFrame<ConstantExpressionAnalyzer.Value>> registry = new ConcurrentHashMap<>();
60+
private boolean sealed = false;
6461

65-
public ConstantExpressionRegistry() {
66-
registry = new ConcurrentHashMap<>();
67-
isSealed = false;
62+
private final ConstantExpressionAnalyzer analyzer;
63+
64+
ConstantExpressionRegistry(ConstantExpressionAnalyzer analyzer) {
65+
this.analyzer = analyzer;
6866
}
6967

70-
public void analyzeAndStore(ConstantExpressionAnalyzer analyzer, ResolvedJavaMethod method, IntrinsicContext intrinsicContext) {
71-
assert !isSealed() : "Cannot store in registry when it is already sealed";
72-
Bytecode bytecode = getBytecode(method, intrinsicContext);
68+
/**
69+
* Analyze the provided {@code bytecode} for constant expressions and store the results in the
70+
* registry.
71+
*/
72+
public void store(Bytecode bytecode) {
73+
assert !sealed : "Cannot store in registry when it is already sealed";
7374
try {
7475
Map<Integer, AbstractFrame<ConstantExpressionAnalyzer.Value>> abstractFrames = analyzer.analyze(bytecode);
75-
abstractFrames.forEach((key, value) -> registry.put(Pair.create(method, key), value));
76+
abstractFrames.forEach((key, value) -> registry.put(new BytecodePosition(null, bytecode.getMethod(), key), value));
7677
} catch (DataFlowAnalysisException e) {
77-
LogUtils.warning("Constant expression analysis failed for " + method.format("%H.%n(%p)") + ": " + e.getMessage());
78+
LogUtils.warning("Constant expression analysis failed for " + bytecode.getMethod().format("%H.%n(%p)") + ": " + e.getMessage());
7879
}
7980
}
8081

81-
private static Bytecode getBytecode(ResolvedJavaMethod method, IntrinsicContext intrinsicContext) {
82-
BytecodeProvider bytecodeProvider = intrinsicContext == null
83-
? ResolvedJavaMethodBytecodeProvider.INSTANCE
84-
: intrinsicContext.getBytecodeProvider();
85-
return bytecodeProvider.getBytecode(method);
86-
}
87-
8882
/**
8983
* Attempt to get the inferred receiver of a {@code targetMethod} invocation at the specified
9084
* code location.
@@ -96,9 +90,12 @@ private static Bytecode getBytecode(ResolvedJavaMethod method, IntrinsicContext
9690
* {@code null} value is represented by {@link ConstantExpressionRegistry#NULL_MARKER}.
9791
*/
9892
public Object getReceiver(ResolvedJavaMethod callerMethod, int bci, ResolvedJavaMethod targetMethod) {
99-
assert !isSealed() : "Registry is already sealed";
93+
assert !sealed : "Registry is already sealed";
10094
assert targetMethod.hasReceiver() : "Method " + targetMethod + " does not have receiver";
101-
AbstractFrame<ConstantExpressionAnalyzer.Value> frame = registry.get(Pair.create(callerMethod, bci));
95+
if (callerMethod == null) {
96+
return null;
97+
}
98+
AbstractFrame<ConstantExpressionAnalyzer.Value> frame = registry.get(new BytecodePosition(null, callerMethod, bci));
10299
if (frame == null) {
103100
return null;
104101
}
@@ -138,10 +135,13 @@ public <T> T getReceiver(ResolvedJavaMethod callerMethod, int bci, ResolvedJavaM
138135
* {@code null} value is represented by {@link ConstantExpressionRegistry#NULL_MARKER}.
139136
*/
140137
public Object getArgument(ResolvedJavaMethod callerMethod, int bci, ResolvedJavaMethod targetMethod, int index) {
141-
assert !isSealed() : "Registry is already sealed";
138+
assert !sealed : "Registry is already sealed";
142139
int numOfParameters = targetMethod.getSignature().getParameterCount(false);
143140
assert 0 <= index && index < numOfParameters : "Argument index " + index + " out of bounds for " + targetMethod;
144-
AbstractFrame<ConstantExpressionAnalyzer.Value> frame = registry.get(Pair.create(callerMethod, bci));
141+
if (callerMethod == null) {
142+
return null;
143+
}
144+
AbstractFrame<ConstantExpressionAnalyzer.Value> frame = registry.get(new BytecodePosition(null, callerMethod, bci));
145145
if (frame == null) {
146146
return null;
147147
}
@@ -193,12 +193,8 @@ private static <T> T tryToCast(Object value, Class<T> type) {
193193
return type.cast(value);
194194
}
195195

196-
public boolean isSealed() {
197-
return isSealed;
198-
}
199-
200-
public void seal() {
201-
isSealed = true;
196+
void seal() {
197+
sealed = true;
202198
registry = null;
203199
}
204200

0 commit comments

Comments
 (0)