Skip to content

Commit

Permalink
Use inlined branch and condition profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Dec 6, 2023
1 parent 34709ea commit ec63041
Show file tree
Hide file tree
Showing 37 changed files with 596 additions and 412 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.oracle.truffle.api.library.Message;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
import com.oracle.truffle.api.source.Source;

import de.hpi.swa.trufflesqueak.SqueakImage;
Expand Down Expand Up @@ -920,8 +920,8 @@ public NativeObject asWideString(final String value) {
return NativeObject.newNativeInts(this, getWideStringClass(), MiscUtils.stringToCodePointsArray(value));
}

public NativeObject asString(final String value, final ConditionProfile wideStringProfile) {
return wideStringProfile.profile(NativeObject.needsWideString(value)) ? asWideString(value) : asByteString(value);
public NativeObject asString(final String value, final InlinedConditionProfile wideStringProfile, final Node node) {
return wideStringProfile.profile(node, NativeObject.needsWideString(value)) ? asWideString(value) : asByteString(value);
}

public PointersObject asPoint(final AbstractPointersObjectWriteNode writeNode, final Object xPos, final Object yPos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
package de.hpi.swa.trufflesqueak.interop;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.GenerateUncached;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
import com.oracle.truffle.api.strings.TruffleString;

import de.hpi.swa.trufflesqueak.model.ArrayObject;
Expand Down Expand Up @@ -83,15 +85,17 @@ protected static final Object doDouble(final double value,

@Specialization
protected final NativeObject doString(final String value,
@Shared("wideStringProfile") @Cached final ConditionProfile wideStringProfile) {
return getContext().asString(value, wideStringProfile);
@Bind("this") final Node node,
@Shared("wideStringProfile") @Cached final InlinedConditionProfile wideStringProfile) {
return getContext().asString(value, wideStringProfile, node);
}

@Specialization
protected final NativeObject doTruffleString(final TruffleString value,
@Bind("this") final Node node,
@Cached final TruffleString.ToJavaStringNode toJavaString,
@Shared("wideStringProfile") @Cached final ConditionProfile wideStringProfile) {
return doString(toJavaString.execute(value), wideStringProfile);
@Shared("wideStringProfile") @Cached final InlinedConditionProfile wideStringProfile) {
return doString(toJavaString.execute(value), node, wideStringProfile);
}

@Specialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedBranchProfile;

import de.hpi.swa.trufflesqueak.exceptions.SqueakExceptions;
import de.hpi.swa.trufflesqueak.image.SqueakImageChunk;
Expand Down Expand Up @@ -104,13 +105,13 @@ public final boolean hasFormatOf(final ClassObject other) {

@Override
public final long getOrCreateSqueakHash() {
return getOrCreateSqueakHash(BranchProfile.getUncached());
return getOrCreateSqueakHash(InlinedBranchProfile.getUncached(), null);
}

public final long getOrCreateSqueakHash(final BranchProfile needsHashProfile) {
public final long getOrCreateSqueakHash(final InlinedBranchProfile needsHashProfile, final Node node) {
if (needsSqueakHash()) {
/** Lazily initialize squeakHash and derive value from hashCode. */
needsHashProfile.enter();
needsHashProfile.enter(node);
setSqueakHash(System.identityHashCode(this) & SQUEAK_HASH_MASK);
}
return getSqueakHash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package de.hpi.swa.trufflesqueak.model;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;

import de.hpi.swa.trufflesqueak.image.SqueakImageChunk;
import de.hpi.swa.trufflesqueak.image.SqueakImageConstants;
Expand Down Expand Up @@ -246,56 +247,56 @@ public void setStorage(final Object newStorage) {
storage = newStorage;
}

public static Object toObjectFromBoolean(final byte value, final BranchProfile isNilTagProfile) {
public static Object toObjectFromBoolean(final byte value, final InlinedBranchProfile isNilTagProfile, final Node node) {
if (value == BOOLEAN_FALSE_TAG) {
return BooleanObject.FALSE;
} else if (value == BOOLEAN_TRUE_TAG) {
return BooleanObject.TRUE;
} else {
isNilTagProfile.enter();
isNilTagProfile.enter(node);
assert value == BOOLEAN_NIL_TAG;
return NilObject.SINGLETON;
}
}

public static Object toObjectFromChar(final char value, final ConditionProfile isNilTagProfile) {
return isNilTagProfile.profile(isCharNilTag(value)) ? NilObject.SINGLETON : value;
public static Object toObjectFromChar(final char value, final InlinedConditionProfile isNilTagProfile, final Node node) {
return isNilTagProfile.profile(node, isCharNilTag(value)) ? NilObject.SINGLETON : value;
}

public static Object toObjectFromLong(final long value, final ConditionProfile isNilTagProfile) {
return isNilTagProfile.profile(isLongNilTag(value)) ? NilObject.SINGLETON : value;
public static Object toObjectFromLong(final long value, final InlinedConditionProfile isNilTagProfile, final Node node) {
return isNilTagProfile.profile(node, isLongNilTag(value)) ? NilObject.SINGLETON : value;
}

public static Object toObjectFromDouble(final double value, final ConditionProfile isNilTagProfile) {
return isNilTagProfile.profile(isDoubleNilTag(value)) ? NilObject.SINGLETON : value;
public static Object toObjectFromDouble(final double value, final InlinedConditionProfile isNilTagProfile, final Node node) {
return isNilTagProfile.profile(node, isDoubleNilTag(value)) ? NilObject.SINGLETON : value;
}

public void transitionFromBooleansToObjects(final BranchProfile isNilTagProfile) {
public void transitionFromBooleansToObjects(final InlinedBranchProfile isNilTagProfile, final Node node) {
LogUtils.ARRAY_STATEGIES.finer("transition from Booleans to Objects");
final byte[] booleans = getBooleanStorage();
final Object[] objects = new Object[booleans.length];
for (int i = 0; i < booleans.length; i++) {
objects[i] = toObjectFromBoolean(booleans[i], isNilTagProfile);
objects[i] = toObjectFromBoolean(booleans[i], isNilTagProfile, node);
}
storage = objects;
}

public void transitionFromCharsToObjects(final ConditionProfile isNilTagProfile) {
public void transitionFromCharsToObjects(final InlinedConditionProfile isNilTagProfile, final Node node) {
LogUtils.ARRAY_STATEGIES.finer("transition from Chars to Objects");
final char[] chars = getCharStorage();
final Object[] objects = new Object[chars.length];
for (int i = 0; i < chars.length; i++) {
objects[i] = toObjectFromChar(chars[i], isNilTagProfile);
objects[i] = toObjectFromChar(chars[i], isNilTagProfile, node);
}
storage = objects;
}

public void transitionFromDoublesToObjects(final ConditionProfile isNilTagProfile) {
public void transitionFromDoublesToObjects(final InlinedConditionProfile isNilTagProfile, final Node node) {
LogUtils.ARRAY_STATEGIES.finer("transition from Doubles to Objects");
final double[] doubles = getDoubleStorage();
final Object[] objects = new Object[doubles.length];
for (int i = 0; i < doubles.length; i++) {
objects[i] = toObjectFromDouble(doubles[i], isNilTagProfile);
objects[i] = toObjectFromDouble(doubles[i], isNilTagProfile, node);
}
storage = objects;
}
Expand Down Expand Up @@ -327,12 +328,12 @@ public void transitionFromEmptyToObjects() {
storage = ArrayUtils.withAll(getEmptyLength(), NilObject.SINGLETON);
}

public void transitionFromLongsToObjects(final ConditionProfile isNilTagProfile) {
public void transitionFromLongsToObjects(final InlinedConditionProfile isNilTagProfile, final Node node) {
LogUtils.ARRAY_STATEGIES.finer("transition from Longs to Objects");
final long[] longs = getLongStorage();
final Object[] objects = new Object[longs.length];
for (int i = 0; i < longs.length; i++) {
objects[i] = toObjectFromLong(longs[i], isNilTagProfile);
objects[i] = toObjectFromLong(longs[i], isNilTagProfile, node);
}
storage = objects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
package de.hpi.swa.trufflesqueak.model;

import com.oracle.truffle.api.CompilerDirectives.ValueType;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;

import de.hpi.swa.trufflesqueak.exceptions.RespecializeException;

Expand Down Expand Up @@ -51,8 +52,8 @@ public static char valueExactOf(final long value) throws RespecializeException {
}
}

public static Object valueOf(final long value, final ConditionProfile isImmediateProfile) {
if (isImmediateProfile.profile(value <= Character.MAX_VALUE)) {
public static Object valueOf(final long value, final InlinedConditionProfile isImmediateProfile, final Node node) {
if (isImmediateProfile.profile(node, value <= Character.MAX_VALUE)) {
return (char) value;
} else {
return new CharacterObject(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.FrameInstance;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;

import de.hpi.swa.trufflesqueak.exceptions.ProcessSwitch;
import de.hpi.swa.trufflesqueak.exceptions.SqueakExceptions.SqueakException;
Expand Down Expand Up @@ -279,9 +280,9 @@ public void removeSender() {
FrameAccess.setSender(getOrCreateTruffleFrame(), NilObject.SINGLETON);
}

public Object getInstructionPointer(final ConditionProfile nilProfile) {
public Object getInstructionPointer(final InlinedConditionProfile nilProfile, final Node node) {
final int pc = FrameAccess.getInstructionPointer(getTruffleFrame());
if (nilProfile.profile(pc == NIL_PC_VALUE)) {
if (nilProfile.profile(node, pc == NIL_PC_VALUE)) {
return NilObject.SINGLETON;
} else {
return (long) pc; // Must be a long.
Expand Down Expand Up @@ -552,7 +553,8 @@ public boolean pointsTo(final Object thang) {
// TODO: make sure this works correctly
if (truffleFrame != null) {
final int stackPointer = getStackPointer();
if (getSender() == thang || thang.equals(getInstructionPointer(ConditionProfile.getUncached())) || thang.equals(stackPointer) || getCodeObject() == thang || getClosure() == thang ||
if (getSender() == thang || thang.equals(getInstructionPointer(InlinedConditionProfile.getUncached(), null)) || thang.equals(stackPointer) || getCodeObject() == thang ||
getClosure() == thang ||
getReceiver() == thang) {
return true;
}
Expand Down Expand Up @@ -646,7 +648,7 @@ public void write(final SqueakImageWriter writer) {
throw SqueakException.create("ContextObject must have slots:", this);
}
writer.writeObject(getSender());
writer.writeObject(getInstructionPointer(ConditionProfile.getUncached()));
writer.writeObject(getInstructionPointer(InlinedConditionProfile.getUncached(), null));
writer.writeSmallInteger(getStackPointer());
writer.writeObject(getCodeObject());
writer.writeObject(NilObject.nullToNil(getClosure()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/
package de.hpi.swa.trufflesqueak.model;

import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;

import de.hpi.swa.trufflesqueak.image.SqueakImageConstants;
import de.hpi.swa.trufflesqueak.image.SqueakImageWriter;
Expand All @@ -22,8 +23,8 @@ public static AbstractSqueakObject nullToNil(final AbstractSqueakObject object)
return object == null ? SINGLETON : object;
}

public static AbstractSqueakObject nullToNil(final AbstractSqueakObject object, final ConditionProfile profile) {
return profile.profile(object == null) ? SINGLETON : object;
public static AbstractSqueakObject nullToNil(final AbstractSqueakObject object, final InlinedConditionProfile profile, final Node node) {
return profile.profile(node, object == null) ? SINGLETON : object;
}

public static Object nullToNil(final Object object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;

import de.hpi.swa.trufflesqueak.image.SqueakImageContext;
import de.hpi.swa.trufflesqueak.image.SqueakImageWriter;
Expand Down Expand Up @@ -66,9 +67,9 @@ public Object getFromVariablePart(final long index) {
}
}

public Object getFromVariablePart(final long index, final ConditionProfile weakRefProfile) {
public Object getFromVariablePart(final long index, final InlinedConditionProfile weakRefProfile, final Node node) {
final Object value = super.getFromVariablePart(index);
if (weakRefProfile.profile(value instanceof WeakRef)) {
if (weakRefProfile.profile(node, value instanceof WeakRef)) {
return NilObject.nullToNil(((WeakRef) value).get());
} else {
assert value != null;
Expand All @@ -78,11 +79,11 @@ public Object getFromVariablePart(final long index, final ConditionProfile weakR

@Override
public void putIntoVariablePart(final long index, final Object value) {
putIntoVariablePart(index, value, ConditionProfile.getUncached());
putIntoVariablePart(index, value, InlinedConditionProfile.getUncached(), null);
}

public void putIntoVariablePart(final long index, final Object value, final ConditionProfile profile) {
super.putIntoVariablePart(index, profile.profile(value instanceof AbstractSqueakObject) ? new WeakRef((AbstractSqueakObject) value, weakPointersQueue) : value);
public void putIntoVariablePart(final long index, final Object value, final InlinedConditionProfile profile, final Node node) {
super.putIntoVariablePart(index, profile.profile(node, value instanceof AbstractSqueakObject) ? new WeakRef((AbstractSqueakObject) value, weakPointersQueue) : value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public final SqueakLanguage getLanguage() {
return SqueakLanguage.get(this);
}

public static SqueakImageContext getContext(final Node node) {
return SqueakImageContext.get(node);
}

public final SqueakImageContext getContext() {
return SqueakImageContext.get(this);
return getContext(this);
}

protected final CompiledCodeObject getCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/
package de.hpi.swa.trufflesqueak.nodes;

import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;

import de.hpi.swa.trufflesqueak.exceptions.Returns.NonLocalReturn;
import de.hpi.swa.trufflesqueak.exceptions.Returns.NonVirtualReturn;
Expand All @@ -32,8 +34,9 @@ public static HandleNonLocalReturnNode create(final CompiledCodeObject code) {

@Specialization
protected final Object doHandle(final VirtualFrame frame, final NonLocalReturn nlr,
@Cached final ConditionProfile hasModifiedSenderProfile) {
if (hasModifiedSenderProfile.profile(FrameAccess.hasModifiedSender(frame))) {
@Bind("this") final Node node,
@Cached final InlinedConditionProfile hasModifiedSenderProfile) {
if (hasModifiedSenderProfile.profile(node, FrameAccess.hasModifiedSender(frame))) {
aboutToReturnNode.executeAboutToReturn(frame, nlr); // handle ensure: or ifCurtailed:
// Sender might have changed.
final ContextObject newSender = FrameAccess.getSenderContext(frame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/
package de.hpi.swa.trufflesqueak.nodes;

import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;

import de.hpi.swa.trufflesqueak.image.SqueakImageContext;
import de.hpi.swa.trufflesqueak.model.ContextObject;
Expand All @@ -30,18 +32,19 @@ protected final void doStartMaterialization(final VirtualFrame frame) {

@Specialization(guards = {"getSqueakImageContext(frame).lastSeenContext != null"})
protected final void doMaterialize(final VirtualFrame frame,
@Cached final ConditionProfile isNotLastSeenContextProfile,
@Cached final ConditionProfile continueProfile,
@Bind("this") final Node node,
@Cached final InlinedConditionProfile isNotLastSeenContextProfile,
@Cached final InlinedConditionProfile continueProfile,
@Cached final GetOrCreateContextNode getOrCreateContextNode) {
final SqueakImageContext image = getContext();
final ContextObject lastSeenContext = image.lastSeenContext;
final ContextObject context = getOrCreateContextNode.executeGet(frame);
if (isNotLastSeenContextProfile.profile(context != lastSeenContext)) {
if (isNotLastSeenContextProfile.profile(node, context != lastSeenContext)) {
assert context.hasTruffleFrame();
if (lastSeenContext != null && !lastSeenContext.hasMaterializedSender()) {
lastSeenContext.setSender(context);
}
if (continueProfile.profile(context.canBeReturnedTo() && context.hasEscaped())) {
if (continueProfile.profile(node, context.canBeReturnedTo() && context.hasEscaped())) {
// Materialization needs to continue in parent frame.
image.lastSeenContext = context;
} else {
Expand Down
Loading

1 comment on commit ec63041

@TruffleSqueak-Bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance Report (ec63041)

Benchmarks ran on graalvm-jdk-21+35.1.

Steady (after 100 iterations)

Benchmark Name Min Geomean Median Mean Max Total (ms) Total (min)
Bounce 530 538 532.97 531 532.96 106593 1.78
CD 438 449 441.94 440 441.93 88388 1.47
DeltaBlue 276 480 410.68 404 408.98 82135 1.37
Havlak 1103 1162 1135.27 1141 1135.18 227053 3.78
Json 360 375 363.12 361 363.1 72623 1.21
List 292 312 293.02 293 293.01 58603 0.98
Mandelbrot 132 142 132.91 133 132.9 26582 0.44
NBody 248 263 252 250 251.98 50399 0.84
Permute 149 165 150.89 150 150.86 30177 0.5
Queens 247 269 264.96 265 264.95 52992 0.88
Richards 1219 1243 1223.41 1222 1223.41 244682 4.08
Sieve 163 173 164.06 164 164.04 32811 0.55
Storage 138 148 139.77 139 139.75 27954 0.47
Towers 196 208 197.93 197 197.92 39586 0.66
5491 5927 5702.89 5690 5700.97 1140578 19.01

ec63041-2-steady.svg

Warmup (first 100 iterations)

ec63041-3-warmup.svg

Please sign in to comment.