Skip to content

Commit 90fdb14

Browse files
msimacektomasstupka
authored andcommitted
Fix inconsistencies in frame materialization
1 parent 2450c9d commit 90fdb14

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,14 @@ static HashingStorage cached(LocalsStorage self, HashingStorage other,
250250
@Exclusive @SuppressWarnings("unused") @Cached("self.frame.getFrameDescriptor()") FrameDescriptor desc) {
251251
HashingStorage result = other;
252252
for (int slot = 0; slot < desc.getNumberOfSlots(); slot++) {
253-
Object value = self.getValue(slot);
254-
if (value != null) {
255-
result = lib.setItem(result, desc.getSlotName(slot), value);
253+
Object identifier = desc.getSlotName(slot);
254+
if (identifier instanceof String) {
255+
if (isUserFrameSlot(identifier)) {
256+
Object value = self.getValue(slot);
257+
if (value != null) {
258+
result = lib.setItem(result, desc.getSlotName(slot), value);
259+
}
260+
}
256261
}
257262
}
258263
return result;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,10 +1872,10 @@ private int bytecodeRaiseVarargs(VirtualFrame virtualFrame, Frame stackFrame, in
18721872
private void raiseUnboundCell(Node[] localNodes, int bci, int oparg) {
18731873
PRaiseNode raiseNode = insertChildNode(localNodes, bci, UNCACHED_RAISE, NODE_RAISE);
18741874
if (oparg < freeoffset) {
1875-
int varIdx = oparg - celloffset;
1875+
int varIdx = oparg;
18761876
throw raiseNode.raise(PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, cellvars[varIdx]);
18771877
} else {
1878-
int varIdx = oparg - freeoffset;
1878+
int varIdx = oparg - cellvars.length;
18791879
throw raiseNode.raise(PythonBuiltinClassType.NameError, ErrorMessages.UNBOUNDFREEVAR, freevars[varIdx]);
18801880
}
18811881
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/MaterializeFrameNode.java

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

43-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4443
import com.oracle.graal.python.builtins.objects.cell.PCell;
4544
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
4645
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
@@ -50,12 +49,12 @@
5049
import com.oracle.graal.python.builtins.objects.frame.PFrame;
5150
import com.oracle.graal.python.builtins.objects.function.PArguments;
5251
import com.oracle.graal.python.nodes.ModuleRootNode;
52+
import com.oracle.graal.python.nodes.PGuards;
5353
import com.oracle.graal.python.nodes.PRootNode;
5454
import com.oracle.graal.python.nodes.SpecialMethodNames;
5555
import com.oracle.graal.python.nodes.bytecode.FrameInfo;
5656
import com.oracle.graal.python.nodes.frame.MaterializeFrameNodeGen.SyncFrameValuesNodeGen;
5757
import com.oracle.graal.python.nodes.function.ClassBodyRootNode;
58-
import com.oracle.graal.python.nodes.object.GetClassNode;
5958
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6059
import com.oracle.graal.python.util.PythonUtils;
6160
import com.oracle.truffle.api.CompilerDirectives;
@@ -432,13 +431,6 @@ static void doGenericDictAST(VirtualFrame frame, PFrame pyFrame, Frame frameToSy
432431
}
433432
}
434433

435-
@Specialization(guards = {"isCustomLocalsObject(pyFrame, frameToSync, frameProfile)"}, limit = "1")
436-
@SuppressWarnings("unused")
437-
static void doCustomLocalsObject(PFrame pyFrame, Frame frameToSync, @SuppressWarnings("unused") Node location,
438-
@Cached("createClassProfile()") ValueProfile frameProfile) {
439-
// nothing to do; we already worked on the custom object
440-
}
441-
442434
@Specialization(guards = {"isBytecodeFrame(frameToSync)", "hasLocalsStorage(pyFrame, frameToSync, frameProfile)", "frameToSync.getFrameDescriptor() == cachedFd",
443435
"variableSlotCount(cachedFd) < 32"}, limit = "1")
444436
@ExplodeLoop
@@ -550,6 +542,12 @@ static void doGenericDict(VirtualFrame frame, PFrame pyFrame, Frame frameToSync,
550542
}
551543
}
552544

545+
@Specialization(guards = "!isDictWithCustomStorage(pyFrame)")
546+
@SuppressWarnings("unused")
547+
static void doCustomLocalsObject(PFrame pyFrame, Frame frameToSync, Node location) {
548+
// nothing to do; we already worked on the custom object
549+
}
550+
553551
private static void syncDict(VirtualFrame frame, int slot, FrameInfo info, Frame frameToSync, PDict localsDict, HashingStorageLibrary lib,
554552
ConditionProfile hasFrame, BranchProfile updatedStorage, ConditionProfile profile) {
555553
HashingStorage storage = localsDict.getDictStorage();
@@ -558,21 +556,25 @@ private static void syncDict(VirtualFrame frame, int slot, FrameInfo info, Frame
558556
if (value != null && profile.profile(value instanceof PCell)) {
559557
value = ((PCell) value).getRef();
560558
}
561-
HashingStorage newStore = null;
559+
HashingStorage newStore;
562560
if (value != null) {
563561
newStore = lib.setItemWithFrame(storage, identifier, value, hasFrame, frame);
562+
if (newStore != storage) {
563+
updatedStorage.enter();
564+
localsDict.setDictStorage(newStore);
565+
}
564566
} else {
565567
// delete variable
566568
// TODO: FIXME: this might call __hash__ twice
567569
boolean hasKey = lib.hasKeyWithFrame(storage, identifier, hasFrame, frame);
568570
if (hasKey) {
569571
newStore = lib.delItemWithFrame(storage, identifier, hasFrame, frame);
572+
if (newStore != storage) {
573+
updatedStorage.enter();
574+
localsDict.setDictStorage(newStore);
575+
}
570576
}
571577
}
572-
if (newStore != storage) {
573-
updatedStorage.enter();
574-
localsDict.setDictStorage(newStore);
575-
}
576578
}
577579

578580
// @ImportStatic doesn't work on this for some reason
@@ -606,11 +608,7 @@ protected static boolean hasLocalsStorage(PFrame pyFrame, Frame frameToSync, Val
606608
protected static boolean isDictWithCustomStorage(PFrame pyFrame) {
607609
Object localsObject = pyFrame.getLocalsDict();
608610
// do not allow subclasses of 'dict'
609-
return GetClassNode.getUncached().execute(localsObject) == PythonBuiltinClassType.PDict;
610-
}
611-
612-
protected static boolean isCustomLocalsObject(PFrame pyFrame, Frame frameToSync, ValueProfile frameProfile) {
613-
return !hasLocalsStorage(pyFrame, frameToSync, frameProfile);
611+
return localsObject instanceof PDict && PGuards.isBuiltinDict((PDict) localsObject);
614612
}
615613

616614
protected static LocalsStorage getLocalsStorage(PFrame pyFrame) {

0 commit comments

Comments
 (0)