|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 2026, 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.nodes.bytecode_dsl; |
| 42 | + |
| 43 | +import com.oracle.graal.python.PythonLanguage; |
| 44 | +import com.oracle.graal.python.builtins.objects.list.PList; |
| 45 | +import com.oracle.graal.python.builtins.objects.list.PList.ListOrigin; |
| 46 | +import com.oracle.graal.python.builtins.objects.tuple.PTuple; |
| 47 | +import com.oracle.graal.python.runtime.PythonContext; |
| 48 | +import com.oracle.graal.python.runtime.PythonOptions; |
| 49 | +import com.oracle.graal.python.runtime.object.PFactory; |
| 50 | +import com.oracle.graal.python.runtime.sequence.storage.ArrayBasedSequenceStorage; |
| 51 | +import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage; |
| 52 | +import com.oracle.graal.python.runtime.sequence.storage.DoubleSequenceStorage; |
| 53 | +import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage; |
| 54 | +import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage; |
| 55 | +import com.oracle.graal.python.runtime.sequence.storage.LongSequenceStorage; |
| 56 | +import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage; |
| 57 | +import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage; |
| 58 | +import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage.StorageType; |
| 59 | +import com.oracle.graal.python.runtime.sequence.storage.SequenceStorageFactory; |
| 60 | +import com.oracle.truffle.api.CompilerDirectives; |
| 61 | +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; |
| 62 | +import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff; |
| 63 | +import com.oracle.truffle.api.TruffleLogger; |
| 64 | +import com.oracle.truffle.api.dsl.Bind; |
| 65 | +import com.oracle.truffle.api.dsl.Cached; |
| 66 | +import com.oracle.truffle.api.dsl.Specialization; |
| 67 | +import com.oracle.truffle.api.nodes.Node; |
| 68 | +import com.oracle.truffle.api.nodes.SlowPathException; |
| 69 | +import com.oracle.truffle.api.profiles.InlinedIntValueProfile; |
| 70 | +import com.oracle.truffle.api.source.SourceSection; |
| 71 | + |
| 72 | +abstract class SequenceFromArrayNode extends Node { |
| 73 | + private static final SlowPathException SLOW_PATH_EXCEPTION = new SlowPathException(); |
| 74 | + @CompilationFinal protected SequenceStorage.StorageType type = StorageType.Uninitialized; |
| 75 | + |
| 76 | + SequenceStorage createSequenceStorage(Object[] objectElements, int length) { |
| 77 | + SequenceStorage storage; |
| 78 | + if (type == SequenceStorage.StorageType.Uninitialized) { |
| 79 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 80 | + storage = initialize(objectElements); |
| 81 | + } else { |
| 82 | + try { |
| 83 | + switch (type) { |
| 84 | + // Ugh. We want to use primitive arrays during unpacking, so |
| 85 | + // we cannot dispatch generically here. |
| 86 | + case Empty: { |
| 87 | + if (length != 0) { |
| 88 | + throw SLOW_PATH_EXCEPTION; |
| 89 | + } |
| 90 | + storage = EmptySequenceStorage.INSTANCE; |
| 91 | + break; |
| 92 | + } |
| 93 | + case Boolean: { |
| 94 | + boolean[] elements = new boolean[getCapacityEstimate(length)]; |
| 95 | + for (int i = 0; i < length; i++) { |
| 96 | + elements[i] = castBoolean(objectElements[i]); |
| 97 | + } |
| 98 | + storage = new BoolSequenceStorage(elements, length); |
| 99 | + break; |
| 100 | + } |
| 101 | + case Int: { |
| 102 | + int[] elements = new int[getCapacityEstimate(length)]; |
| 103 | + for (int i = 0; i < length; i++) { |
| 104 | + elements[i] = castInt(objectElements[i]); |
| 105 | + } |
| 106 | + storage = new IntSequenceStorage(elements, length); |
| 107 | + break; |
| 108 | + } |
| 109 | + case Long: { |
| 110 | + long[] elements = new long[getCapacityEstimate(length)]; |
| 111 | + for (int i = 0; i < length; i++) { |
| 112 | + elements[i] = castLong(objectElements[i]); |
| 113 | + } |
| 114 | + storage = new LongSequenceStorage(elements, length); |
| 115 | + break; |
| 116 | + } |
| 117 | + case Double: { |
| 118 | + double[] elements = new double[getCapacityEstimate(length)]; |
| 119 | + for (int i = 0; i < length; i++) { |
| 120 | + elements[i] = castDouble(objectElements[i]); |
| 121 | + } |
| 122 | + storage = new DoubleSequenceStorage(elements, length); |
| 123 | + break; |
| 124 | + } |
| 125 | + case Generic: { |
| 126 | + storage = new ObjectSequenceStorage(objectElements, length); |
| 127 | + break; |
| 128 | + } |
| 129 | + default: |
| 130 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 131 | + throw new RuntimeException("unexpected state"); |
| 132 | + } |
| 133 | + } catch (SlowPathException e) { |
| 134 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 135 | + type = SequenceStorage.StorageType.Generic; |
| 136 | + storage = new ObjectSequenceStorage(objectElements, length); |
| 137 | + } |
| 138 | + } |
| 139 | + return storage; |
| 140 | + } |
| 141 | + |
| 142 | + @InliningCutoff |
| 143 | + private SequenceStorage initialize(Object[] objectElements) { |
| 144 | + SequenceStorage storage; |
| 145 | + try { |
| 146 | + storage = SequenceStorageFactory.createStorage(objectElements); |
| 147 | + type = storage.getElementType(); |
| 148 | + } catch (Throwable t) { |
| 149 | + // we do not want to repeatedly deopt if a value execution |
| 150 | + // always raises, for example |
| 151 | + type = SequenceStorage.StorageType.Generic; |
| 152 | + throw t; |
| 153 | + } |
| 154 | + return storage; |
| 155 | + } |
| 156 | + |
| 157 | + private static int castInt(Object o) throws SlowPathException { |
| 158 | + if (o instanceof Integer) { |
| 159 | + return (int) o; |
| 160 | + } |
| 161 | + throw SLOW_PATH_EXCEPTION; |
| 162 | + } |
| 163 | + |
| 164 | + private static long castLong(Object o) throws SlowPathException { |
| 165 | + if (o instanceof Long) { |
| 166 | + return (long) o; |
| 167 | + } |
| 168 | + throw SLOW_PATH_EXCEPTION; |
| 169 | + } |
| 170 | + |
| 171 | + private static double castDouble(Object o) throws SlowPathException { |
| 172 | + if (o instanceof Double) { |
| 173 | + return (double) o; |
| 174 | + } |
| 175 | + throw SLOW_PATH_EXCEPTION; |
| 176 | + } |
| 177 | + |
| 178 | + private static boolean castBoolean(Object o) throws SlowPathException { |
| 179 | + if (o instanceof Boolean) { |
| 180 | + return (boolean) o; |
| 181 | + } |
| 182 | + throw SLOW_PATH_EXCEPTION; |
| 183 | + } |
| 184 | + |
| 185 | + protected abstract int getCapacityEstimate(int length); |
| 186 | + |
| 187 | + public abstract static class ListFromArrayNode extends SequenceFromArrayNode implements ListOrigin { |
| 188 | + private static final TruffleLogger LOGGER = PythonLanguage.getLogger(ListFromArrayNode.class); |
| 189 | + private static final ListFromArrayNode UNCACHED = new ListFromArrayNode() { |
| 190 | + @Override |
| 191 | + public PList execute(PythonLanguage language, Object[] elements) { |
| 192 | + return PFactory.createList(language, elements); |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + public static ListFromArrayNode getUncached(int ignored) { |
| 197 | + return UNCACHED; |
| 198 | + } |
| 199 | + |
| 200 | + @CompilationFinal private SizeEstimate initialCapacity; |
| 201 | + |
| 202 | + public abstract PList execute(PythonLanguage language, Object[] elements); |
| 203 | + |
| 204 | + @Specialization |
| 205 | + PList doIt(PythonLanguage language, Object[] elements, |
| 206 | + @Bind Node inliningTarget, |
| 207 | + @Cached InlinedIntValueProfile lengthProfile) { |
| 208 | + SequenceStorage storage = createSequenceStorage(elements, lengthProfile.profile(inliningTarget, elements.length)); |
| 209 | + return PFactory.createList(language, storage, initialCapacity != null ? this : null); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + protected int getCapacityEstimate(int length) { |
| 214 | + assert isAdoptable(); |
| 215 | + if (initialCapacity == null) { |
| 216 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 217 | + initialCapacity = new SizeEstimate(length); |
| 218 | + } |
| 219 | + return Math.max(initialCapacity.estimate(), length); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void reportUpdatedCapacity(ArrayBasedSequenceStorage newStore) { |
| 224 | + if (CompilerDirectives.inInterpreter()) { |
| 225 | + if (PythonContext.get(this).getOption(PythonOptions.OverallocateLiteralLists)) { |
| 226 | + if (newStore.getCapacity() > initialCapacity.estimate()) { |
| 227 | + initialCapacity.updateFrom(newStore.getCapacity()); |
| 228 | + LOGGER.finest(() -> { |
| 229 | + SourceSection encapsulatingSourceSection = getEncapsulatingSourceSection(); |
| 230 | + String sourceSection = encapsulatingSourceSection == null ? "<unavailable source>" : encapsulatingSourceSection.toString(); |
| 231 | + return String.format("Updating list size estimate at %s. Observed capacity: %d, new estimate: %d", sourceSection, newStore.getCapacity(), |
| 232 | + initialCapacity.estimate()); |
| 233 | + }); |
| 234 | + } |
| 235 | + if (newStore.getElementType().generalizesFrom(type)) { |
| 236 | + type = newStore.getElementType(); |
| 237 | + LOGGER.finest(() -> { |
| 238 | + SourceSection encapsulatingSourceSection = getEncapsulatingSourceSection(); |
| 239 | + String sourceSection = encapsulatingSourceSection == null ? "<unavailable source>" : encapsulatingSourceSection.toString(); |
| 240 | + return String.format("Updating list type estimate at %s. New type: %s", sourceSection, type.name()); |
| 241 | + }); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + // n.b.: it's ok that this races when the code is already being compiled |
| 246 | + // or if we're running on multiple threads. if the update isn't seen, we |
| 247 | + // are not incorrect, we just don't benefit from the optimization |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + public abstract static class TupleFromArrayNode extends SequenceFromArrayNode { |
| 252 | + private static final TupleFromArrayNode UNCACHED = new TupleFromArrayNode() { |
| 253 | + @Override |
| 254 | + public PTuple execute(PythonLanguage language, Object[] elements) { |
| 255 | + return PFactory.createTuple(language, elements); |
| 256 | + } |
| 257 | + }; |
| 258 | + |
| 259 | + public static TupleFromArrayNode getUncached() { |
| 260 | + return UNCACHED; |
| 261 | + } |
| 262 | + |
| 263 | + public abstract PTuple execute(PythonLanguage language, Object[] elements); |
| 264 | + |
| 265 | + @Specialization |
| 266 | + PTuple doIt(PythonLanguage language, Object[] elements, |
| 267 | + @Bind Node inliningTarget, |
| 268 | + @Cached InlinedIntValueProfile lengthProfile) { |
| 269 | + return PFactory.createTuple(language, createSequenceStorage(elements, lengthProfile.profile(inliningTarget, elements.length))); |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + protected int getCapacityEstimate(int length) { |
| 274 | + return length; |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments