Skip to content

Commit

Permalink
Reduce code duplication and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Dec 9, 2023
1 parent 37e2359 commit 5205c66
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected static final NativeObject doString(final Node node, final String value

@Specialization
protected static final NativeObject doTruffleString(final Node node, final TruffleString value,
@Cached final TruffleString.ToJavaStringNode toJavaString,
@Cached(inline = false) final TruffleString.ToJavaStringNode toJavaString,
@Shared("wideStringProfile") @Cached final InlinedConditionProfile wideStringProfile) {
return doString(node, toJavaString.execute(value), wideStringProfile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ protected DoItRootNode(final SqueakImageContext image, final TruffleLanguage<?>
}

public static DoItRootNode create(final SqueakImageContext image, final SqueakLanguage language, final Object closure) {
return DoItRootNodeGen.create(image, language, closure);
return DoItRootNodeGen.create(image, (TruffleLanguage<?>) language, closure);
}

public abstract Object execute(VirtualFrame frame);

@Specialization
protected final Object doIt(final VirtualFrame frame,
@Bind("this") final Node node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.hpi.swa.trufflesqueak.model.layout.ObjectLayouts.ASSOCIATION;
import de.hpi.swa.trufflesqueak.nodes.AbstractNode;
import de.hpi.swa.trufflesqueak.nodes.accessing.SqueakObjectAt0Node;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushLiteralVariableNodeFactory.PushLiteralVariableReadonlyNodeGen;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushLiteralVariableNodeFactory.PushLiteralVariableWritableNodeGen;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushReceiverVariableNodeGen;
import de.hpi.swa.trufflesqueak.nodes.bytecodes.PushBytecodesFactory.PushRemoteTempNodeGen;
Expand Down Expand Up @@ -372,7 +373,7 @@ public String toString() {
}

@NodeInfo(cost = NodeCost.NONE)
public abstract static class PushLiteralVariableNode extends AbstractPushNode {
public abstract static class PushLiteralVariableNode extends AbstractInstrumentableBytecodeNode {
private static final String[] READONLY_CLASSES = {"ClassBinding", "ReadOnlyVariableBinding"};
protected final Object literal;

Expand All @@ -381,12 +382,12 @@ private PushLiteralVariableNode(final CompiledCodeObject code, final int index,
this.literal = literal;
}

public static final AbstractPushNode create(final CompiledCodeObject code, final int index, final int numBytecodes, final int literalIndex) {
public static final AbstractInstrumentableBytecodeNode create(final CompiledCodeObject code, final int index, final int numBytecodes, final int literalIndex) {
final Object literal = code.getLiteral(literalIndex);
if (literal instanceof final AbstractSqueakObjectWithClassAndHash l) {
final String squeakClassName = l.getSqueakClassName();
if (ArrayUtils.containsEqual(READONLY_CLASSES, squeakClassName)) {
return new PushLiteralVariableReadonlyNode(code, index, numBytecodes, literal);
return PushLiteralVariableReadonlyNodeGen.create(code, index, numBytecodes, literal);
}
}
return PushLiteralVariableWritableNodeGen.create(code, index, numBytecodes, literal);
Expand Down Expand Up @@ -414,16 +415,17 @@ protected final void doPushLiteralVariable(final VirtualFrame frame,
}
}

private static final class PushLiteralVariableReadonlyNode extends PushLiteralVariableNode {
protected abstract static class PushLiteralVariableReadonlyNode extends PushLiteralVariableNode {
private final Object pushValue;

protected PushLiteralVariableReadonlyNode(final CompiledCodeObject code, final int index, final int numBytecodes, final Object literal) {
super(code, index, numBytecodes, literal);
pushValue = getPushValue(literal);
}

@Override
public void executeVoid(final VirtualFrame frame) {
@Specialization
protected final void doPushLiteralVariable(final VirtualFrame frame,
@Cached final FrameStackPushNode pushNode) {
assert pushValue == getPushValue(literal) : "value of binding changed unexpectedly";
pushNode.execute(frame, pushValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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.GenerateCached;
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
import com.oracle.truffle.api.dsl.NodeFactory;
import com.oracle.truffle.api.dsl.Specialization;
Expand All @@ -24,6 +26,7 @@
import de.hpi.swa.trufflesqueak.model.AbstractSqueakObject;
import de.hpi.swa.trufflesqueak.model.LargeIntegerObject;
import de.hpi.swa.trufflesqueak.model.NativeObject;
import de.hpi.swa.trufflesqueak.nodes.AbstractNode;
import de.hpi.swa.trufflesqueak.nodes.primitives.AbstractPrimitiveFactoryHolder;
import de.hpi.swa.trufflesqueak.nodes.primitives.AbstractPrimitiveNode;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.BinaryPrimitiveFallback;
Expand Down Expand Up @@ -236,11 +239,13 @@ protected static final Object doConvert(final Object receiver, final NativeObjec
for (int i = 0; i < aByteArrayLength; i++) {
final int wordIndex = i / 2;
final long value = aByteArray.getByteUnsigned(i) << 8;
final int intValue;
if (i % 2 == 0) {
aSoundBuffer.setInt(wordIndex, aSoundBuffer.getInt(wordIndex) & 0xffff0000 | (int) value & 0xffff);
intValue = aSoundBuffer.getInt(wordIndex) & 0xffff0000 | (int) value & 0xffff;
} else {
aSoundBuffer.setInt(wordIndex, (int) value << 16 | aSoundBuffer.getInt(wordIndex) & 0xffff);
intValue = (int) value << 16 | aSoundBuffer.getInt(wordIndex) & 0xffff;
}
aSoundBuffer.setInt(wordIndex, intValue);
}
return receiver;
}
Expand Down Expand Up @@ -424,39 +429,49 @@ protected static final long calculateHash(final long initialHash, final byte[] b
@SqueakPrimitive(names = "primitiveStringHash")
/* Byte(Array|String|Symbol)>>#hashWithInitialHash: */
public abstract static class PrimStringHash2Node extends AbstractPrimStringHashNode implements BinaryPrimitiveFallback {
@Specialization(guards = {"string.isByteType()"})
protected static final long doNativeObject(final NativeObject string, final long initialHash) {
return calculateHash(initialHash, string.getByteStorage());
}

@Specialization
protected static final long doLargeInteger(final LargeIntegerObject largeInteger, final long initialHash) {
return calculateHash(initialHash, largeInteger.getBytes());
}

@Specialization(guards = {"isLongMinValue(value)"})
protected static final long doLongMinValue(@SuppressWarnings("unused") final long value, final long initialHash) {
return calculateHash(initialHash, LargeIntegerObject.getLongMinOverflowResultBytes());
protected static final long doStringHash(final Object receiver, final long initialHash,
@Bind("this") final Node node,
@Cached final GetHashBytesNode getHashBytesNode) {
return calculateHash(initialHash, getHashBytesNode.execute(node, receiver));
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveStringHash")
/* (Byte(Array|String|Symbol) class|MiscPrimitivePluginTest)>>#hashBytes:startingWith: */
public abstract static class PrimStringHash3Node extends AbstractPrimStringHashNode implements TernaryPrimitiveFallback {
@Specialization(guards = {"string.isByteType()"})
protected static final long doNativeObject(@SuppressWarnings("unused") final Object receiver, final NativeObject string, final long initialHash) {
return calculateHash(initialHash, string.getByteStorage());
@Specialization
protected static final long doStringHash(@SuppressWarnings("unused") final Object receiver, final Object target, final long initialHash,
@Bind("this") final Node node,
@Cached final GetHashBytesNode getHashBytesNode) {
return calculateHash(initialHash, getHashBytesNode.execute(node, target));
}
}

@GenerateInline
@GenerateCached(false)
protected abstract static class GetHashBytesNode extends AbstractNode {
protected abstract byte[] execute(Node inliningTarget, Object value);

@Specialization(guards = {"value.isByteType()"})
protected static final byte[] doNativeObject(final NativeObject value) {
return value.getByteStorage();
}

@Specialization
protected static final long doLargeInteger(@SuppressWarnings("unused") final Object receiver, final LargeIntegerObject largeInteger, final long initialHash) {
return calculateHash(initialHash, largeInteger.getBytes());
protected static final byte[] doLargeInteger(final LargeIntegerObject value) {
return value.getBytes();
}

@Specialization(guards = {"isLongMinValue(value)"})
protected static final long doLongMinValue(@SuppressWarnings("unused") final Object receiver, @SuppressWarnings("unused") final long value, final long initialHash) {
return calculateHash(initialHash, LargeIntegerObject.getLongMinOverflowResultBytes());
protected static final byte[] doLongMinValue(@SuppressWarnings("unused") final long value) {
return LargeIntegerObject.getLongMinOverflowResultBytes();
}

@Fallback
protected static final byte[] doFallback(@SuppressWarnings("unused") final Object value) {
throw PrimitiveFailed.GENERIC_ERROR;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
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.GenerateCached;
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NodeFactory;
Expand All @@ -29,6 +32,7 @@
import de.hpi.swa.trufflesqueak.model.FloatObject;
import de.hpi.swa.trufflesqueak.model.LargeIntegerObject;
import de.hpi.swa.trufflesqueak.model.PointersObject;
import de.hpi.swa.trufflesqueak.nodes.AbstractNode;
import de.hpi.swa.trufflesqueak.nodes.SqueakGuards;
import de.hpi.swa.trufflesqueak.nodes.accessing.AbstractPointersObjectNodes.AbstractPointersObjectWriteNode;
import de.hpi.swa.trufflesqueak.nodes.accessing.FloatObjectNodes.AsFloatObjectIfNessaryNode;
Expand Down Expand Up @@ -721,23 +725,17 @@ protected static final Object doLargeInteger(final LargeIntegerObject lhs, final
@SqueakPrimitive(indices = 38)
protected abstract static class PrimFloatAtNode extends AbstractArithmeticPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization(guards = "index == 1")
protected static final long doDoubleHigh(final double receiver, @SuppressWarnings("unused") final long index) {
return Integer.toUnsignedLong((int) (Double.doubleToRawLongBits(receiver) >> 32));
}

@Specialization(guards = "index == 2")
protected static final long doDoubleLow(final double receiver, @SuppressWarnings("unused") final long index) {
return Integer.toUnsignedLong((int) Double.doubleToRawLongBits(receiver));
}

@Specialization(guards = "index == 1")
protected static final long doFloatHigh(final FloatObject receiver, final long index) {
return doDoubleHigh(receiver.getValue(), index);
protected static final long doHigh(final Object receiver, @SuppressWarnings("unused") final long index,
@Bind("this") final Node node,
@Shared("toDoubleNode") @Cached final ToDoubleNode toDoubleNode) {
return Integer.toUnsignedLong((int) (Double.doubleToRawLongBits(toDoubleNode.execute(node, receiver)) >> 32));
}

@Specialization(guards = "index == 2")
protected static final long doFloatLow(final FloatObject receiver, final long index) {
return doDoubleLow(receiver.getValue(), index);
protected static final long doLow(final Object receiver, @SuppressWarnings("unused") final long index,
@Bind("this") final Node node,
@Shared("toDoubleNode") @Cached final ToDoubleNode toDoubleNode) {
return Integer.toUnsignedLong((int) Double.doubleToRawLongBits(toDoubleNode.execute(node, receiver)));
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -1651,6 +1649,27 @@ protected static final long exponentNonZero(final double receiver, final Inlined
}
}

@GenerateInline
@GenerateCached(false)
protected abstract static class ToDoubleNode extends AbstractNode {
protected abstract double execute(Node inliningTarget, Object value);

@Specialization
protected static final double doDouble(final double value) {
return value;
}

@Specialization
protected static final double doFloatObject(final FloatObject value) {
return value.getValue();
}

@Fallback
protected static final double doFallback(@SuppressWarnings("unused") final Object value) {
throw PrimitiveFailed.BAD_RECEIVER;
}
}

@Override
public List<? extends NodeFactory<? extends AbstractPrimitiveNode>> getFactories() {
return ArithmeticPrimitivesFactory.getFactories();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected static final Object basicAtPut(final AbstractSqueakObject receiver, fi
@SqueakPrimitive(indices = 60)
protected abstract static class PrimBasicAt2Node extends AbstractBasicAtOrAtPutNode implements BinaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(final Object receiver, final long index,
protected static final Object doSqueakObject(final Object receiver, final long index,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand All @@ -98,7 +98,7 @@ protected final Object doSqueakObject(final Object receiver, final long index,
@SqueakPrimitive(indices = 60)
protected abstract static class PrimBasicAt3Node extends AbstractBasicAtOrAtPutNode implements TernaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final Object target, final long index,
protected static final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final Object target, final long index,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand All @@ -113,7 +113,7 @@ protected final Object doSqueakObject(@SuppressWarnings("unused") final Object r
@SqueakPrimitive(indices = 61)
protected abstract static class PrimBasicAtPut3Node extends AbstractBasicAtOrAtPutNode implements TernaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(final AbstractSqueakObject receiver, final long index, final Object value,
protected static final Object doSqueakObject(final AbstractSqueakObject receiver, final long index, final Object value,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand All @@ -128,7 +128,7 @@ protected final Object doSqueakObject(final AbstractSqueakObject receiver, final
@SqueakPrimitive(indices = 61)
protected abstract static class PrimBasicAtPut4Node extends AbstractBasicAtOrAtPutNode implements QuaternaryPrimitiveFallback {
@Specialization
protected final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final AbstractSqueakObject target, final long index, final Object value,
protected static final Object doSqueakObject(@SuppressWarnings("unused") final Object receiver, final AbstractSqueakObject target, final long index, final Object value,
@Bind("this") final Node node,
@Cached final SqueakObjectSizeNode sizeNode,
@Cached final SqueakObjectInstSizeNode instSizeNode,
Expand Down

1 comment on commit 5205c66

@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 (5205c66)

Benchmarks ran on graalvm-jdk-21+35.1.

Steady (after 100 iterations)

Benchmark Name Min Geomean Median Mean Max Total (ms) Total (min)
Bounce 521 530 524.05 522 524.05 104811 1.75
CD 472 484 476.8 475 476.78 95359 1.59
DeltaBlue 273 458 406.65 405 404.92 81329 1.36
Havlak 1113 1164 1143.32 1148 1143.25 228664 3.81
Json 371 380 373.55 372 373.54 74710 1.25
List 292 303 292.77 293 292.77 58555 0.98
Mandelbrot 125 135 125.75 125 125.74 25149 0.42
NBody 248 259 251.72 250 251.7 50344 0.84
Permute 149 166 150.54 150 150.53 30109 0.5
Queens 230 245 231.01 231 231 46201 0.77
Richards 1208 1226 1212.43 1213 1212.42 242485 4.04
Sieve 163 173 164.07 164 164.06 32814 0.55
Storage 137 170 139.15 138 139.11 27830 0.46
Towers 203 221 203.62 203 203.61 40724 0.68
5505 5914 5695.42 5689 5693.48 1139084 18.98

5205c66-2-steady.svg

Warmup (first 100 iterations)

5205c66-3-warmup.svg

Please sign in to comment.