Skip to content

Commit 8cd3cbd

Browse files
committed
[GR-13928] Remove reads from FastROptions from fast-path.
PullRequest: fastr/1917
2 parents aa91329 + 68c9c2b commit 8cd3cbd

File tree

9 files changed

+125
-94
lines changed

9 files changed

+125
-94
lines changed

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

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 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
@@ -30,9 +30,9 @@
3030

3131
import java.util.Arrays;
3232

33-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3433
import com.oracle.truffle.api.dsl.Cached;
3534
import com.oracle.truffle.api.dsl.Specialization;
35+
import com.oracle.truffle.api.nodes.ControlFlowException;
3636
import com.oracle.truffle.api.profiles.BranchProfile;
3737
import com.oracle.truffle.api.profiles.ConditionProfile;
3838
import com.oracle.truffle.api.profiles.ValueProfile;
@@ -42,6 +42,7 @@
4242
import com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef;
4343
import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
4444
import com.oracle.truffle.r.runtime.RError;
45+
import com.oracle.truffle.r.runtime.RError.Message;
4546
import com.oracle.truffle.r.runtime.RRuntime;
4647
import com.oracle.truffle.r.runtime.RType;
4748
import com.oracle.truffle.r.runtime.builtins.RBuiltin;
@@ -65,6 +66,7 @@ public abstract static class Log extends RBuiltinNode.Arg2 {
6566

6667
private final NAProfile naX = NAProfile.create();
6768
private final BranchProfile nanProfile = BranchProfile.create();
69+
private final BranchProfile warningProfile = BranchProfile.create();
6870

6971
@Override
7072
public Object[] getDefaultParameterValues() {
@@ -206,13 +208,22 @@ private RDoubleVector logInternal(RAbstractDoubleVector vector, double base, Cop
206208
Arrays.fill(resultVector, 0, resultVector.length, Double.NaN);
207209
} else {
208210
xNACheck.enable(vector);
209-
RBaseNode[] warningCtx = new RBaseNode[1];
211+
ShowWarningException showWarning = null;
210212
for (int i = 0; i < vector.getLength(); i++) {
211213
double value = vector.getDataAt(i);
212-
resultVector[i] = xNACheck.check(value) ? RRuntime.DOUBLE_NA : logb(value, base, warningCtx);
214+
if (xNACheck.check(value)) {
215+
resultVector[i] = RRuntime.DOUBLE_NA;
216+
} else {
217+
try {
218+
resultVector[i] = logb(value, base);
219+
} catch (ShowWarningException ex) {
220+
showWarning = ex;
221+
resultVector[i] = ex.result;
222+
}
223+
}
213224
}
214-
if (warningCtx[0] != null) {
215-
RError.warning(warningCtx[0], RError.Message.NAN_PRODUCED);
225+
if (showWarning != null) {
226+
RError.warning(showWarning.context, showWarning.message);
216227
}
217228
}
218229
boolean complete = xNACheck.neverSeenNA() && baseNACheck.neverSeenNA();
@@ -228,32 +239,50 @@ private double logb(double x, double base, NAProfile naBase) {
228239
nanProfile.enter();
229240
return base;
230241
}
231-
RBaseNode[] warningCtx = new RBaseNode[1];
232-
double ret = logb(x, base, warningCtx);
233-
if (warningCtx[0] != null) {
234-
RError.warning(warningCtx[0], RError.Message.NAN_PRODUCED);
242+
try {
243+
return logb(x, base);
244+
} catch (ShowWarningException showWarn) {
245+
RError.warning(showWarn.context, showWarn.message);
246+
return showWarn.result;
235247
}
236-
return ret;
237248
}
238249

239-
@TruffleBoundary
240-
private double logb(double x, double base, RBaseNode[] warningCtx) {
250+
private double logb(double x, double base) throws ShowWarningException {
241251
double logx = Math.log(x);
252+
RBaseNode warningCtx = null;
242253
if (!Double.isNaN(x) && Double.isNaN(logx)) {
243-
warningCtx[0] = this;
254+
warningProfile.enter();
255+
warningCtx = this;
244256
}
257+
double result;
245258
if (base == Math.E) {
246-
return logx;
259+
result = logx;
260+
} else {
261+
result = logx / Math.log(base);
262+
if (warningCtx == null && Double.isNaN(result)) {
263+
warningProfile.enter();
264+
warningCtx = RError.SHOW_CALLER;
265+
}
247266
}
248-
249-
double result = logx / Math.log(base);
250-
if (warningCtx[0] == null && Double.isNaN(result)) {
251-
warningCtx[0] = RError.SHOW_CALLER;
267+
if (warningCtx != null) {
268+
throw new ShowWarningException(result, warningCtx, RError.Message.NAN_PRODUCED);
252269
}
253-
254270
return result;
255271
}
256272

273+
private static final class ShowWarningException extends ControlFlowException {
274+
private static final long serialVersionUID = -5922014313815330744L;
275+
final RBaseNode context;
276+
final RError.Message message;
277+
final double result;
278+
279+
ShowWarningException(double result, RBaseNode context, Message message) {
280+
this.result = result;
281+
this.context = context;
282+
this.message = message;
283+
}
284+
}
285+
257286
private RComplexVector logInternal(RAbstractComplexVector vector, RComplex base, BinaryMapArithmeticFunctionNode divNode,
258287
InitDimsNamesDimNamesNode initDimsNamesDimNames, CopyOfRegAttributesNode copyAttrsNode, NACheck xNACheck, NACheck baseNACheck) {
259288
baseNACheck.enable(base);

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.truffle.r.runtime.builtins.RBuiltin;
5151
import com.oracle.truffle.r.runtime.context.ChildContextInfo;
5252
import com.oracle.truffle.r.runtime.context.EvalThread;
53+
import com.oracle.truffle.r.runtime.context.FastROptions;
5354
import com.oracle.truffle.r.runtime.context.RContext;
5455
import com.oracle.truffle.r.runtime.context.RContext.ConsoleIO;
5556
import com.oracle.truffle.r.runtime.context.RContext.ContextKind;
@@ -119,7 +120,7 @@ private static void handleSharedContexts(ContextKind contextKind) {
119120
public abstract static class Spawn extends RBuiltinNode.Arg2 {
120121
@Override
121122
public Object[] getDefaultParameterValues() {
122-
return new Object[]{RMissing.instance, RContext.getInstance().getOption(SharedContexts) ? "SHARE_ALL" : "SHARE_NOTHING"};
123+
return new Object[]{RMissing.instance, FastROptions.sharedContextsOptionValue ? "SHARE_ALL" : "SHARE_NOTHING"};
123124
}
124125

125126
static {

com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/function/ArgumentStatePush.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public ArgumentStatePush(int index) {
6363
}
6464

6565
protected int createWriteArgMask(VirtualFrame frame, RShareable shareable) {
66+
if (RContext.getInstance().getOption(RefCountIncrementOnly)) {
67+
return -1;
68+
}
6669
if (shareable instanceof RAbstractContainer) {
6770
if ((shareable instanceof RPairList && ((RPairList) shareable).isLanguage()) || ((RAbstractContainer) shareable).getLength() < REF_COUNT_SIZE_THRESHOLD) {
6871
// don't decrement ref count for small objects or language objects- this
@@ -97,7 +100,7 @@ public void transitionState(VirtualFrame frame, RSharingAttributeStorage shareab
97100
@Cached("createWriteArgMask(frame, shareable)") int writeArgMask) {
98101
if (isRefCountUpdateable.profile(!shareable.isSharedPermanent())) {
99102
shareable.incRefCount();
100-
if (writeArgMask != -1 && !RContext.getInstance().getOption(RefCountIncrementOnly)) {
103+
if (writeArgMask != -1) {
101104
if (frameSlot == null) {
102105
CompilerDirectives.transferToInterpreterAndInvalidate();
103106
synchronized (FrameSlotChangeMonitor.class) {

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/DSLConfig.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,26 @@
2222
*/
2323
package com.oracle.truffle.r.runtime;
2424

25-
import static com.oracle.truffle.r.runtime.context.FastROptions.DSLCacheSizeFactor;
26-
import com.oracle.truffle.r.runtime.context.RContext;
25+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
2726

2827
/**
2928
* Class that should eventually contain all DSL (and AST rewriting) related constants.
3029
*/
3130
public final class DSLConfig {
3231

32+
@CompilationFinal private static double cacheSizeFactor = -1;
33+
3334
private DSLConfig() {
3435
// only static methods
3536
}
3637

38+
public static void initialize(double cacheSizeFactorValue) {
39+
if (cacheSizeFactor != -1 && cacheSizeFactorValue != cacheSizeFactor) {
40+
throw RInternalError.shouldNotReachHere("DSLCacheSizeFactor option must be initialized to the same value for all the contexts in one JVM.");
41+
}
42+
cacheSizeFactor = cacheSizeFactorValue;
43+
}
44+
3745
/**
3846
* This method should be used to set any cache size that is used to create specialized variants
3947
* of vector access nodes like {@link com.oracle.truffle.r.runtime.data.nodes.VectorAccess}.
@@ -58,7 +66,7 @@ public static int getGenericVectorAccessCacheSize() {
5866
* generic specialization available.
5967
*/
6068
public static int getCacheSize(int suggestedSize) {
61-
return (int) (suggestedSize * RContext.getInstance().getNonNegativeDoubleOption(DSLCacheSizeFactor));
69+
return (int) (suggestedSize * cacheSizeFactor);
6270
}
6371

6472
/**
@@ -67,6 +75,6 @@ public static int getCacheSize(int suggestedSize) {
6775
* to {@code 1} and use this final field as a guard.
6876
*/
6977
public static boolean getLimit1Guard() {
70-
return RContext.getInstance().getNonNegativeDoubleOption(DSLCacheSizeFactor) != 0;
78+
return cacheSizeFactor != 0;
7179
}
7280
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RChannel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
import com.oracle.truffle.api.frame.MaterializedFrame;
3333
import com.oracle.truffle.api.object.DynamicObject;
3434
import static com.oracle.truffle.r.runtime.context.FastROptions.ChannelReceiveTimeout;
35-
import static com.oracle.truffle.r.runtime.context.FastROptions.SharedContexts;
35+
3636
import com.oracle.truffle.r.runtime.builtins.RBuiltinDescriptor;
3737
import com.oracle.truffle.r.runtime.conn.RConnection;
38+
import com.oracle.truffle.r.runtime.context.FastROptions;
3839
import com.oracle.truffle.r.runtime.context.RContext;
3940
import com.oracle.truffle.r.runtime.data.Closure;
4041
import com.oracle.truffle.r.runtime.data.RAttributable;
@@ -730,7 +731,7 @@ private RFunction unserializeFunction(SerializedFunction f) throws IOException {
730731
REnvironment env = (REnvironment) unserializeObject(f.getEnv());
731732
MaterializedFrame enclosingFrame = env.getFrame();
732733
RFunction fn;
733-
if (RContext.getInstance().getOption(SharedContexts)) {
734+
if (FastROptions.sharedContextsOptionValue) {
734735
fn = RDataFactory.createFunction(f.getName(), f.getPackageName(), f.getTarget(), f.getRBuiltin(), enclosingFrame);
735736
} else {
736737
HasSignature root = (HasSignature) f.getTarget().getRootNode();

0 commit comments

Comments
 (0)