Skip to content

Commit 2548852

Browse files
committed
[GR-35958] Use field instead of assumption for single context.
PullRequest: graalpython/2079
2 parents 3b2e4bf + a0e7db6 commit 2548852

File tree

55 files changed

+276
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+276
-338
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
* Copyright (c) 2015, Regents of the University of California
44
*
55
* All rights reserved.
@@ -185,6 +185,12 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
185185

186186
public final Assumption singleContextAssumption = Truffle.getRuntime().createAssumption("Only a single context is active");
187187

188+
@CompilationFinal private boolean singleContext = true;
189+
190+
public boolean isSingleContext() {
191+
return singleContext;
192+
}
193+
188194
/**
189195
* This assumption will be valid if all contexts are single-threaded. Hence, it will be
190196
* invalidated as soon as at least one context has been initialized for multi-threading.
@@ -727,6 +733,7 @@ private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) th
727733
protected void initializeMultipleContexts() {
728734
super.initializeMultipleContexts();
729735
singleContextAssumption.invalidate();
736+
singleContext = false;
730737
}
731738

732739
private final ConcurrentHashMap<String, CallTarget> cachedCode = new ConcurrentHashMap<>();

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,19 +1656,11 @@ private void checkExcessArgs(Object type, Object[] varargs, PKeyword[] kwargs) {
16561656
}
16571657
if (profileNew == null) {
16581658
CompilerDirectives.transferToInterpreterAndInvalidate();
1659-
if (getLanguage().singleContextAssumption.isValid()) {
1660-
profileNew = ValueProfile.createIdentityProfile();
1661-
} else {
1662-
profileNew = ValueProfile.createClassProfile();
1663-
}
1659+
profileNew = createValueIdentityProfile();
16641660
}
16651661
if (profileInit == null) {
16661662
CompilerDirectives.transferToInterpreterAndInvalidate();
1667-
if (getLanguage().singleContextAssumption.isValid()) {
1668-
profileInit = ValueProfile.createIdentityProfile();
1669-
} else {
1670-
profileInit = ValueProfile.createClassProfile();
1671-
}
1663+
profileInit = createValueIdentityProfile();
16721664
}
16731665
if (profileNewFactory == null) {
16741666
CompilerDirectives.transferToInterpreterAndInvalidate();

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@
219219
import com.oracle.graal.python.util.CharsetMapping;
220220
import com.oracle.graal.python.util.PythonUtils;
221221
import com.oracle.graal.python.util.Supplier;
222-
import com.oracle.truffle.api.Assumption;
223222
import com.oracle.truffle.api.CallTarget;
224223
import com.oracle.truffle.api.CompilerAsserts;
225224
import com.oracle.truffle.api.CompilerDirectives;
@@ -1766,7 +1765,6 @@ public abstract static class PrintNode extends PythonBuiltinNode {
17661765
private static final String DEFAULT_END = "\n";
17671766
private static final String DEFAULT_SEPARATOR = " ";
17681767
@Child private ReadAttributeFromObjectNode readStdout;
1769-
@CompilationFinal private Assumption singleContextAssumption;
17701768
@CompilationFinal private PythonModule cachedSys;
17711769

17721770
@Specialization
@@ -1844,12 +1842,8 @@ PNone printGeneric(VirtualFrame frame, Object[] values, Object sepIn, Object end
18441842
}
18451843

18461844
private Object getStdout() {
1847-
if (singleContextAssumption == null) {
1848-
CompilerDirectives.transferToInterpreterAndInvalidate();
1849-
singleContextAssumption = singleContextAssumption();
1850-
}
18511845
PythonModule sys;
1852-
if (singleContextAssumption.isValid()) {
1846+
if (getLanguage().isSingleContext()) {
18531847
if (cachedSys == null) {
18541848
CompilerDirectives.transferToInterpreterAndInvalidate();
18551849
cachedSys = getContext().lookupBuiltinModule("sys");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -168,7 +168,7 @@ public abstract static class GetMagic extends PythonBuiltinNode {
168168
@Child private IntBuiltins.ToBytesNode toBytesNode = IntBuiltins.ToBytesNode.create();
169169
@Child private PythonBufferAccessLibrary bufferLib = PythonBufferAccessLibrary.getFactory().createDispatched(1);
170170

171-
@Specialization(assumptions = "singleContextAssumption()")
171+
@Specialization(guards = "isSingleContext()")
172172
public PBytes runCachedSingleContext(@SuppressWarnings("unused") VirtualFrame frame,
173173
@Cached(value = "getMagicNumberPBytes(frame)", weak = true) PBytes magicBytes) {
174174
return magicBytes;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ static PyCFunctionDecorator decorate(Object fun0, Object fun1) {
17481748
@ImportStatic(PythonOptions.class)
17491749
abstract static class PyType_IsSubtype extends PythonBinaryBuiltinNode {
17501750

1751-
@Specialization(guards = {"a == cachedA", "b == cachedB"}, assumptions = "singleContextAssumption()")
1751+
@Specialization(guards = {"isSingleContext()", "a == cachedA", "b == cachedB"})
17521752
static int doCached(@SuppressWarnings("unused") VirtualFrame frame, @SuppressWarnings("unused") PythonNativeWrapper a, @SuppressWarnings("unused") PythonNativeWrapper b,
17531753
@Cached(value = "a", weak = true) @SuppressWarnings("unused") PythonNativeWrapper cachedA,
17541754
@Cached(value = "b", weak = true) @SuppressWarnings("unused") PythonNativeWrapper cachedB,
@@ -2127,7 +2127,7 @@ static Object doGeneric(String key) {
21272127
abstract static class PyTruffleTraceMallocTrack extends PythonBuiltinNode {
21282128
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(PyTruffleTraceMallocTrack.class);
21292129

2130-
@Specialization(guards = {"domain == cachedDomain"}, limit = "3", assumptions = "singleContextAssumption()")
2130+
@Specialization(guards = {"isSingleContext()", "domain == cachedDomain"}, limit = "3")
21312131
int doCachedDomainIdx(VirtualFrame frame, @SuppressWarnings("unused") long domain, Object pointerObject, long size,
21322132
@Cached GetThreadStateNode getThreadStateNode,
21332133
@Cached("domain") @SuppressWarnings("unused") long cachedDomain,
@@ -2159,7 +2159,7 @@ int lookupDomain(long domain) {
21592159
abstract static class PyTruffleTraceMallocUntrack extends PythonBinaryBuiltinNode {
21602160
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(PyTruffleTraceMallocUntrack.class);
21612161

2162-
@Specialization(guards = {"domain == cachedDomain"}, limit = "3", assumptions = "singleContextAssumption()")
2162+
@Specialization(guards = {"isSingleContext()", "domain == cachedDomain"}, limit = "3")
21632163
int doCachedDomainIdx(@SuppressWarnings("unused") long domain, Object pointerObject,
21642164
@Cached("domain") @SuppressWarnings("unused") long cachedDomain,
21652165
@Cached("lookupDomain(domain)") int cachedDomainIdx) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
import com.oracle.graal.python.runtime.PythonOptions;
121121
import com.oracle.graal.python.runtime.exception.PException;
122122
import com.oracle.graal.python.util.PythonUtils;
123-
import com.oracle.truffle.api.Assumption;
124123
import com.oracle.truffle.api.CompilerDirectives;
125124
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
126125
import com.oracle.truffle.api.TruffleLanguage;
@@ -161,14 +160,6 @@ public abstract class PythonAbstractObject extends DynamicObject implements Truf
161160
protected static final SpecialMethodSlot Iter = SpecialMethodSlot.Iter;
162161
protected static final SpecialMethodSlot Next = SpecialMethodSlot.Next;
163162

164-
public static final Assumption singleContextAssumption() {
165-
return PythonLanguage.get(null).singleContextAssumption;
166-
}
167-
168-
public static final Assumption singleContextAssumption(Node node) {
169-
return PythonLanguage.get(node).singleContextAssumption;
170-
}
171-
172163
protected static final Shape ABSTRACT_SHAPE = Shape.newBuilder().build();
173164

174165
protected PythonAbstractObject(Shape shape) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cell/CellBuiltins.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252

5353
import java.util.List;
5454

55-
import com.oracle.graal.python.PythonLanguage;
5655
import com.oracle.graal.python.builtins.Builtin;
5756
import com.oracle.graal.python.builtins.CoreFunctions;
5857
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -64,18 +63,17 @@
6463
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
6564
import com.oracle.graal.python.lib.PyObjectRichCompareBool;
6665
import com.oracle.graal.python.nodes.ErrorMessages;
66+
import com.oracle.graal.python.nodes.PNodeWithContext;
6767
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6868
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
6969
import com.oracle.graal.python.nodes.object.GetClassNode;
7070
import com.oracle.graal.python.util.PythonUtils;
71-
import com.oracle.truffle.api.Assumption;
7271
import com.oracle.truffle.api.dsl.Cached;
7372
import com.oracle.truffle.api.dsl.Fallback;
7473
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7574
import com.oracle.truffle.api.dsl.NodeFactory;
7675
import com.oracle.truffle.api.dsl.Specialization;
7776
import com.oracle.truffle.api.frame.VirtualFrame;
78-
import com.oracle.truffle.api.nodes.Node;
7977
import com.oracle.truffle.api.profiles.ConditionProfile;
8078

8179
@CoreFunctions(extendClasses = PythonBuiltinClassType.PCell)
@@ -298,16 +296,11 @@ Object set(PCell self, Object ref) {
298296
}
299297
}
300298

301-
public abstract static class GetRefNode extends Node {
299+
public abstract static class GetRefNode extends PNodeWithContext {
302300
public abstract Object execute(PCell self);
303301

304-
protected Assumption singleContextAssumption() {
305-
return PythonLanguage.get(this).singleContextAssumption;
306-
}
307-
308-
@Specialization(guards = "self == cachedSelf", assumptions = {"cachedSelf.isEffectivelyFinalAssumption()", "singleContextAssumption"}, limit = "1")
302+
@Specialization(guards = {"isSingleContext()", "self == cachedSelf"}, assumptions = {"cachedSelf.isEffectivelyFinalAssumption()"}, limit = "1")
309303
Object cached(@SuppressWarnings("unused") PCell self,
310-
@SuppressWarnings("unused") @Cached("singleContextAssumption()") Assumption singleContextAssumption,
311304
@SuppressWarnings("unused") @Cached("self") PCell cachedSelf,
312305
@Cached("self.getRef()") Object ref) {
313306
return ref;

0 commit comments

Comments
 (0)