Skip to content

Commit b6fbb86

Browse files
otethaltimfel
authored andcommitted
Use inlined profiles
1 parent 8f44ed8 commit b6fbb86

File tree

7 files changed

+334
-261
lines changed

7 files changed

+334
-261
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/CodeNodes.java

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242

4343
import java.util.Arrays;
4444

45-
import com.oracle.truffle.api.dsl.Bind;
4645
import org.graalvm.polyglot.io.ByteSequence;
4746

4847
import com.oracle.graal.python.PythonLanguage;
4948
import com.oracle.graal.python.builtins.modules.MarshalModuleBuiltins;
49+
import com.oracle.graal.python.builtins.objects.code.CodeNodesFactory.GetCodeCallTargetNodeGen;
5050
import com.oracle.graal.python.builtins.objects.function.Signature;
5151
import com.oracle.graal.python.compiler.CodeUnit;
5252
import com.oracle.graal.python.nodes.IndirectCallNode;
@@ -63,18 +63,17 @@
6363
import com.oracle.truffle.api.Assumption;
6464
import com.oracle.truffle.api.CallTarget;
6565
import com.oracle.truffle.api.CompilerDirectives;
66-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
6766
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6867
import com.oracle.truffle.api.RootCallTarget;
6968
import com.oracle.truffle.api.Truffle;
69+
import com.oracle.truffle.api.dsl.Bind;
7070
import com.oracle.truffle.api.dsl.Cached;
7171
import com.oracle.truffle.api.dsl.GenerateUncached;
7272
import com.oracle.truffle.api.dsl.NeverDefault;
7373
import com.oracle.truffle.api.dsl.Specialization;
7474
import com.oracle.truffle.api.frame.VirtualFrame;
7575
import com.oracle.truffle.api.nodes.Node;
7676
import com.oracle.truffle.api.nodes.RootNode;
77-
import com.oracle.truffle.api.profiles.ConditionProfile;
7877
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
7978
import com.oracle.truffle.api.source.Source;
8079
import com.oracle.truffle.api.strings.TruffleString;
@@ -183,69 +182,40 @@ public static CreateCodeNode create() {
183182
}
184183
}
185184

186-
public static final class GetCodeCallTargetNode extends Node {
187-
private static final GetCodeCallTargetNode UNCACHED = new GetCodeCallTargetNode(false);
185+
@GenerateUncached
186+
public abstract static class GetCodeCallTargetNode extends PNodeWithContext {
188187

189-
private final boolean isAdoptable;
190-
@CompilationFinal private ConditionProfile hasCtProfile;
191-
@CompilationFinal private PCode cachedCode1;
192-
@CompilationFinal private PCode cachedCode2;
193-
@CompilationFinal private RootCallTarget cachedCt1;
194-
@CompilationFinal private RootCallTarget cachedCt2;
188+
GetCodeCallTargetNode() {
189+
}
195190

196-
private GetCodeCallTargetNode(boolean isAdoptable) {
197-
this.isAdoptable = isAdoptable;
191+
public abstract RootCallTarget execute(PCode code);
192+
193+
@Specialization(guards = {"cachedCode == code", "isSingleContext()"}, limit = "2")
194+
final RootCallTarget doCachedCode(@SuppressWarnings("unused") PCode code,
195+
@SuppressWarnings("unused") @Cached("code") PCode cachedCode,
196+
@Cached("code.initializeCallTarget()") RootCallTarget cachedRootCallTarget) {
197+
return cachedRootCallTarget;
198198
}
199199

200-
public final RootCallTarget execute(PCode code) {
201-
if (isAdoptable) {
202-
if (hasCtProfile == null) {
203-
if (PythonLanguage.get(this).isSingleContext()) {
204-
if (cachedCode1 == null) {
205-
CompilerDirectives.transferToInterpreterAndInvalidate();
206-
cachedCode1 = code;
207-
cachedCt1 = code.initializeCallTarget();
208-
return cachedCt1;
209-
}
210-
if (cachedCode1 == code) {
211-
return cachedCt1;
212-
}
213-
if (cachedCode2 == null) {
214-
CompilerDirectives.transferToInterpreterAndInvalidate();
215-
cachedCode2 = code;
216-
cachedCt2 = code.initializeCallTarget();
217-
return cachedCt2;
218-
}
219-
if (cachedCode2 == code) {
220-
return cachedCt2;
221-
}
222-
}
223-
CompilerDirectives.transferToInterpreterAndInvalidate();
224-
cachedCode1 = cachedCode2 = null;
225-
cachedCt1 = cachedCt2 = null;
226-
hasCtProfile = ConditionProfile.create();
227-
}
228-
RootCallTarget ct = code.callTarget;
229-
if (hasCtProfile.profile(ct == null)) {
230-
ct = code.initializeCallTarget();
231-
}
232-
return ct;
233-
} else {
234-
RootCallTarget ct = code.callTarget;
235-
if (ct == null) {
236-
ct = code.initializeCallTarget();
237-
}
238-
return ct;
200+
@Specialization(replaces = "doCachedCode")
201+
final RootCallTarget doGeneric(PCode code,
202+
@Bind("this") Node inliningTarget,
203+
@Cached InlinedConditionProfile hasCtProfile) {
204+
RootCallTarget ct = code.callTarget;
205+
if (hasCtProfile.profile(inliningTarget, ct == null)) {
206+
ct = code.initializeCallTarget();
239207
}
208+
return ct;
240209
}
241210

242211
@NeverDefault
243212
public static GetCodeCallTargetNode create() {
244-
return new GetCodeCallTargetNode(true);
213+
return GetCodeCallTargetNodeGen.create();
245214
}
246215

216+
@NeverDefault
247217
public static GetCodeCallTargetNode getUncached() {
248-
return UNCACHED;
218+
return GetCodeCallTargetNodeGen.getUncached();
249219
}
250220
}
251221

@@ -297,7 +267,7 @@ public boolean isAdoptable() {
297267
return isAdoptable;
298268
}
299269

300-
public final RootNode execute(PCode code) {
270+
public RootNode execute(PCode code) {
301271
if (getCodeCallTargetNode == null) {
302272
CompilerDirectives.transferToInterpreterAndInvalidate();
303273
getCodeCallTargetNode = insert(GetCodeCallTargetNode.create());

0 commit comments

Comments
 (0)