use of com.oracle.svm.core.graal.word.SubstrateWordTypes in project graal by oracle.
the class IntrinsifyMethodHandlesInvocationPlugin method processInvokeWithMethodHandle.
@SuppressWarnings("try")
private boolean processInvokeWithMethodHandle(GraphBuilderContext b, Replacements replacements, ResolvedJavaMethod methodHandleMethod, ValueNode[] methodHandleArguments) {
/*
* When parsing for compilation, we must not intrinsify method handles that were not
* intrinsified during analysis. Otherwise new code that was not seen as reachable by the
* static analysis would be compiled.
*/
if (reason != ParsingReason.PointsToAnalysis && intrinsificationRegistry.get(b.getMethod(), b.bci()) != Boolean.TRUE) {
return false;
}
Plugins graphBuilderPlugins = new Plugins(parsingProviders.getReplacements().getGraphBuilderPlugins());
registerInvocationPlugins(graphBuilderPlugins.getInvocationPlugins(), replacements);
graphBuilderPlugins.prependParameterPlugin(new MethodHandlesParameterPlugin(methodHandleArguments));
graphBuilderPlugins.clearInlineInvokePlugins();
graphBuilderPlugins.prependInlineInvokePlugin(new MethodHandlesInlineInvokePlugin());
graphBuilderPlugins.prependNodePlugin(new MethodHandlePlugin(parsingProviders.getConstantReflection().getMethodHandleAccess(), false));
/* We do all the word type rewriting because parameters to the lambda can be word types. */
SnippetReflectionProvider originalSnippetReflection = GraalAccess.getOriginalSnippetReflection();
WordOperationPlugin wordOperationPlugin = new WordOperationPlugin(originalSnippetReflection, new SubstrateWordTypes(parsingProviders.getMetaAccess(), FrameAccess.getWordKind()));
graphBuilderPlugins.appendInlineInvokePlugin(wordOperationPlugin);
graphBuilderPlugins.appendTypePlugin(wordOperationPlugin);
graphBuilderPlugins.appendTypePlugin(new TrustedInterfaceTypePlugin());
graphBuilderPlugins.appendNodePlugin(wordOperationPlugin);
graphBuilderPlugins.setClassInitializationPlugin(new NoClassInitializationPlugin());
GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getSnippetDefault(graphBuilderPlugins);
GraphBuilderPhase.Instance graphBuilder = new GraphBuilderPhase.Instance(parsingProviders, graphBuilderConfig, OptimisticOptimizations.NONE, null);
DebugContext debug = b.getDebug();
StructuredGraph graph = new StructuredGraph.Builder(b.getOptions(), debug).method(NativeImageUtil.toOriginal(methodHandleMethod)).build();
try (DebugContext.Scope s = debug.scope("IntrinsifyMethodHandles", graph)) {
graphBuilder.apply(graph);
/*
* The canonicalizer converts unsafe field accesses for get/set method handles back to
* high-level field load and store nodes.
*/
CanonicalizerPhase.create().apply(graph, parsingProviders);
debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Intrinisfication graph before transplant");
NodeMap<Node> transplanted = new NodeMap<>(graph);
for (ParameterNode oParam : graph.getNodes(ParameterNode.TYPE)) {
transplanted.put(oParam, methodHandleArguments[oParam.index()]);
}
Transplanter transplanter = new Transplanter(b, transplanted);
try {
transplanter.graph(graph);
if (reason == ParsingReason.PointsToAnalysis) {
/*
* Successfully intrinsified during analysis, remember that we can intrinsify
* when parsing for compilation.
*/
intrinsificationRegistry.add(b.getMethod(), b.bci(), Boolean.TRUE);
}
return true;
} catch (AbortTransplantException ex) {
/*
* The method handle cannot be intrinsified. If non-constant method handles are not
* supported, the code that throws an error at runtime was already appended, so
* nothing more to do. If non-constant method handles are supported, we return false
* so that the bytecode parser emit a regular invoke bytecode, i.e., the constant
* method handle is treated as if it were non-constant.
*/
return ex.handled;
}
} catch (Throwable ex) {
throw debug.handle(ex);
}
}
use of com.oracle.svm.core.graal.word.SubstrateWordTypes in project graal by oracle.
the class NativeImageGenerator method setupNativeImage.
@SuppressWarnings("try")
private void setupNativeImage(String imageName, OptionValues options, Map<Method, CEntryPointData> entryPoints, JavaMainSupport javaMainSupport, SubstitutionProcessor harnessSubstitutions, ForkJoinPool analysisExecutor, SnippetReflectionProvider originalSnippetReflection, DebugContext debug) {
try (Indent ignored = debug.logAndIndent("setup native-image builder")) {
try (StopTimer ignored1 = TimerCollection.createTimerAndStart(TimerCollection.Registry.SETUP)) {
SubstrateTargetDescription target = createTarget(loader.platform);
ImageSingletons.add(Platform.class, loader.platform);
ImageSingletons.add(SubstrateTargetDescription.class, target);
ImageSingletons.add(SubstrateOptions.ReportingSupport.class, new SubstrateOptions.ReportingSupport(DiagnosticsMode.getValue() ? DiagnosticsDir.getValue() : Paths.get("reports").toString()));
if (javaMainSupport != null) {
ImageSingletons.add(JavaMainSupport.class, javaMainSupport);
}
Providers originalProviders = GraalAccess.getOriginalProviders();
MetaAccessProvider originalMetaAccess = originalProviders.getMetaAccess();
ClassInitializationSupport classInitializationSupport = new ConfigurableClassInitialization(originalMetaAccess, loader);
ImageSingletons.add(RuntimeClassInitializationSupport.class, classInitializationSupport);
ClassInitializationFeature.processClassInitializationOptions(classInitializationSupport);
if (ImageBuildStatistics.Options.CollectImageBuildStatistics.getValue(options)) {
ImageSingletons.add(ImageBuildStatistics.class, new ImageBuildStatistics());
}
if (SubstrateOptions.useEconomyCompilerConfig()) {
HostedConfiguration.setInstanceIfEmpty(new EconomyHostedConfiguration());
GraalConfiguration.setHostedInstanceIfEmpty(new EconomyGraalConfiguration());
}
/* Init the BuildPhaseProvider before any features need it. */
BuildPhaseProvider.init();
featureHandler.registerFeatures(loader, debug);
AfterRegistrationAccessImpl access = new AfterRegistrationAccessImpl(featureHandler, loader, originalMetaAccess, mainEntryPoint, debug);
featureHandler.forEachFeature(feature -> feature.afterRegistration(access));
setDefaultLibCIfMissing();
if (!Pair.<Method, CEntryPointData>empty().equals(access.getMainEntryPoint())) {
setAndVerifyMainEntryPoint(access, entryPoints);
}
registerEntryPoints(entryPoints);
/*
* Check if any configuration factory class was registered. If not, register the
* basic one.
*/
HostedConfiguration.setDefaultIfEmpty();
GraalConfiguration.setDefaultIfEmpty();
AnnotationSubstitutionProcessor annotationSubstitutions = createAnnotationSubstitutionProcessor(originalMetaAccess, loader, classInitializationSupport);
CEnumCallWrapperSubstitutionProcessor cEnumProcessor = new CEnumCallWrapperSubstitutionProcessor();
aUniverse = createAnalysisUniverse(options, target, loader, originalMetaAccess, originalSnippetReflection, annotationSubstitutions, cEnumProcessor, classInitializationSupport, Collections.singletonList(harnessSubstitutions));
AnalysisMetaAccess aMetaAccess = new SVMAnalysisMetaAccess(aUniverse, originalMetaAccess);
AnalysisConstantReflectionProvider aConstantReflection = new AnalysisConstantReflectionProvider(aUniverse, aMetaAccess, originalProviders.getConstantReflection(), classInitializationSupport);
WordTypes aWordTypes = new SubstrateWordTypes(aMetaAccess, FrameAccess.getWordKind());
HostedSnippetReflectionProvider aSnippetReflection = new HostedSnippetReflectionProvider(aWordTypes);
ForeignCallsProvider aForeignCalls = new SubstrateForeignCallsProvider(aMetaAccess, null);
bb = createBigBang(options, target, aUniverse, analysisExecutor, watchdog::recordActivity, aMetaAccess, aConstantReflection, aWordTypes, aSnippetReflection, annotationSubstitutions, aForeignCalls, classInitializationSupport, originalProviders);
aUniverse.setBigBang(bb);
/* Create the HeapScanner and install it into the universe. */
ImageHeap imageHeap = new ImageHeap();
AnalysisObjectScanningObserver aScanningObserver = new AnalysisObjectScanningObserver(bb);
ImageHeapScanner heapScanner = new SVMImageHeapScanner(imageHeap, loader, aMetaAccess, aSnippetReflection, aConstantReflection, aScanningObserver);
aUniverse.setHeapScanner(heapScanner);
HeapSnapshotVerifier heapVerifier = new SVMImageHeapVerifier(bb, imageHeap, heapScanner);
aUniverse.setHeapVerifier(heapVerifier);
/* Register already created types as assignable. */
aUniverse.getTypes().forEach(t -> {
t.registerAsAssignable(bb);
if (t.isReachable()) {
bb.onTypeInitialized(t);
}
});
boolean withoutCompilerInvoker = CAnnotationProcessorCache.Options.ExitAfterQueryCodeGeneration.getValue() || (NativeImageOptions.ExitAfterRelocatableImageWrite.getValue() && CAnnotationProcessorCache.Options.UseCAPCache.getValue());
if (!withoutCompilerInvoker) {
CCompilerInvoker compilerInvoker = CCompilerInvoker.create(ImageSingletons.lookup(TemporaryBuildDirectoryProvider.class).getTemporaryBuildDirectory());
compilerInvoker.verifyCompiler();
ImageSingletons.add(CCompilerInvoker.class, compilerInvoker);
}
nativeLibraries = setupNativeLibraries(imageName, aConstantReflection, aMetaAccess, aSnippetReflection, cEnumProcessor, classInitializationSupport, debug);
try (Indent ignored2 = debug.logAndIndent("process startup initializers")) {
FeatureImpl.DuringSetupAccessImpl config = new FeatureImpl.DuringSetupAccessImpl(featureHandler, loader, bb, debug);
featureHandler.forEachFeature(feature -> feature.duringSetup(config));
}
initializeBigBang(bb, options, featureHandler, nativeLibraries, debug, aMetaAccess, aUniverse.getSubstitutions(), loader, true, new SubstrateClassInitializationPlugin((SVMHost) aUniverse.hostVM()));
entryPoints.forEach((method, entryPointData) -> CEntryPointCallStubSupport.singleton().registerStubForMethod(method, () -> entryPointData));
}
ProgressReporter.singleton().printInitializeEnd(nativeLibraries.getLibraries());
}
}
use of com.oracle.svm.core.graal.word.SubstrateWordTypes in project graal by oracle.
the class NativeImageGenerator method createAnalysisUniverse.
public static AnalysisUniverse createAnalysisUniverse(OptionValues options, TargetDescription target, ImageClassLoader loader, MetaAccessProvider originalMetaAccess, SnippetReflectionProvider originalSnippetReflection, AnnotationSubstitutionProcessor annotationSubstitutions, SubstitutionProcessor cEnumProcessor, ClassInitializationSupport classInitializationSupport, List<SubstitutionProcessor> additionalSubstitutions) {
UnsafeAutomaticSubstitutionProcessor automaticSubstitutions = createAutomaticUnsafeSubstitutions(originalSnippetReflection, annotationSubstitutions);
SubstitutionProcessor aSubstitutions = createAnalysisSubstitutionProcessor(originalMetaAccess, originalSnippetReflection, cEnumProcessor, automaticSubstitutions, annotationSubstitutions, additionalSubstitutions);
SVMHost hostVM = HostedConfiguration.instance().createHostVM(options, loader.getClassLoader(), classInitializationSupport, automaticSubstitutions, loader.platform, originalSnippetReflection);
automaticSubstitutions.init(loader, originalMetaAccess);
AnalysisPolicy analysisPolicy = PointstoOptions.AllocationSiteSensitiveHeap.getValue(options) ? new BytecodeSensitiveAnalysisPolicy(options) : new DefaultAnalysisPolicy(options);
return new AnalysisUniverse(hostVM, target.wordJavaKind, analysisPolicy, aSubstitutions, originalMetaAccess, originalSnippetReflection, new SubstrateSnippetReflectionProvider(new SubstrateWordTypes(originalMetaAccess, FrameAccess.getWordKind())), new PointsToAnalysisFactory());
}
use of com.oracle.svm.core.graal.word.SubstrateWordTypes in project graal by oracle.
the class NativeImageGenerator method registerGraphBuilderPlugins.
public static void registerGraphBuilderPlugins(FeatureHandler featureHandler, RuntimeConfiguration runtimeConfig, HostedProviders providers, AnalysisMetaAccess aMetaAccess, AnalysisUniverse aUniverse, HostedMetaAccess hMetaAccess, HostedUniverse hUniverse, NativeLibraries nativeLibs, ImageClassLoader loader, ParsingReason reason, AnnotationSubstitutionProcessor annotationSubstitutionProcessor, ClassInitializationPlugin classInitializationPlugin, ClassInitializationSupport classInitializationSupport, TargetDescription target) {
GraphBuilderConfiguration.Plugins plugins = new GraphBuilderConfiguration.Plugins(new SubstitutionInvocationPlugins(annotationSubstitutionProcessor));
WordOperationPlugin wordOperationPlugin = new SubstrateWordOperationPlugins(providers.getSnippetReflection(), providers.getWordTypes());
SubstrateReplacements replacements = (SubstrateReplacements) providers.getReplacements();
plugins.appendInlineInvokePlugin(replacements);
plugins.appendNodePlugin(new IntrinsifyMethodHandlesInvocationPlugin(reason, providers, aUniverse, hUniverse));
plugins.appendNodePlugin(new DeletedFieldsPlugin());
plugins.appendNodePlugin(new InjectedAccessorsPlugin());
ResolvedJavaType resolvedJavaType = providers.getMetaAccess().lookupJavaType(ClassInitializationTracking.class);
if (resolvedJavaType instanceof AnalysisType) {
((AnalysisType) resolvedJavaType).registerAsReachable();
ResolvedJavaField field = providers.getMetaAccess().lookupJavaField(ReflectionUtil.lookupField(ClassInitializationTracking.class, "IS_IMAGE_BUILD_TIME"));
((AnalysisField) field).registerAsAccessed();
}
plugins.appendNodePlugin(new EarlyConstantFoldLoadFieldPlugin(providers.getMetaAccess(), providers.getSnippetReflection()));
plugins.appendNodePlugin(new ConstantFoldLoadFieldPlugin(classInitializationSupport));
plugins.appendNodePlugin(new CInterfaceInvocationPlugin(providers.getMetaAccess(), providers.getWordTypes(), nativeLibs));
plugins.appendNodePlugin(new LocalizationFeature.CharsetNodePlugin());
plugins.appendInlineInvokePlugin(wordOperationPlugin);
plugins.appendTypePlugin(wordOperationPlugin);
plugins.appendTypePlugin(new TrustedInterfaceTypePlugin());
plugins.appendNodePlugin(wordOperationPlugin);
plugins.setClassInitializationPlugin(classInitializationPlugin);
featureHandler.forEachGraalFeature(feature -> feature.registerGraphBuilderPlugins(providers, plugins, reason));
HostedSnippetReflectionProvider hostedSnippetReflection = new HostedSnippetReflectionProvider(new SubstrateWordTypes(aMetaAccess, FrameAccess.getWordKind()));
HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) HotSpotJVMCIRuntime.runtime().getCompiler();
NodeIntrinsificationProvider nodeIntrinsificationProvider;
if (!SubstrateUtil.isBuildingLibgraal()) {
nodeIntrinsificationProvider = new NodeIntrinsificationProvider(providers.getMetaAccess(), hostedSnippetReflection, providers.getForeignCalls(), providers.getWordTypes(), target);
} else {
nodeIntrinsificationProvider = new NodeIntrinsificationProvider(providers.getMetaAccess(), hostedSnippetReflection, providers.getForeignCalls(), providers.getWordTypes(), target) {
@Override
public <T> T getInjectedArgument(Class<T> type) {
if (type.isAssignableFrom(GraalHotSpotVMConfig.class)) {
return type.cast(compiler.getGraalRuntime().getVMConfig());
}
if (type.isAssignableFrom(HotSpotGraalRuntimeProvider.class)) {
return type.cast(compiler.getGraalRuntime());
}
return super.getInjectedArgument(type);
}
};
}
for (Class<? extends GeneratedPluginFactory> factoryClass : loader.findSubclasses(GeneratedPluginFactory.class, true)) {
if (!Modifier.isAbstract(factoryClass.getModifiers()) && !factoryClass.getName().contains("hotspot")) {
GeneratedPluginFactory factory;
try {
factory = factoryClass.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw VMError.shouldNotReachHere(ex);
}
factory.registerPlugins(plugins.getInvocationPlugins(), nodeIntrinsificationProvider);
}
}
final boolean arrayEqualsSubstitution = !SubstrateOptions.useLLVMBackend();
registerInvocationPlugins(providers.getSnippetReflection(), plugins.getInvocationPlugins(), replacements, reason == ParsingReason.JITCompilation, true, arrayEqualsSubstitution, providers.getLowerer());
Architecture architecture = ConfigurationValues.getTarget().arch;
OptionValues options = aUniverse.hostVM().options();
ImageSingletons.lookup(TargetGraphBuilderPlugins.class).register(plugins, replacements, architecture, /* registerForeignCallMath */
false, options);
/*
* When the context is hosted, i.e., ahead-of-time compilation, and after the analysis we
* need the hosted meta access.
*/
MetaAccessProvider pluginsMetaAccess;
if (reason == ParsingReason.PointsToAnalysis || reason == ParsingReason.JITCompilation) {
pluginsMetaAccess = aMetaAccess;
} else {
VMError.guarantee(reason == ParsingReason.AOTCompilation);
pluginsMetaAccess = hMetaAccess;
}
assert pluginsMetaAccess != null;
SubstrateGraphBuilderPlugins.registerInvocationPlugins(annotationSubstitutionProcessor, pluginsMetaAccess, hostedSnippetReflection, plugins.getInvocationPlugins(), replacements, reason);
featureHandler.forEachGraalFeature(feature -> feature.registerInvocationPlugins(providers, hostedSnippetReflection, plugins, reason));
providers.setGraphBuilderPlugins(plugins);
replacements.setGraphBuilderPlugins(plugins);
if (runtimeConfig != null && runtimeConfig.getProviders() instanceof HostedProviders) {
((HostedProviders) runtimeConfig.getProviders()).setGraphBuilderPlugins(plugins);
for (SubstrateBackend backend : runtimeConfig.getBackends()) {
((HostedProviders) backend.getProviders()).setGraphBuilderPlugins(plugins);
}
}
}
use of com.oracle.svm.core.graal.word.SubstrateWordTypes in project graal by oracle.
the class SharedRuntimeConfigurationBuilder method build.
public SharedRuntimeConfigurationBuilder build() {
EnumMap<ConfigKind, RegisterConfig> registerConfigs = new EnumMap<>(ConfigKind.class);
for (ConfigKind config : ConfigKind.values()) {
registerConfigs.put(config, ImageSingletons.lookup(SubstrateRegisterConfigFactory.class).newRegisterFactory(config, metaAccess, ConfigurationValues.getTarget(), SubstrateOptions.PreserveFramePointer.getValue()));
}
wordTypes = new SubstrateWordTypes(metaAccess, FrameAccess.getWordKind());
Providers p = createProviders(null, null, null, null, null, null, null, null, null, null, null);
StampProvider stampProvider = createStampProvider(p);
p = createProviders(null, null, null, null, null, null, stampProvider, null, null, null, null);
ConstantReflectionProvider constantReflection = createConstantReflectionProvider(p);
p = createProviders(null, constantReflection, null, null, null, null, stampProvider, null, null, null, null);
ConstantFieldProvider constantFieldProvider = createConstantFieldProvider(p);
SnippetReflectionProvider snippetReflection = createSnippetReflectionProvider();
ForeignCallsProvider foreignCalls = createForeignCallsProvider(registerConfigs.get(ConfigKind.NORMAL));
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, null, null, stampProvider, snippetReflection, null, null, null);
BarrierSet barrierSet = ImageSingletons.lookup(Heap.class).createBarrierSet(metaAccess);
PlatformConfigurationProvider platformConfig = new SubstratePlatformConfigurationProvider(barrierSet);
MetaAccessExtensionProvider metaAccessExtensionProvider = new SubstrateMetaAccessExtensionProvider();
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, null, null, stampProvider, snippetReflection, platformConfig, metaAccessExtensionProvider, null);
LoweringProvider lowerer = createLoweringProvider(p);
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, lowerer, null, stampProvider, snippetReflection, platformConfig, metaAccessExtensionProvider, null);
Replacements replacements = createReplacements(p, snippetReflection);
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, stampProvider, snippetReflection, platformConfig, metaAccessExtensionProvider, null);
LoopsDataProvider loopsDataProvider = originalLoopsDataProvider;
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, stampProvider, snippetReflection, platformConfig, metaAccessExtensionProvider, loopsDataProvider);
EnumMap<ConfigKind, SubstrateBackend> backends = new EnumMap<>(ConfigKind.class);
for (ConfigKind config : ConfigKind.values()) {
CodeCacheProvider codeCacheProvider = createCodeCacheProvider(registerConfigs.get(config));
Providers newProviders = createProviders(codeCacheProvider, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, stampProvider, snippetReflection, platformConfig, metaAccessExtensionProvider, loopsDataProvider);
backends.put(config, GraalConfiguration.runtimeInstance().createBackend(newProviders));
}
runtimeConfig = new RuntimeConfiguration(p, snippetReflection, backends, wordTypes);
return this;
}
Aggregations