Skip to content

Commit 8639c06

Browse files
committed
[GR-45042] Converting nodes to DSL inlinable
PullRequest: truffleruby/3836
2 parents 38cf0ba + dec7375 commit 8639c06

16 files changed

+137
-92
lines changed

src/main/java/org/truffleruby/builtins/CoreMethodNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
package org.truffleruby.builtins;
1111

1212
import com.oracle.truffle.api.CompilerDirectives;
13+
import com.oracle.truffle.api.dsl.GenerateInline;
1314
import org.truffleruby.language.RubyContextSourceNode;
1415

1516
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
1617
import org.truffleruby.language.RubyNode;
1718

1819
@GenerateNodeFactory
20+
@GenerateInline(value = false, inherit = true)
1921
public abstract class CoreMethodNode extends RubyContextSourceNode {
2022

2123
@Override

src/main/java/org/truffleruby/builtins/PrimitiveArrayArgumentsNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
*/
1010
package org.truffleruby.builtins;
1111

12+
import com.oracle.truffle.api.dsl.GenerateInline;
1213
import org.truffleruby.annotations.Primitive;
1314
import org.truffleruby.language.RubyNode;
1415

1516
import com.oracle.truffle.api.dsl.NodeChild;
1617

1718
@NodeChild(value = "argumentNodes", type = RubyNode[].class)
19+
@GenerateInline(value = false, inherit = true)
1820
public abstract class PrimitiveArrayArgumentsNode extends PrimitiveNode {
1921

2022
public abstract RubyNode[] getArgumentNodes();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ protected Object callWithCExtLockAndFrame(
252252
MutexOperations.lockInternal(getContext(), lock, this);
253253
}
254254
try {
255-
return unwrapNode.execute(
255+
return unwrapNode.execute(this,
256256
InteropNodes.execute(receiver, args, receivers, translateInteropExceptionNode));
257257
} finally {
258258
runMarksNode.execute(extensionStack);
@@ -262,7 +262,7 @@ protected Object callWithCExtLockAndFrame(
262262
}
263263
} else {
264264
try {
265-
return unwrapNode.execute(
265+
return unwrapNode.execute(this,
266266
InteropNodes.execute(receiver, args, receivers, translateInteropExceptionNode));
267267
} finally {
268268
runMarksNode.execute(extensionStack);
@@ -1787,7 +1787,7 @@ public abstract static class UnwrapValueNode extends PrimitiveArrayArgumentsNode
17871787
protected Object unwrap(Object value,
17881788
@Cached InlinedBranchProfile exceptionProfile,
17891789
@Cached UnwrapNode unwrapNode) {
1790-
Object object = unwrapNode.execute(value);
1790+
Object object = unwrapNode.execute(this, value);
17911791
if (object == null) {
17921792
exceptionProfile.enter(this);
17931793
throw new RaiseException(getContext(), coreExceptions().runtimeError(exceptionMessage(value), this));
@@ -1822,7 +1822,7 @@ public abstract static class AddToMarkList extends CoreMethodArrayArgumentsNode
18221822
protected Object addToMarkList(Object markedObject,
18231823
@Cached InlinedBranchProfile noExceptionProfile,
18241824
@Cached UnwrapNode.ToWrapperNode toWrapperNode) {
1825-
ValueWrapper wrappedValue = toWrapperNode.execute(markedObject);
1825+
ValueWrapper wrappedValue = toWrapperNode.execute(this, markedObject);
18261826
if (wrappedValue != null) {
18271827
noExceptionProfile.enter(this);
18281828
getContext().getMarkingService()
@@ -1844,7 +1844,7 @@ protected Object addToMarkList(Object guardedObject,
18441844
@Cached MarkingServiceNodes.KeepAliveNode keepAliveNode,
18451845
@Cached InlinedBranchProfile noExceptionProfile,
18461846
@Cached UnwrapNode.ToWrapperNode toWrapperNode) {
1847-
ValueWrapper wrappedValue = toWrapperNode.execute(guardedObject);
1847+
ValueWrapper wrappedValue = toWrapperNode.execute(this, guardedObject);
18481848
if (wrappedValue != null) {
18491849
noExceptionProfile.enter(this);
18501850
keepAliveNode.execute(wrappedValue);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected RubySymbol unwrapStaticSymbol(long value,
4848
@Specialization(guards = "!isStaticSymbol(value)")
4949
protected RubySymbol unwrapDynamicSymbol(Object value,
5050
@Cached UnwrapNode unwrapNode) {
51-
return (RubySymbol) unwrapNode.execute(value);
51+
return (RubySymbol) unwrapNode.execute(this, value);
5252
}
5353

5454
public static boolean isStaticSymbol(Object value) {

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

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
import com.oracle.truffle.api.CompilerDirectives;
1717
import com.oracle.truffle.api.TruffleSafepoint;
1818
import com.oracle.truffle.api.dsl.Bind;
19+
import com.oracle.truffle.api.dsl.GenerateCached;
20+
import com.oracle.truffle.api.dsl.GenerateInline;
1921
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
2022
import com.oracle.truffle.api.nodes.ExplodeLoop;
23+
import com.oracle.truffle.api.nodes.Node;
2124
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
2225
import com.oracle.truffle.api.profiles.LoopConditionProfile;
2326
import org.truffleruby.language.NotProvided;
@@ -36,140 +39,153 @@
3639
import com.oracle.truffle.api.library.CachedLibrary;
3740

3841
@GenerateUncached
42+
@GenerateInline
43+
@GenerateCached(false)
3944
@ImportStatic(ValueWrapperManager.class)
4045
public abstract class UnwrapNode extends RubyBaseNode {
4146

4247
@GenerateUncached
48+
@GenerateInline
49+
@GenerateCached(false)
4350
@ImportStatic(ValueWrapperManager.class)
4451
public abstract static class UnwrapNativeNode extends RubyBaseNode {
4552

46-
public abstract Object execute(long handle);
53+
public static Object executeUncached(long handle) {
54+
return UnwrapNodeGen.UnwrapNativeNodeGen.getUncached().execute(null, handle);
55+
}
56+
57+
public abstract Object execute(Node node, long handle);
4758

4859
@Specialization(guards = "handle == FALSE_HANDLE")
49-
protected boolean unwrapFalse(long handle) {
60+
protected static boolean unwrapFalse(long handle) {
5061
return false;
5162
}
5263

5364
@Specialization(guards = "handle == TRUE_HANDLE")
54-
protected boolean unwrapTrue(long handle) {
65+
protected static boolean unwrapTrue(long handle) {
5566
return true;
5667
}
5768

5869
@Specialization(guards = "handle == UNDEF_HANDLE")
59-
protected NotProvided unwrapUndef(long handle) {
70+
protected static NotProvided unwrapUndef(long handle) {
6071
return NotProvided.INSTANCE;
6172
}
6273

6374
@Specialization(guards = "handle == NIL_HANDLE")
64-
protected Object unwrapNil(long handle) {
75+
protected static Object unwrapNil(long handle) {
6576
return nil;
6677
}
6778

6879
@Specialization(guards = "isTaggedLong(handle)")
69-
protected long unwrapTaggedLong(long handle) {
80+
protected static long unwrapTaggedLong(long handle) {
7081
return ValueWrapperManager.untagTaggedLong(handle);
7182
}
7283

7384
@Specialization(guards = "isTaggedObject(handle)")
74-
protected Object unwrapTaggedObject(long handle,
85+
protected static Object unwrapTaggedObject(Node node, long handle,
7586
@Cached InlinedBranchProfile noHandleProfile) {
76-
final ValueWrapper wrapper = getContext()
87+
final ValueWrapper wrapper = getContext(node)
7788
.getValueWrapperManager()
78-
.getWrapperFromHandleMap(handle, getLanguage());
89+
.getWrapperFromHandleMap(handle, getLanguage(node));
7990
if (wrapper == null) {
80-
noHandleProfile.enter(this);
91+
noHandleProfile.enter(node);
8192
raiseError(handle);
8293
}
8394
return wrapper.getObject();
8495
}
8596

8697
@Fallback
87-
protected ValueWrapper unWrapUnexpectedHandle(long handle) {
98+
protected static ValueWrapper unWrapUnexpectedHandle(long handle) {
8899
// Avoid throwing a specialization exception when given an uninitialized or corrupt
89100
// handle.
90101
CompilerDirectives.transferToInterpreterAndInvalidate();
91102
throw CompilerDirectives.shouldNotReachHere("corrupt handle 0x" + Long.toHexString(handle));
92103
}
93104

94105
@TruffleBoundary
95-
private void raiseError(long handle) {
106+
private static void raiseError(long handle) {
96107
throw CompilerDirectives.shouldNotReachHere("dead handle 0x" + Long.toHexString(handle));
97108
}
98109
}
99110

100111
@GenerateUncached
112+
@GenerateInline
113+
@GenerateCached(false)
101114
@ImportStatic(ValueWrapperManager.class)
102115
public abstract static class NativeToWrapperNode extends RubyBaseNode {
103116

104-
public abstract ValueWrapper execute(long handle);
117+
public abstract ValueWrapper execute(Node node, long handle);
105118

106119
@Specialization(guards = "handle == FALSE_HANDLE")
107-
protected ValueWrapper unwrapFalse(long handle) {
120+
protected static ValueWrapper unwrapFalse(long handle) {
108121
return new ValueWrapper(false, FALSE_HANDLE, null);
109122
}
110123

111124
@Specialization(guards = "handle == TRUE_HANDLE")
112-
protected ValueWrapper unwrapTrue(long handle) {
125+
protected static ValueWrapper unwrapTrue(long handle) {
113126
return new ValueWrapper(true, TRUE_HANDLE, null);
114127
}
115128

116129
@Specialization(guards = "handle == UNDEF_HANDLE")
117-
protected ValueWrapper unwrapUndef(long handle) {
130+
protected static ValueWrapper unwrapUndef(long handle) {
118131
return new ValueWrapper(NotProvided.INSTANCE, UNDEF_HANDLE, null);
119132
}
120133

121134
@Specialization(guards = "handle == NIL_HANDLE")
122-
protected ValueWrapper unwrapNil(long handle) {
135+
protected static ValueWrapper unwrapNil(long handle) {
123136
return nil.getValueWrapper();
124137
}
125138

126139
@Specialization(guards = "isTaggedLong(handle)")
127-
protected ValueWrapper unwrapTaggedLong(long handle) {
140+
protected static ValueWrapper unwrapTaggedLong(long handle) {
128141
return new ValueWrapper(null, handle, null);
129142
}
130143

131144
@Specialization(guards = "isTaggedObject(handle)")
132-
protected ValueWrapper unwrapTaggedObject(long handle) {
133-
return getContext().getValueWrapperManager().getWrapperFromHandleMap(handle, getLanguage());
145+
protected static ValueWrapper unwrapTaggedObject(Node node, long handle) {
146+
return getContext(node).getValueWrapperManager().getWrapperFromHandleMap(handle, getLanguage(node));
134147
}
135148

136149
@Fallback
137-
protected ValueWrapper unWrapUnexpectedHandle(long handle) {
150+
protected static ValueWrapper unWrapUnexpectedHandle(long handle) {
138151
// Avoid throwing a specialization exception when given an uninitialized or corrupt
139152
// handle.
140153
return null;
141154
}
142155
}
143156

157+
@GenerateInline
158+
@GenerateCached(false)
144159
@ImportStatic(ValueWrapperManager.class)
145160
public abstract static class ToWrapperNode extends RubyBaseNode {
146161

147-
public abstract ValueWrapper execute(Object value);
162+
public abstract ValueWrapper execute(Node node, Object value);
148163

149164
@Specialization
150-
protected ValueWrapper wrappedValueWrapper(ValueWrapper value) {
165+
protected static ValueWrapper wrappedValueWrapper(ValueWrapper value) {
151166
return value;
152167
}
153168

154169
@Specialization
155-
protected ValueWrapper longToWrapper(long value,
170+
protected static ValueWrapper longToWrapper(Node node, long value,
156171
@Cached @Shared NativeToWrapperNode nativeToWrapperNode) {
157-
return nativeToWrapperNode.execute(value);
172+
return nativeToWrapperNode.execute(node, value);
158173
}
159174

160175
@Fallback
161-
protected ValueWrapper genericToWrapper(Object value,
176+
protected static ValueWrapper genericToWrapper(Node node, Object value,
162177
@CachedLibrary(limit = "getCacheLimit()") InteropLibrary values,
163178
@Cached @Shared NativeToWrapperNode nativeToWrapperNode,
164179
@Cached InlinedBranchProfile unsupportedProfile) {
165180
long handle;
166181
try {
167182
handle = values.asPointer(value);
168183
} catch (UnsupportedMessageException e) {
169-
unsupportedProfile.enter(this);
170-
throw new RaiseException(getContext(), coreExceptions().argumentError(e.getMessage(), this, e));
184+
unsupportedProfile.enter(node);
185+
throw new RaiseException(getContext(node),
186+
coreExceptions(node).argumentError(e.getMessage(), getNode(node), e));
171187
}
172-
return nativeToWrapperNode.execute(handle);
188+
return nativeToWrapperNode.execute(node, handle);
173189
}
174190

175191
protected int getCacheLimit() {
@@ -194,7 +210,7 @@ protected Object[] unwrapCArrayExplode(Object cArray,
194210
final Object[] store = new Object[cachedSize];
195211
for (int i = 0; i < cachedSize; i++) {
196212
final Object cValue = readArrayElement(cArray, interop, i);
197-
store[i] = unwrapNode.execute(cValue);
213+
store[i] = unwrapNode.execute(this, cValue);
198214
}
199215
return store;
200216
}
@@ -210,7 +226,7 @@ protected Object[] unwrapCArray(Object cArray,
210226
try {
211227
for (; loopProfile.inject(i < size); i++) {
212228
final Object cValue = readArrayElement(cArray, interop, i);
213-
store[i] = unwrapNode.execute(cValue);
229+
store[i] = unwrapNode.execute(this, cValue);
214230
TruffleSafepoint.poll(this);
215231
}
216232
} finally {
@@ -236,37 +252,38 @@ private static Object readArrayElement(Object cArray, InteropLibrary interop, in
236252
}
237253
}
238254

239-
public abstract Object execute(Object value);
255+
public abstract Object execute(Node node, Object value);
240256

241257
@Specialization(guards = "!isTaggedLong(value.getHandle())")
242-
protected Object unwrapValueObject(ValueWrapper value) {
258+
protected static Object unwrapValueObject(ValueWrapper value) {
243259
return value.getObject();
244260
}
245261

246262
@Specialization(guards = "isTaggedLong(value.getHandle())")
247-
protected long unwrapValueTaggedLong(ValueWrapper value) {
263+
protected static long unwrapValueTaggedLong(ValueWrapper value) {
248264
return ValueWrapperManager.untagTaggedLong(value.getHandle());
249265
}
250266

251267
@Specialization
252-
protected Object longToWrapper(long value,
268+
protected static Object longToWrapper(Node node, long value,
253269
@Cached @Shared UnwrapNativeNode unwrapNativeNode) {
254-
return unwrapNativeNode.execute(value);
270+
return unwrapNativeNode.execute(node, value);
255271
}
256272

257273
@Specialization(guards = { "!isWrapper(value)", "!isImplicitLong(value)" })
258-
protected Object unwrapGeneric(Object value,
274+
protected static Object unwrapGeneric(Node node, Object value,
259275
@CachedLibrary(limit = "getCacheLimit()") InteropLibrary values,
260276
@Cached @Shared UnwrapNativeNode unwrapNativeNode,
261277
@Cached InlinedBranchProfile unsupportedProfile) {
262278
long handle;
263279
try {
264280
handle = values.asPointer(value);
265281
} catch (UnsupportedMessageException e) {
266-
unsupportedProfile.enter(this);
267-
throw new RaiseException(getContext(), coreExceptions().argumentError(e.getMessage(), this, e));
282+
unsupportedProfile.enter(node);
283+
throw new RaiseException(getContext(node),
284+
coreExceptions(node).argumentError(e.getMessage(), getNode(node), e));
268285
}
269-
return unwrapNativeNode.execute(handle);
286+
return unwrapNativeNode.execute(node, handle);
270287
}
271288

272289
protected int getCacheLimit() {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.concurrent.ConcurrentHashMap;
1616
import java.util.concurrent.atomic.AtomicLong;
1717

18+
import com.oracle.truffle.api.dsl.Bind;
19+
import com.oracle.truffle.api.nodes.Node;
1820
import org.truffleruby.RubyContext;
1921
import org.truffleruby.RubyLanguage;
2022
import org.truffleruby.annotations.SuppressFBWarnings;
@@ -396,8 +398,9 @@ protected boolean isExecutable() {
396398

397399
@ExportMessage
398400
protected Object execute(Object[] arguments,
399-
@Cached UnwrapNode unwrapNode) {
400-
return unwrapNode.execute(arguments[0]);
401+
@Cached UnwrapNode unwrapNode,
402+
@Bind("$node") Node node) {
403+
return unwrapNode.execute(node, arguments[0]);
401404
}
402405
}
403406

@@ -429,8 +432,9 @@ protected boolean isExecutable() {
429432
@ExportMessage
430433
public Object execute(Object[] arguments,
431434
@Cached UnwrapNode unwrapNode,
432-
@Cached SymbolToIDNode symbolTOIDNode) {
433-
return symbolTOIDNode.execute(unwrapNode.execute(arguments[0]));
435+
@Cached SymbolToIDNode symbolTOIDNode,
436+
@Bind("$node") Node node) {
437+
return symbolTOIDNode.execute(unwrapNode.execute(node, arguments[0]));
434438
}
435439
}
436440

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected static Object javaGetEnv(Object name,
116116
if (nullValueProfile.profile(node, value == null)) {
117117
return nil;
118118
} else {
119-
return fromJavaStringNode.executeFromJavaString(value);
119+
return fromJavaStringNode.executeFromJavaString(node, value);
120120
}
121121
}
122122

0 commit comments

Comments
 (0)