Skip to content

Commit c9da64b

Browse files
committed
[GR-2798] Minor fixes in Truffle DSL usage.
PullRequest: fastr/1948
2 parents fb42fec + 38dbf89 commit c9da64b

File tree

16 files changed

+86
-55
lines changed

16 files changed

+86
-55
lines changed

com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/interop/RForeignAccessFactoryImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import com.oracle.truffle.api.CompilerAsserts;
2626
import com.oracle.truffle.api.interop.ForeignAccess;
27-
import com.oracle.truffle.api.interop.MessageResolution;
2827
import com.oracle.truffle.r.ffi.impl.interop.FFI_RForeignAccessFactoryImpl;
2928
import com.oracle.truffle.r.runtime.RInternalError;
3029
import com.oracle.truffle.r.runtime.conn.RConnection;
@@ -57,8 +56,8 @@
5756
import com.oracle.truffle.r.runtime.env.frame.ActiveBinding;
5857

5958
/**
60-
* For most types we use the {@link MessageResolution} facility to automatically generate the
61-
* factory for creating the {@link ForeignAccess} instance. The exceptions are the (many) subclasses
59+
* For most types we use the {@code MessageResolution} facility to automatically generate the
60+
* factory for creating the {@code ForeignAccess} instance. The exceptions are the (many) subclasses
6261
* of {@link RAbstractVector} as these have the same handling but the generator cannot handle
6362
* abstract classes.
6463
*

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/interop/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,7 +21,7 @@
2121
* questions.
2222
*/
2323
/**
24-
* A collection of types and {@link com.oracle.truffle.api.interop.MessageResolution} classes that
24+
* A collection of types and {@code com.oracle.truffle.api.interop.MessageResolution} classes that
2525
* support the implementations.
2626
*/
2727
package com.oracle.truffle.r.ffi.impl.interop;

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Call.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.oracle.truffle.api.CompilerAsserts;
3030
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3131
import com.oracle.truffle.api.dsl.Cached;
32-
import com.oracle.truffle.api.dsl.ImportStatic;
3332
import com.oracle.truffle.api.dsl.Specialization;
3433
import com.oracle.truffle.api.interop.ForeignAccess;
3534
import com.oracle.truffle.api.interop.InteropException;
@@ -72,7 +71,6 @@ protected TruffleObject getFunction(String name, String signature) {
7271
}
7372
}
7473

75-
@ImportStatic(FFIWrapNode.class)
7674
public abstract static class TruffleNFI_InvokeCallNode extends NodeAdapter implements InvokeCallNode {
7775

7876
@TruffleBoundary
@@ -83,7 +81,7 @@ protected TruffleObject getFunction(int arity) {
8381
@Specialization(guards = {"args.length == cachedArgsLength", "nativeCallInfo.address.asTruffleObject() == cachedAddress"})
8482
protected Object invokeCallCached(NativeCallInfo nativeCallInfo, Object[] args,
8583
@Cached("args.length") int cachedArgsLength,
86-
@Cached("create(cachedArgsLength)") FFIWrapNode[] ffiWrapNodes,
84+
@Cached("createWrappers(cachedArgsLength)") FFIWrapNode[] ffiWrapNodes,
8785
@Cached("create()") FFIUnwrapNode unwrap,
8886
@Cached("createExecute()") Node executeNode,
8987
@Cached("nativeCallInfo.address.asTruffleObject()") TruffleObject cachedAddress,
@@ -107,7 +105,7 @@ protected Object invokeCallCached(NativeCallInfo nativeCallInfo, Object[] args,
107105
@Specialization(limit = "99", guards = "args.length == cachedArgsLength")
108106
protected Object invokeCallCachedLength(NativeCallInfo nativeCallInfo, Object[] args,
109107
@Cached("args.length") int cachedArgsLength,
110-
@Cached("create(cachedArgsLength)") FFIWrapNode[] ffiWrapNodes,
108+
@Cached("createWrappers(cachedArgsLength)") FFIWrapNode[] ffiWrapNodes,
111109
@Cached("create()") FFIUnwrapNode unwrap,
112110
@Cached("createExecute()") Node executeNode) {
113111
Object result = null;
@@ -126,6 +124,10 @@ protected Object invokeCallCachedLength(NativeCallInfo nativeCallInfo, Object[]
126124
}
127125
}
128126

127+
protected static FFIWrapNode[] createWrappers(int count) {
128+
return FFIWrapNode.create(count);
129+
}
130+
129131
public static Node createExecute() {
130132
return Message.EXECUTE.createNode();
131133
}

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_UpCallsRFFIImpl.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222
*/
2323
package com.oracle.truffle.r.ffi.impl.nfi;
2424

25+
import java.util.concurrent.atomic.AtomicReference;
26+
27+
import com.oracle.truffle.api.CallTarget;
2528
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
29+
import com.oracle.truffle.api.Truffle;
30+
import com.oracle.truffle.api.frame.VirtualFrame;
2631
import com.oracle.truffle.api.interop.ForeignAccess;
2732
import com.oracle.truffle.api.interop.InteropException;
2833
import com.oracle.truffle.api.interop.Message;
2934
import com.oracle.truffle.api.interop.TruffleObject;
3035
import com.oracle.truffle.api.interop.UnsupportedMessageException;
3136
import com.oracle.truffle.api.nodes.Node;
37+
import com.oracle.truffle.api.nodes.RootNode;
3238
import com.oracle.truffle.r.ffi.impl.common.JavaUpCallsRFFIImpl;
3339
import com.oracle.truffle.r.runtime.RError;
3440
import com.oracle.truffle.r.runtime.RInternalError;
@@ -45,6 +51,7 @@
4551
public class TruffleNFI_UpCallsRFFIImpl extends JavaUpCallsRFFIImpl {
4652

4753
private final Node asPointer = Message.AS_POINTER.createNode();
54+
private AtomicReference<CallTarget> setSymbolCallTarget = new AtomicReference<>();
4855

4956
@Override
5057
public RFFIFactory.Type getRFFIType() {
@@ -85,13 +92,27 @@ public Object getCCallable(String pkgName, String functionName) {
8592
@Override
8693
@TruffleBoundary
8794
protected DotSymbol setSymbol(DLLInfo dllInfo, int nstOrd, Object routines, int index) {
88-
Node executeNode = Message.EXECUTE.createNode();
89-
try {
90-
return (DotSymbol) FFIUnwrapNode.unwrap(
91-
ForeignAccess.sendExecute(executeNode, TruffleNFI_Context.getInstance().lookupNativeFunction(NativeFunction.Rdynload_setSymbol), dllInfo, nstOrd, routines, index));
92-
} catch (InteropException ex) {
93-
throw RInternalError.shouldNotReachHere(ex);
94-
}
95+
setSymbolCallTarget.compareAndSet(null, Truffle.getRuntime().createCallTarget(new RootNode(null) {
96+
@Child private Node executeNode = Message.EXECUTE.createNode();
97+
@Child private FFIUnwrapNode unwrapNode = FFIUnwrapNode.create();
98+
99+
@Override
100+
public Object execute(VirtualFrame frame) {
101+
try {
102+
TruffleObject setSymFun = lookupSetSymbol();
103+
Object result = ForeignAccess.sendExecute(executeNode, setSymFun, frame.getArguments());
104+
return unwrapNode.execute(result);
105+
} catch (InteropException ex) {
106+
throw RInternalError.shouldNotReachHere(ex);
107+
}
108+
}
109+
110+
@TruffleBoundary
111+
private TruffleObject lookupSetSymbol() {
112+
return TruffleNFI_Context.getInstance().lookupNativeFunction(NativeFunction.Rdynload_setSymbol);
113+
}
114+
}));
115+
return (DotSymbol) setSymbolCallTarget.get().call(dllInfo, nstOrd, routines, index);
95116
}
96117

97118
@Override

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/CoerceNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -228,7 +228,7 @@ Object doCached(RList value, @SuppressWarnings("unused") int mode,
228228
}
229229

230230
@Specialization(replaces = {"doCachedNotList", "doCached"}, guards = {"!isS4Object(value)", "isValidMode(mode)"})
231-
Object doCached(Object value, int mode) {
231+
Object doGeneric(Object value, int mode) {
232232
CompilerDirectives.transferToInterpreter();
233233
String type = value != null ? value.getClass().getSimpleName() : "null";
234234
throw RInternalError.unimplemented("Rf_coerceVector unimplemented for type %s or mode %s.", type, mode);

com.oracle.truffle.r.ffi.processor/src/com/oracle/truffle/r/ffi/processor/FFIProcessor.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ private void generateCallClass(ExecutableElement m) throws IOException {
236236
w.append("import com.oracle.truffle.api.frame.VirtualFrame;\n");
237237
w.append("import com.oracle.truffle.api.interop.ForeignAccess;\n");
238238
w.append("import com.oracle.truffle.api.interop.TruffleObject;\n");
239+
w.append("import com.oracle.truffle.api.nodes.Node;\n");
239240
w.append("import com.oracle.truffle.api.nodes.RootNode;\n");
240241
w.append("import com.oracle.truffle.r.runtime.context.RContext;\n");
241242
if (!returnKind.isPrimitive() && returnKind != TypeKind.VOID) {
@@ -244,6 +245,7 @@ private void generateCallClass(ExecutableElement m) throws IOException {
244245
w.append("import com.oracle.truffle.r.runtime.ffi.RFFIContext;\n");
245246
w.append("import com.oracle.truffle.r.runtime.ffi.RFFILog;\n");
246247
w.append("import com.oracle.truffle.r.ffi.impl.upcalls.UpCallsRFFI;\n");
248+
w.append("import com.oracle.truffle.r.ffi.impl.upcalls.UpCallsRFFI.HandleUpCallExceptionNode;\n");
247249
w.append("import com.oracle.truffle.r.runtime.data.RTruffleObject;\n");
248250

249251
if (needsUnwrapImport) {
@@ -326,8 +328,7 @@ private void generateCallClass(ExecutableElement m) throws IOException {
326328
}
327329

328330
}
329-
// TODO: turn this field into "@Child private", initialize lazily
330-
w.append(" com.oracle.truffle.r.ffi.impl.upcalls.UpCallsRFFI.HandleUpCallExceptionNode handleExceptionNode = upCallsImpl.createHandleUpCallExceptionNode();");
331+
w.append(" @Child private HandleUpCallExceptionNode handleExceptionNode;");
331332
w.append("\n");
332333
w.append(" @Override\n");
333334
w.append(" public Object execute(VirtualFrame frame) {\n");
@@ -388,7 +389,7 @@ private void generateCallClass(ExecutableElement m) throws IOException {
388389
w.append(" } catch (Throwable ex) {\n");
389390
w.append(" CompilerDirectives.transferToInterpreter();\n");
390391
w.append(" RFFILog.logException(ex);\n");
391-
w.append(" handleExceptionNode.execute(ex);\n");
392+
w.append(" getHandleExceptionNode().execute(ex);\n");
392393
if (returnKind.isPrimitive()) {
393394
w.append(" resultRObj = Integer.valueOf(-1);\n");
394395
} else if (returnKind != TypeKind.VOID) {
@@ -411,6 +412,15 @@ private void generateCallClass(ExecutableElement m) throws IOException {
411412
w.append(" return resultRObj;\n");
412413
}
413414
w.append(" }\n");
415+
w.append(" \n");
416+
w.append(" private HandleUpCallExceptionNode getHandleExceptionNode() {\n");
417+
w.append(" if (handleExceptionNode == null) {\n");
418+
w.append(" CompilerDirectives.transferToInterpreterAndInvalidate();\n");
419+
w.append(" Node n = (Node) upCallsImpl.createHandleUpCallExceptionNode();\n");
420+
w.append(" handleExceptionNode = (HandleUpCallExceptionNode) insert(n);\n");
421+
w.append(" }\n");
422+
w.append(" return handleExceptionNode;\n");
423+
w.append(" }\n");
414424
w.append(" });\n");
415425
w.append(" }\n");
416426
w.append(" }\n");

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/IsFiniteFunctions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -163,16 +163,16 @@ protected RLogicalVector doComplete(RAbstractIntVector vec) {
163163
return doFunConstant(vec, RRuntime.LOGICAL_TRUE);
164164
}
165165

166-
@Specialization(replaces = "doComplete")
167-
protected RLogicalVector doIsFinite(RAbstractIntVector vec) {
168-
return doFunInt(vec, value -> !RRuntime.isNA(value));
169-
}
170-
171166
@Specialization(guards = "vec.isComplete()")
172167
protected RLogicalVector doComplete(RAbstractLogicalVector vec) {
173168
return doFunConstant(vec, RRuntime.LOGICAL_TRUE);
174169
}
175170

171+
@Specialization(replaces = "doComplete")
172+
protected RLogicalVector doIsFinite(RAbstractIntVector vec) {
173+
return doFunInt(vec, value -> !RRuntime.isNA(value));
174+
}
175+
176176
@Specialization(replaces = "doComplete")
177177
protected RLogicalVector doIsFinite(RAbstractLogicalVector vec) {
178178
return doFunLogical(vec, value -> !RRuntime.isNA(value));

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/MakeUnique.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -81,7 +81,7 @@ protected RAbstractStringVector makeUniqueGeneric(RAbstractStringVector names, S
8181
}
8282

8383
@Specialization
84-
protected RAbstractStringVector makeUnique(RStringSequence names, @SuppressWarnings("unused") String sep) {
84+
protected RAbstractStringVector makeUniqueSequence(RStringSequence names, @SuppressWarnings("unused") String sep) {
8585
// a string sequence cannot have duplicates if stride is not zero
8686
if (names.getStride() != 0) {
8787
return names;

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Match.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public abstract class Match extends RBuiltinNode.Arg4 {
7777
logicalFalse());
7878
}
7979

80-
protected static ProfiledMatchInternalNode createInternal() {
80+
protected static ProfiledMatchInternalNode createProfiledMatchInternal() {
8181
return ProfiledMatchInternalNodeGen.create();
8282
}
8383

@@ -120,41 +120,41 @@ protected boolean isFactor(Object o) {
120120
@Specialization(guards = {"isFactor(x)", "isFactor(table)"})
121121
protected Object matchFactor(RAbstractIntVector x, RAbstractIntVector table, int nomatch, @SuppressWarnings("unused") Object incomparables,
122122
@Cached("create()") RFactorNodes.GetLevels getLevelsNode,
123-
@Cached("createInternal()") ProfiledMatchInternalNode match) {
123+
@Cached("createProfiledMatchInternal()") ProfiledMatchInternalNode match) {
124124
return match.execute(RClosures.createFactorToVector(x, true, getLevelsNode.execute(x)),
125125
RClosures.createFactorToVector(table, true, getLevelsNode.execute(table)), nomatch);
126126
}
127127

128128
@Specialization(guards = {"isFactor(x)", "!isFactor(table)"})
129129
protected Object matchFactor(RAbstractIntVector x, RAbstractVector table, int nomatch, @SuppressWarnings("unused") Object incomparables,
130130
@Cached("create()") RFactorNodes.GetLevels getLevelsNode,
131-
@Cached("createInternal()") ProfiledMatchInternalNode match) {
131+
@Cached("createProfiledMatchInternal()") ProfiledMatchInternalNode match) {
132132
return match.execute(RClosures.createFactorToVector(x, true, getLevelsNode.execute(x)), table, nomatch);
133133
}
134134

135135
@Specialization(guards = {"!isFactor(x)", "isFactor(table)"})
136136
protected Object matchFactor(RAbstractVector x, RAbstractIntVector table, int nomatch, @SuppressWarnings("unused") Object incomparables,
137137
@Cached("create()") RFactorNodes.GetLevels getLevelsNode,
138-
@Cached("createInternal()") ProfiledMatchInternalNode match) {
138+
@Cached("createProfiledMatchInternal()") ProfiledMatchInternalNode match) {
139139
return match.execute(x, RClosures.createFactorToVector(table, true, getLevelsNode.execute(table)), nomatch);
140140
}
141141

142142
@Specialization(guards = {"isCharSXP(x)", "isCharSXP(table)"})
143143
protected Object matchDoubleList(RAbstractListVector x, RAbstractListVector table, int nomatchObj, @SuppressWarnings("unused") Object incomparables,
144-
@Cached("createInternal()") ProfiledMatchInternalNode match) {
144+
@Cached("createProfiledMatchInternal()") ProfiledMatchInternalNode match) {
145145
return match.execute(x, table, nomatchObj);
146146
}
147147

148148
@Specialization(guards = {"!isRAbstractIntVector(table) || !isFactor(table)"})
149149
protected Object matchList(RAbstractListVector x, RAbstractVector table, int nomatchObj, @SuppressWarnings("unused") Object incomparables,
150150
@Cached("create()") CastStringNode cast,
151-
@Cached("createInternal()") ProfiledMatchInternalNode match) {
151+
@Cached("createProfiledMatchInternal()") ProfiledMatchInternalNode match) {
152152
return match.execute((RAbstractVector) cast.doCast(x), table, nomatchObj);
153153
}
154154

155155
@Specialization(guards = {"!isRAbstractListVector(x)", "!isRAbstractIntVector(x) || !isFactor(x)", "!isRAbstractIntVector(table) || !isFactor(table)"})
156156
protected Object match(RAbstractVector x, RAbstractVector table, int noMatch, @SuppressWarnings("unused") Object incomparables,
157-
@Cached("createInternal()") ProfiledMatchInternalNode match) {
157+
@Cached("createProfiledMatchInternal()") ProfiledMatchInternalNode match) {
158158
return match.execute(x, table, noMatch);
159159
}
160160

@@ -169,21 +169,21 @@ protected abstract static class ProfiledMatchInternalNode extends Node {
169169

170170
protected abstract Object execute(RAbstractVector x, RAbstractVector table, int noMatch);
171171

172-
protected static MatchInternalNode createInternal() {
172+
protected static MatchInternalNode createMatchInternal() {
173173
return MatchInternalNodeGen.create();
174174
}
175175

176176
@Specialization(limit = "getCacheSize(2)", guards = {"x.getClass() == xClass", "table.getClass() == tableClass"})
177177
protected static Object matchProfiled(RAbstractVector x, RAbstractVector table, int noMatch,
178178
@Cached("x.getClass()") Class<? extends RAbstractVector> xClass,
179179
@Cached("table.getClass()") Class<? extends RAbstractVector> tableClass,
180-
@Cached("createInternal()") MatchInternalNode match) {
180+
@Cached("createMatchInternal()") MatchInternalNode match) {
181181
return match.execute(xClass.cast(x), tableClass.cast(table), noMatch);
182182
}
183183

184184
@Specialization(replaces = "matchProfiled")
185185
protected static Object match(RAbstractVector x, RAbstractVector table, int noMatch,
186-
@Cached("createInternal()") MatchInternalNode match) {
186+
@Cached("createMatchInternal()") MatchInternalNode match) {
187187
return match.execute(x, table, noMatch);
188188
}
189189
}

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/MatchFun.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ protected RFunction matchfunGeneric(VirtualFrame frame, @SuppressWarnings("unuse
139139

140140
@SuppressWarnings("unused")
141141
@Specialization(limit = "getCacheSize(LIMIT)", guards = {"funValue == cachedFunValue", "getCallerFrameDescriptor(frame) == cachedCallerFrameDescriptor"})
142-
protected RFunction matchfunCached(VirtualFrame frame, RPromise funPromise, RSymbol funValue, boolean descend,
142+
protected RFunction matchfunSymbolCached(VirtualFrame frame, RPromise funPromise, RSymbol funValue, boolean descend,
143143
@Cached("funValue") RSymbol cachedFunValue,
144144
@Cached("getCallerFrameDescriptor(frame)") FrameDescriptor cachedCallerFrameDescriptor,
145145
@Cached("createLookup(cachedFunValue.getName(), descend)") ReadVariableNode lookup) {
146146
return checkResult(lookup.execute(frame, getCallerFrame.execute(frame)));
147147
}
148148

149-
@Specialization(replaces = "matchfunCached")
150-
protected RFunction matchfunGeneric(VirtualFrame frame, @SuppressWarnings("unused") RPromise funPromise, RSymbol funValue, boolean descend) {
149+
@Specialization(replaces = "matchfunSymbolCached")
150+
protected RFunction matchfunSymbolGeneric(VirtualFrame frame, @SuppressWarnings("unused") RPromise funPromise, RSymbol funValue, boolean descend) {
151151
return checkResult(slowPathLookup(funValue.getName(), getCallerFrame.execute(frame), descend));
152152
}
153153

0 commit comments

Comments
 (0)