Skip to content

Commit 229aea5

Browse files
committed
[GR-25050] Pre-initialize engine in TruffleBaseFeature.beforeAnalysis().
PullRequest: graal/10539
2 parents 716114e + 7bb0f46 commit 229aea5

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ public boolean getAsBoolean() {
147147
}
148148
}
149149

150-
/**
151-
* True in the first analysis run where contexts are pre-initialized.
152-
*/
153-
private boolean firstAnalysisRun;
154-
155150
// Checkstyle: stop
156151
private ClassLoader imageClassLoader;
157152
private AnalysisMetaAccess metaAccess;
@@ -328,8 +323,13 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
328323
LibraryFactory.class);
329324
config.registerSubtypeReachabilityHandler(TruffleBaseFeature::registerTruffleLibrariesAsInHeap,
330325
LibraryExport.class);
326+
}
331327

332-
firstAnalysisRun = true;
328+
public static void preInitializeEngine() {
329+
invokeStaticMethod("org.graalvm.polyglot.Engine$ImplHolder", "preInitializeEngine",
330+
Collections.emptyList());
331+
invokeStaticMethod("com.oracle.truffle.api.impl.ThreadLocalHandshake", "resetNativeImageState",
332+
Collections.emptyList());
333333
}
334334

335335
/**
@@ -349,15 +349,6 @@ private static void registerTruffleLibrariesAsInHeap(DuringAnalysisAccess access
349349
public void duringAnalysis(DuringAnalysisAccess access) {
350350
StaticObjectSupport.duringAnalysis(access);
351351

352-
if (firstAnalysisRun) {
353-
firstAnalysisRun = false;
354-
invokeStaticMethod("org.graalvm.polyglot.Engine$ImplHolder", "preInitializeEngine",
355-
Collections.emptyList());
356-
invokeStaticMethod("com.oracle.truffle.api.impl.ThreadLocalHandshake", "resetNativeImageState",
357-
Collections.emptyList());
358-
access.requireAnalysisIteration();
359-
}
360-
361352
for (Class<?> clazz : access.reachableSubtypes(com.oracle.truffle.api.nodes.Node.class)) {
362353
registerUnsafeAccess(access, clazz.asSubclass(com.oracle.truffle.api.nodes.Node.class));
363354

substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleFeature.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
363363
* don't expect it to be used except for profiling enabled check.
364364
*/
365365
TruffleBaseFeature.invokeStaticMethod("com.oracle.truffle.polyglot.PolyglotEngineImpl", "resetFallbackEngine", Collections.emptyList());
366+
TruffleBaseFeature.preInitializeEngine();
366367
}
367368

368369
static class TruffleParsingInlineInvokePlugin implements InlineInvokePlugin {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Args = -H:Features=com.oracle.truffle.api.test.polyglot.RegisterTestClassesForReflectionFeature \
22
-H:ReflectionConfigurationResources=com/oracle/truffle/api/test/polyglot/reflection.json \
3-
-H:DynamicProxyConfigurationResources=com/oracle/truffle/api/test/polyglot/proxys.json
3+
-H:DynamicProxyConfigurationResources=com/oracle/truffle/api/test/polyglot/proxys.json \
4+
-Dpolyglot.image-build-time.PreinitializeContexts=ContextPreintializationNativeImageLanguage

truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/ContextPreInitializationNativeImageTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,20 @@
6767
*
6868
* This could potentially be improved using some white-box API that allows to explicitly store and
6969
* restore the preinitialized context.
70+
*
71+
* This test needs
72+
* -Dpolyglot.image-build-time.PreinitializeContexts=ContextPreintializationNativeImageLanguage
73+
* provided via com.oracle.truffle.api.test/src/META-INF/native-image/native-image.properties.
74+
* Setting the property programmatically in a static initializer via
75+
* System.setProperty("polyglot.image-build-time.PreinitializeContexts", LANGUAGE) is not reliable
76+
* as its publishing depends on when the class is initialized. The property needs to be available
77+
* before com.oracle.truffle.polyglot.PolyglotContextImpl#preInitialize() is invoked, i.e., before
78+
* com.oracle.svm.truffle.TruffleBaseFeature#beforeAnalysis().
7079
*/
7180
public class ContextPreInitializationNativeImageTest {
7281

7382
static final String LANGUAGE = "ContextPreintializationNativeImageLanguage";
7483

75-
static {
76-
System.setProperty("polyglot.image-build-time.PreinitializeContexts", LANGUAGE);
77-
}
78-
7984
@BeforeClass
8085
public static void runWithWeakEncapsulationOnly() {
8186
TruffleTestAssumptions.assumeWeakEncapsulation();

0 commit comments

Comments
 (0)