Skip to content

Commit

Permalink
[GR-45042] Converting nodes to DSL inlinable
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/3847
  • Loading branch information
horakivo committed Jun 5, 2023
2 parents fe994ee + 999a761 commit e0c429e
Show file tree
Hide file tree
Showing 24 changed files with 323 additions and 237 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/truffleruby/RubyContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public TruffleLanguage.Env getEnv() {
}

/** Hashing for a RubyNode, the seed should only be used for a Ruby-level #hash method */
public Hashing getHashing(RubyBaseNode node) {
public Hashing getHashing(Node node) {
return hashing;
}

Expand Down
69 changes: 34 additions & 35 deletions src/main/java/org/truffleruby/cext/CExtNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

import com.oracle.truffle.api.TruffleSafepoint;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.GenerateCached;
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.object.DynamicObjectLibrary;
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
Expand All @@ -35,7 +36,6 @@
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
import org.truffleruby.builtins.CoreMethodNode;
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
import org.truffleruby.cext.CExtNodesFactory.StringToNativeNodeGen;
import org.truffleruby.cext.UnwrapNode.UnwrapCArrayNode;
import org.truffleruby.core.MarkingService.ExtensionCallStack;
import org.truffleruby.core.MarkingServiceNodes;
Expand Down Expand Up @@ -713,7 +713,7 @@ public abstract static class RbEncCodeRangeClear extends CoreMethodArrayArgument
@Specialization
protected RubyString clearCodeRange(RubyString string,
@Cached StringToNativeNode stringToNativeNode) {
stringToNativeNode.executeToNative(string);
stringToNativeNode.executeToNative(this, string);
string.clearCodeRange();

return string;
Expand Down Expand Up @@ -814,7 +814,7 @@ public abstract static class RbStrCapacityNode extends CoreMethodArrayArgumentsN
@Specialization
protected long capacity(Object string,
@Cached StringToNativeNode stringToNativeNode) {
return getNativeStringCapacity(stringToNativeNode.executeToNative(string));
return getNativeStringCapacity(stringToNativeNode.executeToNative(this, string));
}
}

Expand All @@ -827,7 +827,7 @@ protected RubyString strSetLen(RubyString string, int newByteLength,
@Cached StringToNativeNode stringToNativeNode,
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode,
@Cached InlinedConditionProfile minLengthOneProfile) {
var pointer = stringToNativeNode.executeToNative(string);
var pointer = stringToNativeNode.executeToNative(this, string);

var encoding = libString.getEncoding(string);
int minLength = encoding.jcoding.minLength();
Expand Down Expand Up @@ -857,7 +857,7 @@ protected RubyString rbStrResize(RubyString string, int newByteLength,
@Cached RubyStringLibrary libString,
@Cached StringToNativeNode stringToNativeNode,
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
var pointer = stringToNativeNode.executeToNative(string);
var pointer = stringToNativeNode.executeToNative(this, string);
var tencoding = libString.getTEncoding(string);
int byteLength = string.tstring.byteLength(tencoding);

Expand Down Expand Up @@ -886,7 +886,7 @@ protected RubyString trStrCapaResize(RubyString string, int newCapacity,
@Cached RubyStringLibrary libString,
@Cached StringToNativeNode stringToNativeNode,
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
var pointer = stringToNativeNode.executeToNative(string);
var pointer = stringToNativeNode.executeToNative(this, string);
var tencoding = libString.getTEncoding(string);

if (getNativeStringCapacity(pointer) == newCapacity) {
Expand Down Expand Up @@ -1333,37 +1333,34 @@ public abstract static class RbHashNode extends CoreMethodArrayArgumentsNode {
@Specialization
protected int rbHash(Object object,
@Cached HashingNodes.ToHashByHashCode toHashByHashCode) {
return toHashByHashCode.execute(object);
return toHashByHashCode.execute(this, object);
}
}

@GenerateInline
@GenerateCached(false)
public abstract static class StringToNativeNode extends RubyBaseNode {

@NeverDefault
public static StringToNativeNode create() {
return StringToNativeNodeGen.create();
}

public abstract Pointer executeToNative(Object string);
public abstract Pointer executeToNative(Node node, Object string);

@Specialization
protected Pointer toNative(RubyString string,
protected static Pointer toNative(Node node, RubyString string,
@Cached RubyStringLibrary libString,
@Cached InlinedConditionProfile convertProfile,
@Cached TruffleString.CopyToNativeMemoryNode copyToNativeMemoryNode,
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode,
@Cached TruffleString.GetInternalNativePointerNode getInternalNativePointerNode) {
@Cached(inline = false) TruffleString.CopyToNativeMemoryNode copyToNativeMemoryNode,
@Cached(inline = false) MutableTruffleString.FromNativePointerNode fromNativePointerNode,
@Cached(inline = false) TruffleString.GetInternalNativePointerNode getInternalNativePointerNode) {
var tstring = string.tstring;
var tencoding = libString.getTEncoding(string);

final Pointer pointer;

if (convertProfile.profile(this, tstring.isNative())) {
if (convertProfile.profile(node, tstring.isNative())) {
assert tstring.isMutable();
pointer = (Pointer) getInternalNativePointerNode.execute(tstring, tencoding);
} else {
int byteLength = tstring.byteLength(tencoding);
pointer = allocateAndCopyToNative(getLanguage(), getContext(), tstring, tencoding, byteLength,
pointer = allocateAndCopyToNative(getLanguage(node), getContext(node), tstring, tencoding, byteLength,
copyToNativeMemoryNode);

var nativeTString = fromNativePointerNode.execute(pointer, 0, byteLength, tencoding, false);
Expand All @@ -1374,8 +1371,8 @@ protected Pointer toNative(RubyString string,
}

@Specialization
protected Pointer toNativeImmutable(ImmutableRubyString string) {
return string.getNativeString(getLanguage());
protected static Pointer toNativeImmutable(Node node, ImmutableRubyString string) {
return string.getNativeString(getLanguage(node));
}

public static Pointer allocateAndCopyToNative(RubyLanguage language, RubyContext context,
Expand All @@ -1394,7 +1391,7 @@ public abstract static class StringPointerToNativeNode extends PrimitiveArrayArg
@Specialization
protected long toNative(Object string,
@Cached StringToNativeNode stringToNativeNode) {
return stringToNativeNode.executeToNative(string).getAddress();
return stringToNativeNode.executeToNative(this, string).getAddress();
}
}

Expand All @@ -1404,7 +1401,7 @@ public abstract static class StringToFFIPointerNode extends CoreMethodArrayArgum
@Specialization
protected RubyPointer toNative(Object string,
@Cached StringToNativeNode stringToNativeNode) {
var pointer = stringToNativeNode.executeToNative(string);
var pointer = stringToNativeNode.executeToNative(this, string);

final RubyPointer instance = new RubyPointer(
coreLibrary().truffleFFIPointerClass,
Expand Down Expand Up @@ -1543,56 +1540,58 @@ public abstract static class ExtractRubyTag extends CoreMethodArrayArgumentsNode
@Specialization
protected int extractRubyTag(CapturedException captured,
@Cached ExtractRubyTagHelperNode helperNode) {
return helperNode.execute(captured.getException());
return helperNode.execute(this, captured.getException());
}
}

@GenerateInline
@GenerateCached(false)
public abstract static class ExtractRubyTagHelperNode extends RubyBaseNode {

public abstract int execute(Throwable e);
public abstract int execute(Node node, Throwable e);

@Specialization
protected int dynamicReturnTag(DynamicReturnException e) {
protected static int dynamicReturnTag(DynamicReturnException e) {
return RUBY_TAG_RETURN;
}

@Specialization
protected int localReturnTag(LocalReturnException e) {
protected static int localReturnTag(LocalReturnException e) {
return RUBY_TAG_RETURN;
}

@Specialization
protected int breakTag(BreakException e) {
protected static int breakTag(BreakException e) {
return RUBY_TAG_BREAK;
}

@Specialization
protected int nextTag(NextException e) {
protected static int nextTag(NextException e) {
return RUBY_TAG_NEXT;
}

@Specialization
protected int retryTag(RetryException e) {
protected static int retryTag(RetryException e) {
return RUBY_TAG_RETRY;
}

@Specialization
protected int redoTag(RedoException e) {
protected static int redoTag(RedoException e) {
return RUBY_TAG_REDO;
}

@Specialization
protected int raiseTag(RaiseException e) {
protected static int raiseTag(RaiseException e) {
return RUBY_TAG_RAISE;
}

@Specialization
protected int throwTag(ThrowException e) {
protected static int throwTag(ThrowException e) {
return RUBY_TAG_THROW;
}

@Fallback
protected int noTag(Throwable e) {
protected static int noTag(Throwable e) {
return 0;
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/truffleruby/cext/IsNativeObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import static org.truffleruby.cext.ValueWrapperManager.isMallocAligned;

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.nodes.Node;
import org.truffleruby.language.RubyBaseNode;

import com.oracle.truffle.api.dsl.GenerateUncached;
Expand All @@ -20,18 +23,20 @@
/** The IsNativeObjectNode is implemented to determine if a native pointer belongs to a natively allocated NODE* which
* are used in Ripper. */
@GenerateUncached
@GenerateInline
@GenerateCached(false)
public abstract class IsNativeObjectNode extends RubyBaseNode {

/** Returns true if handle was natively allocated. */
public abstract Object execute(Object handle);
public abstract Object execute(Node node, Object handle);

@Specialization
protected boolean isNativeObjectTaggedObject(long handle) {
protected static boolean isNativeObjectTaggedObject(long handle) {
return isMallocAligned(handle) && handle < ValueWrapperManager.ALLOCATION_BASE;
}

@Fallback
protected boolean isNativeObjectFallback(Object handle) {
protected static boolean isNativeObjectFallback(Object handle) {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/truffleruby/cext/ValueWrapperManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,9 @@ protected boolean isExecutable() {

@ExportMessage
protected Object execute(Object[] arguments,
@Cached IsNativeObjectNode isNativeObjectNode) {
return isNativeObjectNode.execute(arguments[0]);
@Cached IsNativeObjectNode isNativeObjectNode,
@Bind("$node") Node node) {
return isNativeObjectNode.execute(node, arguments[0]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/truffleruby/core/VMPrimitiveNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ protected Object startHashNotNumber(Object salt,
@Cached InlinedConditionProfile isIntegerProfile,
@Cached InlinedConditionProfile isLongProfile,
@Cached InlinedConditionProfile isBignumProfile) {
Object result = toRubyInteger.execute(salt);
Object result = toRubyInteger.execute(this, salt);
if (isIntegerProfile.profile(this, result instanceof Integer)) {
return getContext().getHashing(this).start((int) result);
} else if (isLongProfile.profile(this, result instanceof Long)) {
Expand Down Expand Up @@ -508,7 +508,7 @@ protected Object updateHash(long hash, Object value,
@Cached InlinedConditionProfile isIntegerProfile,
@Cached InlinedConditionProfile isLongProfile,
@Cached InlinedConditionProfile isBignumProfile) {
Object result = toRubyInteger.execute(value);
Object result = toRubyInteger.execute(this, value);
if (isIntegerProfile.profile(this, result instanceof Integer)) {
return Hashing.update(hash, (int) result);
} else if (isLongProfile.profile(this, result instanceof Long)) {
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/org/truffleruby/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import com.oracle.truffle.api.TruffleSafepoint;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.Shape;
import com.oracle.truffle.api.profiles.InlinedIntValueProfile;
import com.oracle.truffle.api.profiles.InlinedLoopConditionProfile;
import com.oracle.truffle.api.profiles.LoopConditionProfile;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.strings.TruffleString;
Expand Down Expand Up @@ -477,7 +480,7 @@ protected RubyArray clear(RubyArray array,
@Cached IsSharedNode isSharedNode,
@Cached ConditionProfile sharedProfile) {
setStoreAndSize(array,
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(array))),
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(this, array))),
0);
return array;
}
Expand Down Expand Up @@ -1058,25 +1061,26 @@ public abstract static class HashNode extends PrimitiveArrayArgumentsNode {
private static final int CLASS_SALT = 42753062; // random number, stops hashes for similar values but different classes being the same, static because we want deterministic hashes

@Specialization(limit = "storageStrategyLimit()")
protected long hash(VirtualFrame frame, RubyArray array,
protected static long hash(VirtualFrame frame, RubyArray array,
@Bind("array.getStore()") Object store,
@CachedLibrary("store") ArrayStoreLibrary stores,
@Cached HashingNodes.ToHashByHashCode toHashByHashCode,
@Cached @Shared IntValueProfile arraySizeProfile,
@Cached @Shared LoopConditionProfile loopProfile) {
final int size = arraySizeProfile.profile(array.size);
long h = getContext().getHashing(this).start(size);
@Cached @Shared InlinedIntValueProfile arraySizeProfile,
@Cached @Shared InlinedLoopConditionProfile loopProfile,
@Bind("this") Node node) {
final int size = arraySizeProfile.profile(node, array.size);
long h = getContext(node).getHashing(node).start(size);
h = Hashing.update(h, CLASS_SALT);

int n = 0;
try {
for (; loopProfile.inject(n < size); n++) {
for (; loopProfile.inject(node, n < size); n++) {
final Object value = stores.read(store, n);
h = Hashing.update(h, toHashByHashCode.execute(value));
TruffleSafepoint.poll(this);
h = Hashing.update(h, toHashByHashCode.execute(node, value));
TruffleSafepoint.poll(node);
}
} finally {
profileAndReportLoopCount(loopProfile, n);
profileAndReportLoopCount(node, loopProfile, n);
}

return Hashing.end(h);
Expand Down Expand Up @@ -1138,7 +1142,8 @@ protected RubyArray initializeNoArgs(RubyArray array, NotProvided size, NotProvi
@Cached @Shared IsSharedNode isSharedNode,
@Cached @Shared ConditionProfile sharedProfile) {
setStoreAndSize(array,
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(array))), 0);
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(this, array))),
0);
return array;
}

Expand All @@ -1154,7 +1159,8 @@ protected RubyArray initializeOnlyBlock(
}

setStoreAndSize(array,
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(array))), 0);
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(this, array))),
0);
return array;
}

Expand Down Expand Up @@ -1186,7 +1192,7 @@ protected RubyArray initializeWithSizeNoValue(RubyArray array, int size, NotProv
@Cached @Shared ConditionProfile sharedProfile,
@CachedLibrary(limit = "2") @Exclusive ArrayStoreLibrary stores) {
final Object store;
if (sharedProfile.profile(isSharedNode.executeIsShared(array))) {
if (sharedProfile.profile(isSharedNode.executeIsShared(this, array))) {
store = new SharedArrayStorage(new Object[size]);
} else {
store = new Object[size];
Expand Down Expand Up @@ -1249,7 +1255,7 @@ protected Object initializeBlock(RubyArray array, int size, Object unusedFilling
} finally {
profileAndReportLoopCount(loopProfile, n);
Object store = arrayBuilder.finish(state, n);
if (sharedProfile.profile(isSharedNode.executeIsShared(array))) {
if (sharedProfile.profile(isSharedNode.executeIsShared(this, array))) {
store = stores.makeShared(store, n);
}
setStoreAndSize(array, store, n);
Expand Down Expand Up @@ -1832,7 +1838,7 @@ protected RubyArray replace(RubyArray array, RubyArray other,
@CachedLibrary(limit = "2") ArrayStoreLibrary stores) {
final int size = other.size;
Object store = cowNode.execute(other, 0, size);
if (sharedProfile.profile(isSharedNode.executeIsShared(array))) {
if (sharedProfile.profile(isSharedNode.executeIsShared(this, array))) {
store = stores.makeShared(store, size);
}
setStoreAndSize(array, store, size);
Expand Down Expand Up @@ -2293,9 +2299,7 @@ public abstract static class StoreToNativeNode extends PrimitiveArrayArgumentsNo
protected RubyArray storeToNative(RubyArray array,
@Bind("array.getStore()") Object store,
@CachedLibrary("store") ArrayStoreLibrary stores,
@Cached IntValueProfile arraySizeProfile,
@Cached IsSharedNode isSharedNode,
@Cached ConditionProfile sharedProfile) {
@Cached IntValueProfile arraySizeProfile) {
final int size = arraySizeProfile.profile(array.size);
Pointer pointer = Pointer.mallocAutoRelease(getLanguage(), getContext(), size * Pointer.SIZE);
Object newStore = new NativeArrayStorage(pointer, size);
Expand Down
Loading

0 comments on commit e0c429e

Please sign in to comment.