|
| 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