Skip to content

Commit 8b03e0a

Browse files
committed
[GR-14690] Performance optimizations mostly for single context mode
PullRequest: graalpython/545
2 parents b2b8ca7 + 33678b6 commit 8b03e0a

File tree

17 files changed

+77
-45
lines changed

17 files changed

+77
-45
lines changed

ci_common/mixins.libsonnet

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ local const = import 'constants.libsonnet';
2626
linux:: linux,
2727

2828
local linuxBench = linux + {
29-
capabilities +: ["no_frequency_scaling", "tmpfs25g", "x62"],
29+
capabilities +: ["no_frequency_scaling", "tmpfs25g", "x52"],
3030
},
3131
linuxBench:: linuxBench,
3232

@@ -122,4 +122,4 @@ local const = import 'constants.libsonnet';
122122
],
123123
},
124124
bench:: bench,
125-
}
125+
}

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
200200
case "-dump":
201201
if (wantsExperimental) {
202202
subprocessArgs.add("Dgraal.Dump=");
203+
subprocessArgs.add("Dgraal.TraceTruffleCompilation=true");
204+
subprocessArgs.add("Dgraal.TraceTruffleInlining=true");
205+
subprocessArgs.add("Dgraal.TraceTruffleTransferToInterpreter=true");
206+
subprocessArgs.add("Dgraal.TruffleBackgroundCompilation=false");
203207
inputArgs.remove("-dump");
204208
} else {
205209
unrecognized.add(arg);

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
import java.util.Set;
5353
import java.util.stream.Collectors;
5454

55-
import org.junit.Test;
56-
5755
import com.oracle.graal.python.PythonLanguage;
5856
import com.oracle.graal.python.builtins.objects.PNone;
5957
import com.oracle.graal.python.builtins.objects.complex.PComplex;
@@ -119,9 +117,11 @@
119117
import com.oracle.truffle.api.nodes.Node;
120118
import com.oracle.truffle.api.nodes.NodeUtil;
121119
import com.oracle.truffle.api.nodes.RootNode;
122-
import com.oracle.truffle.api.profiles.BranchProfile;
120+
import com.oracle.truffle.api.profiles.ConditionProfile;
123121
import com.oracle.truffle.api.source.Source;
124122

123+
import org.junit.Test;
124+
125125
public class TestParserTranslator {
126126
PythonContext context;
127127

@@ -131,7 +131,7 @@ public TestParserTranslator() {
131131
}
132132

133133
private static class JUnitRootNode extends PRootNode {
134-
private final BranchProfile profile = BranchProfile.create();
134+
private final ConditionProfile profile = ConditionProfile.createCountingProfile();
135135
@Child private ExpressionNode body;
136136
@Child private CalleeContext calleeContext = CalleeContext.create();
137137

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PythonCextBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ public TruffleObject getCallable() {
743743

744744
@Specialization
745745
Object doIt(VirtualFrame frame,
746-
@Cached BranchProfile customLocalsProfile,
746+
@Cached("createCountingProfile()") ConditionProfile customLocalsProfile,
747747
@Cached CExtNodes.AsPythonObjectNode asPythonObjectNode,
748748
@CachedContext(PythonLanguage.class) PythonContext ctx,
749749
@Cached PRaiseNode raiseNode) {
@@ -1475,7 +1475,7 @@ abstract static class MethodDescriptorRoot extends PRootNode {
14751475
@Child protected ReadIndexedArgumentNode readSelfNode;
14761476
@Child private CalleeContext calleeContext = CalleeContext.create();
14771477

1478-
private final BranchProfile customLocalsProfile = BranchProfile.create();
1478+
private final ConditionProfile customLocalsProfile = ConditionProfile.createCountingProfile();
14791479
protected final PythonObjectFactory factory;
14801480

14811481
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PThreadState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
import com.oracle.truffle.api.library.CachedLibrary;
8585
import com.oracle.truffle.api.library.ExportLibrary;
8686
import com.oracle.truffle.api.library.ExportMessage;
87-
import com.oracle.truffle.api.profiles.BranchProfile;
87+
import com.oracle.truffle.api.profiles.ConditionProfile;
8888
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
8989

9090
@ExportLibrary(InteropLibrary.class)
@@ -481,7 +481,7 @@ protected GetTracebackRootNode(TruffleLanguage<?> language) {
481481
@Child private GetTracebackNode getTracebackNode;
482482
@Child private CalleeContext calleeContext = CalleeContext.create();
483483

484-
private final BranchProfile profile = BranchProfile.create();
484+
private final ConditionProfile profile = ConditionProfile.createCountingProfile();
485485

486486
@Override
487487
public Object execute(VirtualFrame frame) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class PFunction extends PythonObject {
5353
private final Assumption codeStableAssumption;
5454
private final Assumption defaultsStableAssumption;
5555
private final PythonObject globals;
56-
private final PCell[] closure;
56+
@CompilationFinal(dimensions = 1) private final PCell[] closure;
5757
private final boolean isStatic;
5858
@CompilationFinal private PCode code;
5959
@CompilationFinal(dimensions = 1) private Object[] defaultValues;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ModuleRootNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
import com.oracle.truffle.api.frame.FrameDescriptor;
3939
import com.oracle.truffle.api.frame.FrameSlot;
4040
import com.oracle.truffle.api.frame.VirtualFrame;
41-
import com.oracle.truffle.api.profiles.BranchProfile;
41+
import com.oracle.truffle.api.profiles.ConditionProfile;
4242
import com.oracle.truffle.api.source.SourceSection;
4343

4444
public class ModuleRootNode extends PClosureRootNode {
4545
private static final Signature SIGNATURE = new Signature(false, -1, false, new String[0], new String[0]);
4646
private final String name;
4747
private final String doc;
48-
private final BranchProfile customLocalsProfile = BranchProfile.create();
48+
private final ConditionProfile customLocalsProfile = ConditionProfile.createCountingProfile();
4949
@Child private ExpressionNode body;
5050
@Child private WriteGlobalNode writeModuleDoc;
5151
@Child private CalleeContext calleeContext = CalleeContext.create();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PClosureFunctionRootNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
*/
4141
package com.oracle.graal.python.nodes;
4242

43+
import com.oracle.graal.python.PythonLanguage;
4344
import com.oracle.graal.python.builtins.objects.function.Signature;
4445
import com.oracle.graal.python.parser.ExecutionCellSlots;
4546
import com.oracle.truffle.api.Assumption;
4647
import com.oracle.truffle.api.CompilerDirectives;
47-
import com.oracle.truffle.api.TruffleLanguage;
4848
import com.oracle.truffle.api.frame.FrameDescriptor;
4949
import com.oracle.truffle.api.frame.FrameSlot;
5050

@@ -53,7 +53,7 @@ public abstract class PClosureFunctionRootNode extends PClosureRootNode {
5353
@CompilerDirectives.CompilationFinal(dimensions = 1) protected final Assumption[] cellEffectivelyFinalAssumptions;
5454
private final Signature signature;
5555

56-
protected PClosureFunctionRootNode(TruffleLanguage<?> language, FrameDescriptor frameDescriptor, ExecutionCellSlots executionCellSlots, Signature signature) {
56+
protected PClosureFunctionRootNode(PythonLanguage language, FrameDescriptor frameDescriptor, ExecutionCellSlots executionCellSlots, Signature signature) {
5757
super(language, frameDescriptor, executionCellSlots.getFreeVarSlots());
5858
this.cellVarSlots = executionCellSlots.getCellVarSlots();
5959
this.cellEffectivelyFinalAssumptions = executionCellSlots.getCellVarAssumptions();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PClosureRootNode.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,69 @@
4040
*/
4141
package com.oracle.graal.python.nodes;
4242

43+
import com.oracle.graal.python.PythonLanguage;
4344
import com.oracle.graal.python.builtins.objects.cell.PCell;
4445
import com.oracle.graal.python.builtins.objects.function.PArguments;
46+
import com.oracle.truffle.api.Assumption;
4547
import com.oracle.truffle.api.CompilerDirectives;
46-
import com.oracle.truffle.api.TruffleLanguage;
4748
import com.oracle.truffle.api.frame.Frame;
4849
import com.oracle.truffle.api.frame.FrameDescriptor;
4950
import com.oracle.truffle.api.frame.FrameSlot;
5051
import com.oracle.truffle.api.frame.VirtualFrame;
5152
import com.oracle.truffle.api.nodes.ExplodeLoop;
5253

5354
public abstract class PClosureRootNode extends PRootNode {
55+
private static final PCell[] NO_CLOSURE = new PCell[0];
56+
private final Assumption singleContextAssumption;
5457
@CompilerDirectives.CompilationFinal(dimensions = 1) protected final FrameSlot[] freeVarSlots;
58+
@CompilerDirectives.CompilationFinal(dimensions = 1) protected PCell[] closure;
5559
private final int length;
5660

57-
protected PClosureRootNode(TruffleLanguage<?> language, FrameDescriptor frameDescriptor, FrameSlot[] freeVarSlots) {
61+
protected PClosureRootNode(PythonLanguage language, FrameDescriptor frameDescriptor, FrameSlot[] freeVarSlots) {
5862
super(language, frameDescriptor);
63+
this.singleContextAssumption = language.singleContextAssumption;
5964
this.freeVarSlots = freeVarSlots;
6065
this.length = freeVarSlots != null ? freeVarSlots.length : 0;
6166
}
6267

6368
protected void addClosureCellsToLocals(Frame frame) {
64-
PCell[] closure = PArguments.getClosure(frame);
65-
if (closure != null) {
69+
PCell[] frameClosure = PArguments.getClosure(frame);
70+
if (frameClosure != null) {
71+
if (singleContextAssumption.isValid() && closure == null) {
72+
CompilerDirectives.transferToInterpreterAndInvalidate();
73+
closure = frameClosure;
74+
} else if (closure != NO_CLOSURE && ((!singleContextAssumption.isValid() && closure != null) || closure != frameClosure)) {
75+
CompilerDirectives.transferToInterpreterAndInvalidate();
76+
closure = NO_CLOSURE;
77+
}
6678
assert freeVarSlots != null : "closure root node: the free var slots cannot be null when the closure is not null";
67-
assert closure.length == freeVarSlots.length : "closure root node: the closure must have the same length as the free var slots array";
68-
if (freeVarSlots.length < 32) {
69-
addClosureCellsToLocalsExploded(frame, closure);
79+
assert frameClosure.length == freeVarSlots.length : "closure root node: the closure must have the same length as the free var slots array";
80+
if (closure != null && closure != NO_CLOSURE) {
81+
if (freeVarSlots.length < 32) {
82+
addClosureCellsToLocalsExploded(frame, closure);
83+
} else {
84+
addClosureCellsToLocalsLoop(frame, closure);
85+
}
7086
} else {
71-
addClosureCellsToLocalsLoop(frame, closure);
87+
if (freeVarSlots.length < 32) {
88+
addClosureCellsToLocalsExploded(frame, frameClosure);
89+
} else {
90+
addClosureCellsToLocalsLoop(frame, frameClosure);
91+
}
7292
}
7393
}
7494
}
7595

76-
protected void addClosureCellsToLocalsLoop(Frame frame, PCell[] closure) {
96+
protected void addClosureCellsToLocalsLoop(Frame frame, PCell[] frameClosure) {
7797
for (int i = 0; i < length; i++) {
78-
frame.setObject(freeVarSlots[i], closure[i]);
98+
frame.setObject(freeVarSlots[i], frameClosure[i]);
7999
}
80100
}
81101

82102
@ExplodeLoop
83-
protected void addClosureCellsToLocalsExploded(Frame frame, PCell[] closure) {
103+
protected void addClosureCellsToLocalsExploded(Frame frame, PCell[] frameClosure) {
84104
for (int i = 0; i < length; i++) {
85-
frame.setObject(freeVarSlots[i], closure[i]);
105+
frame.setObject(freeVarSlots[i], frameClosure[i]);
86106
}
87107
}
88108

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/ReadAttributeFromDynamicObjectNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ protected Object readDirectFinal(DynamicObject dynamicObject, Object key,
122122
}, //
123123
assumptions = {
124124
"layoutAssumption"
125-
})
125+
}, //
126+
replaces = "readDirectFinal")
126127
protected Object readDirect(DynamicObject dynamicObject, Object key,
127128
@Cached("key") Object cachedKey,
128129
@Cached("attrKey(cachedKey)") Object attrKey,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/BuiltinFunctionRootNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@
4848
import com.oracle.truffle.api.dsl.NodeFactory;
4949
import com.oracle.truffle.api.frame.VirtualFrame;
5050
import com.oracle.truffle.api.nodes.Node;
51-
import com.oracle.truffle.api.profiles.BranchProfile;
51+
import com.oracle.truffle.api.profiles.ConditionProfile;
5252

5353
public final class BuiltinFunctionRootNode extends PRootNode {
5454
private final Signature signature;
5555
private final Builtin builtin;
5656
private final String name;
5757
private final NodeFactory<? extends PythonBuiltinBaseNode> factory;
5858
private final boolean declaresExplicitSelf;
59-
private final BranchProfile customLocalsProfile = BranchProfile.create();
59+
private final ConditionProfile customLocalsProfile = ConditionProfile.createCountingProfile();
6060
@Child private BuiltinCallNode body;
6161
@Child private CalleeContext calleeContext = CalleeContext.create();
6262

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/FunctionRootNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import com.oracle.truffle.api.frame.VirtualFrame;
4545
import com.oracle.truffle.api.nodes.ExplodeLoop;
4646
import com.oracle.truffle.api.nodes.NodeUtil;
47-
import com.oracle.truffle.api.profiles.BranchProfile;
47+
import com.oracle.truffle.api.profiles.ConditionProfile;
4848
import com.oracle.truffle.api.profiles.ValueProfile;
4949
import com.oracle.truffle.api.source.SourceSection;
5050

@@ -59,7 +59,7 @@ public class FunctionRootNode extends PClosureFunctionRootNode {
5959
private final SourceSection sourceSection;
6060
private final boolean isGenerator;
6161
private final ValueProfile generatorFrameProfile;
62-
private final BranchProfile customLocalsProfile = BranchProfile.create();
62+
private final ConditionProfile customLocalsProfile = ConditionProfile.createCountingProfile();
6363

6464
@Child private ExpressionNode body;
6565
@Child private CalleeContext calleeContext = CalleeContext.create();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/ChannelNodes.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ public abstract static class WriteToChannelNode extends WriteToChannelBaseNode {
298298

299299
@Specialization
300300
int writeSeekable(SeekableByteChannel channel, SequenceStorage s, int len,
301+
@Cached BranchProfile limitProfile,
302+
@Cached("createBinaryProfile()") ConditionProfile maxSizeProfile,
301303
@Cached PRaiseNode raise) {
302304
long availableSize;
303305
try {
@@ -306,31 +308,34 @@ int writeSeekable(SeekableByteChannel channel, SequenceStorage s, int len,
306308
gotException.enter();
307309
throw raise.raise(OSError, e);
308310
}
309-
if (availableSize > MAX_WRITE) {
311+
if (maxSizeProfile.profile(availableSize > MAX_WRITE)) {
310312
availableSize = MAX_WRITE;
311313
}
312314
int sz = (int) Math.min(availableSize, len);
313-
return writeWritable(channel, s, sz, raise);
314-
315+
return writeWritable(channel, s, sz, limitProfile, raise);
315316
}
316317

317318
@Specialization
318319
int writeWritable(WritableByteChannel channel, SequenceStorage s, int len,
320+
@Cached BranchProfile limitProfile,
319321
@Cached PRaiseNode raise) {
320322
ByteBuffer src = allocateBuffer(getBytes(s));
321323
if (src.remaining() > len) {
324+
limitProfile.enter();
322325
src.limit(len);
323326
}
324327
return writeFromBuffer(channel, src, raise);
325328
}
326329

327330
@Specialization
328331
int writeGeneric(Channel channel, SequenceStorage s, int len,
332+
@Cached BranchProfile limitProfile,
333+
@Cached("createBinaryProfile()") ConditionProfile maxSizeProfile,
329334
@Cached PRaiseNode raise) {
330335
if (channel instanceof SeekableByteChannel) {
331-
return writeSeekable((SeekableByteChannel) channel, s, len, raise);
336+
return writeSeekable((SeekableByteChannel) channel, s, len, limitProfile, maxSizeProfile, raise);
332337
} else if (channel instanceof ReadableByteChannel) {
333-
return writeWritable((WritableByteChannel) channel, s, len, raise);
338+
return writeWritable((WritableByteChannel) channel, s, len, limitProfile, raise);
334339
} else {
335340
throw raise.raise(OSError, "file not opened for reading");
336341
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
import com.oracle.truffle.api.frame.VirtualFrame;
6868
import com.oracle.truffle.api.nodes.Node;
6969
import com.oracle.truffle.api.nodes.Node.Child;
70-
import com.oracle.truffle.api.profiles.BranchProfile;
70+
import com.oracle.truffle.api.profiles.ConditionProfile;
7171

7272
/**
7373
* A handler for asynchronous actions events that need to be handled on a main thread of execution,
@@ -142,7 +142,7 @@ private static class CallRootNode extends PRootNode {
142142
@Child private ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode.create();
143143
@Child private CalleeContext calleeContext = CalleeContext.create();
144144

145-
private final BranchProfile profile = BranchProfile.create();
145+
private final ConditionProfile profile = ConditionProfile.createCountingProfile();
146146

147147
protected CallRootNode(TruffleLanguage<?> language) {
148148
super(language);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/ExecutionContext.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import com.oracle.truffle.api.frame.FrameInstance;
5959
import com.oracle.truffle.api.frame.VirtualFrame;
6060
import com.oracle.truffle.api.nodes.Node;
61-
import com.oracle.truffle.api.profiles.BranchProfile;
6261
import com.oracle.truffle.api.profiles.ConditionProfile;
6362

6463
/**
@@ -179,16 +178,15 @@ public static final class CalleeContext extends Node {
179178
/**
180179
* Wrap the execution of a Python callee called from a Python frame.
181180
*/
182-
public static void enter(VirtualFrame frame, BranchProfile profile) {
181+
public static void enter(VirtualFrame frame, ConditionProfile profile) {
183182
// tfel: Create our frame reference here and store it so that
184183
// there's no reference to it from the caller side.
185184
PFrame.Reference thisFrameRef = new PFrame.Reference(PArguments.getCallerFrameInfo(frame));
186185
Object customLocals = PArguments.getCustomLocals(frame);
187186
PArguments.setCurrentFrameInfo(frame, thisFrameRef);
188187
// tfel: If there are custom locals, write them into an (incomplete)
189188
// PFrame here
190-
if (customLocals != null && !(customLocals instanceof PFrame.Reference)) {
191-
profile.enter();
189+
if (profile.profile(customLocals != null && !(customLocals instanceof PFrame.Reference))) {
192190
thisFrameRef.setCustomLocals(customLocals);
193191
}
194192
}

mx.graalpython/mx_graalpython.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from mx_gate import Task
4444
from mx_graalpython_bench_param import PATH_MESO, BENCHMARKS
4545
from mx_graalpython_benchmark import PythonBenchmarkSuite, python_vm_registry, CPythonVm, PyPyVm, GraalPythonVm, \
46-
CONFIGURATION_DEFAULT, CONFIG_EXPERIMENTAL_SPLITTING, CONFIGURATION_SANDBOXED, CONFIGURATION_NATIVE
46+
CONFIGURATION_DEFAULT, CONFIG_EXPERIMENTAL_SPLITTING, CONFIGURATION_SANDBOXED, CONFIGURATION_NATIVE, CONFIGURATION_DUMP
4747

4848
SUITE = mx.suite('graalpython')
4949
SUITE_COMPILER = mx.suite("compiler", fatalIfMissing=False)
@@ -1003,6 +1003,9 @@ def _register_vms(namespace):
10031003
python_vm_registry.add_vm(GraalPythonVm(config_name=CONFIGURATION_NATIVE, extra_polyglot_args=[
10041004
"--llvm.sandboxed=false"
10051005
]), SUITE, 10)
1006+
python_vm_registry.add_vm(GraalPythonVm(config_name=CONFIGURATION_DUMP, extra_polyglot_args=[
1007+
"--experimental-options", "-dump"
1008+
]), SUITE, 10)
10061009

10071010

10081011
def _register_bench_suites(namespace):

mx.graalpython/mx_graalpython_benchmark.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
CONFIGURATION_NATIVE = "native"
5959
CONFIG_EXPERIMENTAL_SPLITTING = "experimental_splitting"
6060
CONFIGURATION_SANDBOXED = "sandboxed"
61+
CONFIGURATION_DUMP = "dump"
6162

6263
DEFAULT_ITERATIONS = 10
6364

@@ -311,7 +312,7 @@ def rules(self, output, benchmarks, bm_suite_args):
311312
]
312313

313314
def runAndReturnStdOut(self, benchmarks, bmSuiteArgs):
314-
# host-vm rewrite rules
315+
# host-vm rewrite rules
315316
ret_code, out, dims = super(PythonBenchmarkSuite, self).runAndReturnStdOut(benchmarks, bmSuiteArgs)
316317

317318
def _replace_host_vm(key):

0 commit comments

Comments
 (0)