Skip to content

Commit 4edfe81

Browse files
committed
[GR-39410] [GR-15630] Fix and externalize Comparability of Hosted{Type,Method,Field} and allow classes with same name.
PullRequest: graal/12081
2 parents dacb26d + 82a0b09 commit 4edfe81

File tree

16 files changed

+257
-173
lines changed

16 files changed

+257
-173
lines changed

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/api/HostVM.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.lang.reflect.AnnotatedElement;
3030
import java.util.ArrayList;
31+
import java.util.Comparator;
3132
import java.util.List;
3233
import java.util.Optional;
3334
import java.util.concurrent.CopyOnWriteArrayList;
@@ -238,4 +239,6 @@ public void clearInThread() {
238239
public Object getConfiguration() {
239240
return null;
240241
}
242+
243+
public abstract Comparator<? super ResolvedJavaType> getTypeComparator();
241244
}

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/results/AbstractAnalysisResultsBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Collection;
2828
import java.util.HashMap;
2929
import java.util.Map;
30+
import java.util.stream.Stream;
3031

3132
import com.oracle.graal.pointsto.BigBang;
3233
import com.oracle.graal.pointsto.api.PointstoOptions;
@@ -38,6 +39,7 @@
3839
import jdk.vm.ci.meta.JavaMethodProfile;
3940
import jdk.vm.ci.meta.JavaTypeProfile;
4041
import jdk.vm.ci.meta.ResolvedJavaMethod;
42+
import jdk.vm.ci.meta.ResolvedJavaType;
4143
import jdk.vm.ci.meta.TriState;
4244

4345
public abstract class AbstractAnalysisResultsBuilder {
@@ -126,9 +128,12 @@ private JavaTypeProfile cachedTypeProfile(JavaTypeProfile[] cache, int cacheIdx,
126128

127129
private JavaTypeProfile createTypeProfile(TypeState typeState) {
128130
double probability = 1d / typeState.typesCount();
129-
JavaTypeProfile.ProfiledType[] pitems = typeState.typesStream(bb)
130-
.map(analysisType -> converter == null ? analysisType : converter.lookup(analysisType))
131-
.sorted()
131+
132+
Stream<? extends ResolvedJavaType> stream = typeState.typesStream(bb);
133+
if (converter != null) {
134+
stream = stream.map(converter::lookup).sorted(converter.hostVM().getTypeComparator());
135+
}
136+
JavaTypeProfile.ProfiledType[] pitems = stream
132137
.map(type -> new JavaTypeProfile.ProfiledType(type, probability))
133138
.toArray(JavaTypeProfile.ProfiledType[]::new);
134139

substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.concurrent.atomic.AtomicLong;
4545
import java.util.function.BiFunction;
4646

47-
import com.oracle.svm.shadowed.org.bytedeco.llvm.global.LLVM;
4847
import org.graalvm.compiler.code.CompilationResult;
4948
import org.graalvm.compiler.core.common.CompressEncoding;
5049
import org.graalvm.compiler.core.common.LIRKind;
@@ -83,7 +82,6 @@
8382
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
8483
import org.graalvm.nativeimage.c.constant.CEnum;
8584
import org.graalvm.nativeimage.c.function.CEntryPoint;
86-
import com.oracle.svm.util.GuardedAnnotationAccess;
8785

8886
import com.oracle.svm.core.FrameAccess;
8987
import com.oracle.svm.core.ReservedRegisters;
@@ -125,6 +123,8 @@
125123
import com.oracle.svm.shadowed.org.bytedeco.llvm.LLVM.LLVMBasicBlockRef;
126124
import com.oracle.svm.shadowed.org.bytedeco.llvm.LLVM.LLVMTypeRef;
127125
import com.oracle.svm.shadowed.org.bytedeco.llvm.LLVM.LLVMValueRef;
126+
import com.oracle.svm.shadowed.org.bytedeco.llvm.global.LLVM;
127+
import com.oracle.svm.util.GuardedAnnotationAccess;
128128
import com.oracle.svm.util.ReflectionUtil;
129129

130130
import jdk.vm.ci.code.CallingConvention;
@@ -186,7 +186,7 @@ public class LLVMGenerator implements LIRGeneratorTool, SubstrateLIRGenerator {
186186
this.lirKindTool = new LLVMUtils.LLVMKindTool(builder);
187187
this.debugInfoPrinter = new DebugInfoPrinter(this, debugLevel);
188188

189-
this.functionName = SubstrateUtil.uniqueShortName(method);
189+
this.functionName = ((HostedMethod) method).getUniqueShortName();
190190
this.isEntryPoint = isEntryPoint(method);
191191
this.modifiesSpecialRegisters = modifiesSpecialRegisters(graph);
192192

@@ -291,7 +291,7 @@ byte[] getBitcode() {
291291
}
292292

293293
private static String getFunctionName(ResolvedJavaMethod method) {
294-
return SubstrateUtil.uniqueShortName(method);
294+
return ((HostedMethod) method).getUniqueShortName();
295295
}
296296

297297
private static boolean isEntryPoint(ResolvedJavaMethod method) {

substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMNativeImageCodeCache.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import com.oracle.objectfile.ObjectFile.Element;
6464
import com.oracle.objectfile.SectionName;
6565
import com.oracle.svm.core.SubstrateOptions;
66-
import com.oracle.svm.core.SubstrateUtil;
6766
import com.oracle.svm.core.c.CGlobalDataImpl;
6867
import com.oracle.svm.core.graal.code.CGlobalDataInfo;
6968
import com.oracle.svm.core.graal.code.CGlobalDataReference;
@@ -224,7 +223,7 @@ private void linkCompiledBatches(BatchExecutor executor, DebugContext debug, int
224223

225224
executor.forEach(getOrderedCompilations(), pair -> (debugContext) -> {
226225
HostedMethod method = pair.getLeft();
227-
int offset = textSectionInfo.getOffset(SubstrateUtil.uniqueShortName(method));
226+
int offset = textSectionInfo.getOffset(method.getUniqueShortName());
228227
int nextFunctionStartOffset = textSectionInfo.getNextOffset(offset);
229228
int functionSize = nextFunctionStartOffset - offset;
230229

substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/util/LLVMObjectFileReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343

4444
import com.oracle.objectfile.ObjectFile;
4545
import com.oracle.objectfile.SectionName;
46-
import com.oracle.svm.core.SubstrateUtil;
4746
import com.oracle.svm.core.graal.llvm.LLVMGenerator;
4847
import com.oracle.svm.core.graal.llvm.LLVMNativeImageCodeCache.StackMapDumper;
4948
import com.oracle.svm.core.heap.SubstrateReferenceMap;
49+
import com.oracle.svm.hosted.meta.HostedMethod;
5050
import com.oracle.svm.shadowed.org.bytedeco.javacpp.BytePointer;
5151
import com.oracle.svm.shadowed.org.bytedeco.javacpp.Pointer;
5252
import com.oracle.svm.shadowed.org.bytedeco.llvm.LLVM.LLVMMemoryBufferRef;
@@ -161,7 +161,7 @@ private LLVMStackMapInfo readStackMapSection(LLVMSectionIteratorRef sectionItera
161161
}
162162

163163
public void readStackMap(LLVMStackMapInfo info, CompilationResult compilation, ResolvedJavaMethod method, int id) {
164-
String methodSymbolName = SYMBOL_PREFIX + SubstrateUtil.uniqueShortName(method);
164+
String methodSymbolName = SYMBOL_PREFIX + ((HostedMethod) method).getUniqueShortName();
165165

166166
long startPatchpointID = compilation.getInfopoints().stream().filter(ip -> ip.reason == InfopointReason.METHOD_START).findFirst()
167167
.orElseThrow(() -> new GraalError("no method start infopoint: " + methodSymbolName)).pcOffset;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateUtil.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
import jdk.vm.ci.meta.ResolvedJavaMethod;
6262
import jdk.vm.ci.meta.ResolvedJavaType;
63+
import jdk.vm.ci.meta.Signature;
6364
import jdk.vm.ci.services.Services;
6465

6566
public class SubstrateUtil {
@@ -278,21 +279,38 @@ public static String digest(String value) {
278279
* name includes a digest of the fully qualified method name, which ensures uniqueness.
279280
*/
280281
public static String uniqueShortName(ResolvedJavaMethod m) {
281-
StringBuilder fullName = new StringBuilder();
282-
fullName.append(m.getDeclaringClass().toClassName()).append(".").append(m.getName()).append("(");
283-
for (int i = 0; i < m.getSignature().getParameterCount(false); i++) {
284-
fullName.append(m.getSignature().getParameterType(i, null).toClassName()).append(",");
282+
return uniqueShortName("", m.getDeclaringClass(), m.getName(), m.getSignature(), m.isConstructor());
283+
}
284+
285+
public static String uniqueShortName(String loaderNameAndId, ResolvedJavaType declaringClass, String methodName, Signature methodSignature, boolean isConstructor) {
286+
StringBuilder sb = new StringBuilder(loaderNameAndId);
287+
sb.append(declaringClass.toClassName()).append(".").append(methodName).append("(");
288+
for (int i = 0; i < methodSignature.getParameterCount(false); i++) {
289+
sb.append(methodSignature.getParameterType(i, null).toClassName()).append(",");
285290
}
286-
fullName.append(')');
287-
if (!m.isConstructor()) {
288-
fullName.append(m.getSignature().getReturnType(null).toClassName());
291+
sb.append(')');
292+
if (!isConstructor) {
293+
sb.append(methodSignature.getReturnType(null).toClassName());
289294
}
290295

291-
return stripPackage(m.getDeclaringClass().toJavaName()) + "_" +
292-
(m.isConstructor() ? "constructor" : m.getName()) + "_" +
293-
SubstrateUtil.digest(fullName.toString());
296+
return stripPackage(declaringClass.toJavaName()) + "_" +
297+
(isConstructor ? "constructor" : methodName) + "_" +
298+
SubstrateUtil.digest(sb.toString());
294299
}
295300

301+
public static String classLoaderNameAndId(ClassLoader loader) {
302+
if (loader == null) {
303+
return "";
304+
}
305+
try {
306+
return (String) classLoaderNameAndId.get(loader);
307+
} catch (IllegalAccessException e) {
308+
throw VMError.shouldNotReachHere("Cannot reflectively access ClassLoader.nameAndId");
309+
}
310+
}
311+
312+
private static final Field classLoaderNameAndId = ReflectionUtil.lookupField(ClassLoader.class, "nameAndId");
313+
296314
/**
297315
* Returns a short, reasonably descriptive, but still unique name for the provided
298316
* {@link Method}, {@link Constructor}, or {@link Field}. The name includes a digest of the

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.lang.reflect.AnnotatedElement;
3232
import java.lang.reflect.Modifier;
3333
import java.util.Collections;
34+
import java.util.Comparator;
3435
import java.util.EnumSet;
3536
import java.util.HashMap;
3637
import java.util.HashSet;
@@ -65,7 +66,6 @@
6566
import org.graalvm.nativeimage.Platform;
6667
import org.graalvm.nativeimage.Platforms;
6768
import org.graalvm.nativeimage.c.function.RelocatedPointer;
68-
import com.oracle.svm.util.GuardedAnnotationAccess;
6969

7070
import com.oracle.graal.pointsto.BigBang;
7171
import com.oracle.graal.pointsto.api.HostVM;
@@ -109,10 +109,12 @@
109109
import com.oracle.svm.hosted.code.UninterruptibleAnnotationChecker;
110110
import com.oracle.svm.hosted.heap.PodSupport;
111111
import com.oracle.svm.hosted.meta.HostedType;
112+
import com.oracle.svm.hosted.meta.HostedUniverse;
112113
import com.oracle.svm.hosted.phases.AnalysisGraphBuilderPhase;
113114
import com.oracle.svm.hosted.phases.ImplicitAssertionsPhase;
114115
import com.oracle.svm.hosted.phases.InlineBeforeAnalysisPolicyImpl;
115116
import com.oracle.svm.hosted.substitute.UnsafeAutomaticSubstitutionProcessor;
117+
import com.oracle.svm.util.GuardedAnnotationAccess;
116118

117119
import jdk.vm.ci.meta.DeoptimizationReason;
118120
import jdk.vm.ci.meta.ResolvedJavaField;
@@ -743,4 +745,11 @@ public boolean neverInlineTrivial(AnalysisMethod caller, AnalysisMethod callee)
743745
}
744746
return false;
745747
}
748+
749+
@Override
750+
public Comparator<? super ResolvedJavaType> getTypeComparator() {
751+
return (Comparator<ResolvedJavaType>) (o1, o2) -> {
752+
return HostedUniverse.TYPE_COMPARATOR.compare((HostedType) o1, (HostedType) o2);
753+
};
754+
}
746755
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/CompileQueue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,6 @@ private void parseDeoptimizationTargetMethods() {
646646
universe.getMethods().stream()
647647
.filter(method -> method.getWrapped().isImplementationInvoked() && canDeoptForTesting(method))
648648
.forEach(this::ensureParsedForDeoptTesting);
649-
650649
}
651650

652651
private void ensureParsedForDeoptTesting(HostedMethod method) {
@@ -1675,7 +1674,7 @@ private static void insertDeoptTests(HostedMethod method, StructuredGraph graph)
16751674
}
16761675

16771676
public Map<HostedMethod, CompilationResult> getCompilationResults() {
1678-
Map<HostedMethod, CompilationResult> result = new TreeMap<>();
1677+
Map<HostedMethod, CompilationResult> result = new TreeMap<>(HostedUniverse.METHOD_COMPARATOR);
16791678
for (Entry<HostedMethod, CompileTask> entry : compilations.entrySet()) {
16801679
result.put(entry.getKey(), entry.getValue().result);
16811680
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedArrayClass.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,4 @@ public boolean isLocal() {
9292
public boolean isMember() {
9393
return false;
9494
}
95-
96-
@Override
97-
int compareToEqualClass(HostedType other) {
98-
assert getClass().equals(other.getClass());
99-
return getComponentType().compareTo(other.getComponentType());
100-
}
10195
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedField.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
/**
4242
* Store the compile-time information for a field in the Substrate VM, such as the field offset.
4343
*/
44-
public class HostedField implements OriginalFieldProvider, SharedField, Comparable<HostedField>, WrappedJavaField, AnnotationWrapper {
44+
public class HostedField implements OriginalFieldProvider, SharedField, WrappedJavaField, AnnotationWrapper {
4545

4646
private final HostedUniverse universe;
4747
private final HostedMetaAccess metaAccess;
@@ -198,20 +198,6 @@ public JavaKind getStorageKind() {
198198
return getType().getStorageKind();
199199
}
200200

201-
@Override
202-
public int compareTo(HostedField other) {
203-
/*
204-
* Order by JavaKind. This is required, since we want instance fields of the same size and
205-
* kind consecutive.
206-
*/
207-
int result = other.getJavaKind().ordinal() - this.getJavaKind().ordinal();
208-
/*
209-
* If the kind is the same, i.e., result == 0, we return 0 so that the sorting keeps the
210-
* order unchanged and therefore keeps the field order we get from the hosting VM.
211-
*/
212-
return result;
213-
}
214-
215201
@Override
216202
public Field getJavaField() {
217203
return OriginalFieldProvider.getJavaField(getDeclaringClass().universe.getSnippetReflection(), wrapped);

0 commit comments

Comments
 (0)