1
1
/*
2
- * Copyright (c) 2018, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2018, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* The Universal Permissive License (UPL), Version 1.0
65
65
import com .oracle .graal .python .lib .GetNextNode ;
66
66
import com .oracle .graal .python .lib .PyCallableCheckNode ;
67
67
import com .oracle .graal .python .lib .PyObjectGetIter ;
68
+ import com .oracle .graal .python .nodes .PGuards ;
68
69
import com .oracle .graal .python .nodes .call .CallNode ;
69
70
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
70
71
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
71
72
import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
72
73
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
74
+ import com .oracle .graal .python .nodes .object .BuiltinClassProfiles .IsBuiltinObjectProfile ;
73
75
import com .oracle .graal .python .nodes .object .GetDictIfExistsNode ;
74
- import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
75
76
import com .oracle .graal .python .runtime .exception .PException ;
76
77
import com .oracle .graal .python .util .PythonUtils ;
78
+ import com .oracle .truffle .api .CompilerDirectives ;
79
+ import com .oracle .truffle .api .dsl .Bind ;
77
80
import com .oracle .truffle .api .dsl .Cached ;
78
81
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
79
82
import com .oracle .truffle .api .dsl .NodeFactory ;
80
83
import com .oracle .truffle .api .dsl .Specialization ;
81
84
import com .oracle .truffle .api .frame .VirtualFrame ;
85
+ import com .oracle .truffle .api .nodes .Node ;
82
86
import com .oracle .truffle .api .profiles .ConditionProfile ;
87
+ import com .oracle .truffle .api .profiles .InlinedConditionProfile ;
83
88
84
89
@ CoreFunctions (defineModule = "_functools" )
85
90
public class FunctoolsModuleBuiltins extends PythonBuiltins {
@@ -99,28 +104,21 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
99
104
"sequence is empty." )
100
105
@ GenerateNodeFactory
101
106
public abstract static class ReduceNode extends PythonTernaryBuiltinNode {
102
- @ Specialization (guards = "isNoValue(initial)" )
103
- Object doReduceNoInitial (VirtualFrame frame , Object function , Object sequence , @ SuppressWarnings ("unused" ) PNone initial ,
104
- @ Cached PyObjectGetIter getIter ,
105
- @ Cached GetNextNode nextNode ,
106
- @ Cached CallNode callNode ,
107
- @ Cached IsBuiltinClassProfile stopIterProfile ,
108
- @ Cached IsBuiltinClassProfile typeError ) {
109
- return doReduce (frame , function , sequence , null , getIter , nextNode , callNode , stopIterProfile , typeError );
110
- }
111
-
112
- @ Specialization (guards = "!isNoValue(initial)" )
113
- Object doReduce (VirtualFrame frame , Object function , Object sequence , Object initial ,
107
+ @ Specialization
108
+ Object doReduce (VirtualFrame frame , Object function , Object sequence , Object initialIn ,
109
+ @ Bind ("this" ) Node inliningTarget ,
114
110
@ Cached PyObjectGetIter getIter ,
115
111
@ Cached GetNextNode nextNode ,
116
112
@ Cached CallNode callNode ,
117
- @ Cached IsBuiltinClassProfile stopIterProfile ,
118
- @ Cached IsBuiltinClassProfile typeError ) {
113
+ @ Cached InlinedConditionProfile initialNoValueProfile ,
114
+ @ Cached IsBuiltinObjectProfile stopIterProfile ,
115
+ @ Cached IsBuiltinObjectProfile typeError ) {
116
+ Object initial = initialNoValueProfile .profile (inliningTarget , PGuards .isNoValue (initialIn )) ? null : initialIn ;
119
117
Object seqIterator , result = initial ;
120
118
try {
121
119
seqIterator = getIter .execute (frame , sequence );
122
120
} catch (PException pe ) {
123
- pe .expectTypeError (typeError );
121
+ pe .expectTypeError (inliningTarget , typeError );
124
122
throw raise (PythonBuiltinClassType .TypeError , S_ARG_N_MUST_SUPPORT_ITERATION , "reduce()" , 2 );
125
123
}
126
124
@@ -139,9 +137,11 @@ Object doReduce(VirtualFrame frame, Object function, Object sequence, Object ini
139
137
args [1 ] = op2 ;
140
138
result = callNode .execute (frame , function , args );
141
139
}
142
- count ++;
140
+ if (CompilerDirectives .hasNextTier ()) {
141
+ count ++;
142
+ }
143
143
} catch (PException e ) {
144
- e .expectStopIteration (stopIterProfile );
144
+ e .expectStopIteration (inliningTarget , stopIterProfile );
145
145
break ;
146
146
}
147
147
}
0 commit comments