Skip to content

Commit 398c68f

Browse files
committed
Add boundary data for codecs registry initialization
1 parent 2adcfb0 commit 398c68f

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ static void doCustom(VirtualFrame frame, TruffleDecoder decoder, TruffleString e
490490
@Cached PyCodecLookupErrorNode lookupErrorNode,
491491
@Cached PRaiseNode raiseNode) {
492492
try {
493-
Object errorHandler = lookupErrorNode.execute(inliningTarget, errorAction);
493+
Object errorHandler = lookupErrorNode.execute(frame, inliningTarget, errorAction);
494494
if (errorHandler == null) {
495495
throw raiseNode.raise(inliningTarget, LookupError, UNKNOWN_ERROR_HANDLER, errorAction);
496496
}
@@ -975,6 +975,7 @@ public abstract static class PyCodecLookupNode extends PNodeWithContext {
975975

976976
@Specialization
977977
static PTuple lookup(VirtualFrame frame, Node inliningTarget, TruffleString encoding,
978+
@Cached CodecsRegistry.EnsureRegistryInitializedNode ensureRegistryInitializedNode,
978979
@Cached(inline = false) CallUnaryMethodNode callNode,
979980
@Cached PyObjectTypeCheck typeCheck,
980981
@Cached(inline = false) PyObjectSizeNode sizeNode,
@@ -986,7 +987,7 @@ static PTuple lookup(VirtualFrame frame, Node inliningTarget, TruffleString enco
986987
@Cached PRaiseNode raiseNode) {
987988
TruffleString normalizedEncoding = normalizeEncodingNameNode.execute(inliningTarget, encoding);
988989
PythonContext context = PythonContext.get(inliningTarget);
989-
ensureRegistryInitialized(context);
990+
ensureRegistryInitializedNode.execute(frame, inliningTarget, context);
990991
PTuple result = getSearchPath(context, normalizedEncoding);
991992
if (hasSearchPathProfile.profile(inliningTarget, result != null)) {
992993
return result;
@@ -1040,21 +1041,18 @@ private static boolean isTupleInstanceCheck(VirtualFrame frame, Node inliningTar
10401041
return typeCheck.execute(inliningTarget, result, PythonBuiltinClassType.PTuple) && sizeNode.execute(frame, inliningTarget, result) == len;
10411042
}
10421043

1043-
private static void ensureRegistryInitialized(PythonContext context) {
1044-
CodecsRegistry.ensureRegistryInitialized(context);
1045-
}
1046-
10471044
@Builtin(name = "register", minNumOfPositionalArgs = 1)
10481045
@GenerateNodeFactory
10491046
abstract static class RegisterNode extends PythonUnaryBuiltinNode {
10501047
@Specialization
1051-
static Object lookup(Object searchFunction,
1048+
static Object lookup(VirtualFrame frame, Object searchFunction,
10521049
@Bind Node inliningTarget,
1050+
@Cached CodecsRegistry.EnsureRegistryInitializedNode ensureRegistryInitializedNode,
10531051
@Cached PyCallableCheckNode callableCheckNode,
10541052
@Cached PRaiseNode raiseNode) {
10551053
if (callableCheckNode.execute(inliningTarget, searchFunction)) {
10561054
PythonContext context = PythonContext.get(inliningTarget);
1057-
ensureRegistryInitialized(context);
1055+
ensureRegistryInitializedNode.execute(frame, inliningTarget, context);
10581056
add(context, searchFunction);
10591057
return PNone.NONE;
10601058
} else {
@@ -1116,10 +1114,10 @@ protected ArgumentClinicProvider getArgumentClinic() {
11161114
}
11171115

11181116
@Specialization
1119-
Object register(TruffleString name, Object handler,
1117+
Object register(VirtualFrame frame, TruffleString name, Object handler,
11201118
@Bind Node inliningTarget,
11211119
@Cached PyCodecRegisterErrorNode registerErrorNode) {
1122-
registerErrorNode.execute(inliningTarget, name, handler);
1120+
registerErrorNode.execute(frame, inliningTarget, name, handler);
11231121
return PNone.NONE;
11241122
}
11251123
}
@@ -1135,10 +1133,10 @@ protected ArgumentClinicProvider getArgumentClinic() {
11351133
}
11361134

11371135
@Specialization
1138-
Object lookup(TruffleString name,
1136+
Object lookup(VirtualFrame frame, TruffleString name,
11391137
@Bind Node inliningTarget,
11401138
@Cached PyCodecLookupErrorNode errorNode) {
1141-
return errorNode.execute(inliningTarget, name);
1139+
return errorNode.execute(frame, inliningTarget, name);
11421140
}
11431141
}
11441142

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cjkcodecs/MultibyteCodecUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static Object callErrorCallback(VirtualFrame frame, Node inliningTarget, Object
150150
@Cached(inline = false) CallNode callNode) {
151151
assert (PyUnicodeCheckNode.executeUncached(errors));
152152
TruffleString str = castToStringNode.execute(inliningTarget, errors);
153-
Object cb = lookupErrorNode.execute(inliningTarget, str);
153+
Object cb = lookupErrorNode.execute(frame, inliningTarget, str);
154154
return callNode.execute(frame, cb, exc);
155155
}
156156
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/codecs/CodecsRegistry.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import static com.oracle.graal.python.runtime.exception.PythonErrorType.LookupError;
5555
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
5656

57+
import com.oracle.graal.python.PythonLanguage;
5758
import com.oracle.graal.python.builtins.modules.codecs.ErrorHandlersFactory.BackslashReplaceErrorHandlerNodeFactory;
5859
import com.oracle.graal.python.builtins.modules.codecs.ErrorHandlersFactory.IgnoreErrorHandlerNodeFactory;
5960
import com.oracle.graal.python.builtins.modules.codecs.ErrorHandlersFactory.NameReplaceErrorHandlerNodeFactory;
@@ -67,16 +68,20 @@
6768
import com.oracle.graal.python.nodes.PRaiseNode;
6869
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6970
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
71+
import com.oracle.graal.python.runtime.ExecutionContext;
72+
import com.oracle.graal.python.runtime.IndirectCallData;
7073
import com.oracle.graal.python.runtime.PythonContext;
7174
import com.oracle.graal.python.util.PythonUtils;
72-
import com.oracle.truffle.api.CompilerDirectives;
7375
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7476
import com.oracle.truffle.api.dsl.Cached;
7577
import com.oracle.truffle.api.dsl.Cached.Shared;
7678
import com.oracle.truffle.api.dsl.GenerateCached;
7779
import com.oracle.truffle.api.dsl.GenerateInline;
80+
import com.oracle.truffle.api.dsl.GenerateUncached;
7881
import com.oracle.truffle.api.dsl.NodeFactory;
7982
import com.oracle.truffle.api.dsl.Specialization;
83+
import com.oracle.truffle.api.frame.Frame;
84+
import com.oracle.truffle.api.frame.VirtualFrame;
8085
import com.oracle.truffle.api.nodes.Node;
8186
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
8287
import com.oracle.truffle.api.strings.TruffleString;
@@ -88,14 +93,15 @@ public final class CodecsRegistry {
8893
@GenerateCached(false)
8994
public abstract static class PyCodecLookupErrorNode extends Node {
9095

91-
public abstract Object execute(Node inliningTarget, TruffleString name);
96+
public abstract Object execute(Frame frame, Node inliningTarget, TruffleString name);
9297

9398
@Specialization
94-
static Object lookup(Node inliningTarget, TruffleString name,
99+
static Object lookup(VirtualFrame frame, Node inliningTarget, TruffleString name,
100+
@Cached CodecsRegistry.EnsureRegistryInitializedNode ensureRegistryInitializedNode,
95101
@Cached InlinedConditionProfile resultProfile,
96102
@Cached PRaiseNode raiseNode) {
97103
PythonContext context = PythonContext.get(inliningTarget);
98-
ensureRegistryInitialized(context);
104+
ensureRegistryInitializedNode.execute(frame, inliningTarget, context);
99105
if (name == null) {
100106
name = T_STRICT;
101107
}
@@ -112,13 +118,14 @@ static Object lookup(Node inliningTarget, TruffleString name,
112118
@GenerateCached(false)
113119
public abstract static class PyCodecRegisterErrorNode extends Node {
114120

115-
public abstract void execute(Node inliningTarget, TruffleString name, Object handler);
121+
public abstract void execute(VirtualFrame frame, Node inliningTarget, TruffleString name, Object handler);
116122

117123
@Specialization(guards = "callableCheckNode.execute(inliningTarget, handler)")
118-
static void register(Node inliningTarget, TruffleString name, Object handler,
119-
@SuppressWarnings("unused") @Cached @Shared("callableCheck") PyCallableCheckNode callableCheckNode) {
124+
static void register(VirtualFrame frame, Node inliningTarget, TruffleString name, Object handler,
125+
@SuppressWarnings("unused") @Cached @Shared("callableCheck") PyCallableCheckNode callableCheckNode,
126+
@Cached CodecsRegistry.EnsureRegistryInitializedNode ensureRegistryInitializedNode) {
120127
PythonContext context = PythonContext.get(inliningTarget);
121-
ensureRegistryInitialized(context);
128+
ensureRegistryInitializedNode.execute(frame, inliningTarget, context);
122129
putErrorHandler(context, name, handler);
123130
}
124131

@@ -129,9 +136,27 @@ static void registerNoCallable(@SuppressWarnings("unused") Node inliningTarget,
129136
}
130137
}
131138

132-
public static void ensureRegistryInitialized(PythonContext context) {
133-
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, !context.isCodecsInitialized())) {
134-
doInitialize(context);
139+
@GenerateInline
140+
@GenerateCached(value = false)
141+
@GenerateUncached
142+
public abstract static class EnsureRegistryInitializedNode extends Node {
143+
public abstract void execute(Frame frame, Node inliningTarget, PythonContext context);
144+
145+
@Specialization(guards = "context.isCodecsInitialized()")
146+
static void ensure(@SuppressWarnings("unused") PythonContext context) {
147+
// nothing to do
148+
}
149+
150+
@Specialization(guards = "!context.isCodecsInitialized()")
151+
static void ensure(VirtualFrame frame, Node inliningTarget, PythonContext context,
152+
@Cached("createFor($node)") IndirectCallData.BoundaryCallData boundaryCallData) {
153+
PythonLanguage language = context.getLanguage(inliningTarget);
154+
Object savedState = ExecutionContext.BoundaryCallContext.enter(frame, language, context, boundaryCallData);
155+
try {
156+
doInitialize(context);
157+
} finally {
158+
ExecutionContext.BoundaryCallContext.exit(frame, language, context, savedState);
159+
}
135160
}
136161
}
137162

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/codecs/ErrorHandlers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ static DecodingErrorHandlerResult doIt(VirtualFrame frame, Node inliningTarget,
890890
@Cached PyUnicodeDecodeErrorGetObjectNode getObjectNode,
891891
@Cached PyObjectSizeNode sizeNode,
892892
@Cached PRaiseNode raiseNode) {
893-
cache.errorHandlerObject = cache.errorHandlerObject == null ? lookupErrorNode.execute(inliningTarget, errors) : cache.errorHandlerObject;
893+
cache.errorHandlerObject = cache.errorHandlerObject == null ? lookupErrorNode.execute(frame, inliningTarget, errors) : cache.errorHandlerObject;
894894
cache.exceptionObject = makeDecodeExceptionNode.execute(frame, inliningTarget, cache.exceptionObject, encoding, srcObj, startPos, endPos, reason);
895895
Object resultObj = callNode.execute(frame, cache.errorHandlerObject, cache.exceptionObject);
896896
DecodingErrorHandlerResult result = parseResultNode.execute(frame, inliningTarget, resultObj);
@@ -967,7 +967,7 @@ static EncodingErrorHandlerResult doIt(VirtualFrame frame, Node inliningTarget,
967967
@Cached ParseEncodingErrorHandlerResultNode parseResultNode,
968968
@Cached TruffleString.CodePointLengthNode codePointLengthNode,
969969
@Cached PRaiseNode raiseNode) {
970-
cache.errorHandlerObject = cache.errorHandlerObject == null ? lookupErrorNode.execute(inliningTarget, errors) : cache.errorHandlerObject;
970+
cache.errorHandlerObject = cache.errorHandlerObject == null ? lookupErrorNode.execute(frame, inliningTarget, errors) : cache.errorHandlerObject;
971971
int len = codePointLengthNode.execute(srcObj, TS_ENCODING);
972972
cache.exceptionObject = makeEncodeExceptionNode.execute(frame, inliningTarget, cache.exceptionObject, encoding, srcObj, startPos, endPos, reason);
973973
Object resultObj = callNode.execute(frame, cache.errorHandlerObject, cache.exceptionObject);

0 commit comments

Comments
 (0)