Skip to content

Commit e0c429e

Browse files
committed
[GR-45042] Converting nodes to DSL inlinable
PullRequest: truffleruby/3847
2 parents fe994ee + 999a761 commit e0c429e

24 files changed

+323
-237
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public TruffleLanguage.Env getEnv() {
558558
}
559559

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

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
import com.oracle.truffle.api.TruffleSafepoint;
1717
import com.oracle.truffle.api.dsl.Bind;
18-
import com.oracle.truffle.api.dsl.NeverDefault;
18+
import com.oracle.truffle.api.dsl.GenerateCached;
19+
import com.oracle.truffle.api.dsl.GenerateInline;
1920
import com.oracle.truffle.api.object.DynamicObjectLibrary;
2021
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
2122
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
@@ -35,7 +36,6 @@
3536
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
3637
import org.truffleruby.builtins.CoreMethodNode;
3738
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
38-
import org.truffleruby.cext.CExtNodesFactory.StringToNativeNodeGen;
3939
import org.truffleruby.cext.UnwrapNode.UnwrapCArrayNode;
4040
import org.truffleruby.core.MarkingService.ExtensionCallStack;
4141
import org.truffleruby.core.MarkingServiceNodes;
@@ -713,7 +713,7 @@ public abstract static class RbEncCodeRangeClear extends CoreMethodArrayArgument
713713
@Specialization
714714
protected RubyString clearCodeRange(RubyString string,
715715
@Cached StringToNativeNode stringToNativeNode) {
716-
stringToNativeNode.executeToNative(string);
716+
stringToNativeNode.executeToNative(this, string);
717717
string.clearCodeRange();
718718

719719
return string;
@@ -814,7 +814,7 @@ public abstract static class RbStrCapacityNode extends CoreMethodArrayArgumentsN
814814
@Specialization
815815
protected long capacity(Object string,
816816
@Cached StringToNativeNode stringToNativeNode) {
817-
return getNativeStringCapacity(stringToNativeNode.executeToNative(string));
817+
return getNativeStringCapacity(stringToNativeNode.executeToNative(this, string));
818818
}
819819
}
820820

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

832832
var encoding = libString.getEncoding(string);
833833
int minLength = encoding.jcoding.minLength();
@@ -857,7 +857,7 @@ protected RubyString rbStrResize(RubyString string, int newByteLength,
857857
@Cached RubyStringLibrary libString,
858858
@Cached StringToNativeNode stringToNativeNode,
859859
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
860-
var pointer = stringToNativeNode.executeToNative(string);
860+
var pointer = stringToNativeNode.executeToNative(this, string);
861861
var tencoding = libString.getTEncoding(string);
862862
int byteLength = string.tstring.byteLength(tencoding);
863863

@@ -886,7 +886,7 @@ protected RubyString trStrCapaResize(RubyString string, int newCapacity,
886886
@Cached RubyStringLibrary libString,
887887
@Cached StringToNativeNode stringToNativeNode,
888888
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode) {
889-
var pointer = stringToNativeNode.executeToNative(string);
889+
var pointer = stringToNativeNode.executeToNative(this, string);
890890
var tencoding = libString.getTEncoding(string);
891891

892892
if (getNativeStringCapacity(pointer) == newCapacity) {
@@ -1333,37 +1333,34 @@ public abstract static class RbHashNode extends CoreMethodArrayArgumentsNode {
13331333
@Specialization
13341334
protected int rbHash(Object object,
13351335
@Cached HashingNodes.ToHashByHashCode toHashByHashCode) {
1336-
return toHashByHashCode.execute(object);
1336+
return toHashByHashCode.execute(this, object);
13371337
}
13381338
}
13391339

1340+
@GenerateInline
1341+
@GenerateCached(false)
13401342
public abstract static class StringToNativeNode extends RubyBaseNode {
13411343

1342-
@NeverDefault
1343-
public static StringToNativeNode create() {
1344-
return StringToNativeNodeGen.create();
1345-
}
1346-
1347-
public abstract Pointer executeToNative(Object string);
1344+
public abstract Pointer executeToNative(Node node, Object string);
13481345

13491346
@Specialization
1350-
protected Pointer toNative(RubyString string,
1347+
protected static Pointer toNative(Node node, RubyString string,
13511348
@Cached RubyStringLibrary libString,
13521349
@Cached InlinedConditionProfile convertProfile,
1353-
@Cached TruffleString.CopyToNativeMemoryNode copyToNativeMemoryNode,
1354-
@Cached MutableTruffleString.FromNativePointerNode fromNativePointerNode,
1355-
@Cached TruffleString.GetInternalNativePointerNode getInternalNativePointerNode) {
1350+
@Cached(inline = false) TruffleString.CopyToNativeMemoryNode copyToNativeMemoryNode,
1351+
@Cached(inline = false) MutableTruffleString.FromNativePointerNode fromNativePointerNode,
1352+
@Cached(inline = false) TruffleString.GetInternalNativePointerNode getInternalNativePointerNode) {
13561353
var tstring = string.tstring;
13571354
var tencoding = libString.getTEncoding(string);
13581355

13591356
final Pointer pointer;
13601357

1361-
if (convertProfile.profile(this, tstring.isNative())) {
1358+
if (convertProfile.profile(node, tstring.isNative())) {
13621359
assert tstring.isMutable();
13631360
pointer = (Pointer) getInternalNativePointerNode.execute(tstring, tencoding);
13641361
} else {
13651362
int byteLength = tstring.byteLength(tencoding);
1366-
pointer = allocateAndCopyToNative(getLanguage(), getContext(), tstring, tencoding, byteLength,
1363+
pointer = allocateAndCopyToNative(getLanguage(node), getContext(node), tstring, tencoding, byteLength,
13671364
copyToNativeMemoryNode);
13681365

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

13761373
@Specialization
1377-
protected Pointer toNativeImmutable(ImmutableRubyString string) {
1378-
return string.getNativeString(getLanguage());
1374+
protected static Pointer toNativeImmutable(Node node, ImmutableRubyString string) {
1375+
return string.getNativeString(getLanguage(node));
13791376
}
13801377

13811378
public static Pointer allocateAndCopyToNative(RubyLanguage language, RubyContext context,
@@ -1394,7 +1391,7 @@ public abstract static class StringPointerToNativeNode extends PrimitiveArrayArg
13941391
@Specialization
13951392
protected long toNative(Object string,
13961393
@Cached StringToNativeNode stringToNativeNode) {
1397-
return stringToNativeNode.executeToNative(string).getAddress();
1394+
return stringToNativeNode.executeToNative(this, string).getAddress();
13981395
}
13991396
}
14001397

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

14091406
final RubyPointer instance = new RubyPointer(
14101407
coreLibrary().truffleFFIPointerClass,
@@ -1543,56 +1540,58 @@ public abstract static class ExtractRubyTag extends CoreMethodArrayArgumentsNode
15431540
@Specialization
15441541
protected int extractRubyTag(CapturedException captured,
15451542
@Cached ExtractRubyTagHelperNode helperNode) {
1546-
return helperNode.execute(captured.getException());
1543+
return helperNode.execute(this, captured.getException());
15471544
}
15481545
}
15491546

1547+
@GenerateInline
1548+
@GenerateCached(false)
15501549
public abstract static class ExtractRubyTagHelperNode extends RubyBaseNode {
15511550

1552-
public abstract int execute(Throwable e);
1551+
public abstract int execute(Node node, Throwable e);
15531552

15541553
@Specialization
1555-
protected int dynamicReturnTag(DynamicReturnException e) {
1554+
protected static int dynamicReturnTag(DynamicReturnException e) {
15561555
return RUBY_TAG_RETURN;
15571556
}
15581557

15591558
@Specialization
1560-
protected int localReturnTag(LocalReturnException e) {
1559+
protected static int localReturnTag(LocalReturnException e) {
15611560
return RUBY_TAG_RETURN;
15621561
}
15631562

15641563
@Specialization
1565-
protected int breakTag(BreakException e) {
1564+
protected static int breakTag(BreakException e) {
15661565
return RUBY_TAG_BREAK;
15671566
}
15681567

15691568
@Specialization
1570-
protected int nextTag(NextException e) {
1569+
protected static int nextTag(NextException e) {
15711570
return RUBY_TAG_NEXT;
15721571
}
15731572

15741573
@Specialization
1575-
protected int retryTag(RetryException e) {
1574+
protected static int retryTag(RetryException e) {
15761575
return RUBY_TAG_RETRY;
15771576
}
15781577

15791578
@Specialization
1580-
protected int redoTag(RedoException e) {
1579+
protected static int redoTag(RedoException e) {
15811580
return RUBY_TAG_REDO;
15821581
}
15831582

15841583
@Specialization
1585-
protected int raiseTag(RaiseException e) {
1584+
protected static int raiseTag(RaiseException e) {
15861585
return RUBY_TAG_RAISE;
15871586
}
15881587

15891588
@Specialization
1590-
protected int throwTag(ThrowException e) {
1589+
protected static int throwTag(ThrowException e) {
15911590
return RUBY_TAG_THROW;
15921591
}
15931592

15941593
@Fallback
1595-
protected int noTag(Throwable e) {
1594+
protected static int noTag(Throwable e) {
15961595
return 0;
15971596
}
15981597
}

src/main/java/org/truffleruby/cext/IsNativeObjectNode.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import static org.truffleruby.cext.ValueWrapperManager.isMallocAligned;
1313

1414
import com.oracle.truffle.api.dsl.Fallback;
15+
import com.oracle.truffle.api.dsl.GenerateCached;
16+
import com.oracle.truffle.api.dsl.GenerateInline;
17+
import com.oracle.truffle.api.nodes.Node;
1518
import org.truffleruby.language.RubyBaseNode;
1619

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

2530
/** Returns true if handle was natively allocated. */
26-
public abstract Object execute(Object handle);
31+
public abstract Object execute(Node node, Object handle);
2732

2833
@Specialization
29-
protected boolean isNativeObjectTaggedObject(long handle) {
34+
protected static boolean isNativeObjectTaggedObject(long handle) {
3035
return isMallocAligned(handle) && handle < ValueWrapperManager.ALLOCATION_BASE;
3136
}
3237

3338
@Fallback
34-
protected boolean isNativeObjectFallback(Object handle) {
39+
protected static boolean isNativeObjectFallback(Object handle) {
3540
return false;
3641
}
3742

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ protected boolean isExecutable() {
465465

466466
@ExportMessage
467467
protected Object execute(Object[] arguments,
468-
@Cached IsNativeObjectNode isNativeObjectNode) {
469-
return isNativeObjectNode.execute(arguments[0]);
468+
@Cached IsNativeObjectNode isNativeObjectNode,
469+
@Bind("$node") Node node) {
470+
return isNativeObjectNode.execute(node, arguments[0]);
470471
}
471472
}
472473

src/main/java/org/truffleruby/core/VMPrimitiveNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ protected Object startHashNotNumber(Object salt,
475475
@Cached InlinedConditionProfile isIntegerProfile,
476476
@Cached InlinedConditionProfile isLongProfile,
477477
@Cached InlinedConditionProfile isBignumProfile) {
478-
Object result = toRubyInteger.execute(salt);
478+
Object result = toRubyInteger.execute(this, salt);
479479
if (isIntegerProfile.profile(this, result instanceof Integer)) {
480480
return getContext().getHashing(this).start((int) result);
481481
} else if (isLongProfile.profile(this, result instanceof Long)) {
@@ -508,7 +508,7 @@ protected Object updateHash(long hash, Object value,
508508
@Cached InlinedConditionProfile isIntegerProfile,
509509
@Cached InlinedConditionProfile isLongProfile,
510510
@Cached InlinedConditionProfile isBignumProfile) {
511-
Object result = toRubyInteger.execute(value);
511+
Object result = toRubyInteger.execute(this, value);
512512
if (isIntegerProfile.profile(this, result instanceof Integer)) {
513513
return Hashing.update(hash, (int) result);
514514
} else if (isLongProfile.profile(this, result instanceof Long)) {

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import com.oracle.truffle.api.TruffleSafepoint;
1919
import com.oracle.truffle.api.dsl.NeverDefault;
20+
import com.oracle.truffle.api.nodes.Node;
2021
import com.oracle.truffle.api.object.Shape;
22+
import com.oracle.truffle.api.profiles.InlinedIntValueProfile;
23+
import com.oracle.truffle.api.profiles.InlinedLoopConditionProfile;
2124
import com.oracle.truffle.api.profiles.LoopConditionProfile;
2225
import com.oracle.truffle.api.source.SourceSection;
2326
import com.oracle.truffle.api.strings.TruffleString;
@@ -477,7 +480,7 @@ protected RubyArray clear(RubyArray array,
477480
@Cached IsSharedNode isSharedNode,
478481
@Cached ConditionProfile sharedProfile) {
479482
setStoreAndSize(array,
480-
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(array))),
483+
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(this, array))),
481484
0);
482485
return array;
483486
}
@@ -1058,25 +1061,26 @@ public abstract static class HashNode extends PrimitiveArrayArgumentsNode {
10581061
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
10591062

10601063
@Specialization(limit = "storageStrategyLimit()")
1061-
protected long hash(VirtualFrame frame, RubyArray array,
1064+
protected static long hash(VirtualFrame frame, RubyArray array,
10621065
@Bind("array.getStore()") Object store,
10631066
@CachedLibrary("store") ArrayStoreLibrary stores,
10641067
@Cached HashingNodes.ToHashByHashCode toHashByHashCode,
1065-
@Cached @Shared IntValueProfile arraySizeProfile,
1066-
@Cached @Shared LoopConditionProfile loopProfile) {
1067-
final int size = arraySizeProfile.profile(array.size);
1068-
long h = getContext().getHashing(this).start(size);
1068+
@Cached @Shared InlinedIntValueProfile arraySizeProfile,
1069+
@Cached @Shared InlinedLoopConditionProfile loopProfile,
1070+
@Bind("this") Node node) {
1071+
final int size = arraySizeProfile.profile(node, array.size);
1072+
long h = getContext(node).getHashing(node).start(size);
10691073
h = Hashing.update(h, CLASS_SALT);
10701074

10711075
int n = 0;
10721076
try {
1073-
for (; loopProfile.inject(n < size); n++) {
1077+
for (; loopProfile.inject(node, n < size); n++) {
10741078
final Object value = stores.read(store, n);
1075-
h = Hashing.update(h, toHashByHashCode.execute(value));
1076-
TruffleSafepoint.poll(this);
1079+
h = Hashing.update(h, toHashByHashCode.execute(node, value));
1080+
TruffleSafepoint.poll(node);
10771081
}
10781082
} finally {
1079-
profileAndReportLoopCount(loopProfile, n);
1083+
profileAndReportLoopCount(node, loopProfile, n);
10801084
}
10811085

10821086
return Hashing.end(h);
@@ -1138,7 +1142,8 @@ protected RubyArray initializeNoArgs(RubyArray array, NotProvided size, NotProvi
11381142
@Cached @Shared IsSharedNode isSharedNode,
11391143
@Cached @Shared ConditionProfile sharedProfile) {
11401144
setStoreAndSize(array,
1141-
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(array))), 0);
1145+
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(this, array))),
1146+
0);
11421147
return array;
11431148
}
11441149

@@ -1154,7 +1159,8 @@ protected RubyArray initializeOnlyBlock(
11541159
}
11551160

11561161
setStoreAndSize(array,
1157-
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(array))), 0);
1162+
ArrayStoreLibrary.initialStorage(sharedProfile.profile(isSharedNode.executeIsShared(this, array))),
1163+
0);
11581164
return array;
11591165
}
11601166

@@ -1186,7 +1192,7 @@ protected RubyArray initializeWithSizeNoValue(RubyArray array, int size, NotProv
11861192
@Cached @Shared ConditionProfile sharedProfile,
11871193
@CachedLibrary(limit = "2") @Exclusive ArrayStoreLibrary stores) {
11881194
final Object store;
1189-
if (sharedProfile.profile(isSharedNode.executeIsShared(array))) {
1195+
if (sharedProfile.profile(isSharedNode.executeIsShared(this, array))) {
11901196
store = new SharedArrayStorage(new Object[size]);
11911197
} else {
11921198
store = new Object[size];
@@ -1249,7 +1255,7 @@ protected Object initializeBlock(RubyArray array, int size, Object unusedFilling
12491255
} finally {
12501256
profileAndReportLoopCount(loopProfile, n);
12511257
Object store = arrayBuilder.finish(state, n);
1252-
if (sharedProfile.profile(isSharedNode.executeIsShared(array))) {
1258+
if (sharedProfile.profile(isSharedNode.executeIsShared(this, array))) {
12531259
store = stores.makeShared(store, n);
12541260
}
12551261
setStoreAndSize(array, store, n);
@@ -1832,7 +1838,7 @@ protected RubyArray replace(RubyArray array, RubyArray other,
18321838
@CachedLibrary(limit = "2") ArrayStoreLibrary stores) {
18331839
final int size = other.size;
18341840
Object store = cowNode.execute(other, 0, size);
1835-
if (sharedProfile.profile(isSharedNode.executeIsShared(array))) {
1841+
if (sharedProfile.profile(isSharedNode.executeIsShared(this, array))) {
18361842
store = stores.makeShared(store, size);
18371843
}
18381844
setStoreAndSize(array, store, size);
@@ -2293,9 +2299,7 @@ public abstract static class StoreToNativeNode extends PrimitiveArrayArgumentsNo
22932299
protected RubyArray storeToNative(RubyArray array,
22942300
@Bind("array.getStore()") Object store,
22952301
@CachedLibrary("store") ArrayStoreLibrary stores,
2296-
@Cached IntValueProfile arraySizeProfile,
2297-
@Cached IsSharedNode isSharedNode,
2298-
@Cached ConditionProfile sharedProfile) {
2302+
@Cached IntValueProfile arraySizeProfile) {
22992303
final int size = arraySizeProfile.profile(array.size);
23002304
Pointer pointer = Pointer.mallocAutoRelease(getLanguage(), getContext(), size * Pointer.SIZE);
23012305
Object newStore = new NativeArrayStorage(pointer, size);

0 commit comments

Comments
 (0)