Skip to content

Commit edd6371

Browse files
committed
Profile if an error occurred
1 parent 5f5c383 commit edd6371

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3620,6 +3620,7 @@ static boolean checkLayout(Object moduleDef, InteropLibrary moduleDefLib) {
36203620
static Object doGeneric(CApiContext capiContext, ModuleSpec moduleSpec, PythonAbstractNativeObject moduleDefWrapper,
36213621
@CachedLanguage PythonLanguage language,
36223622
@Cached PythonObjectFactory factory,
3623+
@Cached ConditionProfile errOccurredProfile,
36233624
@Cached GetSulongTypeNode getSulongTypeNode,
36243625
@Cached PCallCapiFunction callAttachTypeNode,
36253626
@Cached PCallCapiFunction callGetterNode,
@@ -3699,9 +3700,8 @@ static Object doGeneric(CApiContext capiContext, ModuleSpec moduleSpec, PythonAb
36993700
Object[] cArguments = new Object[]{moduleSpecToNativeNode.execute(capiContext, moduleSpec.originalModuleSpec), moduleDef};
37003701
try {
37013702
Object result = interopLib.execute(createFunction, cArguments);
3702-
DefaultCheckFunctionResultNode.checkFunctionResult(mName, interopLib.isNull(result), false, language, capiContext.getContext(), raiseNode, factory,
3703-
CREATION_FAILD_WITHOUT_EXCEPTION,
3704-
CREATION_RAISED_EXCEPTION);
3703+
DefaultCheckFunctionResultNode.checkFunctionResult(mName, interopLib.isNull(result), false, language, capiContext.getContext(), raiseNode, factory, errOccurredProfile,
3704+
CREATION_FAILD_WITHOUT_EXCEPTION, CREATION_RAISED_EXCEPTION);
37053705
module = toJavaNode.execute(capiContext, result);
37063706

37073707
/*
@@ -3773,6 +3773,7 @@ public abstract static class ExecModuleNode extends MultiPhaseExtensionModuleIni
37733773
static int doGeneric(CApiContext capiContext, PythonModule module, Object moduleDef,
37743774
@CachedLanguage PythonLanguage language,
37753775
@Cached PythonObjectFactory factory,
3776+
@Cached ConditionProfile errOccurredProfile,
37763777
@Cached ModuleGetNameNode getNameNode,
37773778
@Cached PCallCapiFunction callGetterNode,
37783779
@Cached WriteNativeMemberNode writeNativeMemberNode,
@@ -3837,7 +3838,7 @@ static int doGeneric(CApiContext capiContext, PythonModule module, Object module
38373838
* and won't ignore this if no error is set. This is then the same
38383839
* behaviour if we would have a pointer return type and got 'NULL'.
38393840
*/
3840-
DefaultCheckFunctionResultNode.checkFunctionResult(mName, iResult != 0, false, language, capiContext.getContext(), raiseNode, factory,
3841+
DefaultCheckFunctionResultNode.checkFunctionResult(mName, iResult != 0, false, language, capiContext.getContext(), raiseNode, factory, errOccurredProfile,
38413842
EXECUTION_FAILED_WITHOUT_EXCEPTION, EXECUTION_RAISED_EXCEPTION);
38423843
break;
38433844
default:

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,57 +1698,63 @@ static Object doNativeWrapper(PythonContext context, String name, DynamicObjectN
16981698
static Object doPrimitiveWrapper(PythonContext context, String name, @SuppressWarnings("unused") PythonNativeWrapper result,
16991699
@Shared("language") @CachedLanguage PythonLanguage language,
17001700
@Shared("fact") @Cached PythonObjectFactory factory,
1701-
@Shared("raise") @Cached PRaiseNode raise) {
1702-
checkFunctionResult(name, false, false, language, context, raise, factory);
1701+
@Shared("raise") @Cached PRaiseNode raise,
1702+
@Shared("errOccurredProfile") @Cached ConditionProfile errOccurredProfile) {
1703+
checkFunctionResult(name, false, false, language, context, raise, factory, errOccurredProfile);
17031704
return result;
17041705
}
17051706

17061707
@Specialization(guards = "isNoValue(result)")
17071708
static Object doNoValue(PythonContext context, String name, @SuppressWarnings("unused") PNone result,
17081709
@Shared("language") @CachedLanguage PythonLanguage language,
17091710
@Shared("fact") @Cached PythonObjectFactory factory,
1710-
@Shared("raise") @Cached PRaiseNode raise) {
1711-
checkFunctionResult(name, true, false, language, context, raise, factory);
1711+
@Shared("raise") @Cached PRaiseNode raise,
1712+
@Shared("errOccurredProfile") @Cached ConditionProfile errOccurredProfile) {
1713+
checkFunctionResult(name, true, false, language, context, raise, factory, errOccurredProfile);
17121714
return PNone.NO_VALUE;
17131715
}
17141716

17151717
@Specialization(guards = "!isNoValue(result)")
17161718
static Object doPythonObject(PythonContext context, String name, @SuppressWarnings("unused") PythonAbstractObject result,
17171719
@Shared("language") @CachedLanguage PythonLanguage language,
17181720
@Shared("fact") @Cached PythonObjectFactory factory,
1719-
@Shared("raise") @Cached PRaiseNode raise) {
1720-
checkFunctionResult(name, false, false, language, context, raise, factory);
1721+
@Shared("raise") @Cached PRaiseNode raise,
1722+
@Shared("errOccurredProfile") @Cached ConditionProfile errOccurredProfile) {
1723+
checkFunctionResult(name, false, false, language, context, raise, factory, errOccurredProfile);
17211724
return result;
17221725
}
17231726

17241727
@Specialization
17251728
static Object doPythonNativeNull(PythonContext context, String name, @SuppressWarnings("unused") PythonNativeNull result,
17261729
@Shared("language") @CachedLanguage PythonLanguage language,
17271730
@Shared("fact") @Cached PythonObjectFactory factory,
1728-
@Shared("raise") @Cached PRaiseNode raise) {
1729-
checkFunctionResult(name, true, false, language, context, raise, factory);
1731+
@Shared("raise") @Cached PRaiseNode raise,
1732+
@Shared("errOccurredProfile") @Cached ConditionProfile errOccurredProfile) {
1733+
checkFunctionResult(name, true, false, language, context, raise, factory, errOccurredProfile);
17301734
return result;
17311735
}
17321736

17331737
@Specialization
17341738
static int doInteger(PythonContext context, String name, int result,
17351739
@Shared("language") @CachedLanguage PythonLanguage language,
17361740
@Shared("fact") @Cached PythonObjectFactory factory,
1737-
@Shared("raise") @Cached PRaiseNode raise) {
1741+
@Shared("raise") @Cached PRaiseNode raise,
1742+
@Shared("errOccurredProfile") @Cached ConditionProfile errOccurredProfile) {
17381743
// If the native functions returns a primitive int, only a value '-1' indicates an
17391744
// error.
1740-
checkFunctionResult(name, result == -1, true, language, context, raise, factory);
1745+
checkFunctionResult(name, result == -1, true, language, context, raise, factory, errOccurredProfile);
17411746
return result;
17421747
}
17431748

17441749
@Specialization
17451750
static long doLong(PythonContext context, String name, long result,
17461751
@Shared("language") @CachedLanguage PythonLanguage language,
17471752
@Shared("fact") @Cached PythonObjectFactory factory,
1748-
@Shared("raise") @Cached PRaiseNode raise) {
1753+
@Shared("raise") @Cached PRaiseNode raise,
1754+
@Shared("errOccurredProfile") @Cached ConditionProfile errOccurredProfile) {
17491755
// If the native functions returns a primitive int, only a value '-1' indicates an
17501756
// error.
1751-
checkFunctionResult(name, result == -1, true, language, context, raise, factory);
1757+
checkFunctionResult(name, result == -1, true, language, context, raise, factory, errOccurredProfile);
17521758
return result;
17531759
}
17541760

@@ -1764,15 +1770,16 @@ static Object doForeign(PythonContext context, String name, Object result,
17641770
@Exclusive @CachedLibrary(limit = "3") InteropLibrary lib,
17651771
@Shared("language") @CachedLanguage PythonLanguage language,
17661772
@Shared("fact") @Cached PythonObjectFactory factory,
1767-
@Shared("raise") @Cached PRaiseNode raise) {
1768-
checkFunctionResult(name, isNullProfile.profile(lib.isNull(result)), false, language, context, raise, factory);
1773+
@Shared("raise") @Cached PRaiseNode raise,
1774+
@Shared("errOccurredProfile") @Cached ConditionProfile errOccurredProfile) {
1775+
checkFunctionResult(name, isNullProfile.profile(lib.isNull(result)), false, language, context, raise, factory, errOccurredProfile);
17691776
return result;
17701777
}
17711778

17721779
private static void checkFunctionResult(String name, boolean indicatesError, boolean isPrimitiveResult, PythonLanguage language, PythonContext context, PRaiseNode raise,
1773-
PythonObjectFactory factory) {
1774-
checkFunctionResult(name, indicatesError, isPrimitiveResult, language, context, raise, factory, ErrorMessages.RETURNED_NULL_WO_SETTING_ERROR,
1775-
ErrorMessages.RETURNED_RESULT_WITH_ERROR_SET);
1780+
PythonObjectFactory factory, ConditionProfile errOccurredProfile) {
1781+
checkFunctionResult(name, indicatesError, isPrimitiveResult, language, context, raise, factory, errOccurredProfile,
1782+
ErrorMessages.RETURNED_NULL_WO_SETTING_ERROR, ErrorMessages.RETURNED_RESULT_WITH_ERROR_SET);
17761783
}
17771784

17781785
/**
@@ -1791,16 +1798,18 @@ private static void checkFunctionResult(String name, boolean indicatesError, boo
17911798
* @param context The Python context.
17921799
* @param raise A raise node to raise {@code SystemError}s.
17931800
* @param factory A factory to create a base exception object.
1801+
* @param errOccurredProfile Profiles if a Python exception occurred and is set in the
1802+
* context.
17941803
* @param nullButNoErrorMessage Error message used if the value indicates an error and is
17951804
* not primitive but no error was set.
17961805
* @param resultWithErrorMessage Error message used if an error was set but the value does
17971806
* not indicate and error.
17981807
*/
17991808
static void checkFunctionResult(String name, boolean indicatesError, boolean isPrimitiveResult, PythonLanguage language, PythonContext context, PRaiseNode raise,
1800-
PythonObjectFactory factory, String nullButNoErrorMessage, String resultWithErrorMessage) {
1809+
PythonObjectFactory factory, ConditionProfile errOccurredProfile, String nullButNoErrorMessage, String resultWithErrorMessage) {
18011810
PythonThreadState threadState = context.getThreadState(language);
18021811
PException currentException = threadState.getCurrentException();
1803-
boolean errOccurred = currentException != null;
1812+
boolean errOccurred = errOccurredProfile.profile(currentException != null);
18041813
if (indicatesError) {
18051814
// consume exception
18061815
threadState.setCurrentException(null);

0 commit comments

Comments
 (0)