Skip to content

Commit e4756fd

Browse files
committed
Move 'HandleCacheMR' to separate file.
1 parent 58239ec commit e4756fd

File tree

2 files changed

+175
-101
lines changed

2 files changed

+175
-101
lines changed

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

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,8 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext;
4242

43-
import com.oracle.graal.python.builtins.objects.cext.HandleCacheFactory.HandleCacheMRFactory.GetOrInsertNodeGen;
44-
import com.oracle.truffle.api.CompilerDirectives;
45-
import com.oracle.truffle.api.dsl.Cached;
46-
import com.oracle.truffle.api.dsl.ImportStatic;
47-
import com.oracle.truffle.api.dsl.Specialization;
48-
import com.oracle.truffle.api.interop.ArityException;
4943
import com.oracle.truffle.api.interop.ForeignAccess;
50-
import com.oracle.truffle.api.interop.Message;
51-
import com.oracle.truffle.api.interop.MessageResolution;
52-
import com.oracle.truffle.api.interop.Resolve;
5344
import com.oracle.truffle.api.interop.TruffleObject;
54-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
55-
import com.oracle.truffle.api.interop.UnsupportedTypeException;
56-
import com.oracle.truffle.api.nodes.ControlFlowException;
57-
import com.oracle.truffle.api.nodes.ExplodeLoop;
58-
import com.oracle.truffle.api.nodes.Node;
59-
import com.oracle.truffle.api.profiles.BranchProfile;
6045

6146
public final class HandleCache implements TruffleObject {
6247
public static final int CACHE_SIZE = 10;
@@ -88,90 +73,4 @@ public ForeignAccess getForeignAccess() {
8873
public static boolean isInstance(TruffleObject obj) {
8974
return obj instanceof HandleCache;
9075
}
91-
92-
@MessageResolution(receiverType = HandleCache.class)
93-
static class HandleCacheMR {
94-
95-
@Resolve(message = "EXECUTE")
96-
abstract static class ExecuteNode extends Node {
97-
@Child private GetOrInsertNode getOrInsertNode = GetOrInsertNodeGen.create();
98-
99-
private final BranchProfile invalidArgCountProfile = BranchProfile.create();
100-
101-
Object access(HandleCache receiver, Object[] args) {
102-
if (args.length != 1) {
103-
invalidArgCountProfile.enter();
104-
throw ArityException.raise(1, args.length);
105-
}
106-
return getOrInsertNode.execute(receiver, (long) args[0]);
107-
}
108-
109-
}
110-
111-
static class InvalidCacheEntryException extends ControlFlowException {
112-
private static final long serialVersionUID = 1L;
113-
public static final InvalidCacheEntryException INSTANCE = new InvalidCacheEntryException();
114-
}
115-
116-
@ImportStatic(HandleCache.class)
117-
abstract static class GetOrInsertNode extends Node {
118-
@Child private Node executeNode;
119-
120-
private final BranchProfile errorProfile = BranchProfile.create();
121-
122-
public abstract Object execute(HandleCache cache, long handle);
123-
124-
@Specialization(limit = "CACHE_SIZE", guards = {"cache.len() == cachedLen",
125-
"handle == cachedHandle"}, rewriteOn = InvalidCacheEntryException.class)
126-
Object doCached(HandleCache cache, @SuppressWarnings("unused") long handle,
127-
@Cached("handle") long cachedHandle,
128-
@Cached("cache.len()") @SuppressWarnings("unused") int cachedLen,
129-
@Cached("cache.getPtrToResolveHandle()") @SuppressWarnings("unused") TruffleObject ptrToResolveHandle,
130-
@Cached("lookupPosition(cache, handle, cachedLen, ptrToResolveHandle)") int cachedPosition) throws InvalidCacheEntryException {
131-
if (cache.keys[cachedPosition] == cachedHandle) {
132-
return cache.values[cachedPosition];
133-
}
134-
throw InvalidCacheEntryException.INSTANCE;
135-
}
136-
137-
@Specialization(guards = {"cache.len() == cachedLen"}, replaces = "doCached")
138-
Object doFullLookup(HandleCache cache, long handle,
139-
@Cached("cache.len()") int cachedLen,
140-
@Cached("cache.getPtrToResolveHandle()") TruffleObject ptrToResolveHandle) {
141-
int pos = lookupPosition(cache, handle, cachedLen, ptrToResolveHandle);
142-
return cache.values[pos];
143-
}
144-
145-
@ExplodeLoop
146-
protected int lookupPosition(HandleCache cache, long handle, int cachedLen, TruffleObject ptrToResolveHandle) {
147-
for (int i = 0; i < cachedLen; i++) {
148-
if (cache.keys[i] == handle) {
149-
return i;
150-
}
151-
}
152-
153-
try {
154-
Object resolved = ForeignAccess.sendExecute(getExecuteNode(), ptrToResolveHandle, handle);
155-
156-
int insertPos = cache.pos;
157-
cache.keys[insertPos] = handle;
158-
cache.values[insertPos] = resolved;
159-
cache.pos = (insertPos + 1) % cache.len();
160-
161-
return insertPos;
162-
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
163-
errorProfile.enter();
164-
throw e.raise();
165-
}
166-
}
167-
168-
private Node getExecuteNode() {
169-
if (executeNode == null) {
170-
CompilerDirectives.transferToInterpreterAndInvalidate();
171-
executeNode = insert(Message.EXECUTE.createNode());
172-
}
173-
return executeNode;
174-
}
175-
}
176-
}
17776
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.objects.cext;
42+
43+
import com.oracle.graal.python.builtins.objects.cext.HandleCacheMRFactory.GetOrInsertNodeGen;
44+
import com.oracle.graal.python.nodes.PNodeWithContext;
45+
import com.oracle.truffle.api.CompilerDirectives;
46+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
47+
import com.oracle.truffle.api.dsl.Cached;
48+
import com.oracle.truffle.api.dsl.ImportStatic;
49+
import com.oracle.truffle.api.dsl.Specialization;
50+
import com.oracle.truffle.api.interop.ArityException;
51+
import com.oracle.truffle.api.interop.ForeignAccess;
52+
import com.oracle.truffle.api.interop.Message;
53+
import com.oracle.truffle.api.interop.MessageResolution;
54+
import com.oracle.truffle.api.interop.Resolve;
55+
import com.oracle.truffle.api.interop.TruffleObject;
56+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
57+
import com.oracle.truffle.api.interop.UnsupportedTypeException;
58+
import com.oracle.truffle.api.nodes.ControlFlowException;
59+
import com.oracle.truffle.api.nodes.ExplodeLoop;
60+
import com.oracle.truffle.api.nodes.Node;
61+
import com.oracle.truffle.api.profiles.BranchProfile;
62+
63+
@MessageResolution(receiverType = HandleCache.class)
64+
public class HandleCacheMR {
65+
66+
@Resolve(message = "EXECUTE")
67+
abstract static class ExecuteNode extends Node {
68+
@Child private HandleCacheMR.GetOrInsertNode getOrInsertNode = GetOrInsertNodeGen.create();
69+
70+
private final BranchProfile invalidArgCountProfile = BranchProfile.create();
71+
72+
Object access(HandleCache receiver, Object[] args) {
73+
if (args.length != 1) {
74+
invalidArgCountProfile.enter();
75+
throw ArityException.raise(1, args.length);
76+
}
77+
return getOrInsertNode.execute(receiver, (long) args[0]);
78+
}
79+
80+
}
81+
82+
static class InvalidCacheEntryException extends ControlFlowException {
83+
private static final long serialVersionUID = 1L;
84+
public static final HandleCacheMR.InvalidCacheEntryException INSTANCE = new InvalidCacheEntryException();
85+
}
86+
87+
@ImportStatic(HandleCache.class)
88+
abstract static class GetOrInsertNode extends PNodeWithContext {
89+
@Child private Node executeNode;
90+
91+
private final BranchProfile errorProfile = BranchProfile.create();
92+
@CompilationFinal private TruffleObject resolveHandleFunction;
93+
94+
public abstract Object execute(HandleCache cache, long handle);
95+
96+
@Specialization(limit = "CACHE_SIZE", guards = {"cache.len() == cachedLen",
97+
"handle == cachedHandle"}, rewriteOn = HandleCacheMR.InvalidCacheEntryException.class, assumptions = "singleContextAssumption()")
98+
Object doCachedSingleContext(HandleCache cache, @SuppressWarnings("unused") long handle,
99+
@Cached("handle") long cachedHandle,
100+
@Cached("cache.len()") @SuppressWarnings("unused") int cachedLen,
101+
@Cached("lookupPosition(cache, handle, cachedLen, getResolveHandleFunction(cache))") int cachedPosition) throws HandleCacheMR.InvalidCacheEntryException {
102+
if (cache.keys[cachedPosition] == cachedHandle) {
103+
return cache.values[cachedPosition];
104+
}
105+
throw InvalidCacheEntryException.INSTANCE;
106+
}
107+
108+
@Specialization(guards = {"cache.len() == cachedLen"}, replaces = "doCachedSingleContext", assumptions = "singleContextAssumption()")
109+
Object doFullLookupSingleContext(HandleCache cache, long handle,
110+
@Cached("cache.len()") int cachedLen) {
111+
int pos = lookupPosition(cache, handle, cachedLen, getResolveHandleFunction(cache));
112+
return cache.values[pos];
113+
}
114+
115+
@Specialization(limit = "CACHE_SIZE", guards = {"cache.len() == cachedLen",
116+
"handle == cachedHandle", "cache.getPtrToResolveHandle() == cachedResolveHandleFunction"}, rewriteOn = HandleCacheMR.InvalidCacheEntryException.class)
117+
Object doCached(HandleCache cache, @SuppressWarnings("unused") long handle,
118+
@Cached("handle") long cachedHandle,
119+
@Cached("cache.len()") @SuppressWarnings("unused") int cachedLen,
120+
@Cached("cache.getPtrToResolveHandle()") @SuppressWarnings("unused") TruffleObject cachedResolveHandleFunction,
121+
@Cached("lookupPosition(cache, handle, cachedLen, cachedResolveHandleFunction)") int cachedPosition) throws HandleCacheMR.InvalidCacheEntryException {
122+
if (cache.keys[cachedPosition] == cachedHandle) {
123+
return cache.values[cachedPosition];
124+
}
125+
throw InvalidCacheEntryException.INSTANCE;
126+
}
127+
128+
@Specialization(guards = {"cache.len() == cachedLen", "cache.getPtrToResolveHandle() == cachedResolveHandleFunction"}, replaces = "doCached")
129+
Object doFullLookup(HandleCache cache, long handle,
130+
@Cached("cache.len()") int cachedLen,
131+
@Cached("cache.getPtrToResolveHandle()") TruffleObject cachedResolveHandleFunction) {
132+
int pos = lookupPosition(cache, handle, cachedLen, cachedResolveHandleFunction);
133+
return cache.values[pos];
134+
}
135+
136+
@ExplodeLoop
137+
protected int lookupPosition(HandleCache cache, long handle, int cachedLen, TruffleObject ptrToResolveHandle) {
138+
for (int i = 0; i < cachedLen; i++) {
139+
if (cache.keys[i] == handle) {
140+
return i;
141+
}
142+
}
143+
144+
try {
145+
Object resolved = ForeignAccess.sendExecute(getExecuteNode(), ptrToResolveHandle, handle);
146+
147+
int insertPos = cache.pos;
148+
cache.keys[insertPos] = handle;
149+
cache.values[insertPos] = resolved;
150+
cache.pos = (insertPos + 1) % cache.len();
151+
152+
return insertPos;
153+
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
154+
errorProfile.enter();
155+
throw e.raise();
156+
}
157+
}
158+
159+
protected TruffleObject getResolveHandleFunction(HandleCache cache) {
160+
if (resolveHandleFunction == null) {
161+
CompilerDirectives.transferToInterpreterAndInvalidate();
162+
resolveHandleFunction = cache.getPtrToResolveHandle();
163+
}
164+
return resolveHandleFunction;
165+
}
166+
167+
private Node getExecuteNode() {
168+
if (executeNode == null) {
169+
CompilerDirectives.transferToInterpreterAndInvalidate();
170+
executeNode = insert(Message.EXECUTE.createNode());
171+
}
172+
return executeNode;
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)