Skip to content

Commit f6c3764

Browse files
committed
[GR-64249] [GR-64247] Review ExplodeLoop usages in Bytecode DSL interpreter.
PullRequest: graalpython/3768
2 parents 9592e53 + eab53ac commit f6c3764

File tree

19 files changed

+411
-318
lines changed

19 files changed

+411
-318
lines changed

graalpython/com.oracle.graal.python.benchmarks/python/harness.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -270,9 +270,10 @@ def get_bench_module(bench_file):
270270
name = bench_file.rpartition(os.sep)[2].partition(".")[0].replace('.py', '')
271271
directory = bench_file.rpartition(os.sep)[0]
272272
pkg = []
273-
while any(f.endswith("__init__.py") for f in os.listdir(directory)):
274-
directory, slash, postfix = directory.rpartition("/")
275-
pkg.insert(0, postfix)
273+
if directory:
274+
while any(f.endswith("__init__.py") for f in os.listdir(directory)):
275+
directory, slash, postfix = directory.rpartition("/")
276+
pkg.insert(0, postfix)
276277

277278
if pkg:
278279
sys.path.insert(0, directory)

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/objects/ObjectHashMapTests.java

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

55-
import com.oracle.graal.python.lib.RichCmpOp;
5655
import org.junit.Assert;
5756
import org.junit.Test;
5857

@@ -68,8 +67,10 @@
6867
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap;
6968
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.MapCursor;
7069
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.PopNode;
70+
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.PutUnsafeNode;
7171
import com.oracle.graal.python.lib.PyObjectHashNode;
7272
import com.oracle.graal.python.lib.PyObjectRichCompareBool;
73+
import com.oracle.graal.python.lib.RichCmpOp;
7374
import com.oracle.truffle.api.frame.Frame;
7475
import com.oracle.truffle.api.interop.TruffleObject;
7576
import com.oracle.truffle.api.nodes.Node;
@@ -100,7 +101,7 @@ public boolean execute(Frame frame, Node inliningTarget, Object a, Object b, Ric
100101

101102
@Test
102103
public void testCollisionsByPuttingManyKeysWithSameHash() {
103-
ObjectHashMap map = new ObjectHashMap();
104+
ObjectHashMap map = new ObjectHashMap(true);
104105
LinkedHashMap<DictKey, Object> expected = new LinkedHashMap<>();
105106
for (int i = 0; i < 100; i++) {
106107
DictKey key = new DictKey(42);
@@ -126,7 +127,7 @@ public void testCollisionsByPuttingManyKeysWithSameHash() {
126127

127128
@Test
128129
public void testCollisionsByPuttingAndRemovingTheSameKey() {
129-
ObjectHashMap map = new ObjectHashMap();
130+
ObjectHashMap map = new ObjectHashMap(true);
130131
LinkedHashMap<DictKey, Object> expected = new LinkedHashMap<>();
131132
DictKey key = new DictKey(42);
132133
for (int i = 0; i < 100; i++) {
@@ -143,7 +144,7 @@ public void testCollisionsByPuttingAndRemovingTheSameKey() {
143144

144145
@Test
145146
public void testCollisionsByPuttingAndRemovingTheSameKeys() {
146-
ObjectHashMap map = new ObjectHashMap();
147+
ObjectHashMap map = new ObjectHashMap(true);
147148
LinkedHashMap<DictKey, Object> expected = new LinkedHashMap<>();
148149
DictKey[] keys = new DictKey[]{new DictKey(42), new DictKey(1)};
149150
for (int i = 0; i < 100; i++) {
@@ -162,7 +163,7 @@ public void testCollisionsByPuttingAndRemovingTheSameKeys() {
162163

163164
@Test
164165
public void testLongHashMapStressTest() {
165-
ObjectHashMap map = new ObjectHashMap();
166+
ObjectHashMap map = new ObjectHashMap(true);
166167

167168
// put/remove many random (with fixed seed) keys, check consistency against LinkedHashMap
168169
testBasics(map);
@@ -373,7 +374,7 @@ private static void remove(ObjectHashMap map, Object key, long hash) {
373374

374375
private static void put(ObjectHashMap map, Object key, long hash, Object value) {
375376
InlinedCountingConditionProfile uncachedCounting = InlinedCountingConditionProfile.getUncached();
376-
ObjectHashMap.PutNode.doPutWithRestart(null, null, map, key, hash, value,
377+
PutUnsafeNode.doPutWithRestart(null, null, map, key, hash, value,
377378
InlinedBranchProfile.getUncached(), uncachedCounting, uncachedCounting, uncachedCounting,
378379
uncachedCounting, InlinedBranchProfile.getUncached(), InlinedBranchProfile.getUncached(),
379380
new EqNodeStub());

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_pystate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -111,4 +111,4 @@ def other_thread():
111111
SetAsyncExcCaller.trigger_ex(t.ident, Exception("test my message"))
112112
t.join()
113113

114-
assert "test my message" in str(caught_ex)
114+
assert "test my message" in str(caught_ex), str(caught_ex)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
5252
import com.oracle.graal.python.builtins.objects.dict.PDict;
5353
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
54-
import com.oracle.graal.python.lib.PyObjectHashNode;
5554
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5655
import com.oracle.graal.python.runtime.object.PFactory;
5756
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -82,6 +81,6 @@ public void initialize(Python3Core core) {
8281

8382
private void addConstant(int number, TruffleString name, EconomicMapStorage storage) {
8483
addBuiltinConstant(name, number);
85-
storage.putUncachedWithJavaEq(number, PyObjectHashNode.hash(number), name);
84+
storage.putUncached(number, name);
8685
}
8786
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,8 +2874,8 @@ abstract static class SysconfNode extends PythonUnaryBuiltinNode {
28742874
public static final EconomicMapStorage SYSCONF_NAMES = EconomicMapStorage.create();
28752875
static {
28762876
// TODO populate from constants
2877-
SYSCONF_NAMES.putUncachedWithJavaEq(T_SC_CLK_TCK, SC_CLK_TCK);
2878-
SYSCONF_NAMES.putUncachedWithJavaEq(T_SC_NPROCESSORS_ONLN, SC_NPROCESSORS_ONLN);
2877+
SYSCONF_NAMES.putUncached(T_SC_CLK_TCK, SC_CLK_TCK);
2878+
SYSCONF_NAMES.putUncached(T_SC_NPROCESSORS_ONLN, SC_NPROCESSORS_ONLN);
28792879
}
28802880

28812881
@Specialization

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ public void postInitialize0(Python3Core core) {
611611
}
612612
sys.setAttribute(tsLiteral("dont_write_bytecode"), context.getOption(PythonOptions.DontWriteBytecodeFlag));
613613
TruffleString pycachePrefix = context.getOption(PythonOptions.PyCachePrefix);
614+
if (pycachePrefix.isEmpty() && PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && System.getenv("CI") == null) {
615+
pycachePrefix = PythonUtils.toTruffleStringUncached("__bci_dsl_pycache__");
616+
}
614617
sys.setAttribute(tsLiteral("pycache_prefix"), pycachePrefix.isEmpty() ? PNone.NONE : pycachePrefix);
615618
sys.setAttribute(tsLiteral("_stdlib_dir"), stdlibHome);
616619

@@ -683,7 +686,7 @@ public void postInitialize0(Python3Core core) {
683686
private static PFrozenSet createStdLibModulesSet(PythonLanguage language) {
684687
EconomicMapStorage storage = EconomicMapStorage.create(STDLIB_MODULE_NAMES.length);
685688
for (String s : STDLIB_MODULE_NAMES) {
686-
storage.putUncachedWithJavaEq(s, PNone.NONE);
689+
storage.putUncached(s, PNone.NONE);
687690
}
688691
return PFactory.createFrozenSet(language, storage);
689692
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/hashlib/HashlibModuleBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
157157
public void initialize(Python3Core core) {
158158
EconomicMapStorage algos = EconomicMapStorage.create(DIGEST_ALGORITHMS.length);
159159
for (var digest : DIGEST_ALGORITHMS) {
160-
algos.putUncachedWithJavaEq(digest, PNone.NONE);
160+
algos.putUncached(digest, PNone.NONE);
161161
}
162162
PythonLanguage language = core.getLanguage();
163163
addBuiltinConstant("openssl_md_meth_names", PFactory.createFrozenSet(language, algos));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/EconomicMapStorage.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, 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
@@ -50,6 +50,7 @@
5050
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.DictKey;
5151
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.MapCursor;
5252
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.PutNode;
53+
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.PutUnsafeNode;
5354
import com.oracle.graal.python.lib.PyObjectHashNode;
5455
import com.oracle.truffle.api.CompilerAsserts;
5556
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -131,11 +132,15 @@ void clear() {
131132
map.clear();
132133
}
133134

135+
public boolean mapIsEqualTo(ObjectHashMap other) {
136+
return other == this.map;
137+
}
138+
134139
public HashingStorage copy() {
135140
return new EconomicMapStorage(this.map, true);
136141
}
137142

138-
protected void setValueForAllKeys(VirtualFrame frame, Node inliningTarget, Object value, PutNode putNode, InlinedLoopConditionProfile loopProfile) {
143+
protected void setValueForAllKeys(VirtualFrame frame, Node inliningTarget, Object value, PutUnsafeNode putNode, InlinedLoopConditionProfile loopProfile) {
139144
MapCursor cursor = map.getEntries();
140145
final int size = map.size();
141146
loopProfile.profileCounted(inliningTarget, size);
@@ -147,26 +152,23 @@ protected void setValueForAllKeys(VirtualFrame frame, Node inliningTarget, Objec
147152

148153
@TruffleBoundary
149154
public void putUncached(TruffleString key, Object value) {
150-
ObjectHashMap.PutNode.putUncached(this.map, key, PyObjectHashNode.hash(key, HashCodeNode.getUncached()), value);
151-
}
152-
153-
private void putUncached(Object key, Object value) {
154-
ObjectHashMap.PutNode.putUncached(this.map, key, PyObjectHashNode.executeUncached(key), value);
155+
PutUnsafeNode.putUncached(this.map, key, PyObjectHashNode.hash(key, HashCodeNode.getUncached()), value);
155156
}
156157

157-
// Solves boot-order problem, do not use in normal code or during startup when __eq__ of
158-
// builtins may not be properly set-up
159-
public void putUncachedWithJavaEq(Object key, long keyHash, Object value) {
160-
ObjectHashMap.PutNode.putUncachedWithJavaEq(this.map, key, keyHash, value);
158+
@TruffleBoundary
159+
public void putUncached(Object key, Object value) {
160+
PutNode.getUncached().execute(null, null, this.map, key, PyObjectHashNode.executeUncached(key), value);
161161
}
162162

163-
public void putUncachedWithJavaEq(TruffleString key, Object value) {
164-
putUncachedWithJavaEq(key, PyObjectHashNode.hash(key, HashCodeNode.getUncached()), value);
163+
@TruffleBoundary
164+
public void putUncached(int key, Object value) {
165+
PutUnsafeNode.putUncached(this.map, key, PyObjectHashNode.hash(key), value);
165166
}
166167

167-
public void putUncachedWithJavaEq(String key, Object value) {
168+
@TruffleBoundary
169+
public void putUncached(String key, Object value) {
168170
TruffleString ts = toTruffleStringUncached(key);
169-
putUncachedWithJavaEq(ts, value);
171+
putUncached(ts, value);
170172
}
171173

172174
private static void putAllUncached(LinkedHashMap<String, Object> map, EconomicMapStorage result) {
@@ -212,7 +214,7 @@ public abstract static class EconomicMapSetStringKey extends SpecializedSetStrin
212214
@Specialization
213215
static void doIt(Node inliningTarget, HashingStorage self, TruffleString key, Object value,
214216
@Cached PyObjectHashNode hashNode,
215-
@Cached PutNode putNode) {
217+
@Cached PutUnsafeNode putNode) {
216218
putNode.put(null, inliningTarget, ((EconomicMapStorage) self).map, key, hashNode.execute(null, inliningTarget, key), value);
217219
}
218220
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingCollectionNodes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageIteratorKey;
5252
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageIteratorNext;
5353
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageSetItem;
54+
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap.PutUnsafeNode;
5455
import com.oracle.graal.python.builtins.objects.dict.DictNodes;
5556
import com.oracle.graal.python.builtins.objects.dict.PDict;
5657
import com.oracle.graal.python.builtins.objects.dict.PDictView;
@@ -120,7 +121,7 @@ abstract static class SetValueHashingStorageNode extends PNodeWithContext {
120121

121122
@Specialization
122123
static HashingStorage doEconomicStorage(VirtualFrame frame, Node inliningTarget, EconomicMapStorage map, Object value,
123-
@Cached ObjectHashMap.PutNode putNode,
124+
@Cached PutUnsafeNode putNode,
124125
@Cached InlinedLoopConditionProfile loopProfile) {
125126
// We want to avoid calling __hash__() during map.put
126127
map.setValueForAllKeys(frame, inliningTarget, value, putNode, loopProfile);

0 commit comments

Comments
 (0)