Skip to content

Commit

Permalink
[GR-54709] Introduce ApplicationLayerOnly and MultiLayer Image Single…
Browse files Browse the repository at this point in the history
…tons.

PullRequest: graal/18024
  • Loading branch information
teshull committed Jun 18, 2024
2 parents a527b70 + 7bb5681 commit 139f165
Show file tree
Hide file tree
Showing 28 changed files with 1,264 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public boolean isBackedByHostedObject() {
return constantData.hostedObject != null;
}

public static int getConstantID(ImageHeapConstant constant) {
return constant.getConstantData().id;
}

@Override
public JavaKind getJavaKind() {
return JavaKind.Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ private ObjectArrayData(AnalysisType type, JavaConstant hostedObject, Object[] a
super(new ObjectArrayData(type, hostedObject, null, length, identityHashCode), false);
}

ImageHeapObjectArray(AnalysisType type, JavaConstant hostedObject, Object[] arrayElementValues, int identityHashCode) {
super(new ObjectArrayData(type, hostedObject, arrayElementValues, arrayElementValues.length, identityHashCode), false);
}

ImageHeapObjectArray(AnalysisType type, int length) {
super(new ObjectArrayData(type, null, new Object[length], length, -1), false);
}
Expand All @@ -93,6 +97,10 @@ void setElementValues(Object[] elementValues) {
AnalysisError.guarantee(success, "Unexpected field values reference for constant %s", this);
}

public static ImageHeapObjectArray createUnbackedImageHeapArray(AnalysisType type, Object[] elementValues) {
return new ImageHeapObjectArray(type, null, elementValues, -1);
}

/**
* {@link ObjectArrayData#arrayElementValues} are only set once, in
* {@link #setElementValues(Object[])} and shouldn't be accessed before set, i.e., read access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ protected ImageHeapConstant getValueForObject(Object object) {
throw AnalysisError.shouldNotReachHere("The constant was not in the persisted heap.");
}

protected ImageHeapConstant getOrCreateConstant(int id) {
public ImageHeapConstant getOrCreateConstant(int id) {
return getOrCreateConstant(get(jsonMap, CONSTANTS_TAG), id, null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.graal.nodes;

import com.oracle.svm.core.heap.ReferenceAccess;
import com.oracle.svm.core.imagelayer.LoadImageSingletonFactory.LoadImageSingletonData;

import jdk.graal.compiler.core.common.memory.BarrierType;
import jdk.graal.compiler.core.common.memory.MemoryOrderMode;
import jdk.graal.compiler.core.common.type.AbstractObjectStamp;
import jdk.graal.compiler.core.common.type.Stamp;
import jdk.graal.compiler.core.common.type.StampFactory;
import jdk.graal.compiler.core.common.type.TypeReference;
import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.nodeinfo.NodeCycles;
import jdk.graal.compiler.nodeinfo.NodeInfo;
import jdk.graal.compiler.nodeinfo.NodeSize;
import jdk.graal.compiler.nodes.CompressionNode;
import jdk.graal.compiler.nodes.ConstantNode;
import jdk.graal.compiler.nodes.FixedWithNextNode;
import jdk.graal.compiler.nodes.NamedLocationIdentity;
import jdk.graal.compiler.nodes.NodeView;
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.memory.ReadNode;
import jdk.graal.compiler.nodes.memory.address.AddressNode;
import jdk.graal.compiler.nodes.memory.address.OffsetAddressNode;
import jdk.graal.compiler.nodes.spi.Canonicalizable;
import jdk.graal.compiler.nodes.spi.CanonicalizerTool;
import jdk.graal.compiler.nodes.spi.Lowerable;
import jdk.graal.compiler.nodes.spi.LoweringTool;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.MetaAccessProvider;

@NodeInfo(cycles = NodeCycles.CYCLES_4, size = NodeSize.SIZE_2)
public class LoadImageSingletonNode extends FixedWithNextNode implements Lowerable, Canonicalizable {
public static final NodeClass<LoadImageSingletonNode> TYPE = NodeClass.create(LoadImageSingletonNode.class);

private final LoadImageSingletonData singletonInfo;

protected LoadImageSingletonNode(LoadImageSingletonData singletonInfo, Stamp stamp) {
super(TYPE, stamp);
this.singletonInfo = singletonInfo;
}

public static LoadImageSingletonNode createLoadImageSingleton(LoadImageSingletonData singletonInfo, MetaAccessProvider metaAccess) {
return new LoadImageSingletonNode(singletonInfo, StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(singletonInfo.getLoadType()))));
}

@Override
public Node canonical(CanonicalizerTool tool) {
if (tool.allUsagesAvailable() && hasNoUsages()) {
// can remove this load if it is never used.
return null;
}

return this;
}

@Override
public void lower(LoweringTool tool) {
StructuredGraph graph = graph();
var singletonAccessInfo = singletonInfo.getAccessInfo();

/*
* Load the starting address of the singleton table.
*/

CGlobalDataLoadAddressNode baseAddress = graph.unique(new CGlobalDataLoadAddressNode(singletonAccessInfo.tableBase()));

/*
* Read from the appropriate offset of the singleton table.
*/

AddressNode address = graph.unique(new OffsetAddressNode(baseAddress, ConstantNode.forIntegerKind(JavaKind.Long, singletonAccessInfo.offset(), graph)));
var tableReadStamp = SubstrateNarrowOopStamp.compressed((AbstractObjectStamp) stamp(NodeView.DEFAULT), ReferenceAccess.singleton().getCompressEncoding());
ReadNode tableRead = graph.add(new ReadNode(address, NamedLocationIdentity.FINAL_LOCATION, tableReadStamp, BarrierType.NONE, MemoryOrderMode.PLAIN));

CompressionNode uncompress = SubstrateCompressionNode.uncompress(graph(), tableRead, ReferenceAccess.singleton().getCompressEncoding());

replaceAtUsages(uncompress);
graph.replaceFixed(this, tableRead);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
*/
package com.oracle.svm.core.imagelayer;

import java.util.EnumSet;

import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
import com.oracle.svm.core.layeredimagesingleton.UnsavedSingleton;

import jdk.graal.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

/**
* Support for tracking the image layer stage of this native-image build. When image layers are
Expand All @@ -58,8 +55,10 @@
* | (D) Application Layer |
* |------------------------|
* </pre>
*
* Note this is intentionally not a LayeredImageSingleton itself to prevent circular dependencies.
*/
public abstract class ImageLayerBuildingSupport implements UnsavedSingleton {
public abstract class ImageLayerBuildingSupport {
protected final boolean buildingImageLayer;
private final boolean buildingInitialLayer;
private final boolean buildingApplicationLayer;
Expand All @@ -74,6 +73,11 @@ private static ImageLayerBuildingSupport singleton() {
return ImageSingletons.lookup(ImageLayerBuildingSupport.class);
}

@Platforms(Platform.HOSTED_ONLY.class)
public static boolean lastImageBuild() {
return !buildingImageLayer() || buildingApplicationLayer();
}

@Fold
public static boolean buildingImageLayer() {
return singleton().buildingImageLayer;
Expand All @@ -98,9 +102,4 @@ public static boolean buildingExtensionLayer() {
public static boolean buildingSharedLayer() {
return singleton().buildingImageLayer && !singleton().buildingApplicationLayer;
}

@Override
public final EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
return LayeredImageSingletonBuilderFlags.ALL_ACCESS_ALLOW_FOLDING;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.imagelayer;

import java.util.function.BooleanSupplier;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

@Platforms(Platform.HOSTED_ONLY.class)
public class LastImageBuildPredicate implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
return ImageLayerBuildingSupport.lastImageBuild();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.imagelayer;

import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.graal.code.CGlobalDataInfo;
import com.oracle.svm.core.graal.nodes.LoadImageSingletonNode;

import jdk.vm.ci.meta.MetaAccessProvider;

public abstract class LoadImageSingletonFactory {

public record SingletonAccessInfo(CGlobalDataInfo tableBase, int offset) {

}

/**
* Provides compiler-relevant information about the value which will be loaded.
*/
public interface LoadImageSingletonData {

Class<?> getLoadType();

SingletonAccessInfo getAccessInfo();
}

protected abstract LoadImageSingletonData getApplicationLayerOnlyImageSingletonInfo(Class<?> clazz);

protected abstract LoadImageSingletonData getLayeredImageSingletonInfo(Class<?> clazz);

public static LoadImageSingletonNode loadApplicationOnlyImageSingleton(Class<?> clazz, MetaAccessProvider metaAccess) {
LoadImageSingletonData singletonInfo = ImageSingletons.lookup(LoadImageSingletonFactory.class).getApplicationLayerOnlyImageSingletonInfo(clazz);
return LoadImageSingletonNode.createLoadImageSingleton(singletonInfo, metaAccess);
}

public static LoadImageSingletonNode loadLayeredImageSingleton(Class<?> clazz, MetaAccessProvider metaAccess) {
LoadImageSingletonData singletonInfo = ImageSingletons.lookup(LoadImageSingletonFactory.class).getLayeredImageSingletonInfo(clazz);
return LoadImageSingletonNode.createLoadImageSingleton(singletonInfo, metaAccess);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.layeredimagesingleton;

public interface ApplicationLayerOnlyImageSingleton extends LayeredImageSingleton {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ public interface ImageSingletonLoader {

List<Integer> readIntList(String keyName);

long readLong(String keyName);

String readString(String keyName);

List<String> readStringList(String keyName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ public interface ImageSingletonWriter {

void writeIntList(String keyName, List<Integer> value);

void writeLong(String keyName, long value);

void writeString(String keyName, String value);

void writeStringList(String keyName, List<String> value);
}
Loading

0 comments on commit 139f165

Please sign in to comment.